mapper-factory 3.1.2 → 3.2.0
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 +3 -1
- package/dist/field-decorators/array.decorator.js +3 -2
- package/dist/field-decorators/date.decorator.d.ts +3 -1
- package/dist/field-decorators/date.decorator.js +2 -1
- package/dist/field-decorators/object.decorator.d.ts +3 -1
- package/dist/field-decorators/object.decorator.js +3 -2
- package/dist/test.js +9 -7
- package/package.json +1 -1
- package/dist/example.d.ts +0 -1
- package/dist/example.js +0 -210
- package/dist/examples/example.d.ts +0 -1
- package/dist/examples/example.js +0 -135
- package/dist/field.decorator.d.ts +0 -22
- package/dist/field.decorator.js +0 -54
- package/dist/mapper-functions.d.ts +0 -5
- package/dist/mapper-functions.js +0 -20
- package/dist/mapper.d.ts +0 -190
- package/dist/mapper.js +0 -371
- package/dist/mapper.to-remove.d.ts +0 -190
- package/dist/mapper.to-remove.js +0 -371
- package/dist/src/field.decorator.d.ts +0 -22
- package/dist/src/field.decorator.js +0 -54
- package/dist/src/index.d.ts +0 -10
- package/dist/src/index.js +0 -13
- package/dist/src/mapper.d.ts +0 -46
- package/dist/src/mapper.js +0 -286
- package/dist/src/types.d.ts +0 -6
- package/dist/src/types.js +0 -2
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { MapInterface } from "../class.decorator";
|
|
2
|
-
export declare function ArrayField<T extends MapInterface<T>>(clsFactory:
|
|
2
|
+
export declare function ArrayField<T extends MapInterface<T>>(clsFactory: new () => T, opt?: {
|
|
3
|
+
src?: string;
|
|
4
|
+
}): PropertyDecorator;
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ArrayField = ArrayField;
|
|
4
4
|
const field_decorator_1 = require("./field.decorator");
|
|
5
|
-
function ArrayField(clsFactory) {
|
|
6
|
-
const Ctor = clsFactory
|
|
5
|
+
function ArrayField(clsFactory, opt) {
|
|
6
|
+
const Ctor = clsFactory;
|
|
7
7
|
return (0, field_decorator_1.MapField)({
|
|
8
|
+
src: opt?.src,
|
|
8
9
|
transformer: (arr) => Array.isArray(arr)
|
|
9
10
|
? arr.map((item) => new Ctor().from(item))
|
|
10
11
|
: [],
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DateField = DateField;
|
|
4
4
|
const field_decorator_1 = require("./field.decorator");
|
|
5
|
-
function DateField() {
|
|
5
|
+
function DateField(opt) {
|
|
6
6
|
return (0, field_decorator_1.MapField)({
|
|
7
|
+
src: opt?.src,
|
|
7
8
|
transformer: (dateISO) => dateISO ? new Date(dateISO) : null,
|
|
8
9
|
reverser: (date) => date?.toISOString() ?? null,
|
|
9
10
|
});
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { MapInterface } from "../class.decorator";
|
|
2
|
-
export declare function ObjectField<T extends MapInterface<T>>(clsFactory:
|
|
2
|
+
export declare function ObjectField<T extends MapInterface<T>>(clsFactory: new () => T, opt?: {
|
|
3
|
+
src?: string;
|
|
4
|
+
}): PropertyDecorator;
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ObjectField = ObjectField;
|
|
4
4
|
const field_decorator_1 = require("./field.decorator");
|
|
5
|
-
function ObjectField(clsFactory) {
|
|
6
|
-
const Ctor = clsFactory
|
|
5
|
+
function ObjectField(clsFactory, opt) {
|
|
6
|
+
const Ctor = clsFactory;
|
|
7
7
|
return (0, field_decorator_1.MapField)({
|
|
8
|
+
src: opt?.src,
|
|
8
9
|
transformer: (obj) => (obj ? new Ctor().from(obj) : null),
|
|
9
10
|
reverser: (obj) => obj?.toMap?.() ?? null,
|
|
10
11
|
});
|
package/dist/test.js
CHANGED
|
@@ -246,7 +246,7 @@ let ObjDecorator = class ObjDecorator {
|
|
|
246
246
|
testObject;
|
|
247
247
|
};
|
|
248
248
|
__decorate([
|
|
249
|
-
(0, object_decorator_1.ObjectField)(
|
|
249
|
+
(0, object_decorator_1.ObjectField)(ObjDecorator),
|
|
250
250
|
__metadata("design:type", ObjDecorator)
|
|
251
251
|
], ObjDecorator.prototype, "testObject", void 0);
|
|
252
252
|
ObjDecorator = __decorate([
|
|
@@ -258,22 +258,22 @@ let TestDecorators = class TestDecorators {
|
|
|
258
258
|
obj;
|
|
259
259
|
};
|
|
260
260
|
__decorate([
|
|
261
|
-
(0, date_decorator_1.DateField)(),
|
|
261
|
+
(0, date_decorator_1.DateField)({ src: "dateSrc" }),
|
|
262
262
|
__metadata("design:type", Date)
|
|
263
263
|
], TestDecorators.prototype, "date", void 0);
|
|
264
264
|
__decorate([
|
|
265
|
-
(0, array_decorator_1.ArrayField)(
|
|
265
|
+
(0, array_decorator_1.ArrayField)(ObjDecorator),
|
|
266
266
|
__metadata("design:type", Array)
|
|
267
267
|
], TestDecorators.prototype, "objList", void 0);
|
|
268
268
|
__decorate([
|
|
269
|
-
(0, object_decorator_1.ObjectField)(
|
|
269
|
+
(0, object_decorator_1.ObjectField)(ObjDecorator),
|
|
270
270
|
__metadata("design:type", ObjDecorator)
|
|
271
271
|
], TestDecorators.prototype, "obj", void 0);
|
|
272
272
|
TestDecorators = __decorate([
|
|
273
273
|
(0, class_decorator_1.MapClass)()
|
|
274
274
|
], TestDecorators);
|
|
275
275
|
const JSONTestDecorators = {
|
|
276
|
-
|
|
276
|
+
dateSrc: "2023-10-01T00:00:00Z",
|
|
277
277
|
objList: [
|
|
278
278
|
{ id: "1", testObject: { id: "2" } },
|
|
279
279
|
{ id: "3", testObject: { id: "4" } },
|
|
@@ -281,9 +281,11 @@ const JSONTestDecorators = {
|
|
|
281
281
|
obj: { id: "5", testObject: { id: "6" } },
|
|
282
282
|
};
|
|
283
283
|
const testDecorators = new TestDecorators().from(JSONTestDecorators);
|
|
284
|
-
console.log("TEST WITH DECORATORS",
|
|
284
|
+
console.log("TEST WITH DECORATORS", testDecorators.date.toISOString() &&
|
|
285
285
|
testDecorators.objList?.length === 2 &&
|
|
286
286
|
testDecorators.obj instanceof ObjDecorator &&
|
|
287
287
|
testDecorators.objList[0] instanceof ObjDecorator &&
|
|
288
|
-
testDecorators.objList[0].testObject instanceof ObjDecorator
|
|
288
|
+
testDecorators.objList[0].testObject instanceof ObjDecorator
|
|
289
|
+
? "✅"
|
|
290
|
+
: "❌");
|
|
289
291
|
console.log("\n");
|
package/package.json
CHANGED
package/dist/example.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/example.js
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const field_decorator_1 = require("../src/field.decorator");
|
|
13
|
-
const class_decorator_1 = require("./class.decorator");
|
|
14
|
-
/*import { MapperFactory } from "../src/mapper";
|
|
15
|
-
|
|
16
|
-
class TestClass extends MapperFactory {
|
|
17
|
-
field1: string;
|
|
18
|
-
field2: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const testInstance = new TestClass({ field1: 'value1', field2: 'value2' });
|
|
22
|
-
console.log('Test instance:', testInstance);
|
|
23
|
-
|
|
24
|
-
class History extends MapperFactory {
|
|
25
|
-
id: string;
|
|
26
|
-
|
|
27
|
-
@MapField({
|
|
28
|
-
transformer: (arr) => " TEST TRASFORMER",
|
|
29
|
-
reverser: (arr) => " TEST REVERSER",
|
|
30
|
-
})
|
|
31
|
-
name: string;
|
|
32
|
-
|
|
33
|
-
@MapField({
|
|
34
|
-
src: "control"
|
|
35
|
-
})
|
|
36
|
-
testControl: string;
|
|
37
|
-
|
|
38
|
-
@MapField({
|
|
39
|
-
initialize: true,
|
|
40
|
-
transformer: (arr, obj) => { return [obj.monday, obj.tuesday] },
|
|
41
|
-
reverser: (arr) => {
|
|
42
|
-
return { monday: arr && arr[0], tuesday: arr && arr[1] }
|
|
43
|
-
},
|
|
44
|
-
})
|
|
45
|
-
daysActive: string[];
|
|
46
|
-
|
|
47
|
-
@MapField({
|
|
48
|
-
src: "test.concatenation"
|
|
49
|
-
})
|
|
50
|
-
testConcatenation: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
class User extends MapperFactory {
|
|
54
|
-
@MapField({
|
|
55
|
-
src: 'firstName'
|
|
56
|
-
})
|
|
57
|
-
name: string;
|
|
58
|
-
|
|
59
|
-
@MapField({
|
|
60
|
-
src: 'lastName'
|
|
61
|
-
})
|
|
62
|
-
surname: string;
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
//let emp1: User = new User({ firstName: "Summer", lastName: "Smith" });
|
|
67
|
-
//let emp2: User = new User({ firstName: "Morty", lastName: "Smith" });
|
|
68
|
-
//let JSONObject = { firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"], boss: { firstName: "Nello", lastName: "Stanco" } };
|
|
69
|
-
|
|
70
|
-
//TEST constructor
|
|
71
|
-
//let u = new User(JSONObject);
|
|
72
|
-
|
|
73
|
-
//const passedConstructorTest: boolean = u.name == JSONObject.firstName && u.surname == JSONObject.lastName && u.employees?.map(emp => emp.name == emp1.name) && u.roles?.map(role => role == "CEO") && u.boss.name == "Nello" && u.boss.surname == "Stanco";
|
|
74
|
-
//console.log("TEST constructor", passedConstructorTest ? '✅' : '❌');
|
|
75
|
-
|
|
76
|
-
//TEST objToModel method with JS Object
|
|
77
|
-
let u1 = new User();
|
|
78
|
-
console.log("\nTEST objToModel method with JS Object");
|
|
79
|
-
console.log(u1.objToModel(u));
|
|
80
|
-
console.log("\n\n");
|
|
81
|
-
|
|
82
|
-
//TEST objToModel method with JSON Object
|
|
83
|
-
console.log("\nTEST objToModel method with JSON Object");
|
|
84
|
-
console.log(u.objToModel({ name: "Rick TEST-objToModel", roles: ["CEO TEST-objToModel", "EMPLOYEE TEST-objToModel"] }));
|
|
85
|
-
console.log("\n\n");
|
|
86
|
-
|
|
87
|
-
//TEST toMap method
|
|
88
|
-
console.log("\nTEST toMap method");
|
|
89
|
-
console.log(u.toMap());
|
|
90
|
-
console.log("\n\n");
|
|
91
|
-
|
|
92
|
-
//TEST empty method
|
|
93
|
-
console.log("\nTEST empty method");
|
|
94
|
-
console.log(u.empty());
|
|
95
|
-
console.log("\n\n");
|
|
96
|
-
|
|
97
|
-
//TEST get AND set method
|
|
98
|
-
u.set("employees[1].name", "name editato");
|
|
99
|
-
console.log("\nTEST get AND set method");
|
|
100
|
-
console.log(u);
|
|
101
|
-
console.log(u.get("employees[1].name"));
|
|
102
|
-
console.log("\n\n");
|
|
103
|
-
|
|
104
|
-
//TEST deep copy
|
|
105
|
-
console.log("\nTEST deep copy");
|
|
106
|
-
let dpCopy = new User(u.toMap());
|
|
107
|
-
dpCopy.name = "nome dpCopy";
|
|
108
|
-
console.log(u);
|
|
109
|
-
console.log(dpCopy);
|
|
110
|
-
console.log("\n\n");
|
|
111
|
-
|
|
112
|
-
//TEST trasformer/reverser
|
|
113
|
-
console.log("\nTEST reverser");
|
|
114
|
-
let h1 = new History({ name: "h1" });
|
|
115
|
-
let h2 = new History({ name: "h2" });
|
|
116
|
-
u.histories = [h1, h2];
|
|
117
|
-
console.log(u);
|
|
118
|
-
console.log(u.toMap());
|
|
119
|
-
console.log("\n\n");
|
|
120
|
-
|
|
121
|
-
//TEST ref
|
|
122
|
-
console.log("\nTEST REF");
|
|
123
|
-
let hTest = new History({ monday: "0", tuesday: "1", control: "control" });
|
|
124
|
-
console.log(hTest)
|
|
125
|
-
hTest.daysActive = ['1', '0'];
|
|
126
|
-
console.log(hTest.toMap());
|
|
127
|
-
console.log("\n\n");
|
|
128
|
-
|
|
129
|
-
//TEST concat with point
|
|
130
|
-
console.log("\nTEST CONCAT W POINT");
|
|
131
|
-
let hTest2 = new History({ test: { concatenation: "resolve " }, control: "control" });
|
|
132
|
-
console.log(hTest2)
|
|
133
|
-
console.log(hTest2.toMap());
|
|
134
|
-
console.log("\n\n");
|
|
135
|
-
|
|
136
|
-
//TEST FUNC MAPPER
|
|
137
|
-
console.log("\nTEST FUNC MAPPER");
|
|
138
|
-
console.log(emp1);
|
|
139
|
-
console.log(toMap(emp1));
|
|
140
|
-
const testEmp = toModel<User>(toMap(emp1));
|
|
141
|
-
console.log("USER MODEL: ", testEmp);
|
|
142
|
-
console.log(objToModel(emp1, { name: "test", surname: "prova" }));
|
|
143
|
-
console.log("\n\n");
|
|
144
|
-
*/
|
|
145
|
-
//TEST NEW MapperFactory
|
|
146
|
-
console.log("\nNew MapperFactory");
|
|
147
|
-
let Test = class Test {
|
|
148
|
-
a;
|
|
149
|
-
};
|
|
150
|
-
__decorate([
|
|
151
|
-
(0, field_decorator_1.MapField)({
|
|
152
|
-
src: 'b',
|
|
153
|
-
transformer: value => 'test transformer',
|
|
154
|
-
reverser: value => ({ a: 'test reverser' }),
|
|
155
|
-
}),
|
|
156
|
-
__metadata("design:type", String)
|
|
157
|
-
], Test.prototype, "a", void 0);
|
|
158
|
-
Test = __decorate([
|
|
159
|
-
(0, class_decorator_1.MapClass)()
|
|
160
|
-
], Test);
|
|
161
|
-
const testEmpty = new Test().from();
|
|
162
|
-
const testFilled = new Test().from({ b: 'filled' });
|
|
163
|
-
const checkTest1 = (testEmpty && testEmpty.empty() == true && testEmpty.filled() == false);
|
|
164
|
-
const checkTest2 = (testFilled && testFilled.empty() == false && testFilled.filled() == true);
|
|
165
|
-
console.log("TEST EMPTY/FILLED", (checkTest1 && checkTest2) ? '✅' : '❌');
|
|
166
|
-
const model3 = new Test().toModel({ a: 'test to model' });
|
|
167
|
-
console.log("TEST TO MODEL", (model3.a == 'test to model') ? '✅' : '❌');
|
|
168
|
-
const model4 = model3.toMap();
|
|
169
|
-
console.log("TEST TO MAP", (model4.b.a == 'test reverser') ? '✅' : '❌');
|
|
170
|
-
const model5 = model3.copy();
|
|
171
|
-
console.log("TEST COPY", (Object.keys(model5).every(k => model5[k] == model3[k])) ? '✅' : '❌');
|
|
172
|
-
console.log("\n");
|
|
173
|
-
//TEST TestFlag with initialize
|
|
174
|
-
console.log("TestFlag with initialize");
|
|
175
|
-
let TestFlag = class TestFlag {
|
|
176
|
-
flTest;
|
|
177
|
-
a;
|
|
178
|
-
};
|
|
179
|
-
__decorate([
|
|
180
|
-
(0, field_decorator_1.MapField)({
|
|
181
|
-
transformer: (num) => num == '1',
|
|
182
|
-
reverser: bool => bool ? '1' : '0',
|
|
183
|
-
initialize: true,
|
|
184
|
-
}),
|
|
185
|
-
__metadata("design:type", Boolean)
|
|
186
|
-
], TestFlag.prototype, "flTest", void 0);
|
|
187
|
-
__decorate([
|
|
188
|
-
(0, field_decorator_1.MapField)({
|
|
189
|
-
src: 'b',
|
|
190
|
-
transformer: value => 'test transformer',
|
|
191
|
-
reverser: value => ({ a: 'test reverser' }),
|
|
192
|
-
initialize: true,
|
|
193
|
-
}),
|
|
194
|
-
__metadata("design:type", String)
|
|
195
|
-
], TestFlag.prototype, "a", void 0);
|
|
196
|
-
TestFlag = __decorate([
|
|
197
|
-
(0, class_decorator_1.MapClass)()
|
|
198
|
-
], TestFlag);
|
|
199
|
-
const testFlagInitialize = new TestFlag().from();
|
|
200
|
-
console.log("TEST INITIALIZE", (testFlagInitialize && testFlagInitialize.a == 'test transformer') ? '✅' : '❌');
|
|
201
|
-
const testFlag0 = new TestFlag().from();
|
|
202
|
-
const testFlag0Map = testFlag0.toMap();
|
|
203
|
-
console.log("TEST FLAG0", ((testFlag0.flTest === false) && testFlag0Map.flTest == '0') ? '✅' : '❌');
|
|
204
|
-
const testFlag1 = new TestFlag().from({ flTest: '1' });
|
|
205
|
-
const testFlag1Map = testFlag1.toMap();
|
|
206
|
-
console.log("TEST FLAG1", (testFlag1.flTest && testFlag1Map.flTest == '1') ? '✅' : '❌');
|
|
207
|
-
const testFlag2 = new TestFlag().from({ flTest: '0' });
|
|
208
|
-
const testFlag2Map = testFlag2.toMap();
|
|
209
|
-
console.log("TEST FLAG2", (!testFlag2.flTest && testFlag2Map.flTest == '0') ? '✅' : '❌');
|
|
210
|
-
console.log("\n");
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/examples/example.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const field_decorator_1 = require("../src/field.decorator");
|
|
13
|
-
const mapper_1 = require("../src/mapper");
|
|
14
|
-
class History extends mapper_1.MapperFactory {
|
|
15
|
-
}
|
|
16
|
-
__decorate([
|
|
17
|
-
(0, field_decorator_1.MapField)({
|
|
18
|
-
transformer: (arr) => " TEST TRASFORMER",
|
|
19
|
-
reverser: (arr) => " TEST REVERSER",
|
|
20
|
-
}),
|
|
21
|
-
__metadata("design:type", String)
|
|
22
|
-
], History.prototype, "name", void 0);
|
|
23
|
-
__decorate([
|
|
24
|
-
(0, field_decorator_1.MapField)({
|
|
25
|
-
src: "control"
|
|
26
|
-
}),
|
|
27
|
-
__metadata("design:type", String)
|
|
28
|
-
], History.prototype, "testControl", void 0);
|
|
29
|
-
__decorate([
|
|
30
|
-
(0, field_decorator_1.MapField)({
|
|
31
|
-
initialize: true,
|
|
32
|
-
transformer: (arr, obj) => { return [obj.monday, obj.tuesday]; },
|
|
33
|
-
reverser: (arr) => {
|
|
34
|
-
return { monday: arr && arr[0], tuesday: arr && arr[1] };
|
|
35
|
-
},
|
|
36
|
-
}),
|
|
37
|
-
__metadata("design:type", Array)
|
|
38
|
-
], History.prototype, "daysActive", void 0);
|
|
39
|
-
__decorate([
|
|
40
|
-
(0, field_decorator_1.MapField)({
|
|
41
|
-
src: "test.concatenation"
|
|
42
|
-
}),
|
|
43
|
-
__metadata("design:type", String)
|
|
44
|
-
], History.prototype, "testConcatenation", void 0);
|
|
45
|
-
class User extends mapper_1.MapperFactory {
|
|
46
|
-
}
|
|
47
|
-
__decorate([
|
|
48
|
-
(0, field_decorator_1.MapField)({
|
|
49
|
-
src: 'firstName'
|
|
50
|
-
}),
|
|
51
|
-
__metadata("design:type", String)
|
|
52
|
-
], User.prototype, "name", void 0);
|
|
53
|
-
__decorate([
|
|
54
|
-
(0, field_decorator_1.MapField)({
|
|
55
|
-
src: 'lastName'
|
|
56
|
-
}),
|
|
57
|
-
__metadata("design:type", String)
|
|
58
|
-
], User.prototype, "surname", void 0);
|
|
59
|
-
__decorate([
|
|
60
|
-
(0, field_decorator_1.MapField)({
|
|
61
|
-
src: 'rolesToMap',
|
|
62
|
-
transformer: (arr) => arr?.map(role => role + " TEST TRASFORMER"),
|
|
63
|
-
reverser: (arr) => arr?.map(role => role.replace(" TEST TRASFORMER", "")),
|
|
64
|
-
}),
|
|
65
|
-
__metadata("design:type", Array)
|
|
66
|
-
], User.prototype, "roles", void 0);
|
|
67
|
-
__decorate([
|
|
68
|
-
(0, field_decorator_1.MapField)({
|
|
69
|
-
transformer: (arr) => arr?.map(user => new User(user))
|
|
70
|
-
}),
|
|
71
|
-
__metadata("design:type", Array)
|
|
72
|
-
], User.prototype, "employees", void 0);
|
|
73
|
-
__decorate([
|
|
74
|
-
(0, field_decorator_1.MapField)({
|
|
75
|
-
transformer: (user) => new User(user)
|
|
76
|
-
}),
|
|
77
|
-
__metadata("design:type", User)
|
|
78
|
-
], User.prototype, "boss", void 0);
|
|
79
|
-
__decorate([
|
|
80
|
-
(0, field_decorator_1.MapField)({
|
|
81
|
-
transformer: histories => histories?.map(hst => new History(hst)),
|
|
82
|
-
reverser: histories => histories?.map(hst => hst.toMap()),
|
|
83
|
-
}),
|
|
84
|
-
__metadata("design:type", Array)
|
|
85
|
-
], User.prototype, "histories", void 0);
|
|
86
|
-
let emp1 = new User({ firstName: "Summer", lastName: "Smith" });
|
|
87
|
-
let emp2 = new User({ firstName: "Morty", lastName: "Smith" });
|
|
88
|
-
let JSONObject = { firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"], boss: { firstName: "Nello", lastName: "Stanco" } };
|
|
89
|
-
//TEST constructor
|
|
90
|
-
let u = new User(JSONObject);
|
|
91
|
-
console.log("\nTEST constructor");
|
|
92
|
-
console.log(JSONObject);
|
|
93
|
-
console.log(u);
|
|
94
|
-
//TEST objToModel method with JS Object
|
|
95
|
-
let u1 = new User();
|
|
96
|
-
console.log("\nTEST objToModel method with JS Object");
|
|
97
|
-
console.log(u1.objToModel(u));
|
|
98
|
-
//TEST objToModel method with JSON Object
|
|
99
|
-
console.log("\nTEST objToModel method with JSON Object");
|
|
100
|
-
console.log(u.objToModel({ name: "Rick TEST-objToModel", roles: ["CEO TEST-objToModel", "EMPLOYEE TEST-objToModel"] }));
|
|
101
|
-
//TEST toMap method
|
|
102
|
-
console.log("\nTEST toMap method");
|
|
103
|
-
console.log(u.toMap());
|
|
104
|
-
//TEST empty method
|
|
105
|
-
console.log("\nTEST empty method");
|
|
106
|
-
console.log(u.empty());
|
|
107
|
-
//TEST get AND set method
|
|
108
|
-
u.set("employees[1].name", "name editato");
|
|
109
|
-
console.log("\nTEST get AND set method");
|
|
110
|
-
console.log(u);
|
|
111
|
-
console.log(u.get("employees[1].name"));
|
|
112
|
-
//TEST deep copy
|
|
113
|
-
console.log("\nTEST deep copy");
|
|
114
|
-
let dpCopy = new User(u.toMap());
|
|
115
|
-
dpCopy.name = "nome dpCopy";
|
|
116
|
-
console.log(u);
|
|
117
|
-
console.log(dpCopy);
|
|
118
|
-
//TEST trasformer/reverser
|
|
119
|
-
console.log("\nTEST reverser");
|
|
120
|
-
let h1 = new History({ name: "h1" });
|
|
121
|
-
let h2 = new History({ name: "h2" });
|
|
122
|
-
u.histories = [h1, h2];
|
|
123
|
-
console.log(u);
|
|
124
|
-
console.log(u.toMap());
|
|
125
|
-
//TEST ref
|
|
126
|
-
console.log("\nTEST REF");
|
|
127
|
-
let hTest = new History({ monday: "0", tuesday: "1", control: "control" });
|
|
128
|
-
console.log(hTest);
|
|
129
|
-
hTest.daysActive = ['1', '0'];
|
|
130
|
-
console.log(hTest.toMap());
|
|
131
|
-
//TEST concat with point
|
|
132
|
-
console.log("\nTEST CONCAT W POINT");
|
|
133
|
-
let hTest2 = new History({ test: { concatenation: "resolve " }, control: "control" });
|
|
134
|
-
console.log(hTest2);
|
|
135
|
-
console.log(hTest2.toMap());
|
|
@@ -1,22 +0,0 @@
|
|
|
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;
|
package/dist/field.decorator.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
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;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { MapperFactory } from "./mapper.to-remove";
|
|
2
|
-
export declare function toMap(model: MapperFactory): Object;
|
|
3
|
-
export declare function toModel<T extends MapperFactory>(obj: Object): T;
|
|
4
|
-
export declare function objToModel<T extends MapperFactory>(model: MapperFactory, obj: Object): T;
|
|
5
|
-
export declare function copy<T extends MapperFactory>(model: MapperFactory): T;
|
package/dist/mapper-functions.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.copy = exports.objToModel = exports.toModel = exports.toMap = void 0;
|
|
4
|
-
const mapper_to_remove_1 = require("./mapper.to-remove");
|
|
5
|
-
function toMap(model) {
|
|
6
|
-
return model.toMap();
|
|
7
|
-
}
|
|
8
|
-
exports.toMap = toMap;
|
|
9
|
-
function toModel(obj) {
|
|
10
|
-
return new mapper_to_remove_1.MapperFactory(obj);
|
|
11
|
-
}
|
|
12
|
-
exports.toModel = toModel;
|
|
13
|
-
function objToModel(model, obj) {
|
|
14
|
-
return model.objToModel(obj);
|
|
15
|
-
}
|
|
16
|
-
exports.objToModel = objToModel;
|
|
17
|
-
function copy(model) {
|
|
18
|
-
return new mapper_to_remove_1.MapperFactory(model.toMap());
|
|
19
|
-
}
|
|
20
|
-
exports.copy = copy;
|