typescript-class-helpers
Version:
Usefull helper to have in your typescript project
77 lines • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.describeFromClassStringify = describeFromClassStringify;
exports.describeByDefaultModelsAndMapping = describeByDefaultModelsAndMapping;
const lib_1 = require("tnp-core/lib");
const symbols_1 = require("./symbols");
/**
* @DEPRECATED
* Describe fields assigned in class
*/
const FRegEx = new RegExp(/(?:this\.)(.+?(?= ))/g);
function describeFromClassStringify(target, parent = false) {
// @ts-ignore
var result = [];
if (parent) {
var proto = Object.getPrototypeOf(target.prototype);
if (proto) {
// @ts-ignore
result = result.concat(describeFromClassStringify(proto.constructor, parent));
}
}
const classString = target.toString();
const matches = classString.match(FRegEx) || [];
// console.log({
// classString,
// });
result = result.concat(matches);
return result.map(prop => prop
.replace('this.', '')
.replace('.', '')
.replace(')', '')
.replace('(', ''));
}
/**
* Describe fields assigne through @DefaultModelWithMapping decorator
* without functions
*/
function describeByDefaultModelsAndMapping(target) {
let res = {};
if (target) {
// @ts-ignore
if (target[symbols_1.SYMBOL.DEFAULT_MODEL]) {
// @ts-ignore
Object.keys(target[symbols_1.SYMBOL.DEFAULT_MODEL])
.filter(key => {
// @ts-ignore
return !lib_1._.isFunction(target[symbols_1.SYMBOL.DEFAULT_MODEL][key]);
})
.forEach(key => {
// @ts-ignore
return res[key] = null;
});
}
// @ts-ignore
let mapping = target[symbols_1.SYMBOL.MODELS_MAPPING];
if (lib_1._.isArray(mapping)) {
mapping.forEach(m => {
Object.keys(m)
.forEach(key => {
// @ts-ignore
res[key] = null;
});
});
}
else if (mapping) {
Object.keys(mapping)
.forEach(key => {
// @ts-ignore
res[key] = null;
});
}
}
let propNames = Object.keys(res).filter(f => !!f);
propNames = (!propNames.includes('id') ? ['id'] : []).concat(propNames);
return propNames;
}
//# sourceMappingURL=describe-class.js.map