mapper-factory 1.0.14 → 1.0.15
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/README.md +24 -7
- package/dist/mapper.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Mapper-Factory
|
|
2
2
|
Mapper-Factory is a fully documented TypeScript library that provides a simple and easy-to-use way to map objects from one type to another. With just a few lines of code, you can convert complex, nested objects into the desired format.
|
|
3
3
|
|
|
4
|
+
Work well on data structure and after enjoy the coding process :)
|
|
5
|
+
|
|
4
6
|
## Installation
|
|
5
7
|
To install the package, you can use npm by running the following command:
|
|
6
8
|
|
|
@@ -26,19 +28,29 @@ After that, you can use *@MapField* decorator over single property to specify th
|
|
|
26
28
|
```
|
|
27
29
|
class User extends MapperFactory {
|
|
28
30
|
|
|
31
|
+
@MapField({
|
|
32
|
+
src: 'firstName'
|
|
33
|
+
})
|
|
34
|
+
name: string;
|
|
35
|
+
|
|
29
36
|
@MapField({
|
|
30
37
|
src: 'obj.obj[0][1]',
|
|
31
38
|
transformer: (arr) => arr.map(role => role + " TEST TRASFORMER"),
|
|
32
39
|
reverser: (arr) => arr.map(role => role.replace(" TEST TRASFORMER", "")),
|
|
33
40
|
})
|
|
34
41
|
roles?: string[];
|
|
42
|
+
|
|
43
|
+
@MapField({
|
|
44
|
+
transformer: (user) => new User(user)
|
|
45
|
+
})
|
|
46
|
+
boss: User;
|
|
35
47
|
}
|
|
36
48
|
```
|
|
37
49
|
|
|
38
50
|
Inside *@MapField* you can use:
|
|
39
|
-
- ***src
|
|
40
|
-
- ***transform
|
|
41
|
-
- ***reverse
|
|
51
|
+
- ***src***: define a string of original field name (also using a path like *"obj.obj[0][1]"*)
|
|
52
|
+
- ***transform***: function to transform data input in *constructor* of the class
|
|
53
|
+
- ***reverse***: function to transform data input in *toMap* method of the class
|
|
42
54
|
|
|
43
55
|
In this example:
|
|
44
56
|
|
|
@@ -69,10 +81,15 @@ class User extends MapperFactory {
|
|
|
69
81
|
transformer: (arr) => arr.map(user => new User(user))
|
|
70
82
|
})
|
|
71
83
|
employees?: User[];
|
|
84
|
+
|
|
85
|
+
@MapField({
|
|
86
|
+
transformer: (user) => new User(user)
|
|
87
|
+
})
|
|
88
|
+
boss: User;
|
|
72
89
|
}
|
|
73
90
|
```
|
|
74
91
|
|
|
75
|
-
|
|
92
|
+
You can define a new User ***u***:
|
|
76
93
|
|
|
77
94
|
```
|
|
78
95
|
let emp1: User = new User({ firstName: "Summer", lastName: "Smith" });
|
|
@@ -81,7 +98,7 @@ let emp2: User = new User({ firstName: "Morty", lastName: "Smith" });
|
|
|
81
98
|
let u = new User({ firstName: "Rick", lastName: "Sanchez", employees: [emp1, emp2], rolesToMap: ["CEO", "EMPLOYEE"] });
|
|
82
99
|
```
|
|
83
100
|
|
|
84
|
-
In that way you can create a new JS Object User passing a JSON object. Automatically constructor use *src* and *transformer*
|
|
101
|
+
In that way you can create a new JS Object User passing a JSON object. Automatically constructor use *src* and *transformer* to obtain the correct object you want.
|
|
85
102
|
|
|
86
103
|
In this specific case we have trasformed a JSON object:
|
|
87
104
|
|
|
@@ -101,7 +118,7 @@ u.toMap()
|
|
|
101
118
|
|
|
102
119
|
Obtaining the original JSON Object.
|
|
103
120
|
|
|
104
|
-
You can also fill properties of an object from another by using ***objToModel()*** method, in that way:
|
|
121
|
+
You can also fill properties of an object from another (typically with same class) by using ***objToModel()*** method, in that way:
|
|
105
122
|
|
|
106
123
|
```
|
|
107
124
|
let uCopy = new User();
|
|
@@ -125,7 +142,7 @@ user.name = "Rick";
|
|
|
125
142
|
user.empty(); //FALSE
|
|
126
143
|
```
|
|
127
144
|
|
|
128
|
-
It is implemented also a GET/SET method
|
|
145
|
+
It is implemented also a GET/SET method whitch use the path. Using ***get(path: string)*** and ***set(path: string, value: any)*** you can access to the property you want and then GET or SET the value:
|
|
129
146
|
|
|
130
147
|
```
|
|
131
148
|
u.set("name", "Rick TEST-SET");
|
package/dist/mapper.js
CHANGED
|
@@ -186,11 +186,11 @@ class MapperFactory {
|
|
|
186
186
|
if (metadataList[propertyName].transformer) {
|
|
187
187
|
if (Array.isArray(obj[propertyName])) {
|
|
188
188
|
this[propertyName] = obj[propertyName].map(item => {
|
|
189
|
-
return
|
|
189
|
+
return item;
|
|
190
190
|
});
|
|
191
191
|
}
|
|
192
192
|
else {
|
|
193
|
-
this[propertyName]
|
|
193
|
+
this[propertyName] = obj[propertyName];
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
196
|
else {
|
|
@@ -211,7 +211,7 @@ class MapperFactory {
|
|
|
211
211
|
empty() {
|
|
212
212
|
let check = true;
|
|
213
213
|
Object.keys(this).forEach(propertyName => {
|
|
214
|
-
if (this[propertyName]
|
|
214
|
+
if (this[propertyName] !== undefined && this[propertyName] !== null) {
|
|
215
215
|
check = false;
|
|
216
216
|
}
|
|
217
217
|
});
|