mapper-factory 1.0.12 → 1.0.14

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  # Mapper-Factory
2
- Mapper-Factory is a 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.
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
4
  ## Installation
5
5
  To install the package, you can use npm by running the following command:
@@ -12,4 +12,128 @@ Or using yarn
12
12
 
13
13
  ## Usage
14
14
  ### Mapping simple objects
15
- - TODO
15
+
16
+ Your class must extends *MapperFactory*
17
+
18
+ ```
19
+ class User extends MapperFactory {
20
+ ...
21
+ }
22
+ ```
23
+
24
+ After that, you can use *@MapField* decorator over single property to specify the mapping:
25
+
26
+ ```
27
+ class User extends MapperFactory {
28
+
29
+ @MapField({
30
+ src: 'obj.obj[0][1]',
31
+ transformer: (arr) => arr.map(role => role + " TEST TRASFORMER"),
32
+ reverser: (arr) => arr.map(role => role.replace(" TEST TRASFORMER", "")),
33
+ })
34
+ roles?: string[];
35
+ }
36
+ ```
37
+
38
+ Inside *@MapField* you can use:
39
+ - ***src*** define a string of original field name
40
+ - ***transform*** function to transform used in *constructor* of the class
41
+ - ***reverse*** function to reverse used in *toMap* method of the class
42
+
43
+ In this example:
44
+
45
+ ```
46
+ class User extends MapperFactory {
47
+
48
+ id: string;
49
+ username: string;
50
+
51
+ @MapField({
52
+ src: 'firstName'
53
+ })
54
+ name: string;
55
+
56
+ @MapField({
57
+ src: 'lastName'
58
+ })
59
+ surname: string;
60
+
61
+ @MapField({
62
+ src: 'rolesToMap',
63
+ transformer: (arr) => arr.map(role => role + " TEST TRASFORMER"),
64
+ reverser: (arr) => arr.map(role => role.replace(" TEST TRASFORMER", "")),
65
+ })
66
+ roles?: string[];
67
+
68
+ @MapField({
69
+ transformer: (arr) => arr.map(user => new User(user))
70
+ })
71
+ employees?: User[];
72
+ }
73
+ ```
74
+
75
+ We can define a new User ***u***:
76
+
77
+ ```
78
+ let emp1: User = new User({ firstName: "Summer", lastName: "Smith" });
79
+ let emp2: User = new User({ firstName: "Morty", lastName: "Smith" });
80
+
81
+ let u = new User({ firstName: "Rick", lastName: "Sanchez", employees: [emp1, emp2], rolesToMap: ["CEO", "EMPLOYEE"] });
82
+ ```
83
+
84
+ In that way you can create a new JS Object User passing a JSON object. Automatically constructor use *src* and *transformer* field to obtain the correct object you want.
85
+
86
+ In this specific case we have trasformed a JSON object:
87
+
88
+ ![JSON_Object](images/json_object.png "JSON Object to convert")
89
+
90
+ In this JS Object:
91
+
92
+ ![JS_Object](images/js_object_mapped.png "JS Object mapped")
93
+
94
+ Just using the constructor of *User* class.
95
+
96
+ If you want to return to the original JSON Object you can just call ***toMap()*** method, in that way:
97
+
98
+ ```
99
+ u.toMap()
100
+ ```
101
+
102
+ Obtaining the original JSON Object.
103
+
104
+ You can also fill properties of an object from another by using ***objToModel()*** method, in that way:
105
+
106
+ ```
107
+ let uCopy = new User();
108
+ uCopy.objToModel(u);
109
+ ```
110
+
111
+ This method is meant to be used also when you have a JSON object but in the correct format, for example:
112
+
113
+ ```
114
+ let uCopy = new User();
115
+ uCopy.objToModel({ name: "Rick", surname: "Sanchez", employees: [emp1, emp2], roles: ["CEO", "EMPLOYEE"] })
116
+ ```
117
+
118
+ Another utility method is ***empty()*** method, you can check if your object is empty or not in that way:
119
+
120
+ ```
121
+ let user = new User();
122
+ user.empty(); //TRUE
123
+
124
+ user.name = "Rick";
125
+ user.empty(); //FALSE
126
+ ```
127
+
128
+ It is implemented also a GET/SET method by path. Using ***get(path: string)*** and ***set(path: string, value: any)*** you can access to the property you want and GET or SET the value:
129
+
130
+ ```
131
+ u.set("name", "Rick TEST-SET");
132
+ console.log(u.get("name"));
133
+ ```
134
+
135
+ With this mapper you can easily obtain a performant ***deep copy*** of your object doing:
136
+
137
+ ```
138
+ let userDeepCopy = new User(u.toMap());
139
+ ```
Binary file
Binary file
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "mapper-factory",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "mapper for typescript object",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
- "/dist"
8
+ "/dist",
9
+ "/images"
9
10
  ],
10
11
  "scripts": {
11
12
  "build": "npx tsc",