power-di
Version:
A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.
62 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.symbolString = exports.isExtendOf = exports.getGlobalType = exports.isClass = void 0;
var class_1 = require("../class");
var getSuperClassInfo_1 = require("./getSuperClassInfo");
var _uid = 0;
var _globalTypes = {};
function isClass(target) {
return (target === null || target === void 0 ? void 0 : target.prototype) && target.prototype.constructor === target;
// If browser, maybe no class
// getPrototypeOf a class, is not Function, not instanceof Function, but typeof 'function'
}
exports.isClass = isClass;
/**
* getGlobalType
* @param key class, string or symbol.
* @param prefix the prefix of type.
*/
function getGlobalType(key, prefix) {
if (prefix === void 0) { prefix = ''; }
if (!key)
throw new Error('no key.');
// object for ie11 symbol
if (['string', 'symbol', 'object'].includes(typeof key)) {
return key;
}
if (key.hasOwnProperty('__type')) {
return key['__type'];
}
var type;
var keyName = key[class_1.nameSymbol] || key.name;
if (typeof key !== 'function' || !keyName) {
// Only for compatible with es5 class
throw new Error('data MUST be a class or string.');
}
type = prefix + keyName;
if (_globalTypes[type]) {
type = type + '_' + _uid++;
}
_globalTypes[type] = true;
Object.defineProperty(key, '__type', {
configurable: false,
enumerable: false,
writable: false,
value: type,
});
return type;
}
exports.getGlobalType = getGlobalType;
function isExtendOf(classType, superClassType) {
if (!isClass(classType) || !isClass(superClassType)) {
throw new Error('classType and superClassType MUST be a class.');
}
var type = getGlobalType(superClassType);
return !!(0, getSuperClassInfo_1.getSuperClassInfo)(classType).find(function (cls) { return cls.type === type; });
}
exports.isExtendOf = isExtendOf;
function symbolString(key) {
return typeof key === 'symbol' ? key.toString() : key;
}
exports.symbolString = symbolString;
//# sourceMappingURL=getGlobalType.js.map