@itrocks/class-type
Version:
Helper types and functions to identify, validate, and manipulate classes, objects, prototypes and their properties
57 lines • 1.69 kB
JavaScript
const reservedClassNames = [];
export function addReservedClassNames(...classNames) {
reservedClassNames.push(...classNames);
if (baseType !== baseTypeWithReservedClassNames) {
baseType = baseTypeWithReservedClassNames;
}
}
export let baseType = (target) => {
while (!target.name.length) {
target = Object.getPrototypeOf(target);
}
return target;
};
function baseTypeWithReservedClassNames(target) {
while (!target.name.length || reservedClassNames.includes(target.name)) {
target = Object.getPrototypeOf(target);
}
return target;
}
export function inherits(type, superType) {
while (type) {
if (type === superType)
return true;
type = Object.getPrototypeOf(type);
}
return false;
}
export function isAnyFunction(value) {
return ((typeof value)[0] === 'f') && ((value + '')[0] !== 'c');
}
export function isAnyFunctionOrType(value) {
return (typeof value)[0] === 'f';
}
export function isAnyObject(value) {
return value && ((typeof value)[0] === 'o');
}
export function isAnyType(value) {
return ((typeof value)[0] === 'f') && ((value + '')[0] === 'c');
}
export function isObject(target) {
return (typeof target)[0] === 'o';
}
export function isType(target) {
return (typeof target)[0] === 'f';
}
export function prototypeOf(target) {
return isType(target) ? target.prototype : target;
}
export function typeIdentifier(type) {
return Symbol.for(type.prototype.constructor.name);
}
export function typeOf(target) {
return isObject(target)
? Object.getPrototypeOf(target).constructor
: target;
}
//# sourceMappingURL=class-type.js.map