typescript-class-helpers
Version:
Usefull helper to have in your typescript project
100 lines • 3.96 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.setClassName = setClassName;
const lib_1 = require("tnp-core/lib");
const symbols_1 = require("./symbols");
const storage_1 = require("./storage");
const classname_1 = require("./classname");
const index_1 = require("./index");
function getClasses() {
const s = (0, storage_1.getStorage)();
return s[symbols_1.SYMBOL.CLASSES];
}
// @ts-ignore
function setClassName(target, className, options) {
let { classFamily, uniqueKey, classNameInBrowser, singleton } = options || {
classFamily: void 0,
uniqueKey: 'id',
classNameInBrowser: void 0,
singleton: void 0,
autoinstance: false
};
if (!lib_1._.isUndefined(singleton) && lib_1._.isBoolean(singleton) && singleton) {
singleton = 'first-instance';
}
if (!uniqueKey) {
uniqueKey = 'id';
}
if (target) {
const config = lib_1._.first(classname_1.CLASSNAME.getClassConfig(target));
config.className = className;
config.uniqueKey = uniqueKey;
config.classNameInBrowser = classNameInBrowser;
// console.log(`Setting class Name to "${target.name}"`)
}
const existed = getClasses()
.find(f => f.className === className);
if (existed) {
existed.target = target;
}
else {
const res = {
className,
classNameInBrowser,
target,
uniqueKey,
classFamily
};
if (lib_1._.isUndefined(classFamily)) {
Object.defineProperty(res, 'classFamily', {
get: function () {
const parent = Object.getPrototypeOf(target);
if (!lib_1._.isFunction(parent) || parent.name === 'Object' || parent.name === '') {
return className;
}
const classNameNew = classname_1.CLASSNAME.getClassName(parent);
return classname_1.CLASSNAME.getClassFamilyByClassName(classNameNew);
}
});
}
getClasses().push(res);
}
const Original = target;
if (singleton === 'first-instance' || singleton === 'last-instance') {
const obj = {
// @ts-ignore
decoratedConstructor: function (...args) {
// console.log(`DECORATED CONSTRUCTOR OF ${Original.name}`)
const context = Original.apply(this, args);
const existedSingleton = index_1.CLASS.getSingleton(Original);
if (!existedSingleton || singleton === 'last-instance') {
index_1.CLASS.setSingletonObj(Original, this);
index_1.CLASS.setSingletonObj(obj.decoratedConstructor, this);
// console.log(`Singleton created for "${className}", mode: ${singleton} `);
}
else {
// console.log('ingleton exists')
}
return context;
}
};
// copy prototype so intanceof operator still works
obj.decoratedConstructor.prototype = Original.prototype;
// @ts-ignore
Object.keys(Original).forEach((name) => { obj.decoratedConstructor[name] = Original[name]; });
Object.defineProperty(obj.decoratedConstructor, 'name', {
value: className,
configurable: true,
});
// (obj.decoratedConstructor as any).name = className;
// console.log('return new contruor', decoratedConstructor)
return obj.decoratedConstructor;
}
else if (singleton === 'autoinstance') {
// console.log(`AUTOINSTANCE FOR ${target.name}`)
const auto = new Original();
index_1.CLASS.setSingletonObj(Original, auto);
// console.log(`Singleton created for "${className}", mode: ${singleton} `)
}
}
//# sourceMappingURL=set-class-name.js.map
;