mapper-factory 3.0.0 → 3.1.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/test.js CHANGED
@@ -1,200 +1,218 @@
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
- var User_1;
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- const class_decorator_1 = require("./class.decorator");
14
- const field_decorator_1 = require("./field.decorator");
15
- //MAPPER FACTORY - TEST
16
- console.log("\nMAPPER FACTORY - TEST");
17
- console.log("\n");
18
- let History = class History {
19
- id;
20
- name;
21
- testControl;
22
- daysActive;
23
- testConcatenation;
24
- };
25
- __decorate([
26
- (0, field_decorator_1.MapField)({
27
- transformer: (arr) => " TEST TRASFORMER",
28
- reverser: (arr) => " TEST REVERSER",
29
- }),
30
- __metadata("design:type", String)
31
- ], History.prototype, "name", void 0);
32
- __decorate([
33
- (0, field_decorator_1.MapField)({
34
- src: "control"
35
- }),
36
- __metadata("design:type", String)
37
- ], History.prototype, "testControl", void 0);
38
- __decorate([
39
- (0, field_decorator_1.MapField)({
40
- initialize: true,
41
- transformer: (arr, obj) => { return [obj.monday, obj.tuesday]; },
42
- reverser: (arr) => {
43
- return { monday: arr && arr[0], tuesday: arr && arr[1] };
44
- },
45
- }),
46
- __metadata("design:type", Array)
47
- ], History.prototype, "daysActive", void 0);
48
- __decorate([
49
- (0, field_decorator_1.MapField)({
50
- src: "test.concatenation"
51
- }),
52
- __metadata("design:type", String)
53
- ], History.prototype, "testConcatenation", void 0);
54
- History = __decorate([
55
- (0, class_decorator_1.MapClass)()
56
- ], History);
57
- let User = User_1 = class User {
58
- id;
59
- username;
60
- name;
61
- surname;
62
- roles;
63
- employees;
64
- boss;
65
- histories;
66
- };
67
- __decorate([
68
- (0, field_decorator_1.MapField)({
69
- src: 'firstName'
70
- }),
71
- __metadata("design:type", String)
72
- ], User.prototype, "name", void 0);
73
- __decorate([
74
- (0, field_decorator_1.MapField)({
75
- src: 'lastName'
76
- }),
77
- __metadata("design:type", String)
78
- ], User.prototype, "surname", void 0);
79
- __decorate([
80
- (0, field_decorator_1.MapField)({
81
- src: 'rolesToMap',
82
- transformer: (arr) => arr?.map(role => role + " TEST TRASFORMER"),
83
- reverser: (arr) => arr?.map(role => role.replace(" TEST TRASFORMER", "")),
84
- }),
85
- __metadata("design:type", Array)
86
- ], User.prototype, "roles", void 0);
87
- __decorate([
88
- (0, field_decorator_1.MapField)({
89
- transformer: (arr) => arr?.map(user => new User_1().from(user))
90
- }),
91
- __metadata("design:type", Array)
92
- ], User.prototype, "employees", void 0);
93
- __decorate([
94
- (0, field_decorator_1.MapField)({
95
- transformer: (user) => new User_1().from(user)
96
- }),
97
- __metadata("design:type", User)
98
- ], User.prototype, "boss", void 0);
99
- __decorate([
100
- (0, field_decorator_1.MapField)({
101
- transformer: histories => histories?.map(hst => new History().from(hst)),
102
- reverser: histories => histories?.map(hst => hst.toMap()),
103
- }),
104
- __metadata("design:type", Array)
105
- ], User.prototype, "histories", void 0);
106
- User = User_1 = __decorate([
107
- (0, class_decorator_1.MapClass)()
108
- ], User);
109
- const emp1 = new User().from({ firstName: "Summer", lastName: "Smith" });
110
- const emp2 = new User().from({ firstName: "Morty", lastName: "Smith" });
111
- const JSONObject = { firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"], boss: { firstName: "Nello", lastName: "Stanco" } };
112
- //TEST constructor
113
- const u = new User().from(JSONObject);
114
- const constructorTest = 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";
115
- console.log("TEST CONSTRUCTOR", constructorTest ? '✅' : '❌');
116
- //TEST toModel method with JS Object
117
- const u1 = new User().toModel(u);
118
- const toModelTest = u1.name == JSONObject.firstName && u1.surname == JSONObject.lastName && u1.employees?.map(emp => emp.name == emp1.name) && u1.roles?.map(role => role == "CEO") && u1.boss.name == "Nello" && u1.boss.surname == "Stanco";
119
- console.log("TEST TO MODEL USER", toModelTest ? '✅' : '❌');
120
- //TEST [GET, SET] METHODS
121
- u.set("employees[1].name", "name editato");
122
- const toModelTest1 = u.get("employees[1].name") == "name editato";
123
- console.log("TEST [GET, SET] METHODS", toModelTest1 ? '✅' : '❌');
124
- //TEST TRANSFORMER/REVERSER
125
- const h1 = new History().from({ name: "h1" });
126
- const h2 = new History().from({ name: "h2" });
127
- u.histories = [h1, h2];
128
- const uMapped = u.toMap();
129
- const toModelTest2 = uMapped.histories?.map(h => h.name == " TEST REVERSER") && u.histories?.map(h => h.name == " TEST TRASFORMER");
130
- console.log("TEST TRANSFORMER/REVERSER", toModelTest2 ? '✅' : '❌');
131
- //TEST REF
132
- const hTest = new History().from({ monday: "0", tuesday: "1", control: "control" });
133
- hTest.daysActive = ['1', '0'];
134
- const toModelTest3 = hTest.toMap().daysActive.monday == '1' && hTest.toMap().daysActive.tuesday == '0';
135
- console.log("TEST REF", toModelTest3 ? '✅' : '❌');
136
- //TEST CONCAT WITH POINT
137
- const hTest2 = new History().from({ test: { concatenation: "resolve " }, control: "control" });
138
- const toModelTest4 = hTest2.toMap().test.concatenation == 'resolve ';
139
- console.log("TEST CONCAT WITH POINT", toModelTest4 ? '' : '');
140
- let Test = class Test {
141
- a;
142
- };
143
- __decorate([
144
- (0, field_decorator_1.MapField)({
145
- src: 'b',
146
- transformer: value => 'test transformer',
147
- reverser: value => ({ a: 'test reverser' }),
148
- }),
149
- __metadata("design:type", String)
150
- ], Test.prototype, "a", void 0);
151
- Test = __decorate([
152
- (0, class_decorator_1.MapClass)()
153
- ], Test);
154
- const testEmpty = new Test().from();
155
- const testFilled = new Test().from({ b: 'filled' });
156
- const checkTest1 = (testEmpty && testEmpty.empty() == true && testEmpty.filled() == false);
157
- const checkTest2 = (testFilled && testFilled.empty() == false && testFilled.filled() == true);
158
- console.log("TEST EMPTY/FILLED WITH INITIALIZE", (checkTest1 && checkTest2) ? '✅' : '❌');
159
- const model3 = new Test().toModel({ a: 'test to model' });
160
- console.log("TEST TO MODEL WITH INITIALIZE", (model3.a == 'test to model') ? '✅' : '❌');
161
- const model4 = model3.toMap();
162
- console.log("TEST TO MAP WITH INITIALIZE", (model4.b.a == 'test reverser') ? '✅' : '❌');
163
- const model5 = model3.copy();
164
- console.log("TEST COPY WITH INITIALIZE", (Object.keys(model5).every(k => model5[k] == model3[k])) ? '✅' : '❌');
165
- let TestFlag = class TestFlag {
166
- flTest;
167
- a;
168
- };
169
- __decorate([
170
- (0, field_decorator_1.MapField)({
171
- transformer: (num) => num == '1',
172
- reverser: bool => bool ? '1' : '0',
173
- initialize: true,
174
- }),
175
- __metadata("design:type", Boolean)
176
- ], TestFlag.prototype, "flTest", void 0);
177
- __decorate([
178
- (0, field_decorator_1.MapField)({
179
- src: 'b',
180
- transformer: value => 'test transformer',
181
- reverser: value => ({ a: 'test reverser' }),
182
- initialize: true,
183
- }),
184
- __metadata("design:type", String)
185
- ], TestFlag.prototype, "a", void 0);
186
- TestFlag = __decorate([
187
- (0, class_decorator_1.MapClass)()
188
- ], TestFlag);
189
- const testFlagInitialize = new TestFlag().from();
190
- console.log("TEST INITIALIZE", (testFlagInitialize && testFlagInitialize.a == 'test transformer') ? '✅' : '❌');
191
- const testFlag0 = new TestFlag().from();
192
- const testFlag0Map = testFlag0.toMap();
193
- console.log("TEST FLAG0", ((testFlag0.flTest === false) && testFlag0Map.flTest == '0') ? '✅' : '❌');
194
- const testFlag1 = new TestFlag().from({ flTest: '1' });
195
- const testFlag1Map = testFlag1.toMap();
196
- console.log("TEST FLAG1", (testFlag1.flTest && testFlag1Map.flTest == '1') ? '✅' : '❌');
197
- const testFlag2 = new TestFlag().from({ flTest: '0' });
198
- const testFlag2Map = testFlag2.toMap();
199
- console.log("TEST FLAG2", (!testFlag2.flTest && testFlag2Map.flTest == '0') ? '✅' : '❌');
200
- console.log("\n");
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 class_decorator_1 = require("./class.decorator");
13
+ const field_decorator_1 = require("./field.decorator");
14
+ //MAPPER FACTORY - TEST
15
+ console.log("\nMAPPER FACTORY - TEST");
16
+ console.log("\n");
17
+ let History = class History {
18
+ id;
19
+ name;
20
+ testControl;
21
+ daysActive;
22
+ testConcatenation;
23
+ };
24
+ __decorate([
25
+ (0, field_decorator_1.MapField)({
26
+ transformer: (arr) => " TEST TRASFORMER",
27
+ reverser: (arr) => " TEST REVERSER",
28
+ }),
29
+ __metadata("design:type", String)
30
+ ], History.prototype, "name", void 0);
31
+ __decorate([
32
+ (0, field_decorator_1.MapField)({
33
+ src: "control"
34
+ }),
35
+ __metadata("design:type", String)
36
+ ], History.prototype, "testControl", void 0);
37
+ __decorate([
38
+ (0, field_decorator_1.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
+ __metadata("design:type", Array)
46
+ ], History.prototype, "daysActive", void 0);
47
+ __decorate([
48
+ (0, field_decorator_1.MapField)({
49
+ src: "test.concatenation"
50
+ }),
51
+ __metadata("design:type", String)
52
+ ], History.prototype, "testConcatenation", void 0);
53
+ History = __decorate([
54
+ (0, class_decorator_1.MapClass)()
55
+ ], History);
56
+ let User = class User {
57
+ id;
58
+ username;
59
+ name;
60
+ surname;
61
+ roles;
62
+ employees;
63
+ boss;
64
+ histories;
65
+ };
66
+ __decorate([
67
+ (0, field_decorator_1.MapField)({
68
+ src: 'firstName'
69
+ }),
70
+ __metadata("design:type", String)
71
+ ], User.prototype, "name", void 0);
72
+ __decorate([
73
+ (0, field_decorator_1.MapField)({
74
+ src: 'lastName'
75
+ }),
76
+ __metadata("design:type", String)
77
+ ], User.prototype, "surname", void 0);
78
+ __decorate([
79
+ (0, field_decorator_1.MapField)({
80
+ src: 'rolesToMap',
81
+ transformer: (arr) => arr?.map(role => role + " TEST TRASFORMER"),
82
+ reverser: (arr) => arr?.map(role => role.replace(" TEST TRASFORMER", "")),
83
+ }),
84
+ __metadata("design:type", Array)
85
+ ], User.prototype, "roles", void 0);
86
+ __decorate([
87
+ (0, field_decorator_1.MapField)({
88
+ transformer: (arr) => arr?.map(user => new User().from(user))
89
+ }),
90
+ __metadata("design:type", Array)
91
+ ], User.prototype, "employees", void 0);
92
+ __decorate([
93
+ (0, field_decorator_1.MapField)({
94
+ transformer: (user) => new User().from(user)
95
+ }),
96
+ __metadata("design:type", User)
97
+ ], User.prototype, "boss", void 0);
98
+ __decorate([
99
+ (0, field_decorator_1.MapField)({
100
+ transformer: histories => histories?.map(hst => new History().from(hst)),
101
+ reverser: histories => histories?.map(hst => hst.toMap()),
102
+ }),
103
+ __metadata("design:type", Array)
104
+ ], User.prototype, "histories", void 0);
105
+ User = __decorate([
106
+ (0, class_decorator_1.MapClass)()
107
+ ], User);
108
+ const emp1 = new User().from({ firstName: "Summer", lastName: "Smith" });
109
+ const emp2 = new User().from({ firstName: "Morty", lastName: "Smith" });
110
+ const JSONObject = { username: 'god', firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"], boss: { firstName: "Nello", lastName: "Stanco" } };
111
+ //TEST constructor
112
+ const u = new User().from(JSONObject);
113
+ const constructorTest = u.username == JSONObject.username &&
114
+ u.name == JSONObject.firstName &&
115
+ u.surname == JSONObject.lastName &&
116
+ u.employees?.map(emp => emp.name == emp1.name) &&
117
+ u.roles?.map(role => role == "CEO") &&
118
+ u.boss.name == "Nello" &&
119
+ u.boss.surname == "Stanco";
120
+ console.log("TEST CONSTRUCTOR", constructorTest ? '✅' : '❌');
121
+ //TEST toModel method with JS Object
122
+ const u1 = new User().toModel(u);
123
+ const toModelTest = u1.name == JSONObject.firstName && u1.surname == JSONObject.lastName && u1.employees?.map(emp => emp.name == emp1.name) && u1.roles?.map(role => role == "CEO") && u1.boss.name == "Nello" && u1.boss.surname == "Stanco";
124
+ console.log("TEST TO MODEL USER", toModelTest ? '✅' : '❌');
125
+ //TEST [GET, SET] METHODS
126
+ u.set("employees[1].name", "name editato");
127
+ const toModelTest1 = u.get("employees[1].name") == "name editato";
128
+ console.log("TEST [GET, SET] METHODS", toModelTest1 ? '✅' : '❌');
129
+ //TEST TRANSFORMER/REVERSER
130
+ const h1 = new History().from({ name: "h1" });
131
+ const h2 = new History().from({ name: "h2" });
132
+ u.histories = [h1, h2];
133
+ const uMapped = u.toMap();
134
+ const toModelTest2 = uMapped.histories?.map(h => h.name == " TEST REVERSER") && u.histories?.map(h => h.name == " TEST TRASFORMER");
135
+ console.log("TEST TRANSFORMER/REVERSER", toModelTest2 ? '✅' : '❌');
136
+ //TEST REF
137
+ const hTest = new History().from({ monday: "0", tuesday: "1", control: "control" });
138
+ hTest.daysActive = ['1', '0'];
139
+ const toModelTest3 = hTest.toMap().daysActive.monday == '1' && hTest.toMap().daysActive.tuesday == '0';
140
+ console.log("TEST REF", toModelTest3 ? '✅' : '❌');
141
+ //TEST CONCAT WITH POINT
142
+ const hTest2 = new History().from({ test: { concatenation: "resolve " }, control: "control" });
143
+ const toModelTest4 = hTest2.toMap().test.concatenation == 'resolve ';
144
+ console.log("TEST CONCAT WITH POINT", toModelTest4 ? '✅' : '❌');
145
+ let Test = class Test {
146
+ a;
147
+ };
148
+ __decorate([
149
+ (0, field_decorator_1.MapField)({
150
+ src: 'b',
151
+ transformer: value => 'test transformer',
152
+ reverser: value => ({ a: 'test reverser' }),
153
+ }),
154
+ __metadata("design:type", String)
155
+ ], Test.prototype, "a", void 0);
156
+ Test = __decorate([
157
+ (0, class_decorator_1.MapClass)()
158
+ ], Test);
159
+ const testEmpty = new Test().from();
160
+ const testFilled = new Test().from({ b: 'filled' });
161
+ const checkTest1 = (testEmpty && testEmpty.empty() == true && testEmpty.filled() == false);
162
+ const checkTest2 = (testFilled && testFilled.empty() == false && testFilled.filled() == true);
163
+ console.log("TEST EMPTY/FILLED WITH INITIALIZE", (checkTest1 && checkTest2) ? '✅' : '❌');
164
+ const model3 = new Test().toModel({ a: 'test to model' });
165
+ console.log("TEST TO MODEL WITH INITIALIZE", (model3.a == 'test to model') ? '✅' : '❌');
166
+ const model4 = model3.toMap();
167
+ console.log("TEST TO MAP WITH INITIALIZE", (model4.b.a == 'test reverser') ? '✅' : '❌');
168
+ const model5 = model3.copy();
169
+ console.log("TEST COPY WITH INITIALIZE", (Object.keys(model5).every(k => model5[k] == model3[k])) ? '✅' : '❌');
170
+ let TestFlag = class TestFlag {
171
+ flTest;
172
+ a;
173
+ };
174
+ __decorate([
175
+ (0, field_decorator_1.MapField)({
176
+ transformer: (num) => num == '1',
177
+ reverser: bool => bool ? '1' : '0',
178
+ initialize: true,
179
+ }),
180
+ __metadata("design:type", Boolean)
181
+ ], TestFlag.prototype, "flTest", void 0);
182
+ __decorate([
183
+ (0, field_decorator_1.MapField)({
184
+ src: 'b',
185
+ transformer: value => 'test transformer',
186
+ reverser: value => ({ a: 'test reverser' }),
187
+ initialize: true,
188
+ }),
189
+ __metadata("design:type", String)
190
+ ], TestFlag.prototype, "a", void 0);
191
+ TestFlag = __decorate([
192
+ (0, class_decorator_1.MapClass)()
193
+ ], TestFlag);
194
+ const testFlagInitialize = new TestFlag().from();
195
+ console.log("TEST INITIALIZE", (testFlagInitialize && testFlagInitialize.a == 'test transformer') ? '✅' : '❌');
196
+ const testFlag0 = new TestFlag().from();
197
+ const testFlag0Map = testFlag0.toMap();
198
+ console.log("TEST FLAG0", ((testFlag0.flTest === false) && testFlag0Map.flTest == '0') ? '✅' : '❌');
199
+ const testFlag1 = new TestFlag().from({ flTest: '1' });
200
+ const testFlag1Map = testFlag1.toMap();
201
+ console.log("TEST FLAG1", (testFlag1.flTest && testFlag1Map.flTest == '1') ? '✅' : '❌');
202
+ const testFlag2 = new TestFlag().from({ flTest: '0' });
203
+ const testFlag2Map = testFlag2.toMap();
204
+ console.log("TEST FLAG2", (!testFlag2.flTest && testFlag2Map.flTest == '0') ? '✅' : '❌');
205
+ let TestWithoutMapField = class TestWithoutMapField {
206
+ id;
207
+ name;
208
+ };
209
+ TestWithoutMapField = __decorate([
210
+ (0, class_decorator_1.MapClass)()
211
+ ], TestWithoutMapField);
212
+ const JSONObject2 = {
213
+ id: '1',
214
+ name: 'Supplier 1',
215
+ };
216
+ const testWOMF = new TestWithoutMapField().from(JSONObject2);
217
+ console.log("TEST WITHOUT MAP FIELD", (testWOMF) ? '✅' : '❌');
218
+ console.log("\n");
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export declare type ClassType<Class = any> = {
2
- new (...args: any[]): Class;
3
- };
4
- export declare type AnyFunction<A = any> = (...input: any[]) => A;
5
- export declare type AnyConstructor<A = Record<string, unknown>> = new (...input: any[]) => A;
6
- export declare type Mixin<T extends AnyFunction> = InstanceType<ReturnType<T>>;
1
+ export declare type ClassType<Class = any> = {
2
+ new (...args: any[]): Class;
3
+ };
4
+ export declare type AnyFunction<A = any> = (...input: any[]) => A;
5
+ export declare type AnyConstructor<A = Record<string, unknown>> = new (...input: any[]) => A;
6
+ export declare type Mixin<T extends AnyFunction> = InstanceType<ReturnType<T>>;
package/dist/types.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mapper-factory",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "mapper for typescript object",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "homepage": "https://github.com/lucaAngrisani/mapper-factory#readme",
37
37
  "devDependencies": {
38
- "reflect-metadata": "^0.1.14",
39
- "typescript": "^4.9.4"
38
+ "reflect-metadata": "^0.2.2",
39
+ "typescript": "^5.7.3"
40
40
  }
41
41
  }