@itrocks/class-type
Version:
Helper types and functions to identify, validate, and manipulate classes, objects, prototypes and their properties
72 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.baseType = void 0;
exports.addReservedClassNames = addReservedClassNames;
exports.inherits = inherits;
exports.isAnyFunction = isAnyFunction;
exports.isAnyFunctionOrType = isAnyFunctionOrType;
exports.isAnyObject = isAnyObject;
exports.isAnyType = isAnyType;
exports.isObject = isObject;
exports.isType = isType;
exports.prototypeOf = prototypeOf;
exports.typeIdentifier = typeIdentifier;
exports.typeOf = typeOf;
const reservedClassNames = [];
function addReservedClassNames(...classNames) {
reservedClassNames.push(...classNames);
if (exports.baseType !== baseTypeWithReservedClassNames) {
exports.baseType = baseTypeWithReservedClassNames;
}
}
let baseType = (target) => {
while (!target.name.length) {
target = Object.getPrototypeOf(target);
}
return target;
};
exports.baseType = baseType;
function baseTypeWithReservedClassNames(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 prototypeOf(target) {
return isType(target) ? target.prototype : target;
}
function typeIdentifier(type) {
return Symbol.for(type.prototype.constructor.name);
}
function typeOf(target) {
return isObject(target)
? Object.getPrototypeOf(target).constructor
: target;
}
//# sourceMappingURL=class-type.js.map