mapper-factory 3.0.0 → 3.0.1

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
@@ -213,179 +213,3 @@ let emp2: User = new User().from({ firstName: "Morty", lastName: "Smith" });
213
213
 
214
214
  let u = new User().from({ firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"] });
215
215
  ```
216
-
217
- ## Usage V1
218
-
219
- ### Mapping simple objects
220
-
221
- Your class must extends _MapperFactory_
222
-
223
- ```
224
- class User extends MapperFactory {
225
- ...
226
- }
227
- ```
228
-
229
- After that, you can use _@MapField_ decorator over single property to specify the mapping:
230
-
231
- ```
232
- class User extends MapperFactory {
233
-
234
- @MapField({
235
- src: 'firstName'
236
- })
237
- name: string;
238
-
239
- @MapField({
240
- src: 'obj.obj[0][1]',
241
- transformer: (arr) => arr.map(role => role + " TEST TRASFORMER"),
242
- reverser: (arr) => arr.map(role => role.replace(" TEST TRASFORMER", "")),
243
- })
244
- roles?: string[];
245
-
246
- @MapField({
247
- transformer: (user) => new User(user)
248
- })
249
- boss: User;
250
- }
251
- ```
252
-
253
- Inside _@MapField_ you can use:
254
-
255
- - **_src_**: define a string of original field name (also using a path like _"obj.obj[0][1]"_)
256
- - **_transform_**: function to transform data input in _constructor_ of the class
257
- - **_reverse_**: function to transform data input in _toMap_ method of the class
258
-
259
- In this example:
260
-
261
- ```
262
- class User extends MapperFactory {
263
-
264
- id: string;
265
- username: string;
266
-
267
- @MapField({
268
- src: 'firstName'
269
- })
270
- name: string;
271
-
272
- @MapField({
273
- src: 'lastName'
274
- })
275
- surname: string;
276
-
277
- @MapField({
278
- src: 'rolesToMap',
279
- transformer: (arr) => arr.map(role => role + " TEST TRASFORMER"),
280
- reverser: (arr) => arr.map(role => role.replace(" TEST TRASFORMER", "")),
281
- })
282
- roles?: string[];
283
-
284
- @MapField({
285
- transformer: (arr) => arr.map(user => new User(user))
286
- })
287
- employees?: User[];
288
-
289
- @MapField({
290
- transformer: (user) => new User(user)
291
- })
292
- boss: User;
293
- }
294
- ```
295
-
296
- You can define a new User **_u_**:
297
-
298
- ```
299
- let emp1: User = new User({ firstName: "Summer", lastName: "Smith" });
300
- let emp2: User = new User({ firstName: "Morty", lastName: "Smith" });
301
-
302
- let u = new User({ firstName: "Rick", lastName: "Sanchez", employees: [emp1, emp2], rolesToMap: ["CEO", "EMPLOYEE"] });
303
- ```
304
-
305
- 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.
306
-
307
- In the specific case considered we have trasformed a JSON object:
308
-
309
- ```
310
- {
311
- firstName: 'Rick',
312
- lastName: 'Sanchez',
313
- employees: [
314
- { firstName: 'Summer', lastName: 'Smith' },
315
- { firstName: 'Morty', lastName: 'Smith' }
316
- ],
317
- rolesToMap: [ 'CEO', 'EMPLOYEE' ],
318
- boss: { firstName: 'Jesus', lastName: 'Christ' }
319
- }
320
- ```
321
-
322
- In this JS Object:
323
-
324
- ```
325
- User {
326
- name: 'Rick',
327
- surname: 'Sanchez',
328
- employees: [
329
- User { name: 'Summer', surname: 'Smith' },
330
- User { name: 'Morty', surname: 'Smith' }
331
- ],
332
- roles: [ 'CEO TEST TRASFORMER', 'EMPLOYEE TEST TRASFORMER' ],
333
- boss: User { name: 'Jesus', surname: 'Christ' }
334
- }
335
- ```
336
-
337
- Just using the constructor of _User_ class.
338
-
339
- If you want to return to the original JSON Object you can just call **_toMap()_** method, in that way:
340
-
341
- ```
342
- u.toMap()
343
- ```
344
-
345
- Obtaining the original JSON Object.
346
-
347
- You can also fill properties of an object from another (typically with same class) by using **_objToModel()_** method, in that way:
348
-
349
- ```
350
- let uCopy = new User();
351
- uCopy.objToModel(u);
352
- ```
353
-
354
- This method is meant to be used also when you have a JSON object but in the correct format, for example:
355
-
356
- ```
357
- let uCopy = new User();
358
- uCopy.objToModel({ name: "Rick", surname: "Sanchez", employees: [emp1, emp2], roles: ["CEO", "EMPLOYEE"] })
359
- ```
360
-
361
- Another utility method is **_empty()_** method, you can check if your object is empty or not in that way:
362
-
363
- ```
364
- let user = new User();
365
- user.empty(); //TRUE
366
-
367
- user.name = "Rick";
368
- user.empty(); //FALSE
369
- ```
370
-
371
- 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:
372
-
373
- ```
374
- u.set("name", "Rick TEST-SET");
375
- console.log(u.get("name"));
376
- ```
377
-
378
- With this mapper you can easily obtain a performant **_deep copy_** of your object doing:
379
-
380
- ```
381
- let userDeepCopy = new User(u.toMap());
382
- ```
383
-
384
- ### Functions
385
-
386
- You can also use the following functions:
387
-
388
- - **_toMap(model: MapperFactory): Object_**: function to transform data input in _toMap_ method of the class
389
- - **_toModel\<T extends MapperFactory>(obj: Object)_**: T: function to transform data input (JSON) in the specified model _T_
390
- - **_objToModel\<T extends MapperFactory>(model: MapperFactory, obj: Object): T_**: T: function to get an instance of the object from a JSON representation without mapping it
391
- - **_copy\<T extends MapperFactory>(model: MapperFactory): T_**: T: function to get an instance of the object from a JSON representation without mapping it
package/dist/functions.js CHANGED
@@ -161,7 +161,7 @@ function from(object) {
161
161
  return objCopy;
162
162
  };
163
163
  const setProperty = (metaKey, value, object) => {
164
- const metaProp = metadataList[metaKey];
164
+ const metaProp = metadataList?.[metaKey];
165
165
  if (metaProp?.transformer) {
166
166
  this[metaKey] = metaProp.transformer(value, object);
167
167
  }
@@ -173,7 +173,7 @@ function from(object) {
173
173
  let metaKeys = metadataList && Object.keys(metadataList).filter(metadata => metadataList[metadata]?.src?.split('.')?.includes(propertyName));
174
174
  if (metaKeys?.length) {
175
175
  metaKeys.forEach(metaKey => {
176
- const metaProp = metadataList[metaKey];
176
+ const metaProp = metadataList?.[metaKey];
177
177
  if (metaProp) {
178
178
  const props = metaProp.src.split('.');
179
179
  const propsStereoid = props.map(prop => ({
@@ -189,7 +189,7 @@ function from(object) {
189
189
  else {
190
190
  let metaKey = metadataList && Object.keys(metadataList).find(metadata => metadataList[metadata]?.src == propertyName);
191
191
  if (metaKey) {
192
- const src = metadataList[metaKey].src || propertyName;
192
+ const src = metadataList?.[metaKey].src || propertyName;
193
193
  setProperty(metaKey, object[src], object);
194
194
  }
195
195
  else {
package/dist/index.d.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import { MapClass, MapInterface } from "./class.decorator";
2
2
  import { MapField } from "./field.decorator";
3
- import { objToModel, toMap, toModel } from "./mapper-functions";
4
3
  import { ClassType } from "./types";
5
- export { ClassType, MapClass, MapField, MapInterface, objToModel, toMap, toModel };
4
+ export { ClassType, MapClass, MapField, MapInterface };
6
5
  /**
7
6
  * npx tsc
8
7
  * npx ts-node src/example.ts
9
- * npm version patch
8
+ * npm version ( patch | minor | major )
10
9
  * npm publish
11
10
  */
package/dist/index.js CHANGED
@@ -1,17 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toModel = exports.toMap = exports.objToModel = exports.MapField = exports.MapClass = void 0;
3
+ exports.MapField = exports.MapClass = void 0;
4
4
  const class_decorator_1 = require("./class.decorator");
5
5
  Object.defineProperty(exports, "MapClass", { enumerable: true, get: function () { return class_decorator_1.MapClass; } });
6
6
  const field_decorator_1 = require("./field.decorator");
7
7
  Object.defineProperty(exports, "MapField", { enumerable: true, get: function () { return field_decorator_1.MapField; } });
8
- const mapper_functions_1 = require("./mapper-functions");
9
- Object.defineProperty(exports, "objToModel", { enumerable: true, get: function () { return mapper_functions_1.objToModel; } });
10
- Object.defineProperty(exports, "toMap", { enumerable: true, get: function () { return mapper_functions_1.toMap; } });
11
- Object.defineProperty(exports, "toModel", { enumerable: true, get: function () { return mapper_functions_1.toModel; } });
12
8
  /**
13
9
  * npx tsc
14
10
  * npx ts-node src/example.ts
15
- * npm version patch
11
+ * npm version ( patch | minor | major )
16
12
  * npm publish
17
13
  */
package/dist/test.js CHANGED
@@ -108,10 +108,16 @@ User = User_1 = __decorate([
108
108
  ], User);
109
109
  const emp1 = new User().from({ firstName: "Summer", lastName: "Smith" });
110
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" } };
111
+ const JSONObject = { username: 'god', firstName: "Rick", lastName: "Sanchez", employees: [emp1.toMap(), emp2.toMap()], rolesToMap: ["CEO", "EMPLOYEE"], boss: { firstName: "Nello", lastName: "Stanco" } };
112
112
  //TEST constructor
113
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";
114
+ const constructorTest = u.username == JSONObject.username &&
115
+ u.name == JSONObject.firstName &&
116
+ u.surname == JSONObject.lastName &&
117
+ u.employees?.map(emp => emp.name == emp1.name) &&
118
+ u.roles?.map(role => role == "CEO") &&
119
+ u.boss.name == "Nello" &&
120
+ u.boss.surname == "Stanco";
115
121
  console.log("TEST CONSTRUCTOR", constructorTest ? '✅' : '❌');
116
122
  //TEST toModel method with JS Object
117
123
  const u1 = new User().toModel(u);
@@ -197,4 +203,17 @@ console.log("TEST FLAG1", (testFlag1.flTest && testFlag1Map.flTest == '1') ? '
197
203
  const testFlag2 = new TestFlag().from({ flTest: '0' });
198
204
  const testFlag2Map = testFlag2.toMap();
199
205
  console.log("TEST FLAG2", (!testFlag2.flTest && testFlag2Map.flTest == '0') ? '✅' : '❌');
206
+ let TestWithoutMapField = class TestWithoutMapField {
207
+ id;
208
+ name;
209
+ };
210
+ TestWithoutMapField = __decorate([
211
+ (0, class_decorator_1.MapClass)()
212
+ ], TestWithoutMapField);
213
+ const JSONObject2 = {
214
+ id: '1',
215
+ name: 'Supplier 1',
216
+ };
217
+ const supplier = new TestWithoutMapField().from(JSONObject2);
218
+ console.log("TEST WITHOUT MAP FIELD", (supplier) ? '✅' : '❌');
200
219
  console.log("\n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mapper-factory",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "mapper for typescript object",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",