mapper-factory 2.0.2 → 3.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.
@@ -5,7 +5,7 @@ export interface MapInterface<T> {
5
5
  toModel: (object: any) => T;
6
6
  empty: () => boolean;
7
7
  filled: () => boolean;
8
- get: (path: string) => T;
8
+ get: (path: string) => any;
9
9
  set: (path: string, value: any) => void;
10
10
  copy: () => T;
11
11
  }
package/dist/example.js CHANGED
@@ -9,127 +9,98 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- const _1 = require(".");
13
- const class_decorator_1 = require("../src/class.decorator");
14
12
  const field_decorator_1 = require("../src/field.decorator");
15
- const mapper_1 = require("../src/mapper");
16
- class History extends mapper_1.MapperFactory {
17
- id;
18
- name;
19
- testControl;
20
- daysActive;
21
- testConcatenation;
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;
22
19
  }
23
- __decorate([
24
- (0, field_decorator_1.MapField)({
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({
25
28
  transformer: (arr) => " TEST TRASFORMER",
26
29
  reverser: (arr) => " TEST REVERSER",
27
- }),
28
- __metadata("design:type", String)
29
- ], History.prototype, "name", void 0);
30
- __decorate([
31
- (0, field_decorator_1.MapField)({
30
+ })
31
+ name: string;
32
+
33
+ @MapField({
32
34
  src: "control"
33
- }),
34
- __metadata("design:type", String)
35
- ], History.prototype, "testControl", void 0);
36
- __decorate([
37
- (0, field_decorator_1.MapField)({
35
+ })
36
+ testControl: string;
37
+
38
+ @MapField({
38
39
  initialize: true,
39
- transformer: (arr, obj) => { return [obj.monday, obj.tuesday]; },
40
+ transformer: (arr, obj) => { return [obj.monday, obj.tuesday] },
40
41
  reverser: (arr) => {
41
- return { monday: arr && arr[0], tuesday: arr && arr[1] };
42
+ return { monday: arr && arr[0], tuesday: arr && arr[1] }
42
43
  },
43
- }),
44
- __metadata("design:type", Array)
45
- ], History.prototype, "daysActive", void 0);
46
- __decorate([
47
- (0, field_decorator_1.MapField)({
44
+ })
45
+ daysActive: string[];
46
+
47
+ @MapField({
48
48
  src: "test.concatenation"
49
- }),
50
- __metadata("design:type", String)
51
- ], History.prototype, "testConcatenation", void 0);
52
- class User extends mapper_1.MapperFactory {
53
- id;
54
- username;
55
- name;
56
- surname;
57
- roles;
58
- employees;
59
- boss;
60
- histories;
49
+ })
50
+ testConcatenation: string;
61
51
  }
