mapper-factory 3.1.3 → 4.0.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/class.decorator.js +10 -13
- package/dist/field-decorators/array.decorator.d.ts +3 -1
- package/dist/field-decorators/array.decorator.js +4 -6
- package/dist/field-decorators/date.decorator.d.ts +3 -1
- package/dist/field-decorators/date.decorator.js +4 -6
- package/dist/field-decorators/field.decorator.js +15 -25
- package/dist/field-decorators/object.decorator.d.ts +3 -1
- package/dist/field-decorators/object.decorator.js +4 -6
- package/dist/functions.js +11 -21
- package/dist/index.js +6 -13
- package/dist/test.js +34 -34
- package/dist/types.js +1 -2
- package/package.json +9 -2
- 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
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;
|
package/dist/mapper.d.ts
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { ClassType } from "./types";
|
|
2
|
-
export declare class MapperFactory {
|
|
3
|
-
/**
|
|
4
|
-
* Constructor of the mapper.
|
|
5
|
-
*
|
|
6
|
-
* @param object object to be mapped considering metadata "src", "transformer" and "reverser"
|
|
7
|
-
*/
|
|
8
|
-
constructor(object?: any);
|
|
9
|
-
/**
|
|
10
|
-
* Maps the properties of an object based on the provided metadata list.
|
|
11
|
-
*
|
|
12
|
-
* @param object - The object whose properties need to be mapped.
|
|
13
|
-
* @param metadataList - The list of metadata used to map the object's properties.
|
|
14
|
-
*
|
|
15
|
-
* This method iterates over each property of the given object. For each property, it retrieves the corresponding metadata keys.
|
|
16
|
-
* If metadata keys are found, it maps the property using the `mapProperty` method for each metadata key.
|
|
17
|
-
* If no metadata keys are found, it maps the property directly using the `mapDirectProperty` method.
|
|
18
|
-
*/
|
|
19
|
-
private mapObject;
|
|
20
|
-
/**
|
|
21
|
-
* Retrieves the metadata keys associated with a given property name.
|
|
22
|
-
*
|
|
23
|
-
* @param metadataList - The list of metadata objects to search through.
|
|
24
|
-
* @param propertyName - The name of the property to find metadata keys for.
|
|
25
|
-
* @returns An array of metadata keys that are associated with the specified property name.
|
|
26
|
-
*/
|
|
27
|
-
private getMetaKeys;
|
|
28
|
-
/**
|
|
29
|
-
* Maps a property from the given object to the current instance based on the provided metadata.
|
|
30
|
-
*
|
|
31
|
-
* @param object - The source object containing the property to be mapped.
|
|
32
|
-
* @param metadataList - A list of metadata that provides mapping information.
|
|
33
|
-
* @param propertyName - The name of the property to be mapped.
|
|
34
|
-
* @param metaKey - The key used to retrieve the specific metadata for the property.
|
|
35
|
-
*
|
|
36
|
-
* This method performs the following steps:
|
|
37
|
-
* 1. Retrieves the metadata for the given metaKey.
|
|
38
|
-
* 2. If metadata exists, it gets the properties' stereoid and creates a copy of the object.
|
|
39
|
-
* 3. Iterates over the properties' stereoid and updates the object copy.
|
|
40
|
-
* 4. If a transformer function is defined in the metadata, it applies the transformer to the object copy and the original object.
|
|
41
|
-
* 5. If no metadata exists for the metaKey, it maps the property directly using the `mapDirectProperty` method.
|
|
42
|
-
*/
|
|
43
|
-
private mapProperty;
|
|
44
|
-
/**
|
|
45
|
-
* Parses a dot-separated string and returns an array of PropStereoid objects.
|
|
46
|
-
* Each PropStereoid object contains information about the property name,
|
|
47
|
-
* whether it is an array, and the array index if applicable.
|
|
48
|
-
*
|
|
49
|
-
* @param src - The dot-separated string to parse.
|
|
50
|
-
* @returns An array of PropStereoid objects.
|
|
51
|
-
*/
|
|
52
|
-
private getPropsStereoid;
|
|
53
|
-
/**
|
|
54
|
-
* Retrieves a copy of an object property based on the provided `PropStereoid`.
|
|
55
|
-
*
|
|
56
|
-
* @param objCopy - The object from which the property copy is to be retrieved.
|
|
57
|
-
* @param prop - The `PropStereoid` object containing property details.
|
|
58
|
-
* @returns A copy of the specified property from the object.
|
|
59
|
-
*
|
|
60
|
-
* @remarks
|
|
61
|
-
* If the property is an array, the method navigates through the array indices
|
|
62
|
-
* specified in `prop.arrIndex` to retrieve the nested property.
|
|
63
|
-
* Otherwise, it directly retrieves the property specified in `prop.prop`.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```typescript
|
|
67
|
-
* const obj = { a: { b: [ { c: 1 }, { c: 2 } ] } };
|
|
68
|
-
* const prop = { isArray: true, arrIndex: '[1]', prop: 'b' };
|
|
69
|
-
* const result = getObjectCopy(obj, prop);
|
|
70
|
-
* console.log(result); // Output: { c: 2 }
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
private getObjectCopy;
|
|
74
|
-
/**
|
|
75
|
-
* Maps a direct property from the given object to the current instance based on the provided metadata list.
|
|
76
|
-
*
|
|
77
|
-
* @param object - The source object containing the properties to be mapped.
|
|
78
|
-
* @param metadataList - An optional list of metadata that defines how properties should be mapped and transformed.
|
|
79
|
-
* @param propertyName - The name of the property to be mapped from the source object.
|
|
80
|
-
*
|
|
81
|
-
* The method performs the following steps:
|
|
82
|
-
* 1. Checks if the metadata list contains a key that matches the source property name.
|
|
83
|
-
* 2. If a matching metadata key is found, it uses the metadata to determine the source property name (`src`) and applies any defined transformer function.
|
|
84
|
-
* 3. If no matching metadata key is found, it directly maps the property from the source object, applying any transformer function if defined.
|
|
85
|
-
*/
|
|
86
|
-
private mapDirectProperty;
|
|
87
|
-
/**
|
|
88
|
-
* Initializes properties of the current object based on the provided metadata list.
|
|
89
|
-
*
|
|
90
|
-
* @param metadataList - An object containing metadata information for properties.
|
|
91
|
-
* @param object - The object to be used for initializing properties.
|
|
92
|
-
*
|
|
93
|
-
* The method iterates over the keys of the metadataList. For each key, if the corresponding metadata
|
|
94
|
-
* has an `initialize` property set to true, a `transformer` function, and the current object does not
|
|
95
|
-
* already have a property with the same name, it initializes the property using the transformer function.
|
|
96
|
-
*/
|
|
97
|
-
private initializeProperties;
|
|
98
|
-
/**
|
|
99
|
-
* Converts the current object instance to a mapped object.
|
|
100
|
-
*
|
|
101
|
-
* This method iterates over the properties of the current object and maps them
|
|
102
|
-
* according to the metadata provided by `getMapFieldMetadataList`. If a property
|
|
103
|
-
* has associated metadata, it uses the `mapToJson` method to map it. Otherwise,
|
|
104
|
-
* it directly assigns the property value to the resulting object, applying a
|
|
105
|
-
* reverser function if specified in the metadata.
|
|
106
|
-
*
|
|
107
|
-
* @returns {any} The mapped object.
|
|
108
|
-
*/
|
|
109
|
-
toMap(): any;
|
|
110
|
-
/**
|
|
111
|
-
* Maps the provided object to a JSON structure based on the given metadata list.
|
|
112
|
-
*
|
|
113
|
-
* @param metadataList - An object containing metadata information for mapping.
|
|
114
|
-
* @param obj - The object to be mapped to JSON.
|
|
115
|
-
* @param propertyName - The name of the property to be mapped.
|
|
116
|
-
*
|
|
117
|
-
* The method supports nested properties and arrays. If the source property name
|
|
118
|
-
* (src) contains dots, it indicates nested properties. The method will traverse
|
|
119
|
-
* the object structure accordingly and create nested objects or arrays as needed.
|
|
120
|
-
*
|
|
121
|
-
* The `getPropsStereoid` method is used to parse the nested property names and
|
|
122
|
-
* determine if they are arrays. The `getReversedValue` method is used to get the
|
|
123
|
-
* value to be assigned to the final property.
|
|
124
|
-
*
|
|
125
|
-
* Example:
|
|
126
|
-
* ```
|
|
127
|
-
* const metadataList = {
|
|
128
|
-
* 'nested.property': { src: 'nested.property' },
|
|
129
|
-
* 'array[0].item': { src: 'array[0].item' }
|
|
130
|
-
* };
|
|
131
|
-
* const obj = {};
|
|
132
|
-
* mapToJson(metadataList, obj, 'nested.property');
|
|
133
|
-
* mapToJson(metadataList, obj, 'array[0].item');
|
|
134
|
-
* ```
|
|
135
|
-
* The resulting `obj` will have the structure:
|
|
136
|
-
* ```
|
|
137
|
-
* {
|
|
138
|
-
* nested: {
|
|
139
|
-
* property: <value>
|
|
140
|
-
* },
|
|
141
|
-
* array: [
|
|
142
|
-
* { item: <value> }
|
|
143
|
-
* ]
|
|
144
|
-
* }
|
|
145
|
-
* ```
|
|
146
|
-
*/
|
|
147
|
-
private mapToJson;
|
|
148
|
-
/**
|
|
149
|
-
* Retrieves the reversed value of a property based on the provided metadata.
|
|
150
|
-
*
|
|
151
|
-
* @param metadataList - An object containing metadata for properties.
|
|
152
|
-
* @param propertyName - The name of the property to retrieve the reversed value for.
|
|
153
|
-
* @returns The reversed value of the property. If the property is an array, it applies the reverser function or maps each item to its mapped value. If the property has a `toMap` method, it calls that method. Otherwise, it applies the reverser function or returns the property value directly.
|
|
154
|
-
*/
|
|
155
|
-
private getReversedValue;
|
|
156
|
-
/**
|
|
157
|
-
* Maps the properties of the given object to the current instance based on metadata.
|
|
158
|
-
*
|
|
159
|
-
* @param obj - The source object whose properties are to be mapped.
|
|
160
|
-
* @returns The current instance with mapped properties.
|
|
161
|
-
*/
|
|
162
|
-
objToModel(obj: Object): this;
|
|
163
|
-
/**
|
|
164
|
-
* Checks if all properties of the current object are either `undefined` or `null`.
|
|
165
|
-
*
|
|
166
|
-
* @returns {boolean} `true` if all properties are `undefined` or `null`, otherwise `false`.
|
|
167
|
-
*/
|
|
168
|
-
empty(): boolean;
|
|
169
|
-
/**
|
|
170
|
-
* Retrieves a nested property value from the object based on the provided path.
|
|
171
|
-
* The path can include dot notation and array indices.
|
|
172
|
-
*
|
|
173
|
-
* @template T - The expected type of the property value.
|
|
174
|
-
* @param {string} path - The path to the property, using dot notation and array indices.
|
|
175
|
-
* @returns {T} - The value of the property at the specified path.
|
|
176
|
-
*/
|
|
177
|
-
get<T>(path: string): T;
|
|
178
|
-
/**
|
|
179
|
-
* Sets the value at the specified path within the object.
|
|
180
|
-
* The path is a string that can include dot notation and array indices.
|
|
181
|
-
*
|
|
182
|
-
* @param path - The path to the property to set, using dot notation and array indices.
|
|
183
|
-
* @param value - The value to set at the specified path.
|
|
184
|
-
*/
|
|
185
|
-
set(path: string, value: any): void;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* @deprecated The method should not be used
|
|
189
|
-
*/
|
|
190
|
-
export declare function MapperFactoryFun(baseClass?: ClassType): ClassType;
|