@thangk/easythemer
Version:
Easily generate shades from a colour palette for use in your app
66 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @param {T} object1
* @param {T} object2
* @getters getResult(), hasErrors(), getErrorsList()
* @result boolean
* @description
* This class is used to validate if two objects have the same properties by checking if they object with the most properties has all the properties of the object with the least properties.
*/
class ValidateSameObjectType {
result = false;
errors = false;
errmsg = [];
constructor(object1, object2) {
this.result = this.haveCommonProperties(object1, object2);
}
get getResult() {
return this.result;
}
get hasErrors() {
return this.errors;
}
get getErrorsList() {
return this.errmsg;
}
showErrors() {
this.errmsg.forEach((msg) => {
console.log(msg);
});
}
addError(msg) {
this.errmsg.push(msg);
}
setErrorState() {
this.errors = true;
}
haveCommonProperties(object1, object2) {
let currentKeysCount = 0;
const { minKeyLen, minKeyObj, maxKeyObj } = this.getMinLenObj(object1, object2);
for (const [key, value] of Object.entries(minKeyObj)) {
if (key in maxKeyObj) {
currentKeysCount++;
}
}
if (currentKeysCount === minKeyLen)
return true;
this.addError("Object keys size are not equal.");
this.setErrorState();
return false;
}
getMinLenObj(object1, object2) {
const keysLen1 = Object.keys(object1).length;
const keysLen2 = Object.keys(object2).length;
const minKeyLen = keysLen1 < keysLen2 ? keysLen1 : keysLen2;
const minKeyObj = keysLen1 < keysLen2 ? object1 : object2;
const maxKeyObj = keysLen1 < keysLen2 ? object2 : object1;
return {
minKeyLen,
minKeyObj,
maxKeyObj,
};
}
}
exports.default = ValidateSameObjectType;
//# sourceMappingURL=ValidateSameObjectType.js.map