UNPKG

@thangk/easythemer

Version:

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

60 lines (47 loc) 1.33 kB
/** * @param object * @getters getResult(), hasErrors(), getErrorsList() * @result boolean * @description Validates if object is an object or an array */ export default class ValidateIsObject { private result: boolean = false; private errors: boolean = false; private errmsg: string[] = []; constructor(object: any) { this.result = this.isObject(object); } get getResult() { return this.result; } get hasErrors() { return this.errors; } get getErrorsList() { return this.errmsg; } private showErrors() { this.errmsg.forEach((msg) => { console.log(msg); }); } private addError(msg: string) { this.errmsg.push(msg); } private setErrorState() { this.errors = true; } private isObject(object: any) { const firstIndexElement = Object.keys(object)[0]; console.log("firstIndexElement", firstIndexElement); // non numbers return NaN after doing with Number(), so we can check if it is a number const result = Number(firstIndexElement) ? false : true; if (result) { // result returns NaN, therefore it's not a number, thus it's an object return true; } this.addError("Object is not an object."); this.setErrorState(); return false; } }