merge-change
Version:
Advanced library for deep merging, patching, and immutable updates of data structures. Features declarative operations for specific merging behaviors, property management, custom type merging rules, and difference tracking. Supports complex data transform
74 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.type = type;
exports.typeChain = typeChain;
exports.isInstanceof = isInstanceof;
const toLowerCase = new Set(['Number', 'String', 'Boolean', 'Object', 'Symbol']);
/**
* Type of value - constructor name
* @param value
*/
function type(value) {
if (value === null) {
return 'null';
}
if (typeof value === 'undefined') {
return 'undefined';
}
if (typeof value === 'object' && !('__proto__' in value)) {
return 'object';
}
const name = Object.getPrototypeOf(value).constructor.name;
return toLowerCase.has(name) ? name.toLowerCase() : name;
}
/**
* All instance types along their inheritance chain
* @param value
*/
function typeChain(value) {
const result = [];
if (value === null) {
result.push('null');
}
else if (typeof value === 'undefined') {
result.push('undefined');
}
else {
const getClass = (value) => {
if (value && value.constructor) {
const name = value.constructor.name;
result.push(toLowerCase.has(name) ? name.toLowerCase() : name);
getClass(Object.getPrototypeOf(value));
}
};
getClass(Object.getPrototypeOf(value));
}
return result;
}
/**
* Checking if a value belongs to a class (constructor) by the string name of the class (constructor)
* @param value Value to check
* @param className Name of the class (constructor)
*/
function isInstanceof(value, className) {
if (value === null) {
return className === 'null';
}
else {
const hasClass = (value) => {
if (value && value.constructor) {
const name = value.constructor.name;
const lowerCaseName = toLowerCase.has(name) ? name.toLowerCase() : name;
if (className === lowerCaseName) {
return true;
}
return hasClass(Object.getPrototypeOf(value));
}
else {
return false;
}
};
return hasClass(Object.getPrototypeOf(value));
}
}
//# sourceMappingURL=index.js.map