mapper-factory 3.1.1 → 3.1.2
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/dist/field-decorators/array.decorator.d.ts +2 -0
- package/dist/field-decorators/array.decorator.js +13 -0
- package/dist/field-decorators/date.decorator.d.ts +1 -0
- package/dist/field-decorators/date.decorator.js +10 -0
- package/dist/field-decorators/field.decorator.d.ts +22 -0
- package/dist/field-decorators/field.decorator.js +54 -0
- package/dist/field-decorators/object.decorator.d.ts +2 -0
- package/dist/field-decorators/object.decorator.js +11 -0
- package/dist/functions.d.ts +4 -4
- package/dist/functions.js +89 -71
- package/dist/index.d.ts +7 -4
- package/dist/index.js +10 -4
- package/dist/test.js +125 -54
- package/package.json +1 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayField = ArrayField;
|
|
4
|
+
const field_decorator_1 = require("./field.decorator");
|
|
5
|
+
function ArrayField(clsFactory) {
|
|
6
|
+
const Ctor = clsFactory();
|
|
7
|
+
return (0, field_decorator_1.MapField)({
|
|
8
|
+
transformer: (arr) => Array.isArray(arr)
|
|
9
|
+
? arr.map((item) => new Ctor().from(item))
|
|
10
|
+
: [],
|
|
11
|
+
reverser: (arr) => Array.isArray(arr) ? arr.map((item) => item.toMap()) : [],
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function DateField(): PropertyDecorator;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateField = DateField;
|
|
4
|
+
const field_decorator_1 = require("./field.decorator");
|
|
5
|
+
function DateField() {
|
|
6
|
+
return (0, field_decorator_1.MapField)({
|
|
7
|
+
transformer: (dateISO) => dateISO ? new Date(dateISO) : null,
|
|
8
|
+
reverser: (date) => date?.toISOString() ?? null,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { ClassType } from '../types';
|
|
3
|
+
export declare const MAP_FIELD: unique symbol;
|
|
4
|
+
export interface MapperMetadata<T = any> {
|
|
5
|
+
src?: keyof T;
|
|
6
|
+
initialize?: boolean;
|
|
7
|
+
transformer?: {
|
|
8
|
+
(input: any, ref: any): any;
|
|
9
|
+
};
|
|
10
|
+
reverser?: {
|
|
11
|
+
(input: any): any;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export declare function isClass(func: any): func is ClassType;
|
|
15
|
+
export declare function getPrototype(target: Record<string, unknown> | ClassType): any;
|
|
16
|
+
export declare const MapField: <T = any>({ transformer, reverser, src, initialize, }?: MapperMetadata<T>) => PropertyDecorator;
|
|
17
|
+
export declare const getMapFieldMetadataList: (target: Record<string, unknown> | ClassType | any) => {
|
|
18
|
+
[key: string]: MapperMetadata;
|
|
19
|
+
} | undefined;
|
|
20
|
+
export declare const hasMapFieldMetadataList: (target: Record<string, unknown> | ClassType) => boolean;
|
|
21
|
+
export declare const getMapFieldMetadata: (target: Record<string, unknown> | ClassType, propertyName: string | symbol) => MapperMetadata | undefined;
|
|
22
|
+
export declare const hasMapFieldMetadata: (target: Record<string, unknown> | ClassType, propertyName: string) => boolean;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hasMapFieldMetadata = exports.getMapFieldMetadata = exports.hasMapFieldMetadataList = exports.getMapFieldMetadataList = exports.MapField = exports.MAP_FIELD = void 0;
|
|
4
|
+
exports.isClass = isClass;
|
|
5
|
+
exports.getPrototype = getPrototype;
|
|
6
|
+
require("reflect-metadata");
|
|
7
|
+
exports.MAP_FIELD = Symbol('MAP_FIELD');
|
|
8
|
+
function isClass(func) {
|
|
9
|
+
return (typeof func === 'function' &&
|
|
10
|
+
/^class\s/.test(Function.prototype.toString.call(func)));
|
|
11
|
+
}
|
|
12
|
+
function getPrototype(target) {
|
|
13
|
+
return isClass(target) || !target?.prototype ? !target?.constructor ? target : target?.constructor : target?.prototype;
|
|
14
|
+
}
|
|
15
|
+
const MapField = ({ transformer, reverser, src, initialize = false, } = {}) => {
|
|
16
|
+
return (target, property) => {
|
|
17
|
+
const classConstructor = target.constructor;
|
|
18
|
+
const propertyName = property.toString();
|
|
19
|
+
const metadata = Reflect.getMetadata(exports.MAP_FIELD, classConstructor) || {};
|
|
20
|
+
// create new object reference to avoid this issue: https://github.com/rbuckton/reflect-metadata/issues/62
|
|
21
|
+
const newMetadata = { ...metadata };
|
|
22
|
+
const previousValues = metadata[propertyName];
|
|
23
|
+
newMetadata[propertyName] = {
|
|
24
|
+
...previousValues,
|
|
25
|
+
src,
|
|
26
|
+
initialize,
|
|
27
|
+
transformer,
|
|
28
|
+
reverser,
|
|
29
|
+
};
|
|
30
|
+
Reflect.defineMetadata(exports.MAP_FIELD, newMetadata, classConstructor);
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.MapField = MapField;
|
|
34
|
+
const getMapFieldMetadataList = (target) => {
|
|
35
|
+
return Reflect.getMetadata(exports.MAP_FIELD, getPrototype(target));
|
|
36
|
+
};
|
|
37
|
+
exports.getMapFieldMetadataList = getMapFieldMetadataList;
|
|
38
|
+
const hasMapFieldMetadataList = (target) => {
|
|
39
|
+
return Reflect.hasMetadata(exports.MAP_FIELD, getPrototype(target));
|
|
40
|
+
};
|
|
41
|
+
exports.hasMapFieldMetadataList = hasMapFieldMetadataList;
|
|
42
|
+
const getMapFieldMetadata = (target, propertyName) => {
|
|
43
|
+
const metadata = (0, exports.getMapFieldMetadataList)(target);
|
|
44
|
+
const name = propertyName.toString();
|
|
45
|
+
if (!metadata || !metadata[name])
|
|
46
|
+
return undefined;
|
|
47
|
+
return metadata[name];
|
|
48
|
+
};
|
|
49
|
+
exports.getMapFieldMetadata = getMapFieldMetadata;
|
|
50
|
+
const hasMapFieldMetadata = (target, propertyName) => {
|
|
51
|
+
const metadata = Reflect.getMetadata(exports.MAP_FIELD, getPrototype(target));
|
|
52
|
+
return metadata && !!metadata[propertyName];
|
|
53
|
+
};
|
|
54
|
+
exports.hasMapFieldMetadata = hasMapFieldMetadata;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectField = ObjectField;
|
|
4
|
+
const field_decorator_1 = require("./field.decorator");
|
|
5
|
+
function ObjectField(clsFactory) {
|
|
6
|
+
const Ctor = clsFactory();
|
|
7
|
+
return (0, field_decorator_1.MapField)({
|
|
8
|
+
transformer: (obj) => (obj ? new Ctor().from(obj) : null),
|
|
9
|
+
reverser: (obj) => obj?.toMap?.() ?? null,
|
|
10
|
+
});
|
|
11
|
+
}
|
package/dist/functions.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Convert the instance of this class to JSON Object.
|
|
3
|
-
*
|
|
4
|
-
* @returns JSON object mapped considering metadata "src" and "reverser"
|
|
5
|
-
*/
|
|
2
|
+
* Convert the instance of this class to JSON Object.
|
|
3
|
+
*
|
|
4
|
+
* @returns JSON object mapped considering metadata "src" and "reverser"
|
|
5
|
+
*/
|
|
6
6
|
export declare function toMap(): any;
|
|
7
7
|
/**
|
|
8
8
|
* Convert a JSON Object to an instance of this class.
|
package/dist/functions.js
CHANGED
|
@@ -8,12 +8,12 @@ exports.get = get;
|
|
|
8
8
|
exports.set = set;
|
|
9
9
|
exports.copy = copy;
|
|
10
10
|
exports.from = from;
|
|
11
|
-
const field_decorator_1 = require("./field.decorator");
|
|
11
|
+
const field_decorator_1 = require("./field-decorators/field.decorator");
|
|
12
12
|
/**
|
|
13
|
-
* Convert the instance of this class to JSON Object.
|
|
14
|
-
*
|
|
15
|
-
* @returns JSON object mapped considering metadata "src" and "reverser"
|
|
16
|
-
*/
|
|
13
|
+
* Convert the instance of this class to JSON Object.
|
|
14
|
+
*
|
|
15
|
+
* @returns JSON object mapped considering metadata "src" and "reverser"
|
|
16
|
+
*/
|
|
17
17
|
function toMap() {
|
|
18
18
|
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
19
19
|
let obj = {};
|
|
@@ -22,11 +22,14 @@ function toMap() {
|
|
|
22
22
|
for (let i = 0; i < propsStereoid.length; i++) {
|
|
23
23
|
const prop = propsStereoid[i];
|
|
24
24
|
if (prop.isArray) {
|
|
25
|
-
let arrIndex = prop.arrIndex
|
|
25
|
+
let arrIndex = prop.arrIndex
|
|
26
|
+
.split(/\[(\w+)\]/g)
|
|
27
|
+
.filter((index) => index !== "");
|
|
26
28
|
objCopy[prop.prop] = objCopy[prop.prop] || [];
|
|
27
29
|
objCopy = objCopy[prop.prop];
|
|
28
30
|
arrIndex.forEach((index, i) => {
|
|
29
|
-
objCopy[index] =
|
|
31
|
+
objCopy[index] =
|
|
32
|
+
objCopy[index] || (i == arrIndex.length - 1 ? {} : []);
|
|
30
33
|
if (i != arrIndex.length - 1)
|
|
31
34
|
objCopy = objCopy[index];
|
|
32
35
|
else
|
|
@@ -43,34 +46,38 @@ function toMap() {
|
|
|
43
46
|
}
|
|
44
47
|
objCopy[lastIndex] = reverser ? reverser(value, this) : value;
|
|
45
48
|
};
|
|
46
|
-
this &&
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
this &&
|
|
50
|
+
Object.keys(this).forEach((propertyName) => {
|
|
51
|
+
const metadata = metadataList && metadataList[propertyName];
|
|
52
|
+
const src = metadata?.src || propertyName;
|
|
53
|
+
if (metadata) {
|
|
54
|
+
if (src.includes(".")) {
|
|
55
|
+
let props = src.split(".");
|
|
56
|
+
let propsStereoid = props.map((prop) => ({
|
|
57
|
+
prop: prop.includes("[")
|
|
58
|
+
? prop.substring(0, prop.indexOf("["))
|
|
59
|
+
: prop,
|
|
60
|
+
isArray: prop.includes("[") && prop.includes("]"),
|
|
61
|
+
arrIndex: prop.substring(prop.indexOf("[")),
|
|
62
|
+
}));
|
|
63
|
+
processProperty(obj, propsStereoid, this[propertyName], metadata.reverser);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
obj[src] =
|
|
67
|
+
Array.isArray(this[propertyName]) && !metadata.reverser
|
|
68
|
+
? this[propertyName].map((item) => item?.toMap ? item.toMap() : item)
|
|
69
|
+
: metadata.reverser
|
|
70
|
+
? metadata.reverser(this[propertyName], this)
|
|
71
|
+
: this[propertyName]?.toMap
|
|
72
|
+
? this[propertyName].toMap()
|
|
73
|
+
: this[propertyName];
|
|
74
|
+
}
|
|
58
75
|
}
|
|
59
76
|
else {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
: metadata.reverser
|
|
63
|
-
? metadata.reverser(this[propertyName], this)
|
|
64
|
-
: this[propertyName]?.toMap
|
|
65
|
-
? this[propertyName].toMap()
|
|
66
|
-
: this[propertyName];
|
|
77
|
+
if (this[propertyName] != undefined)
|
|
78
|
+
obj[propertyName] = this[propertyName];
|
|
67
79
|
}
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
if (this[propertyName] != undefined)
|
|
71
|
-
obj[propertyName] = this[propertyName];
|
|
72
|
-
}
|
|
73
|
-
});
|
|
80
|
+
});
|
|
74
81
|
return obj;
|
|
75
82
|
}
|
|
76
83
|
/**
|
|
@@ -82,10 +89,10 @@ function toMap() {
|
|
|
82
89
|
function objToModel(obj) {
|
|
83
90
|
if (!obj)
|
|
84
91
|
return this;
|
|
85
|
-
Object.keys(obj).forEach(propertyName => {
|
|
92
|
+
Object.keys(obj).forEach((propertyName) => {
|
|
86
93
|
const value = obj[propertyName];
|
|
87
94
|
this[propertyName] = Array.isArray(value)
|
|
88
|
-
? value.map(item => item)
|
|
95
|
+
? value.map((item) => item)
|
|
89
96
|
: value;
|
|
90
97
|
});
|
|
91
98
|
return this;
|
|
@@ -96,7 +103,7 @@ function objToModel(obj) {
|
|
|
96
103
|
* @returns true or false
|
|
97
104
|
*/
|
|
98
105
|
function empty() {
|
|
99
|
-
return !Object.keys(this).some(propertyName => this[propertyName] !== undefined && this[propertyName] !== null);
|
|
106
|
+
return !Object.keys(this).some((propertyName) => this[propertyName] !== undefined && this[propertyName] !== null);
|
|
100
107
|
}
|
|
101
108
|
/**
|
|
102
109
|
* Check if this instance is filled.
|
|
@@ -104,7 +111,8 @@ function empty() {
|
|
|
104
111
|
* @returns true or false
|
|
105
112
|
*/
|
|
106
113
|
function filled() {
|
|
107
|
-
return Object.keys(this).length > 0 &&
|
|
114
|
+
return (Object.keys(this).length > 0 &&
|
|
115
|
+
Object.keys(this).every((propertyName) => this[propertyName] !== undefined && this[propertyName] !== null));
|
|
108
116
|
}
|
|
109
117
|
/**
|
|
110
118
|
* GET property value from a string path.
|
|
@@ -113,7 +121,7 @@ function filled() {
|
|
|
113
121
|
* @returns Value of the property
|
|
114
122
|
*/
|
|
115
123
|
function get(path) {
|
|
116
|
-
const props = path.replace(/\[(\w+)\]/g,
|
|
124
|
+
const props = path.replace(/\[(\w+)\]/g, ".$1").split(".");
|
|
117
125
|
return props.reduce((acc, prop) => acc && acc[prop], this);
|
|
118
126
|
}
|
|
119
127
|
/**
|
|
@@ -123,9 +131,9 @@ function get(path) {
|
|
|
123
131
|
* @param value Value of the property
|
|
124
132
|
*/
|
|
125
133
|
function set(path, value) {
|
|
126
|
-
const props = path.replace(/\[(\w+)\]/g,
|
|
134
|
+
const props = path.replace(/\[(\w+)\]/g, ".$1").split(".");
|
|
127
135
|
let obj = this;
|
|
128
|
-
props.slice(0, -1).forEach(prop => {
|
|
136
|
+
props.slice(0, -1).forEach((prop) => {
|
|
129
137
|
if (!obj[prop])
|
|
130
138
|
obj[prop] = {};
|
|
131
139
|
obj = obj[prop];
|
|
@@ -149,9 +157,11 @@ function from(object) {
|
|
|
149
157
|
for (let i = 0; i < propsStereoid.length; i++) {
|
|
150
158
|
const prop = propsStereoid[i];
|
|
151
159
|
if (prop.isArray) {
|
|
152
|
-
let arrIndex = prop.arrIndex
|
|
160
|
+
let arrIndex = prop.arrIndex
|
|
161
|
+
.split(/\[(\w+)\]/g)
|
|
162
|
+
.filter((index) => index !== "");
|
|
153
163
|
objCopy = objCopy[prop.prop];
|
|
154
|
-
arrIndex.forEach(index => {
|
|
164
|
+
arrIndex.forEach((index) => {
|
|
155
165
|
objCopy = objCopy[index];
|
|
156
166
|
});
|
|
157
167
|
}
|
|
@@ -173,39 +183,47 @@ function from(object) {
|
|
|
173
183
|
this[metaKey] = value;
|
|
174
184
|
}
|
|
175
185
|
};
|
|
176
|
-
object &&
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const src = metadataList?.[metaKey].src || propertyName;
|
|
197
|
-
setProperty(metaKey, object[src], object);
|
|
186
|
+
object &&
|
|
187
|
+
Object.keys(object).forEach((propertyName) => {
|
|
188
|
+
let metaKeys = metadataList &&
|
|
189
|
+
Object.keys(metadataList).filter((metadata) => metadataList[metadata]?.src?.split(".")?.includes(propertyName));
|
|
190
|
+
if (metaKeys?.length) {
|
|
191
|
+
metaKeys.forEach((metaKey) => {
|
|
192
|
+
const metaProp = metadataList?.[metaKey];
|
|
193
|
+
if (metaProp) {
|
|
194
|
+
const props = metaProp.src.split(".");
|
|
195
|
+
const propsStereoid = props.map((prop) => ({
|
|
196
|
+
prop: prop.includes("[")
|
|
197
|
+
? prop.substring(0, prop.indexOf("["))
|
|
198
|
+
: prop,
|
|
199
|
+
isArray: prop.includes("[") && prop.includes("]"),
|
|
200
|
+
arrIndex: prop.substring(prop.indexOf("[")),
|
|
201
|
+
}));
|
|
202
|
+
const value = processProperty({ ...object }, propsStereoid);
|
|
203
|
+
setProperty(metaKey, value, object);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
198
206
|
}
|
|
199
207
|
else {
|
|
200
|
-
|
|
208
|
+
let metaKey = metadataList &&
|
|
209
|
+
Object.keys(metadataList).find((metadata) => metadataList[metadata]?.src == propertyName);
|
|
210
|
+
if (metaKey) {
|
|
211
|
+
const src = metadataList?.[metaKey].src || propertyName;
|
|
212
|
+
setProperty(metaKey, object[src], object);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
setProperty(propertyName, object[propertyName], object);
|
|
216
|
+
}
|
|
201
217
|
}
|
|
202
|
-
}
|
|
203
|
-
});
|
|
218
|
+
});
|
|
204
219
|
// Initialize properties with "initialize" metadata
|
|
205
|
-
metadataList &&
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
220
|
+
metadataList &&
|
|
221
|
+
Object.keys(metadataList).forEach((metaName) => {
|
|
222
|
+
if (metadataList[metaName]?.initialize &&
|
|
223
|
+
metadataList[metaName]?.transformer &&
|
|
224
|
+
this[metaName] === undefined) {
|
|
225
|
+
this[metaName] = metadataList[metaName].transformer(null, object);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
210
228
|
return this;
|
|
211
229
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { MapClass, MapInterface } from "./class.decorator";
|
|
2
|
-
import { MapField } from "./field.decorator";
|
|
2
|
+
import { MapField } from "./field-decorators/field.decorator";
|
|
3
|
+
import { ArrayField } from "./field-decorators/array.decorator";
|
|
4
|
+
import { DateField } from "./field-decorators/date.decorator";
|
|
5
|
+
import { ObjectField } from "./field-decorators/object.decorator";
|
|
3
6
|
import { ClassType } from "./types";
|
|
4
|
-
export { ClassType, MapClass, MapField,
|
|
7
|
+
export { ClassType, MapInterface, MapClass, MapField, DateField, ArrayField, ObjectField, };
|
|
5
8
|
/**
|
|
6
9
|
* npx tsc
|
|
7
|
-
* npx ts-node src/
|
|
10
|
+
* npx ts-node src/test.ts
|
|
8
11
|
* npm version ( patch | minor | major )
|
|
9
12
|
* npm publish
|
|
10
|
-
*/
|
|
13
|
+
*/
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MapField = exports.MapClass = void 0;
|
|
3
|
+
exports.ObjectField = exports.ArrayField = exports.DateField = exports.MapField = exports.MapClass = void 0;
|
|
4
4
|
const class_decorator_1 = require("./class.decorator");
|
|
5
5
|
Object.defineProperty(exports, "MapClass", { enumerable: true, get: function () { return class_decorator_1.MapClass; } });
|
|
6
|
-
const field_decorator_1 = require("./field.decorator");
|
|
6
|
+
const field_decorator_1 = require("./field-decorators/field.decorator");
|
|
7
7
|
Object.defineProperty(exports, "MapField", { enumerable: true, get: function () { return field_decorator_1.MapField; } });
|
|
8
|
+
const array_decorator_1 = require("./field-decorators/array.decorator");
|
|
9
|
+
Object.defineProperty(exports, "ArrayField", { enumerable: true, get: function () { return array_decorator_1.ArrayField; } });
|
|
10
|
+
const date_decorator_1 = require("./field-decorators/date.decorator");
|
|
11
|
+
Object.defineProperty(exports, "DateField", { enumerable: true, get: function () { return date_decorator_1.DateField; } });
|
|
12
|
+
const object_decorator_1 = require("./field-decorators/object.decorator");
|
|
13
|
+
Object.defineProperty(exports, "ObjectField", { enumerable: true, get: function () { return object_decorator_1.ObjectField; } });
|
|
8
14
|
/**
|
|
9
15
|
* npx tsc
|
|
10
|
-
* npx ts-node src/
|
|
16
|
+
* npx ts-node src/test.ts
|
|
11
17
|
* npm version ( patch | minor | major )
|
|
12
18
|
* npm publish
|
|
13
|
-
*/
|
|
19
|
+
*/
|
package/dist/test.js
CHANGED
|
@@ -10,7 +10,10 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const class_decorator_1 = require("./class.decorator");
|
|
13
|
-
const
|
|
13
|
+
const array_decorator_1 = require("./field-decorators/array.decorator");
|
|
14
|
+
const date_decorator_1 = require("./field-decorators/date.decorator");
|
|
15
|
+
const field_decorator_1 = require("./field-decorators/field.decorator");
|
|
16
|
+
const object_decorator_1 = require("./field-decorators/object.decorator");
|
|
14
17
|
//MAPPER FACTORY - TEST
|
|
15
18
|
console.log("\nMAPPER FACTORY - TEST");
|
|
16
19
|
console.log("\n");
|
|
@@ -30,14 +33,16 @@ __decorate([
|
|
|
30
33
|
], History.prototype, "name", void 0);
|
|
31
34
|
__decorate([
|
|
32
35
|
(0, field_decorator_1.MapField)({
|
|
33
|
-
src: "control"
|
|
36
|
+
src: "control",
|
|
34
37
|
}),
|
|
35
38
|
__metadata("design:type", String)
|
|
36
39
|
], History.prototype, "testControl", void 0);
|
|
37
40
|
__decorate([
|
|
38
41
|
(0, field_decorator_1.MapField)({
|
|
39
42
|
initialize: true,
|
|
40
|
-
transformer: (arr, obj) => {
|
|
43
|
+
transformer: (arr, obj) => {
|
|
44
|
+
return [obj.monday, obj.tuesday];
|
|
45
|
+
},
|
|
41
46
|
reverser: (arr) => {
|
|
42
47
|
return { monday: arr && arr[0], tuesday: arr && arr[1] };
|
|
43
48
|
},
|
|
@@ -46,7 +51,7 @@ __decorate([
|
|
|
46
51
|
], History.prototype, "daysActive", void 0);
|
|
47
52
|
__decorate([
|
|
48
53
|
(0, field_decorator_1.MapField)({
|
|
49
|
-
src: "test.concatenation"
|
|
54
|
+
src: "test.concatenation",
|
|
50
55
|
}),
|
|
51
56
|
__metadata("design:type", String)
|
|
52
57
|
], History.prototype, "testConcatenation", void 0);
|
|
@@ -65,40 +70,40 @@ let User = class User {
|
|
|
65
70
|
};
|
|
66
71
|
__decorate([
|
|
67
72
|
(0, field_decorator_1.MapField)({
|
|
68
|
-
src:
|
|
73
|
+
src: "firstName",
|
|
69
74
|
}),
|
|
70
75
|
__metadata("design:type", String)
|
|
71
76
|
], User.prototype, "name", void 0);
|
|
72
77
|
__decorate([
|
|
73
78
|
(0, field_decorator_1.MapField)({
|
|
74
|
-
src:
|
|
79
|
+
src: "lastName",
|
|
75
80
|
}),
|
|
76
81
|
__metadata("design:type", String)
|
|
77
82
|
], User.prototype, "surname", void 0);
|
|
78
83
|
__decorate([
|
|
79
84
|
(0, field_decorator_1.MapField)({
|
|
80
|
-
src:
|
|
81
|
-
transformer: (arr) => arr?.map(role => role + " TEST TRASFORMER"),
|
|
82
|
-
reverser: (arr) => arr?.map(role => role.replace(" TEST TRASFORMER", "")),
|
|
85
|
+
src: "rolesToMap",
|
|
86
|
+
transformer: (arr) => arr?.map((role) => role + " TEST TRASFORMER"),
|
|
87
|
+
reverser: (arr) => arr?.map((role) => role.replace(" TEST TRASFORMER", "")),
|
|
83
88
|
}),
|
|
84
89
|
__metadata("design:type", Array)
|
|
85
90
|
], User.prototype, "roles", void 0);
|
|
86
91
|
__decorate([
|
|
87
92
|
(0, field_decorator_1.MapField)({
|
|
88
|
-
transformer: (arr) => arr?.map(user => new User().from(user))
|
|
93
|
+
transformer: (arr) => arr?.map((user) => new User().from(user)),
|
|
89
94
|
}),
|
|
90
95
|
__metadata("design:type", Array)
|
|
91
96
|
], User.prototype, "employees", void 0);
|
|
92
97
|
__decorate([
|
|
93
98
|
(0, field_decorator_1.MapField)({
|
|
94
|
-
transformer: (user) => new User().from(user)
|
|
99
|
+
transformer: (user) => new User().from(user),
|
|
95
100
|
}),
|
|
96
101
|
__metadata("design:type", User)
|
|
97
102
|
], User.prototype, "boss", void 0);
|
|
98
103
|
__decorate([
|
|
99
104
|
(0, field_decorator_1.MapField)({
|
|
100
|
-
transformer: histories => histories?.map(hst => new History().from(hst)),
|
|
101
|
-
reverser: histories => histories?.map(hst => hst.toMap()),
|
|
105
|
+
transformer: (histories) => histories?.map((hst) => new History().from(hst)),
|
|
106
|
+
reverser: (histories) => histories?.map((hst) => hst.toMap()),
|
|
102
107
|
}),
|
|
103
108
|
__metadata("design:type", Array)
|
|
104
109
|
], User.prototype, "histories", void 0);
|
|
@@ -107,49 +112,70 @@ User = __decorate([
|
|
|
107
112
|
], User);
|
|
108
113
|
const emp1 = new User().from({ firstName: "Summer", lastName: "Smith" });
|
|
109
114
|
const emp2 = new User().from({ firstName: "Morty", lastName: "Smith" });
|
|
110
|
-
const JSONObject = {
|
|
115
|
+
const JSONObject = {
|
|
116
|
+
username: "god",
|
|
117
|
+
firstName: "Rick",
|
|
118
|
+
lastName: "Sanchez",
|
|
119
|
+
employees: [emp1.toMap(), emp2.toMap()],
|
|
120
|
+
rolesToMap: ["CEO", "EMPLOYEE"],
|
|
121
|
+
boss: { firstName: "Nello", lastName: "Stanco" },
|
|
122
|
+
};
|
|
111
123
|
//TEST constructor
|
|
112
124
|
const u = new User().from(JSONObject);
|
|
113
125
|
const constructorTest = u.username == JSONObject.username &&
|
|
114
126
|
u.name == JSONObject.firstName &&
|
|
115
127
|
u.surname == JSONObject.lastName &&
|
|
116
|
-
u.employees?.map(emp => emp.name == emp1.name) &&
|
|
117
|
-
u.roles?.map(role => role == "CEO") &&
|
|
128
|
+
u.employees?.map((emp) => emp.name == emp1.name) &&
|
|
129
|
+
u.roles?.map((role) => role == "CEO") &&
|
|
118
130
|
u.boss.name == "Nello" &&
|
|
119
131
|
u.boss.surname == "Stanco";
|
|
120
|
-
console.log("TEST CONSTRUCTOR", constructorTest ?
|
|
132
|
+
console.log("TEST CONSTRUCTOR", constructorTest ? "✅" : "❌");
|
|
121
133
|
//TEST toModel method with JS Object
|
|
122
134
|
const u1 = new User().toModel(u);
|
|
123
|
-
const toModelTest = u1.name == JSONObject.firstName &&
|
|
124
|
-
|
|
135
|
+
const toModelTest = u1.name == JSONObject.firstName &&
|
|
136
|
+
u1.surname == JSONObject.lastName &&
|
|
137
|
+
u1.employees?.map((emp) => emp.name == emp1.name) &&
|
|
138
|
+
u1.roles?.map((role) => role == "CEO") &&
|
|
139
|
+
u1.boss.name == "Nello" &&
|
|
140
|
+
u1.boss.surname == "Stanco";
|
|
141
|
+
console.log("TEST TO MODEL USER", toModelTest ? "✅" : "❌");
|
|
125
142
|
//TEST [GET, SET] METHODS
|
|
126
143
|
u.set("employees[1].name", "name editato");
|
|
127
144
|
const toModelTest1 = u.get("employees[1].name") == "name editato";
|
|
128
|
-
console.log("TEST [GET, SET] METHODS", toModelTest1 ?
|
|
145
|
+
console.log("TEST [GET, SET] METHODS", toModelTest1 ? "✅" : "❌");
|
|
129
146
|
//TEST TRANSFORMER/REVERSER
|
|
130
147
|
const h1 = new History().from({ name: "h1" });
|
|
131
148
|
const h2 = new History().from({ name: "h2" });
|
|
132
149
|
u.histories = [h1, h2];
|
|
133
150
|
const uMapped = u.toMap();
|
|
134
|
-
const toModelTest2 = uMapped.histories?.map(h => h.name == " TEST REVERSER") &&
|
|
135
|
-
|
|
151
|
+
const toModelTest2 = uMapped.histories?.map((h) => h.name == " TEST REVERSER") &&
|
|
152
|
+
u.histories?.map((h) => h.name == " TEST TRASFORMER");
|
|
153
|
+
console.log("TEST TRANSFORMER/REVERSER", toModelTest2 ? "✅" : "❌");
|
|
136
154
|
//TEST REF
|
|
137
|
-
const hTest = new History().from({
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
155
|
+
const hTest = new History().from({
|
|
156
|
+
monday: "0",
|
|
157
|
+
tuesday: "1",
|
|
158
|
+
control: "control",
|
|
159
|
+
});
|
|
160
|
+
hTest.daysActive = ["1", "0"];
|
|
161
|
+
const toModelTest3 = hTest.toMap().daysActive.monday == "1" &&
|
|
162
|
+
hTest.toMap().daysActive.tuesday == "0";
|
|
163
|
+
console.log("TEST REF", toModelTest3 ? "✅" : "❌");
|
|
141
164
|
//TEST CONCAT WITH POINT
|
|
142
|
-
const hTest2 = new History().from({
|
|
143
|
-
|
|
144
|
-
|
|
165
|
+
const hTest2 = new History().from({
|
|
166
|
+
test: { concatenation: "resolve " },
|
|
167
|
+
control: "control",
|
|
168
|
+
});
|
|
169
|
+
const toModelTest4 = hTest2.toMap().test.concatenation == "resolve ";
|
|
170
|
+
console.log("TEST CONCAT WITH POINT", toModelTest4 ? "✅" : "❌");
|
|
145
171
|
let Test = class Test {
|
|
146
172
|
a;
|
|
147
173
|
};
|
|
148
174
|
__decorate([
|
|
149
175
|
(0, field_decorator_1.MapField)({
|
|
150
|
-
src:
|
|
151
|
-
transformer: value =>
|
|
152
|
-
reverser: value => ({ a:
|
|
176
|
+
src: "b",
|
|
177
|
+
transformer: (value) => "test transformer",
|
|
178
|
+
reverser: (value) => ({ a: "test reverser" }),
|
|
153
179
|
}),
|
|
154
180
|
__metadata("design:type", String)
|
|
155
181
|
], Test.prototype, "a", void 0);
|
|
@@ -157,33 +183,33 @@ Test = __decorate([
|
|
|
157
183
|
(0, class_decorator_1.MapClass)()
|
|
158
184
|
], Test);
|
|
159
185
|
const testEmpty = new Test().from();
|
|
160
|
-
const testFilled = new Test().from({ b:
|
|
161
|
-
const checkTest1 =
|
|
162
|
-
const checkTest2 =
|
|
163
|
-
console.log("TEST EMPTY/FILLED WITH INITIALIZE",
|
|
164
|
-
const model3 = new Test().toModel({ a:
|
|
165
|
-
console.log("TEST TO MODEL WITH INITIALIZE",
|
|
186
|
+
const testFilled = new Test().from({ b: "filled" });
|
|
187
|
+
const checkTest1 = testEmpty && testEmpty.empty() == true && testEmpty.filled() == false;
|
|
188
|
+
const checkTest2 = testFilled && testFilled.empty() == false && testFilled.filled() == true;
|
|
189
|
+
console.log("TEST EMPTY/FILLED WITH INITIALIZE", checkTest1 && checkTest2 ? "✅" : "❌");
|
|
190
|
+
const model3 = new Test().toModel({ a: "test to model" });
|
|
191
|
+
console.log("TEST TO MODEL WITH INITIALIZE", model3.a == "test to model" ? "✅" : "❌");
|
|
166
192
|
const model4 = model3.toMap();
|
|
167
|
-
console.log("TEST TO MAP WITH INITIALIZE",
|
|
193
|
+
console.log("TEST TO MAP WITH INITIALIZE", model4.b.a == "test reverser" ? "✅" : "❌");
|
|
168
194
|
const model5 = model3.copy();
|
|
169
|
-
console.log("TEST COPY WITH INITIALIZE",
|
|
195
|
+
console.log("TEST COPY WITH INITIALIZE", Object.keys(model5).every((k) => model5[k] == model3[k]) ? "✅" : "❌");
|
|
170
196
|
let TestFlag = class TestFlag {
|
|
171
197
|
flTest;
|
|
172
198
|
a;
|
|
173
199
|
};
|
|
174
200
|
__decorate([
|
|
175
201
|
(0, field_decorator_1.MapField)({
|
|
176
|
-
transformer: (num) => num ==
|
|
177
|
-
reverser: bool => bool ?
|
|
202
|
+
transformer: (num) => num == "1",
|
|
203
|
+
reverser: (bool) => (bool ? "1" : "0"),
|
|
178
204
|
initialize: true,
|
|
179
205
|
}),
|
|
180
206
|
__metadata("design:type", Boolean)
|
|
181
207
|
], TestFlag.prototype, "flTest", void 0);
|
|
182
208
|
__decorate([
|
|
183
209
|
(0, field_decorator_1.MapField)({
|
|
184
|
-
src:
|
|
185
|
-
transformer: value =>
|
|
186
|
-
reverser: value => ({ a:
|
|
210
|
+
src: "b",
|
|
211
|
+
transformer: (value) => "test transformer",
|
|
212
|
+
reverser: (value) => ({ a: "test reverser" }),
|
|
187
213
|
initialize: true,
|
|
188
214
|
}),
|
|
189
215
|
__metadata("design:type", String)
|
|
@@ -192,16 +218,16 @@ TestFlag = __decorate([
|
|
|
192
218
|
(0, class_decorator_1.MapClass)()
|
|
193
219
|
], TestFlag);
|
|
194
220
|
const testFlagInitialize = new TestFlag().from();
|
|
195
|
-
console.log("TEST INITIALIZE",
|
|
221
|
+
console.log("TEST INITIALIZE", testFlagInitialize && testFlagInitialize.a == "test transformer" ? "✅" : "❌");
|
|
196
222
|
const testFlag0 = new TestFlag().from();
|
|
197
223
|
const testFlag0Map = testFlag0.toMap();
|
|
198
|
-
console.log("TEST FLAG0",
|
|
199
|
-
const testFlag1 = new TestFlag().from({ flTest:
|
|
224
|
+
console.log("TEST FLAG0", testFlag0.flTest === false && testFlag0Map.flTest == "0" ? "✅" : "❌");
|
|
225
|
+
const testFlag1 = new TestFlag().from({ flTest: "1" });
|
|
200
226
|
const testFlag1Map = testFlag1.toMap();
|
|
201
|
-
console.log("TEST FLAG1",
|
|
202
|
-
const testFlag2 = new TestFlag().from({ flTest:
|
|
227
|
+
console.log("TEST FLAG1", testFlag1.flTest && testFlag1Map.flTest == "1" ? "✅" : "❌");
|
|
228
|
+
const testFlag2 = new TestFlag().from({ flTest: "0" });
|
|
203
229
|
const testFlag2Map = testFlag2.toMap();
|
|
204
|
-
console.log("TEST FLAG2",
|
|
230
|
+
console.log("TEST FLAG2", !testFlag2.flTest && testFlag2Map.flTest == "0" ? "✅" : "❌");
|
|
205
231
|
let TestWithoutMapField = class TestWithoutMapField {
|
|
206
232
|
id;
|
|
207
233
|
name;
|
|
@@ -210,9 +236,54 @@ TestWithoutMapField = __decorate([
|
|
|
210
236
|
(0, class_decorator_1.MapClass)()
|
|
211
237
|
], TestWithoutMapField);
|
|
212
238
|
const JSONObject2 = {
|
|
213
|
-
id:
|
|
214
|
-
name:
|
|
239
|
+
id: "1",
|
|
240
|
+
name: "Supplier 1",
|
|
215
241
|
};
|
|
216
242
|
const testWOMF = new TestWithoutMapField().from(JSONObject2);
|
|
217
|
-
console.log("TEST WITHOUT MAP FIELD",
|
|
243
|
+
console.log("TEST WITHOUT MAP FIELD", testWOMF ? "✅" : "❌");
|
|
244
|
+
let ObjDecorator = class ObjDecorator {
|
|
245
|
+
id;
|
|
246
|
+
testObject;
|
|
247
|
+
};
|
|
248
|
+
__decorate([
|
|
249
|
+
(0, object_decorator_1.ObjectField)(() => ObjDecorator),
|
|
250
|
+
__metadata("design:type", ObjDecorator)
|
|
251
|
+
], ObjDecorator.prototype, "testObject", void 0);
|
|
252
|
+
ObjDecorator = __decorate([
|
|
253
|
+
(0, class_decorator_1.MapClass)()
|
|
254
|
+
], ObjDecorator);
|
|
255
|
+
let TestDecorators = class TestDecorators {
|
|
256
|
+
date;
|
|
257
|
+
objList;
|
|
258
|
+
obj;
|
|
259
|
+
};
|
|
260
|
+
__decorate([
|
|
261
|
+
(0, date_decorator_1.DateField)(),
|
|
262
|
+
__metadata("design:type", Date)
|
|
263
|
+
], TestDecorators.prototype, "date", void 0);
|
|
264
|
+
__decorate([
|
|
265
|
+
(0, array_decorator_1.ArrayField)(() => ObjDecorator),
|
|
266
|
+
__metadata("design:type", Array)
|
|
267
|
+
], TestDecorators.prototype, "objList", void 0);
|
|
268
|
+
__decorate([
|
|
269
|
+
(0, object_decorator_1.ObjectField)(() => ObjDecorator),
|
|
270
|
+
__metadata("design:type", ObjDecorator)
|
|
271
|
+
], TestDecorators.prototype, "obj", void 0);
|
|
272
|
+
TestDecorators = __decorate([
|
|
273
|
+
(0, class_decorator_1.MapClass)()
|
|
274
|
+
], TestDecorators);
|
|
275
|
+
const JSONTestDecorators = {
|
|
276
|
+
date: "2023-10-01T00:00:00Z",
|
|
277
|
+
objList: [
|
|
278
|
+
{ id: "1", testObject: { id: "2" } },
|
|
279
|
+
{ id: "3", testObject: { id: "4" } },
|
|
280
|
+
],
|
|
281
|
+
obj: { id: "5", testObject: { id: "6" } },
|
|
282
|
+
};
|
|
283
|
+
const testDecorators = new TestDecorators().from(JSONTestDecorators);
|
|
284
|
+
console.log("TEST WITH DECORATORS", (testDecorators.date.toISOString() &&
|
|
285
|
+
testDecorators.objList?.length === 2 &&
|
|
286
|
+
testDecorators.obj instanceof ObjDecorator &&
|
|
287
|
+
testDecorators.objList[0] instanceof ObjDecorator &&
|
|
288
|
+
testDecorators.objList[0].testObject instanceof ObjDecorator) ? "✅" : "❌");
|
|
218
289
|
console.log("\n");
|