@itrocks/class-type
Version:
Helper types and functions to identify, validate, and manipulate classes, objects, prototypes and their properties
54 lines • 1.61 kB
JavaScript
const reservedClassNames = [];
const typeIdentifiers = new WeakMap();
export function addReservedClassNames(...classNames) {
reservedClassNames.push(...classNames);
}
export function baseType(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 prototypeTargetOf(target) {
return isType(target) ? target.prototype : target;
}
export function typeIdentifier(type) {
let identifier = typeIdentifiers.get(type);
if (identifier)
return identifier;
identifier = Symbol(type.name || 'anonymous');
typeIdentifiers.set(type, identifier);
return identifier;
}
export function typeOf(target) {
return isObject(target)
? Object.getPrototypeOf(target).constructor
: target;
}
//# sourceMappingURL=class-type.js.map