UNPKG

@thangk/easythemer

Version:

Easily generate shades from a colour palette for use in your app

87 lines (71 loc) 2.05 kB
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 */ export default class ValidateOptionName { private result: string | null = null; private errors: boolean = false; private errmsg: string[] = []; private colourName: string = ""; constructor(colourName: string) { 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; } private addError(msg: string) { this.errmsg.push(msg); } private setErrorState() { this.errors = true; } private showErrors() { this.errmsg.forEach((msg) => { console.log(msg); }); } private 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; } private 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; } }