@thangk/easythemer
Version:
Easily generate shades from a colour palette for use in your app
113 lines • 4.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
const utils_1 = require("../utils");
/**
* Validate the channel params object.
*
* @param channelParamsInput Channel params object to validate
* @getters getResult(), getChannelParamsInput(), hasErrors(), getErrorsList()
* @result Channel params object or null
*/
class ValidateChannelParams {
result;
errors = false;
errmsg = [];
channelParamsInput;
constructor(channelParamsInput) {
/**
* validate each of these for "hue", "saturation", "lightness"
* 1. boundState is a boolean
* 2. upperboundDivider is a number between BOUND_LIMITS.UPPERBOUND_DIVIDER.MIN and BOUND_LIMITS.UPPERBOUND_DIVIDER.MAX
* 3. lowerboundDivider is a number between BOUND_LIMITS.LOWERBOUND_DIVIDER.MIN and BOUND_LIMITS.LOWERBOUND_DIVIDER.MAX
* 4. upperboundPadding is a number between BOUND_LIMITS.UPPERBOUND_PADDING.MIN and BOUND_LIMITS.UPPERBOUND_PADDING.MAX
* 5. lowerboundPadding is a number between BOUND_LIMITS.LOWERBOUND_PADDING.MIN and BOUND_LIMITS.LOWERBOUND_PADDING.MAX
*/
this.channelParamsInput = channelParamsInput.map((channelParam) => {
let { useBounds, upperboundDivider, lowerboundDivider, upperboundPadding, lowerboundPadding } = channelParam;
useBounds = (0, utils_1.toBoolean)(useBounds);
upperboundDivider = (0, utils_1.toNumber)(upperboundDivider);
lowerboundDivider = (0, utils_1.toNumber)(lowerboundDivider);
upperboundPadding = (0, utils_1.toNumber)(upperboundPadding);
lowerboundPadding = (0, utils_1.toNumber)(lowerboundPadding);
if (!this.isBoundStateTypeValid(useBounds) || !useBounds)
return;
if (!this.isBoundValueTypeValid(upperboundDivider))
return;
if (!this.isBoundValueTypeValid(lowerboundDivider))
return;
if (!this.isBoundValueTypeValid(upperboundPadding))
return;
if (!this.isBoundValueTypeValid(lowerboundPadding))
return;
if (!this.isBoundWithinRange(upperboundDivider, constants_1.BOUND_LIMITS.MIN_UPPERBOUND_DIVIDER, constants_1.BOUND_LIMITS.MAX_UPPERBOUND_DIVIDER))
return;
if (!this.isBoundWithinRange(lowerboundDivider, constants_1.BOUND_LIMITS.MIN_LOWERBOUND_DIVIDER, constants_1.BOUND_LIMITS.MAX_LOWERBOUND_DIVIDER))
return;
if (!this.isBoundWithinRange(upperboundPadding, constants_1.BOUND_LIMITS.MIN_UPPERBOUND_PADDING, constants_1.BOUND_LIMITS.MAX_UPPERBOUND_PADDING))
return;
if (!this.isBoundWithinRange(lowerboundPadding, constants_1.BOUND_LIMITS.MIN_LOWERBOUND_PADDING, constants_1.BOUND_LIMITS.MAX_LOWERBOUND_PADDING))
return;
return {
...channelParam,
useBounds,
upperboundDivider,
lowerboundDivider,
upperboundPadding,
lowerboundPadding,
};
});
if (!this.errors) {
this.result = this.channelParamsInput;
}
}
get getResult() {
return this.result;
}
get getChannelParamsInput() {
return this.channelParamsInput;
}
get hasErrors() {
return this.errors;
}
get getErrorsList() {
return this.errmsg;
}
addError(msg) {
this.errmsg.push(msg);
}
setErrorState() {
this.errors = true;
}
showErrors() {
this.errmsg.forEach((msg) => {
console.log(msg);
});
}
isBoundStateTypeValid(boundState) {
if (typeof boundState !== "boolean") {
this.addError("Bound state must be a boolean.");
this.setErrorState();
return false;
}
return true;
}
isBoundValueTypeValid(boundValue) {
if (typeof boundValue !== "number") {
this.addError("Bound value must be a number.");
this.setErrorState();
return false;
}
return true;
}
isBoundWithinRange(testValue, boundMin, boundMax) {
if (testValue <= boundMin || testValue >= boundMax) {
this.addError(`Bound value must be between ${boundMin} and ${boundMax}.`);
this.setErrorState();
return false;
}
return true;
}
}
exports.default = ValidateChannelParams;
//# sourceMappingURL=ValidateChannelParams.js.map