@thangk/easythemer
Version:
Easily generate shades from a colour palette for use in your app
73 lines • 2.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MAX_COLOUR_NAME_LEN = 20;
const MIN_COLOUR_NAME_LEN = 2;
/**
* Validate colour name.
* - Allowed characters: letters, numbers, spaces, underscores, and hyphens.
* - Min length: 2 characters.
* - Max length: 20 characters.
*
* @param colourName Colour name string to validate.
* @getters getResult(), getColourName(), hasErrors(), getErrorsList()
* @result string or null
*/
class ValidateColourName {
result = null;
errors = false;
errmsg = [];
colourName = "";
constructor(colourName) {
this.colourName = colourName;
if (this.isLenTooLong() || this.isLenTooShort()) {
this.setErrorState();
return;
}
const regex = /^[a-zA-Z0-9 _-]{2,20}$/;
if (!regex.test(this.colourName)) {
this.setErrorState();
this.addError("Colour name is not valid. Only letters, numbers, spaces, underscores, and hyphens are allowed. Between 2 and 20 characters long.");
return;
}
this.result = this.colourName;
}
get getResult() {
return this.result;
}
get getColourName() {
return this.colourName;
}
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);
});
}
isLenTooLong() {
if (this.colourName.length > MAX_COLOUR_NAME_LEN) {
this.addError(`Colour name is too long. Max length is ${MAX_COLOUR_NAME_LEN}.`);
return true;
}
return false;
}
isLenTooShort() {
if (this.colourName.length < MIN_COLOUR_NAME_LEN) {
this.addError(`Colour name is too short. Min length is ${MIN_COLOUR_NAME_LEN}.`);
return true;
}
return false;
}
}
exports.default = ValidateColourName;
//# sourceMappingURL=ValidateColourName.js.map