UNPKG

typescript-helper-functions

Version:
190 lines 6.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectOperations = void 0; class ObjectOperations { TextDecoder; TextEncoder; constructor() { this.TextDecoder = new TextDecoder(); this.TextEncoder = new TextEncoder(); } AreObjectsEqual(object1, object2, ...excludeList) { const excludes = excludeList || []; const propNames = [ ...new Set(Object.getOwnPropertyNames(object1).concat(Object.getOwnPropertyNames(object2))), ]; const diffList = propNames .filter((column) => excludes.indexOf(column) < 0) .filter((column) => !this.IsPropertyEqual(object1, object2, column)); return diffList.length < 1; } CloneObject(source, ...excludeList) { const result = Object.assign({}, source); if (excludeList) { excludeList.forEach((val) => { result[val] = undefined; }); } return this.RemoveUndefinedElements(result); } ConvertArrayBufferToString(value) { if (this.IsNullOrEmpty(value)) { throw new Error(`Value cannot be null or undefined: [value]`); } return this.TextDecoder.decode(value); } ConvertStringToArrayBuffer(value) { if (this.IsNullOrWhitespace(value)) { throw new Error(`Value cannot be null or undefined: [value]`); } return this.TextEncoder.encode(value); } CopyProperty(source, target, propertyName, append = false) { if (this.IsNullOrEmpty(source)) { throw new Error(`Value cannot be null or undefined: [source]`); } if (this.IsNullOrEmpty(target)) { throw new Error(`Value cannot be null or undefined: [target]`); } if (this.IsNullOrWhitespace(propertyName)) { throw new Error(`Value cannot be null or undefined: [propertyName]`); } if (!source.hasOwnProperty(propertyName)) { return; } if (append || target.hasOwnProperty(propertyName)) { target[propertyName] = source[propertyName]; } } GetProperty(source, propertyName, defaultValue) { if (this.IsNullOrEmpty(source)) { throw new Error(`Value cannot be null or undefined: [source]`); } if (this.IsNullOrWhitespace(propertyName)) { return defaultValue; } const key = Object.keys(source).find((k) => k.toLowerCase() === propertyName.toLowerCase()); const result = key ? source[key] : undefined; return result || defaultValue; } IsMatch(pattern, value) { if (this.IsNullOrWhitespace(pattern)) { throw new Error(`Value cannot be null or undefined: [pattern]`); } if (this.IsNullOrWhitespace(value)) { throw new Error(`Value cannot be null or undefined: [value]`); } pattern = pattern.toUpperCase(); value = value.toUpperCase(); if (pattern === '*') { return true; } if (pattern.startsWith('!') && pattern.substr(1) !== value) { return true; } if (pattern.endsWith('*') && value.startsWith(pattern.substr(0, pattern.length - 1))) { return true; } if (pattern.startsWith('*') && value.endsWith(pattern.substr(1))) { return true; } return pattern === value; } IsNullOrEmpty(value) { switch (typeof value) { case 'object': { const o = value || {}; return Object.keys(o).length === 0; } case 'string': { const s = value || ''; return s.length < 1; } default: { return true; } } } IsNullOrWhitespace(value) { return this.IsNullOrEmpty((value || '').trim()); } IsPropertyEqual(item1, item2, propertyName) { if (this.IsNullOrEmpty(item1)) { throw new Error(`Value cannot be null or undefined: [item1]`); } if (this.IsNullOrEmpty(item2)) { throw new Error(`Value cannot be null or undefined: [item2]`); } if (this.IsNullOrWhitespace(propertyName)) { throw new Error(`Value cannot be null or undefined: [propertyName]`); } const oldValue = item1[propertyName]; const newValue = item2[propertyName]; return Object.is(oldValue, newValue); } MergeObjects(object1, object2) { const result = {}; if (object1) { Object.getOwnPropertyNames(object1).forEach((name) => this.MergeObjectProperty(result, object1, object2, name)); } if (object2) { Object.getOwnPropertyNames(object2).forEach((name) => this.MergeObjectProperty(result, object1, object2, name)); } return result; } MergeObjectProperty(target, source1, source2, propertyName) { const value1 = source1 && source1.hasOwnProperty(propertyName) ? source1[propertyName] : undefined; const value2 = source2 && source2.hasOwnProperty(propertyName) ? source2[propertyName] : undefined; const dest = target; if (typeof value1 === 'object' && !Array.isArray(value1)) { dest[propertyName] = this.MergeObjects(value1, value2); } else if (value2 === undefined) { dest[propertyName] = value1; } else { dest[propertyName] = value2; } } RemoveUndefinedElements(source) { const result = Object.assign({}, source); Object.getOwnPropertyNames(source).forEach((val) => { const value = source[val]; if (value === undefined) { delete result[val]; } }); return result; } SetProperty(target, propertyName, value, append = true) { if (this.IsNullOrEmpty(target)) { throw new Error(`Value cannot be null or undefined: [target]`); } propertyName = propertyName || ''; if (this.IsNullOrWhitespace(propertyName)) { return; } if (append || target.hasOwnProperty(propertyName)) { target[propertyName] = value; } } ToString(value) { switch (typeof value) { case 'boolean': case 'number': case 'string': return value.toString(); case 'object': return JSON.stringify(value); default: return value || ''; } } } exports.ObjectOperations = ObjectOperations; //# sourceMappingURL=object-operations.js.map