@itrocks/class-type
Version:
Helper types and functions to identify, validate, and manipulate classes, objects, prototypes and their properties
68 lines • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addReservedClassNames = addReservedClassNames;
exports.baseType = baseType;
exports.inherits = inherits;
exports.isAnyFunction = isAnyFunction;
exports.isAnyFunctionOrType = isAnyFunctionOrType;
exports.isAnyObject = isAnyObject;
exports.isAnyType = isAnyType;
exports.isObject = isObject;
exports.isType = isType;
exports.prototypeTargetOf = prototypeTargetOf;
exports.typeIdentifier = typeIdentifier;
exports.typeOf = typeOf;
const reservedClassNames = [];
const typeIdentifiers = new WeakMap();
function addReservedClassNames(...classNames) {
reservedClassNames.push(...classNames);
}
function baseType(target) {
while (!target.name.length || reservedClassNames.includes(target.name)) {
target = Object.getPrototypeOf(target);
}
return target;
}
function inherits(type, superType) {
while (type) {
if (type === superType)
return true;
type = Object.getPrototypeOf(type);
}
return false;
}
function isAnyFunction(value) {
return ((typeof value)[0] === 'f') && ((value + '')[0] !== 'c');
}
function isAnyFunctionOrType(value) {
return (typeof value)[0] === 'f';
}
function isAnyObject(value) {
return value && ((typeof value)[0] === 'o');
}
function isAnyType(value) {
return ((typeof value)[0] === 'f') && ((value + '')[0] === 'c');
}
function isObject(target) {
return (typeof target)[0] === 'o';
}
function isType(target) {
return (typeof target)[0] === 'f';
}
function prototypeTargetOf(target) {
return isType(target) ? target.prototype : target;
}
function typeIdentifier(type) {
let identifier = typeIdentifiers.get(type);
if (identifier)
return identifier;
identifier = Symbol(type.name || 'anonymous');
typeIdentifiers.set(type, identifier);
return identifier;
}
function typeOf(target) {
return isObject(target)
? Object.getPrototypeOf(target).constructor
: target;
}
//# sourceMappingURL=class-type.js.map