62
- __decorate([
63
- (0, field_decorator_1.MapField)({
52
+
53
+ class User extends MapperFactory {
54
+ @MapField({
64
55
  src: 'firstName'
65
- }),
66
- __metadata("design:type", String)
67
- ], User.prototype, "name", void 0);
68
- __decorate([
69
- (0, field_decorator_1.MapField)({
56
+ })
57
+ name: string;
58
+
59
+ @MapField({
70
60
  src: 'lastName'
71
- }),
72
- __metadata("design:type", String)
73
- ], User.prototype, "surname", void 0);
74
- __decorate([
75
- (0, field_decorator_1.MapField)({
76
- src: 'rolesToMap',
77
- transformer: (arr) => arr?.map(role => role + " TEST TRASFORMER"),
78
- reverser: (arr) => arr?.map(role => role.replace(" TEST TRASFORMER", "")),
79
- }),
80
- __metadata("design:type", Array)
81
- ], User.prototype, "roles", void 0);
82
- __decorate([
83
- (0, field_decorator_1.MapField)({
84
- transformer: (arr) => arr?.map(user => new User(user))
85
- }),
86
- __metadata("design:type", Array)
87
- ], User.prototype, "employees", void 0);
88
- __decorate([
89
- (0, field_decorator_1.MapField)({
90
- transformer: (user) => new User(user)
91
- }),
92
- __metadata("design:type", User)
93
- ], User.prototype, "boss", void 0);
94
- __decorate([
95
- (0, field_decorator_1.MapField)({
96
- transformer: histories => histories?.map(hst => new History(hst)),
97
- reverser: histories => histories?.map(hst => hst.toMap()),
98
- }),
99
- __metadata("design:type", Array)
100
- ], User.prototype, "histories", void 0);
101
- let emp1 = new User({ firstName: "Summer", lastName: "Smith" });
102
- let emp2 = new User({ firstName: "Morty", lastName: "Smith" });
103
- let JSONObject = { firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"], boss: { firstName: "Nello", lastName: "Stanco" } };
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
+
104
70
  //TEST constructor
105
- let u = new User(JSONObject);
106
- console.log("\nTEST constructor");
107
- console.log(JSONObject);
108
- console.log(u);
109
- console.log("\n\n");
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
+
110
76
  //TEST objToModel method with JS Object
111
77
  let u1 = new User();
112
78
  console.log("\nTEST objToModel method with JS Object");
113
79
  console.log(u1.objToModel(u));
114
80
  console.log("\n\n");
81
+
115
82
  //TEST objToModel method with JSON Object
116
83
  console.log("\nTEST objToModel method with JSON Object");
117
84
  console.log(u.objToModel({ name: "Rick TEST-objToModel", roles: ["CEO TEST-objToModel", "EMPLOYEE TEST-objToModel"] }));
118
85
  console.log("\n\n");
86
+
119
87
  //TEST toMap method
120
88
  console.log("\nTEST toMap method");
121
89
  console.log(u.toMap());
122
90
  console.log("\n\n");
91
+
123
92
  //TEST empty method
124
93
  console.log("\nTEST empty method");
125
94
  console.log(u.empty());
126
95
  console.log("\n\n");
96
+
127
97
  //TEST get AND set method
128
98
  u.set("employees[1].name", "name editato");
129
99
  console.log("\nTEST get AND set method");
130
100
  console.log(u);
131
101
  console.log(u.get("employees[1].name"));
132
102
  console.log("\n\n");
103
+
133
104
  //TEST deep copy
134
105
  console.log("\nTEST deep copy");
135
106
  let dpCopy = new User(u.toMap());
@@ -137,6 +108,7 @@ dpCopy.name = "nome dpCopy";
137
108
  console.log(u);
138
109
  console.log(dpCopy);
139
110
  console.log("\n\n");
111
+
140
112
  //TEST trasformer/reverser
141
113
  console.log("\nTEST reverser");
142
114
  let h1 = new History({ name: "h1" });
@@ -145,29 +117,33 @@ u.histories = [h1, h2];
145
117
  console.log(u);
146
118
  console.log(u.toMap());
147
119
  console.log("\n\n");
120
+
148
121
  //TEST ref
149
122
  console.log("\nTEST REF");
150
123
  let hTest = new History({ monday: "0", tuesday: "1", control: "control" });
151
- console.log(hTest);
124
+ console.log(hTest)
152
125
  hTest.daysActive = ['1', '0'];
153
126
  console.log(hTest.toMap());
154
127
  console.log("\n\n");
128
+
155
129
  //TEST concat with point
156
130
  console.log("\nTEST CONCAT W POINT");
157
131
  let hTest2 = new History({ test: { concatenation: "resolve " }, control: "control" });
158
- console.log(hTest2);
132
+ console.log(hTest2)
159
133
  console.log(hTest2.toMap());
160
134
  console.log("\n\n");
135
+
161
136
  //TEST FUNC MAPPER
162
137
  console.log("\nTEST FUNC MAPPER");
163
138
  console.log(emp1);
164
- console.log((0, _1.toMap)(emp1));
165
- const testEmp = (0, _1.toModel)((0, _1.toMap)(emp1));
139
+ console.log(toMap(emp1));
140
+ const testEmp = toModel<User>(toMap(emp1));
166
141
  console.log("USER MODEL: ", testEmp);
167
- console.log((0, _1.objToModel)(emp1, { name: "test", surname: "prova" }));
142
+ console.log(objToModel(emp1, { name: "test", surname: "prova" }));
168
143
  console.log("\n\n");
144
+ */
169
145
  //TEST NEW MapperFactory
170
- console.log("\nTEST NEW MapperFactory");
146
+ console.log("\nNew MapperFactory");
171
147
  let Test = class Test {
172
148
  a;
173
149
  };
@@ -176,22 +152,59 @@ __decorate([
176
152
  src: 'b',
177
153
  transformer: value => 'test transformer',
178
154
  reverser: value => ({ a: 'test reverser' }),
179
- initialize: true,
180
155
  }),
181
156
  __metadata("design:type", String)
182
157
  ], Test.prototype, "a", void 0);
183
158
  Test = __decorate([
184
159
  (0, class_decorator_1.MapClass)()
185
160
  ], Test);
186
- const test = new Test().from( /** INSIDE HERE YOUR OBJECT TO MAP */);
187
- console.log("TEST 1: ", test);
188
- console.log("EMPTY: ", test.empty());
189
- console.log("FILLED: ", test.filled());
190
- test.from({ b: 'filled' });
191
- console.log("TEST 2: ", test);
192
- console.log("EMPTY: ", test.empty());
193
- console.log("FILLED: ", test.filled());
194
- console.log("TO MODEL: ", test.toModel({ a: 'test to model' }));
195
- console.log("TO MAP: ", test.toMap());
196
- console.log("COPY: ", test.copy());
197
- console.log("\n\n");
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");