typescript-class-helpers 21.0.13 → 21.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/package.json +1 -1
- package/browser-prod/README.md +24 -0
- package/browser-prod/fesm2022/typescript-class-helpers-browser.mjs +623 -0
- package/browser-prod/fesm2022/typescript-class-helpers-browser.mjs.map +1 -0
- package/browser-prod/types/typescript-class-helpers-browser.d.ts +156 -0
- package/lib/build-info._auto-generated_.d.ts +1 -1
- package/lib/build-info._auto-generated_.js +1 -1
- package/lib/env/env.angular-node-app.d.ts +30 -28
- package/lib/env/env.angular-node-app.js +32 -30
- package/lib/env/env.angular-node-app.js.map +1 -1
- package/lib/env/env.docs-webapp.d.ts +30 -28
- package/lib/env/env.docs-webapp.js +32 -30
- package/lib/env/env.docs-webapp.js.map +1 -1
- package/lib/env/env.electron-app.d.ts +30 -28
- package/lib/env/env.electron-app.js +32 -30
- package/lib/env/env.electron-app.js.map +1 -1
- package/lib/env/env.mobile-app.d.ts +30 -28
- package/lib/env/env.mobile-app.js +32 -30
- package/lib/env/env.mobile-app.js.map +1 -1
- package/lib/env/env.npm-lib-and-cli-tool.d.ts +30 -28
- package/lib/env/env.npm-lib-and-cli-tool.js +32 -30
- package/lib/env/env.npm-lib-and-cli-tool.js.map +1 -1
- package/lib/env/env.vscode-plugin.d.ts +30 -28
- package/lib/env/env.vscode-plugin.js +32 -30
- package/lib/env/env.vscode-plugin.js.map +1 -1
- package/lib/index._auto-generated_.d.ts +0 -1
- package/lib-prod/build-info._auto-generated_.d.ts +24 -0
- package/lib-prod/build-info._auto-generated_.js +30 -0
- package/lib-prod/build-info._auto-generated_.js.map +1 -0
- package/lib-prod/classname.d.ts +16 -0
- package/lib-prod/classname.js +198 -0
- package/lib-prod/classname.js.map +1 -0
- package/lib-prod/describe-class.d.ts +6 -0
- package/lib-prod/describe-class.js +77 -0
- package/lib-prod/describe-class.js.map +1 -0
- package/lib-prod/env/env.angular-node-app.d.ts +64 -0
- package/lib-prod/env/env.angular-node-app.js +71 -0
- package/lib-prod/env/env.angular-node-app.js.map +1 -0
- package/lib-prod/env/env.docs-webapp.d.ts +64 -0
- package/lib-prod/env/env.docs-webapp.js +71 -0
- package/lib-prod/env/env.docs-webapp.js.map +1 -0
- package/lib-prod/env/env.electron-app.d.ts +64 -0
- package/lib-prod/env/env.electron-app.js +71 -0
- package/lib-prod/env/env.electron-app.js.map +1 -0
- package/lib-prod/env/env.mobile-app.d.ts +64 -0
- package/lib-prod/env/env.mobile-app.js +71 -0
- package/lib-prod/env/env.mobile-app.js.map +1 -0
- package/lib-prod/env/env.npm-lib-and-cli-tool.d.ts +64 -0
- package/lib-prod/env/env.npm-lib-and-cli-tool.js +71 -0
- package/lib-prod/env/env.npm-lib-and-cli-tool.js.map +1 -0
- package/lib-prod/env/env.vscode-plugin.d.ts +64 -0
- package/lib-prod/env/env.vscode-plugin.js +71 -0
- package/lib-prod/env/env.vscode-plugin.js.map +1 -0
- package/lib-prod/env/index.d.ts +6 -0
- package/lib-prod/env/index.js +23 -0
- package/lib-prod/env/index.js.map +1 -0
- package/lib-prod/errors-messages.d.ts +1 -0
- package/lib-prod/errors-messages.js +15 -0
- package/lib-prod/errors-messages.js.map +1 -0
- package/lib-prod/index._auto-generated_.d.ts +0 -0
- package/lib-prod/index._auto-generated_.js +6 -0
- package/lib-prod/index._auto-generated_.js.map +1 -0
- package/lib-prod/index.d.ts +48 -0
- package/lib-prod/index.js +200 -0
- package/lib-prod/index.js.map +1 -0
- package/lib-prod/models.d.ts +83 -0
- package/lib-prod/models.js +63 -0
- package/lib-prod/models.js.map +1 -0
- package/lib-prod/registerd-db.d.ts +5 -0
- package/lib-prod/registerd-db.js +8 -0
- package/lib-prod/registerd-db.js.map +1 -0
- package/lib-prod/set-class-name.d.ts +2 -0
- package/lib-prod/set-class-name.js +100 -0
- package/lib-prod/set-class-name.js.map +1 -0
- package/lib-prod/storage.d.ts +1 -0
- package/lib-prod/storage.js +27 -0
- package/lib-prod/storage.js.map +1 -0
- package/lib-prod/symbols.d.ts +7 -0
- package/lib-prod/symbols.js +12 -0
- package/lib-prod/symbols.js.map +1 -0
- package/migrations/migrations_index._auto-generated_.d.ts +0 -1
- package/package.json +1 -1
- package/package.json_devDependencies.json +223 -0
- package/package.json_tnp.json5 +56 -0
- package/tmp-environment.json +11 -5
- package/websql/package.json +1 -1
- package/websql-prod/README.md +24 -0
- package/websql-prod/fesm2022/typescript-class-helpers-websql.mjs +622 -0
- package/websql-prod/fesm2022/typescript-class-helpers-websql.mjs.map +1 -0
- package/websql-prod/types/typescript-class-helpers-websql.d.ts +157 -0
package/browser/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# MyLib
|
|
2
|
+
|
|
3
|
+
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 13.2.0.
|
|
4
|
+
|
|
5
|
+
## Code scaffolding
|
|
6
|
+
|
|
7
|
+
Run `ng generate component component-name --project my-lib` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project my-lib`.
|
|
8
|
+
> Note: Don't forget to add `--project my-lib` or else it will be added to the default project in your `angular.json` file.
|
|
9
|
+
|
|
10
|
+
## Build
|
|
11
|
+
|
|
12
|
+
Run `ng build my-lib` to build the project. The build artifacts will be stored in the `dist/` directory.
|
|
13
|
+
|
|
14
|
+
## Publishing
|
|
15
|
+
|
|
16
|
+
After building your library with `ng build my-lib`, go to the dist folder `cd dist/my-lib` and run `npm publish`.
|
|
17
|
+
|
|
18
|
+
## Running unit tests
|
|
19
|
+
|
|
20
|
+
Run `ng test my-lib` to execute the unit tests via [Karma](https://karma-runner.github.io).
|
|
21
|
+
|
|
22
|
+
## Further help
|
|
23
|
+
|
|
24
|
+
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
|
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
import { CoreModels, _, Helpers } from 'tnp-core/browser-prod';
|
|
2
|
+
|
|
3
|
+
var Models;
|
|
4
|
+
(function (Models) {
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated
|
|
7
|
+
*/
|
|
8
|
+
class ParamConfig {
|
|
9
|
+
}
|
|
10
|
+
Models.ParamConfig = ParamConfig;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
class MethodConfig {
|
|
15
|
+
constructor() {
|
|
16
|
+
/* */
|
|
17
|
+
/* */
|
|
18
|
+
this.parameters = {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
Models.MethodConfig = MethodConfig;
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated
|
|
24
|
+
*/
|
|
25
|
+
class ClassConfig {
|
|
26
|
+
constructor() {
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
this.singleton = void 0;
|
|
29
|
+
this.injections = [];
|
|
30
|
+
this.vChildren = [];
|
|
31
|
+
this.methods = {};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
Models.ClassConfig = ClassConfig;
|
|
35
|
+
})(Models || (Models = {}));
|
|
36
|
+
|
|
37
|
+
const SYMBOL = {
|
|
38
|
+
MODELS_MAPPING: Symbol(),
|
|
39
|
+
DEFAULT_MODEL: Symbol(),
|
|
40
|
+
STORAGE: 'classesstorage',
|
|
41
|
+
CLASSES: 'classes',
|
|
42
|
+
ClassNameStaticProperty: CoreModels.ClassNameStaticProperty,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function defaultValues() {
|
|
46
|
+
return _.cloneDeep({
|
|
47
|
+
[SYMBOL.CLASSES]: []
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function getStorage(property) {
|
|
51
|
+
/* */
|
|
52
|
+
/* */
|
|
53
|
+
/* */
|
|
54
|
+
/* */
|
|
55
|
+
if (typeof property === 'string') {
|
|
56
|
+
const storageInClassStaticProp = getStorage();
|
|
57
|
+
return storageInClassStaticProp[property];
|
|
58
|
+
}
|
|
59
|
+
if (typeof defaultValues.prototype[SYMBOL.STORAGE] === 'undefined') {
|
|
60
|
+
defaultValues.prototype[SYMBOL.STORAGE] = defaultValues();
|
|
61
|
+
}
|
|
62
|
+
return defaultValues.prototype[SYMBOL.STORAGE];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getClasses$1() {
|
|
66
|
+
const s = getStorage();
|
|
67
|
+
return s[SYMBOL.CLASSES];
|
|
68
|
+
}
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
function setClassName(target, className, options) {
|
|
71
|
+
let { classFamily, uniqueKey, classNameInBrowser, singleton } = options || {
|
|
72
|
+
classFamily: void 0,
|
|
73
|
+
uniqueKey: 'id',
|
|
74
|
+
classNameInBrowser: void 0,
|
|
75
|
+
singleton: void 0,
|
|
76
|
+
autoinstance: false
|
|
77
|
+
};
|
|
78
|
+
if (!_.isUndefined(singleton) && _.isBoolean(singleton) && singleton) {
|
|
79
|
+
singleton = 'first-instance';
|
|
80
|
+
}
|
|
81
|
+
if (!uniqueKey) {
|
|
82
|
+
uniqueKey = 'id';
|
|
83
|
+
}
|
|
84
|
+
if (target) {
|
|
85
|
+
const config = _.first(CLASSNAME.getClassConfig(target));
|
|
86
|
+
config.className = className;
|
|
87
|
+
config.uniqueKey = uniqueKey;
|
|
88
|
+
config.classNameInBrowser = classNameInBrowser;
|
|
89
|
+
// console.log(`Setting class Name to "${target.name}"`)
|
|
90
|
+
}
|
|
91
|
+
const existed = getClasses$1()
|
|
92
|
+
.find(f => f.className === className);
|
|
93
|
+
if (existed) {
|
|
94
|
+
existed.target = target;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const res = {
|
|
98
|
+
className,
|
|
99
|
+
classNameInBrowser,
|
|
100
|
+
target,
|
|
101
|
+
uniqueKey,
|
|
102
|
+
classFamily
|
|
103
|
+
};
|
|
104
|
+
if (_.isUndefined(classFamily)) {
|
|
105
|
+
Object.defineProperty(res, 'classFamily', {
|
|
106
|
+
get: function () {
|
|
107
|
+
const parent = Object.getPrototypeOf(target);
|
|
108
|
+
if (!_.isFunction(parent) || parent.name === 'Object' || parent.name === '') {
|
|
109
|
+
return className;
|
|
110
|
+
}
|
|
111
|
+
const classNameNew = CLASSNAME.getClassName(parent);
|
|
112
|
+
return CLASSNAME.getClassFamilyByClassName(classNameNew);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
getClasses$1().push(res);
|
|
117
|
+
}
|
|
118
|
+
const Original = target;
|
|
119
|
+
if (singleton === 'first-instance' || singleton === 'last-instance') {
|
|
120
|
+
const obj = {
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
decoratedConstructor: function (...args) {
|
|
123
|
+
// console.log(`DECORATED CONSTRUCTOR OF ${Original.name}`)
|
|
124
|
+
const context = Original.apply(this, args);
|
|
125
|
+
const existedSingleton = CLASS.getSingleton(Original);
|
|
126
|
+
if (!existedSingleton || singleton === 'last-instance') {
|
|
127
|
+
CLASS.setSingletonObj(Original, this);
|
|
128
|
+
CLASS.setSingletonObj(obj.decoratedConstructor, this);
|
|
129
|
+
// console.log(`Singleton created for "${className}", mode: ${singleton} `);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
// console.log('ingleton exists')
|
|
133
|
+
}
|
|
134
|
+
return context;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
// copy prototype so intanceof operator still works
|
|
138
|
+
obj.decoratedConstructor.prototype = Original.prototype;
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
Object.keys(Original).forEach((name) => { obj.decoratedConstructor[name] = Original[name]; });
|
|
141
|
+
Object.defineProperty(obj.decoratedConstructor, 'name', {
|
|
142
|
+
value: className,
|
|
143
|
+
configurable: true,
|
|
144
|
+
});
|
|
145
|
+
// (obj.decoratedConstructor as any).name = className;
|
|
146
|
+
// console.log('return new contruor', decoratedConstructor)
|
|
147
|
+
return obj.decoratedConstructor;
|
|
148
|
+
}
|
|
149
|
+
else if (singleton === 'autoinstance') {
|
|
150
|
+
// console.log(`AUTOINSTANCE FOR ${target.name}`)
|
|
151
|
+
const auto = new Original();
|
|
152
|
+
CLASS.setSingletonObj(Original, auto);
|
|
153
|
+
// console.log(`Singleton created for "${className}", mode: ${singleton} `)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const registerd = {
|
|
158
|
+
configs: [],
|
|
159
|
+
classes: [],
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const ERROR_MSG_CLASS_WITHOUT_DECORATOR = `[typescript-class-helpers][getClassName(...)](PRODUCTION MODE ERROR)
|
|
163
|
+
Please use decoartor @CLASSNAME for each entity or controller
|
|
164
|
+
This is preventing class name problem in minified code.
|
|
165
|
+
|
|
166
|
+
import { CLASS } from 'typescript-class-helpers';
|
|
167
|
+
|
|
168
|
+
@CLASS.NAME('ExampleClass')
|
|
169
|
+
class ExampleClass {
|
|
170
|
+
...
|
|
171
|
+
}
|
|
172
|
+
`;
|
|
173
|
+
|
|
174
|
+
/* */
|
|
175
|
+
/* */
|
|
176
|
+
function getClasses() {
|
|
177
|
+
const s = getStorage();
|
|
178
|
+
return s[SYMBOL.CLASSES];
|
|
179
|
+
}
|
|
180
|
+
var CLASSNAME;
|
|
181
|
+
(function (CLASSNAME_1) {
|
|
182
|
+
function getClassConfig(target, configs = [], callerTarget) {
|
|
183
|
+
if (!_.isFunction(target)) {
|
|
184
|
+
throw `[typescript-class-helper][getClassConfig] Cannot get class config from: ${target}`;
|
|
185
|
+
}
|
|
186
|
+
let config;
|
|
187
|
+
const parentClass = Object.getPrototypeOf(target);
|
|
188
|
+
const isValidParent = _.isFunction(parentClass) && parentClass.name !== '';
|
|
189
|
+
if (registerd.classes.includes(target)) {
|
|
190
|
+
config =
|
|
191
|
+
registerd.configs[registerd.classes.findIndex(c => c === target)];
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
config = new Models.ClassConfig();
|
|
195
|
+
config.classReference = target;
|
|
196
|
+
registerd.classes.push(target);
|
|
197
|
+
}
|
|
198
|
+
registerd.configs[registerd.classes.findIndex(c => c === target)] = config;
|
|
199
|
+
if (callerTarget) {
|
|
200
|
+
const callerTargetConfig = registerd.configs[registerd.classes.findIndex(c => c === callerTarget)];
|
|
201
|
+
if (!config.vChildren.includes(callerTargetConfig)) {
|
|
202
|
+
config.vChildren.push(callerTargetConfig);
|
|
203
|
+
}
|
|
204
|
+
callerTargetConfig.vParent = config;
|
|
205
|
+
}
|
|
206
|
+
configs.push(config);
|
|
207
|
+
return isValidParent
|
|
208
|
+
? getClassConfig(parentClass, configs, target)
|
|
209
|
+
: configs;
|
|
210
|
+
}
|
|
211
|
+
CLASSNAME_1.getClassConfig = getClassConfig;
|
|
212
|
+
/**
|
|
213
|
+
* PLEASE PROVIDE NAME AS TYPED STRING, NOT VARIABLE
|
|
214
|
+
* Decorator requred for production mode
|
|
215
|
+
* @param name Name of class
|
|
216
|
+
*/
|
|
217
|
+
function CLASSNAME(className, options) {
|
|
218
|
+
return function (target) {
|
|
219
|
+
// console.log(`CLASSNAME Inited ${className}`)
|
|
220
|
+
return setClassName(target, className, options);
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
CLASSNAME_1.CLASSNAME = CLASSNAME;
|
|
224
|
+
function getClassName(target, production = false) {
|
|
225
|
+
if (_.isNil(target)) {
|
|
226
|
+
// console.log(target);
|
|
227
|
+
// Helpers.warn(`[typescript-class-helpers][getClassName] target is nil`)
|
|
228
|
+
return void 0;
|
|
229
|
+
}
|
|
230
|
+
if (_.isString(target)) {
|
|
231
|
+
// console.log(target);
|
|
232
|
+
Helpers.log(`[typescript-class-helpers][getClassName] target is string: '${target}', produciton: ${production}`);
|
|
233
|
+
return target;
|
|
234
|
+
}
|
|
235
|
+
if (target === Date) {
|
|
236
|
+
return 'Date';
|
|
237
|
+
}
|
|
238
|
+
if (target === FormData) {
|
|
239
|
+
return 'FormData';
|
|
240
|
+
}
|
|
241
|
+
if (!_.isFunction(target)) {
|
|
242
|
+
// console.log(target);
|
|
243
|
+
Helpers.log(`[typescript-class-helpers][getClassName] target is not a class`);
|
|
244
|
+
return void 0;
|
|
245
|
+
}
|
|
246
|
+
if (target[SYMBOL.ClassNameStaticProperty]) {
|
|
247
|
+
return target[SYMBOL.ClassNameStaticProperty];
|
|
248
|
+
}
|
|
249
|
+
const configs = getClassConfig(target);
|
|
250
|
+
const config = _.first(configs);
|
|
251
|
+
const classNameInBrowser = config?.classNameInBrowser;
|
|
252
|
+
if (Helpers.isBrowser && _.isString(classNameInBrowser)) {
|
|
253
|
+
return classNameInBrowser;
|
|
254
|
+
}
|
|
255
|
+
const className = config?.className;
|
|
256
|
+
if (typeof className === 'string') {
|
|
257
|
+
return className;
|
|
258
|
+
}
|
|
259
|
+
if (production) {
|
|
260
|
+
console.log('class without @CLASS.NAME deocrator', target);
|
|
261
|
+
throw new Error(ERROR_MSG_CLASS_WITHOUT_DECORATOR);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
// Helpers.log('check for ' + target.name)
|
|
265
|
+
// setTimeout(() => {
|
|
266
|
+
// // Helpers.log('check for ' + target.name + ' === ' + config.className)/
|
|
267
|
+
// // TODO this may work, but not yet in singleton/morphi
|
|
268
|
+
// if (!config.className) {
|
|
269
|
+
// if (target?.name && target.name !== 'Object') {
|
|
270
|
+
// ConfigHelpers.log(`[typescript-class-helpers] Please use @CLASS.NAME`
|
|
271
|
+
// + `('${(target?.name && !!target.name) ? target.name : '...'}') decorator for class ${target?.name}`)
|
|
272
|
+
// }
|
|
273
|
+
// }
|
|
274
|
+
// })
|
|
275
|
+
}
|
|
276
|
+
// special thing when cloning classes
|
|
277
|
+
if (target.name?.startsWith('class_')) {
|
|
278
|
+
return '';
|
|
279
|
+
}
|
|
280
|
+
return target.name;
|
|
281
|
+
}
|
|
282
|
+
CLASSNAME_1.getClassName = getClassName;
|
|
283
|
+
// @ts-ignore
|
|
284
|
+
function getObjectIndexPropertyValue(obj) {
|
|
285
|
+
const className = TchHelpers.getNameFromObject(obj);
|
|
286
|
+
// console.log('className',className)
|
|
287
|
+
let c = getClasses().find(c => c.className === className);
|
|
288
|
+
// console.log('c',c)
|
|
289
|
+
if (c) {
|
|
290
|
+
return c.uniqueKey;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
CLASSNAME_1.getObjectIndexPropertyValue = getObjectIndexPropertyValue;
|
|
294
|
+
// @ts-ignore
|
|
295
|
+
function getClassFamilyByClassName(className) {
|
|
296
|
+
let c = getClasses().find(c => c.className === className);
|
|
297
|
+
// console.log('getClasses()', getClasses())
|
|
298
|
+
if (c) {
|
|
299
|
+
return c.classFamily;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
CLASSNAME_1.getClassFamilyByClassName = getClassFamilyByClassName;
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
function getObjectClassFamily(obj) {
|
|
305
|
+
const className = TchHelpers.getNameFromObject(obj);
|
|
306
|
+
// console.log('className',className)
|
|
307
|
+
let c = getClasses().find(c => c.className === className);
|
|
308
|
+
// console.log('c',c)
|
|
309
|
+
if (c) {
|
|
310
|
+
return c.classFamily;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
CLASSNAME_1.getObjectClassFamily = getObjectClassFamily;
|
|
314
|
+
function getObjectIndexValue(obj) {
|
|
315
|
+
const className = TchHelpers.getNameFromObject(obj);
|
|
316
|
+
// console.log('className',className)
|
|
317
|
+
let c = getClasses().find(c => c.className === className);
|
|
318
|
+
// console.log('c',c)
|
|
319
|
+
if (c && _.isString(c.uniqueKey)) {
|
|
320
|
+
return obj[c.uniqueKey];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
CLASSNAME_1.getObjectIndexValue = getObjectIndexValue;
|
|
324
|
+
function getClassBy(className) {
|
|
325
|
+
let res;
|
|
326
|
+
if (Array.isArray(className)) {
|
|
327
|
+
if (className.length !== 1) {
|
|
328
|
+
throw `Mapping error... please use proper class names:
|
|
329
|
+
{
|
|
330
|
+
propertyObject: 'MyClassName',
|
|
331
|
+
propertyWithArray: ['MyClassName']
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
`;
|
|
335
|
+
}
|
|
336
|
+
className = className[0];
|
|
337
|
+
}
|
|
338
|
+
if (typeof className === 'function') {
|
|
339
|
+
// TODO QUICK_FIX
|
|
340
|
+
res = className;
|
|
341
|
+
}
|
|
342
|
+
if (className === 'Date') {
|
|
343
|
+
res = Date;
|
|
344
|
+
}
|
|
345
|
+
if (className === 'FormData') {
|
|
346
|
+
res = FormData;
|
|
347
|
+
}
|
|
348
|
+
let c = getClasses().find(c => c.className === className);
|
|
349
|
+
if (c) {
|
|
350
|
+
res = c.target;
|
|
351
|
+
}
|
|
352
|
+
// console.log(`getClassBy "${className} return \n\n ${res} \n\n`)
|
|
353
|
+
// @ts-ignore
|
|
354
|
+
return res;
|
|
355
|
+
}
|
|
356
|
+
CLASSNAME_1.getClassBy = getClassBy;
|
|
357
|
+
})(CLASSNAME || (CLASSNAME = {}));
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* @DEPRECATED
|
|
361
|
+
* Describe fields assigned in class
|
|
362
|
+
*/
|
|
363
|
+
const FRegEx = new RegExp(/(?:this\.)(.+?(?= ))/g);
|
|
364
|
+
function describeFromClassStringify(target, parent = false) {
|
|
365
|
+
// @ts-ignore
|
|
366
|
+
var result = [];
|
|
367
|
+
if (parent) {
|
|
368
|
+
var proto = Object.getPrototypeOf(target.prototype);
|
|
369
|
+
if (proto) {
|
|
370
|
+
// @ts-ignore
|
|
371
|
+
result = result.concat(describeFromClassStringify(proto.constructor, parent));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const classString = target.toString();
|
|
375
|
+
const matches = classString.match(FRegEx) || [];
|
|
376
|
+
// console.log({
|
|
377
|
+
// classString,
|
|
378
|
+
// });
|
|
379
|
+
result = result.concat(matches);
|
|
380
|
+
return result.map(prop => prop
|
|
381
|
+
.replace('this.', '')
|
|
382
|
+
.replace('.', '')
|
|
383
|
+
.replace(')', '')
|
|
384
|
+
.replace('(', ''));
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Describe fields assigne through @DefaultModelWithMapping decorator
|
|
388
|
+
* without functions
|
|
389
|
+
*/
|
|
390
|
+
function describeByDefaultModelsAndMapping(target) {
|
|
391
|
+
let res = {};
|
|
392
|
+
if (target) {
|
|
393
|
+
// @ts-ignore
|
|
394
|
+
if (target[SYMBOL.DEFAULT_MODEL]) {
|
|
395
|
+
// @ts-ignore
|
|
396
|
+
Object.keys(target[SYMBOL.DEFAULT_MODEL])
|
|
397
|
+
.filter(key => {
|
|
398
|
+
// @ts-ignore
|
|
399
|
+
return !_.isFunction(target[SYMBOL.DEFAULT_MODEL][key]);
|
|
400
|
+
})
|
|
401
|
+
.forEach(key => {
|
|
402
|
+
// @ts-ignore
|
|
403
|
+
return res[key] = null;
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
// @ts-ignore
|
|
407
|
+
let mapping = target[SYMBOL.MODELS_MAPPING];
|
|
408
|
+
if (_.isArray(mapping)) {
|
|
409
|
+
mapping.forEach(m => {
|
|
410
|
+
Object.keys(m)
|
|
411
|
+
.forEach(key => {
|
|
412
|
+
// @ts-ignore
|
|
413
|
+
res[key] = null;
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
else if (mapping) {
|
|
418
|
+
Object.keys(mapping)
|
|
419
|
+
.forEach(key => {
|
|
420
|
+
// @ts-ignore
|
|
421
|
+
res[key] = null;
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
let propNames = Object.keys(res).filter(f => !!f);
|
|
426
|
+
propNames = (!propNames.includes('id') ? ['id'] : []).concat(propNames);
|
|
427
|
+
return propNames;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
class TchHelpers {
|
|
431
|
+
static getBy(className) {
|
|
432
|
+
return CLASSNAME.getClassBy(className);
|
|
433
|
+
}
|
|
434
|
+
static getFromObject(o) {
|
|
435
|
+
if (_.isUndefined(o) || _.isNull(o)) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (o.constructor) {
|
|
439
|
+
return o.constructor;
|
|
440
|
+
}
|
|
441
|
+
const p = Object.getPrototypeOf(o);
|
|
442
|
+
return p && p.constructor;
|
|
443
|
+
}
|
|
444
|
+
static getName(target, production = false) {
|
|
445
|
+
return CLASSNAME.getClassName(target, production);
|
|
446
|
+
}
|
|
447
|
+
static getNameFromObject(o) {
|
|
448
|
+
const fnCLass = this.getFromObject(o);
|
|
449
|
+
return this.getName(fnCLass);
|
|
450
|
+
}
|
|
451
|
+
static getConfigs(target) {
|
|
452
|
+
return CLASSNAME.getClassConfig(target);
|
|
453
|
+
}
|
|
454
|
+
static describeProperites(target) {
|
|
455
|
+
const d1 = describeFromClassStringify(target);
|
|
456
|
+
const d2 = describeByDefaultModelsAndMapping(target);
|
|
457
|
+
let uniq = {};
|
|
458
|
+
// @ts-ignore
|
|
459
|
+
d1.concat(d2).forEach(p => uniq[p] = p);
|
|
460
|
+
return Object.keys(uniq)
|
|
461
|
+
.filter(d => !!d)
|
|
462
|
+
.filter(d => typeof target.prototype[d] !== 'function');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
const notAllowedAsMethodName = [
|
|
466
|
+
'length', 'name',
|
|
467
|
+
'arguments', 'caller',
|
|
468
|
+
'constructor', 'apply',
|
|
469
|
+
'bind', 'call',
|
|
470
|
+
'toString',
|
|
471
|
+
'__defineGetter__',
|
|
472
|
+
'__defineSetter__', 'hasOwnProperty',
|
|
473
|
+
'__lookupGetter__', '__lookupSetter__',
|
|
474
|
+
'isPrototypeOf', 'propertyIsEnumerable',
|
|
475
|
+
'valueOf', '__proto__', 'toLocaleString',
|
|
476
|
+
];
|
|
477
|
+
const getMethodsNames = (classOrClassInstance, allMethodsNames = []) => {
|
|
478
|
+
if (!classOrClassInstance) {
|
|
479
|
+
return allMethodsNames;
|
|
480
|
+
}
|
|
481
|
+
const isClassFunction = _.isFunction(classOrClassInstance);
|
|
482
|
+
const classFun = (isClassFunction ? classOrClassInstance : Object.getPrototypeOf(classOrClassInstance));
|
|
483
|
+
const objectToCheck = isClassFunction ? classOrClassInstance?.prototype : classOrClassInstance;
|
|
484
|
+
const prototypeObj = Object.getPrototypeOf(objectToCheck || {});
|
|
485
|
+
const properties = _.uniq([
|
|
486
|
+
...Object.getOwnPropertyNames(objectToCheck || {}),
|
|
487
|
+
...Object.getOwnPropertyNames(prototypeObj || {}),
|
|
488
|
+
...Object.keys(objectToCheck || {}),
|
|
489
|
+
...Object.keys(prototypeObj || {}),
|
|
490
|
+
])
|
|
491
|
+
.filter(f => !!f && !notAllowedAsMethodName.includes(f));
|
|
492
|
+
properties.filter((methodName) => typeof objectToCheck[methodName] === 'function').forEach(p => allMethodsNames.push(p));
|
|
493
|
+
if (!classFun || !classFun.constructor || classFun?.constructor?.name === 'Object') {
|
|
494
|
+
return allMethodsNames;
|
|
495
|
+
}
|
|
496
|
+
return getMethodsNames(Object.getPrototypeOf(classFun), allMethodsNames);
|
|
497
|
+
};
|
|
498
|
+
const CLASS = {
|
|
499
|
+
NAME: CLASSNAME.CLASSNAME,
|
|
500
|
+
setName: setClassName,
|
|
501
|
+
getBy: TchHelpers.getBy,
|
|
502
|
+
/**
|
|
503
|
+
* @deprecated
|
|
504
|
+
*/
|
|
505
|
+
getSingleton(target) {
|
|
506
|
+
if (typeof target !== 'function') {
|
|
507
|
+
console.error(`[typescript-class-helpers][setSingletonObj] Type of target is not a function`);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
const config = TchHelpers.getConfigs(target)[0];
|
|
511
|
+
// console.log(`getSingleton for ${target.name}: `, config.singleton)
|
|
512
|
+
return config.singleton;
|
|
513
|
+
},
|
|
514
|
+
/**
|
|
515
|
+
* @deprecated
|
|
516
|
+
*/
|
|
517
|
+
setSingletonObj(target, singletonObject) {
|
|
518
|
+
// console.log('SET SINGLETON', singletonObject)
|
|
519
|
+
if (typeof target !== 'function') {
|
|
520
|
+
console.error(`[typescript-class-helpers][setSingletonObj] Type of target is not a function`);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (Array.isArray(singletonObject)) {
|
|
524
|
+
console.error(`[typescript-class-helpers][setSingletonObj] Singleton instance cant be an array`);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (typeof singletonObject !== 'object') {
|
|
528
|
+
console.error(`[typescript-class-helpers][setSingletonObj] Singleton instance cant must be a object`);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
const className = CLASS.getName(target);
|
|
532
|
+
// console.log(`SINGLETON SET for TARGET ${className}`)
|
|
533
|
+
const config = TchHelpers.getConfigs(target)[0];
|
|
534
|
+
if (config.singleton) {
|
|
535
|
+
console.warn(`[typescript-class-helpers] You are trying to set singleton of "${className}" second time with different object.`);
|
|
536
|
+
}
|
|
537
|
+
config.singleton = singletonObject;
|
|
538
|
+
},
|
|
539
|
+
/**
|
|
540
|
+
* @deprecated
|
|
541
|
+
*/
|
|
542
|
+
getConfigs: TchHelpers.getConfigs,
|
|
543
|
+
/**
|
|
544
|
+
* @deprecated
|
|
545
|
+
*/
|
|
546
|
+
getConfig: (target) => {
|
|
547
|
+
return _.first(TchHelpers.getConfigs(target));
|
|
548
|
+
},
|
|
549
|
+
getMethodsNames(classOrClassInstance) {
|
|
550
|
+
return getMethodsNames(classOrClassInstance);
|
|
551
|
+
},
|
|
552
|
+
getFromObject: TchHelpers.getFromObject,
|
|
553
|
+
getName: TchHelpers.getName,
|
|
554
|
+
getNameFromObject: TchHelpers.getNameFromObject,
|
|
555
|
+
describeProperites: TchHelpers.describeProperites,
|
|
556
|
+
/**
|
|
557
|
+
* @deprecated
|
|
558
|
+
*/
|
|
559
|
+
OBJECT: (obj) => {
|
|
560
|
+
return {
|
|
561
|
+
get indexValue() {
|
|
562
|
+
return CLASSNAME.getObjectIndexValue(obj);
|
|
563
|
+
},
|
|
564
|
+
get indexProperty() {
|
|
565
|
+
return CLASSNAME.getObjectIndexPropertyValue(obj);
|
|
566
|
+
},
|
|
567
|
+
get isClassObject() {
|
|
568
|
+
if (!_.isObject(obj) || _.isArray(obj) || _.isRegExp(obj) ||
|
|
569
|
+
_.isBuffer(obj) || _.isArrayBuffer(obj)) {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
if (_.isDate(obj)) {
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
const className = CLASS.getNameFromObject(obj);
|
|
576
|
+
return _.isString(className) && className !== 'Object';
|
|
577
|
+
},
|
|
578
|
+
isEqual: (anotherObj, compareDeep = false) => {
|
|
579
|
+
if (!CLASS.OBJECT(obj).isClassObject || !CLASS.OBJECT(anotherObj).isClassObject) {
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
if (obj === anotherObj) {
|
|
583
|
+
// console.log(`EQ v1: , v2: - class1 ${CLASS.getNameFromObject(obj)}, class2 ${CLASS.getNameFromObject(anotherObj)}`, obj, anotherObj)
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
const v1 = CLASSNAME.getObjectIndexValue(obj);
|
|
587
|
+
const v2 = CLASSNAME.getObjectIndexValue(anotherObj);
|
|
588
|
+
const f1 = CLASSNAME.getObjectClassFamily(obj);
|
|
589
|
+
const f2 = CLASSNAME.getObjectClassFamily(anotherObj);
|
|
590
|
+
const isFamilyDiff = (!_.isString(f1) || !_.isString(f2) || (f1 !== f2));
|
|
591
|
+
// console.log(`DIFF FAMILY ${isFamilyDiff} v1: ${CLASSNAME.getObjectClassFamily(obj)}, v2: ${CLASSNAME.getObjectClassFamily(anotherObj)} - class1 ${CLASS.getNameFromObject(obj)}, class2 ${CLASS.getNameFromObject(anotherObj)}`)
|
|
592
|
+
if (isFamilyDiff) {
|
|
593
|
+
// console.log(`DIFF v1: ${v1}, v2: ${v2} - class1 ${CLASS.getNameFromObject(obj)}, class2 ${CLASS.getNameFromObject(anotherObj)}`)
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
if (!CLASS.OBJECT(obj).isClassObject || !CLASS.OBJECT(anotherObj).isClassObject) {
|
|
597
|
+
// console.log(`NOT CLASS v1: ${v1}, v2: ${v2} - class1 ${CLASS.getNameFromObject(obj)}, class2 ${CLASS.getNameFromObject(anotherObj)}`)
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
// console.log(`v1: ${v1} ,class ${CLASS.getNameFromObject(obj)} ,prop: ${CLASS.OBJECT(obj).indexProperty}`)
|
|
601
|
+
// console.log(`v2: ${v2} ,class ${CLASS.getNameFromObject(anotherObj)} ,prop: ${CLASS.OBJECT(anotherObj).indexProperty}`)
|
|
602
|
+
// console.log(`v1: ${v1}, v2: ${v2} - class1 ${CLASS.getNameFromObject(obj)}, class2 ${CLASS.getNameFromObject(anotherObj)}`)
|
|
603
|
+
// console.log('')
|
|
604
|
+
if ((_.isNumber(v1) && _.isNumber(v2)) || (_.isString(v1) && _.isString(v2))) {
|
|
605
|
+
const res = (v1 === v2);
|
|
606
|
+
// @ts-ignore
|
|
607
|
+
return res;
|
|
608
|
+
}
|
|
609
|
+
if (compareDeep) {
|
|
610
|
+
return _.isEqual(v1, v2);
|
|
611
|
+
}
|
|
612
|
+
return false;
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Generated bundle index. Do not edit.
|
|
620
|
+
*/
|
|
621
|
+
|
|
622
|
+
export { CLASS, Models, SYMBOL, TchHelpers };
|
|
623
|
+
//# sourceMappingURL=typescript-class-helpers-browser.mjs.map
|