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/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
@@ -1,11 +1,11 @@
1
- export declare function MapClass(): (constructor: any) => void;
2
- export interface MapInterface<T> {
3
- from: (object?: any) => T;
4
- toMap: () => any;
5
- toModel: (object: any) => T;
6
- empty: () => boolean;
7
- filled: () => boolean;
8
- get: (path: string) => any;
9
- set: (path: string, value: any) => void;
10
- copy: () => T;
11
- }
1
+ export declare function MapClass(): (constructor: any) => void;
2
+ export interface MapInterface<T> {
3
+ from: (object?: any) => T;
4
+ toMap: () => any;
5
+ toModel: (object: any) => T;
6
+ empty: () => boolean;
7
+ filled: () => boolean;
8
+ get: (path: string) => any;
9
+ set: (path: string, value: any) => void;
10
+ copy: () => T;
11
+ }
@@ -1,17 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MapClass = void 0;
4
- const functions_1 = require("./functions");
5
- function MapClass() {
6
- return function (constructor) {
7
- constructor.prototype.from = functions_1.from;
8
- constructor.prototype.toMap = functions_1.toMap;
9
- constructor.prototype.toModel = functions_1.objToModel;
10
- constructor.prototype.empty = functions_1.empty;
11
- constructor.prototype.filled = functions_1.filled;
12
- constructor.prototype.get = functions_1.get;
13
- constructor.prototype.set = functions_1.set;
14
- constructor.prototype.copy = functions_1.copy;
15
- };
16
- }
17
- exports.MapClass = MapClass;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MapClass = MapClass;
4
+ const functions_1 = require("./functions");
5
+ function MapClass() {
6
+ return function (constructor) {
7
+ constructor.prototype.from = functions_1.from;
8
+ constructor.prototype.toMap = functions_1.toMap;
9
+ constructor.prototype.toModel = functions_1.objToModel;
10
+ constructor.prototype.empty = functions_1.empty;
11
+ constructor.prototype.filled = functions_1.filled;
12
+ constructor.prototype.get = functions_1.get;
13
+ constructor.prototype.set = functions_1.set;
14
+ constructor.prototype.copy = functions_1.copy;
15
+ };
16
+ }
@@ -1,22 +1,22 @@
1
- import 'reflect-metadata';
2
- import { ClassType } from './types';
3
- export declare const MAP_FIELD: unique symbol;
4
- export interface MapperMetadata<T = any> {
5
- src?: keyof T;
6
- initialize?: boolean;
7
- transformer?: {
8
- (input: any, ref: any): any;
9
- };
10
- reverser?: {
11
- (input: any): any;
12
- };
13
- }
14
- export declare function isClass(func: any): func is ClassType;
15
- export declare function getPrototype(target: Record<string, unknown> | ClassType): any;
16
- export declare const MapField: <T = any>({ transformer, reverser, src, initialize, }?: MapperMetadata<T>) => PropertyDecorator;
17
- export declare const getMapFieldMetadataList: (target: Record<string, unknown> | ClassType | any) => {
18
- [key: string]: MapperMetadata<any>;
19
- };
20
- export declare const hasMapFieldMetadataList: (target: Record<string, unknown> | ClassType) => boolean;
21
- export declare const getMapFieldMetadata: (target: Record<string, unknown> | ClassType, propertyName: string | symbol) => MapperMetadata | undefined;
22
- export declare const hasMapFieldMetadata: (target: Record<string, unknown> | ClassType, propertyName: string) => boolean;
1
+ import 'reflect-metadata';
2
+ import { ClassType } from './types';
3
+ export declare const MAP_FIELD: unique symbol;
4
+ export interface MapperMetadata<T = any> {
5
+ src?: keyof T;
6
+ initialize?: boolean;
7
+ transformer?: {
8
+ (input: any, ref: any): any;
9
+ };
10
+ reverser?: {
11
+ (input: any): any;
12
+ };
13
+ }
14
+ export declare function isClass(func: any): func is ClassType;
15
+ export declare function getPrototype(target: Record<string, unknown> | ClassType): any;
16
+ export declare const MapField: <T = any>({ transformer, reverser, src, initialize, }?: MapperMetadata<T>) => PropertyDecorator;
17
+ export declare const getMapFieldMetadataList: (target: Record<string, unknown> | ClassType | any) => {
18
+ [key: string]: MapperMetadata;
19
+ } | undefined;
20
+ export declare const hasMapFieldMetadataList: (target: Record<string, unknown> | ClassType) => boolean;
21
+ export declare const getMapFieldMetadata: (target: Record<string, unknown> | ClassType, propertyName: string | symbol) => MapperMetadata | undefined;
22
+ export declare const hasMapFieldMetadata: (target: Record<string, unknown> | ClassType, propertyName: string) => boolean;
@@ -1,54 +1,54 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasMapFieldMetadata = exports.getMapFieldMetadata = exports.hasMapFieldMetadataList = exports.getMapFieldMetadataList = exports.MapField = exports.getPrototype = exports.isClass = exports.MAP_FIELD = void 0;
4
- require("reflect-metadata");
5
- exports.MAP_FIELD = Symbol('MAP_FIELD');
6
- function isClass(func) {
7
- return (typeof func === 'function' &&
8
- /^class\s/.test(Function.prototype.toString.call(func)));
9
- }
10
- exports.isClass = isClass;
11
- function getPrototype(target) {
12
- return isClass(target) || !target?.prototype ? !target?.constructor ? target : target?.constructor : target?.prototype;
13
- }
14
- exports.getPrototype = getPrototype;
15
- const MapField = ({ transformer, reverser, src, initialize = false, } = {}) => {
16
- return (target, property) => {
17
- const classConstructor = target.constructor;
18
- const propertyName = property.toString();
19
- const metadata = Reflect.getMetadata(exports.MAP_FIELD, classConstructor) || {};
20
- // create new object reference to avoid this issue: https://github.com/rbuckton/reflect-metadata/issues/62
21
- const newMetadata = { ...metadata };
22
- const previousValues = metadata[propertyName];
23
- newMetadata[propertyName] = {
24
- ...previousValues,
25
- src,
26
- initialize,
27
- transformer,
28
- reverser,
29
- };
30
- Reflect.defineMetadata(exports.MAP_FIELD, newMetadata, classConstructor);
31
- };
32
- };
33
- exports.MapField = MapField;
34
- const getMapFieldMetadataList = (target) => {
35
- return Reflect.getMetadata(exports.MAP_FIELD, getPrototype(target));
36
- };
37
- exports.getMapFieldMetadataList = getMapFieldMetadataList;
38
- const hasMapFieldMetadataList = (target) => {
39
- return Reflect.hasMetadata(exports.MAP_FIELD, getPrototype(target));
40
- };
41
- exports.hasMapFieldMetadataList = hasMapFieldMetadataList;
42
- const getMapFieldMetadata = (target, propertyName) => {
43
- const metadata = (0, exports.getMapFieldMetadataList)(target);
44
- const name = propertyName.toString();
45
- if (!metadata || !metadata[name])
46
- return undefined;
47
- return metadata[name];
48
- };
49
- exports.getMapFieldMetadata = getMapFieldMetadata;
50
- const hasMapFieldMetadata = (target, propertyName) => {
51
- const metadata = Reflect.getMetadata(exports.MAP_FIELD, getPrototype(target));
52
- return metadata && !!metadata[propertyName];
53
- };
54
- exports.hasMapFieldMetadata = hasMapFieldMetadata;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasMapFieldMetadata = exports.getMapFieldMetadata = exports.hasMapFieldMetadataList = exports.getMapFieldMetadataList = exports.MapField = exports.MAP_FIELD = void 0;
4
+ exports.isClass = isClass;
5
+ exports.getPrototype = getPrototype;
6
+ require("reflect-metadata");
7
+ exports.MAP_FIELD = Symbol('MAP_FIELD');
8
+ function isClass(func) {
9
+ return (typeof func === 'function' &&
10
+ /^class\s/.test(Function.prototype.toString.call(func)));
11
+ }
12
+ function getPrototype(target) {
13
+ return isClass(target) || !target?.prototype ? !target?.constructor ? target : target?.constructor : target?.prototype;
14
+ }
15
+ const MapField = ({ transformer, reverser, src, initialize = false, } = {}) => {
16
+ return (target, property) => {
17
+ const classConstructor = target.constructor;
18
+ const propertyName = property.toString();
19
+ const metadata = Reflect.getMetadata(exports.MAP_FIELD, classConstructor) || {};
20
+ // create new object reference to avoid this issue: https://github.com/rbuckton/reflect-metadata/issues/62
21
+ const newMetadata = { ...metadata };
22
+ const previousValues = metadata[propertyName];
23
+ newMetadata[propertyName] = {
24
+ ...previousValues,
25
+ src,
26
+ initialize,
27
+ transformer,
28
+ reverser,
29
+ };
30
+ Reflect.defineMetadata(exports.MAP_FIELD, newMetadata, classConstructor);
31
+ };
32
+ };
33
+ exports.MapField = MapField;
34
+ const getMapFieldMetadataList = (target) => {
35
+ return Reflect.getMetadata(exports.MAP_FIELD, getPrototype(target));
36
+ };
37
+ exports.getMapFieldMetadataList = getMapFieldMetadataList;
38
+ const hasMapFieldMetadataList = (target) => {
39
+ return Reflect.hasMetadata(exports.MAP_FIELD, getPrototype(target));
40
+ };
41
+ exports.hasMapFieldMetadataList = hasMapFieldMetadataList;
42
+ const getMapFieldMetadata = (target, propertyName) => {
43
+ const metadata = (0, exports.getMapFieldMetadataList)(target);
44
+ const name = propertyName.toString();
45
+ if (!metadata || !metadata[name])
46
+ return undefined;
47
+ return metadata[name];
48
+ };
49
+ exports.getMapFieldMetadata = getMapFieldMetadata;
50
+ const hasMapFieldMetadata = (target, propertyName) => {
51
+ const metadata = Reflect.getMetadata(exports.MAP_FIELD, getPrototype(target));
52
+ return metadata && !!metadata[propertyName];
53
+ };
54
+ exports.hasMapFieldMetadata = hasMapFieldMetadata;
@@ -1,49 +1,49 @@
1
- /**
2
- * Convert the instance of this class to JSON Object.
3
- *
4
- * @returns JSON object mapped considering metadata "src" and "reverser"
5
- */
6
- export declare function toMap(): any;
7
- /**
8
- * Convert a JSON Object to an instance of this class.
9
- *
10
- * @param obj JSON Object
11
- * @returns Instance of this class
12
- */
13
- export declare function objToModel(obj: Object): any;
14
- /**
15
- * Check if this instance is empty.
16
- *
17
- * @returns true or false
18
- */
19
- export declare function empty(): boolean;
20
- /**
21
- * Check if this instance is filled.
22
- *
23
- * @returns true or false
24
- */
25
- export declare function filled(): boolean;
26
- /**
27
- * GET property value from a string path.
28
- *
29
- * @param path String path
30
- * @returns Value of the property
31
- */
32
- export declare function get<T>(path: string): T;
33
- /**
34
- * SET property value from a string path.
35
- *
36
- * @param path String path
37
- * @param value Value of the property
38
- */
39
- export declare function set(path: string, value: any): void;
40
- /**
41
- * Deep copy of the object caller
42
- */
43
- export declare function copy<T>(): T;
44
- /**
45
- * Constructor of the mapper.
46
- *
47
- * @param object object to be mapped considering metadata "src", "transformer" and "reverser"
48
- */
49
- export declare function from(object: any): any;
1
+ /**
2
+ * Convert the instance of this class to JSON Object.
3
+ *
4
+ * @returns JSON object mapped considering metadata "src" and "reverser"
5
+ */
6
+ export declare function toMap(): any;
7
+ /**
8
+ * Convert a JSON Object to an instance of this class.
9
+ *
10
+ * @param obj JSON Object
11
+ * @returns Instance of this class
12
+ */
13
+ export declare function objToModel(obj: Object): any;
14
+ /**
15
+ * Check if this instance is empty.
16
+ *
17
+ * @returns true or false
18
+ */
19
+ export declare function empty(): boolean;
20
+ /**
21
+ * Check if this instance is filled.
22
+ *
23
+ * @returns true or false
24
+ */
25
+ export declare function filled(): boolean;
26
+ /**
27
+ * GET property value from a string path.
28
+ *
29
+ * @param path String path
30
+ * @returns Value of the property
31
+ */
32
+ export declare function get<T>(path: string): T;
33
+ /**
34
+ * SET property value from a string path.
35
+ *
36
+ * @param path String path
37
+ * @param value Value of the property
38
+ */
39
+ export declare function set(path: string, value: any): void;
40
+ /**
41
+ * Deep copy of the object caller
42
+ */
43
+ export declare function copy<T>(): T;
44
+ /**
45
+ * Constructor of the mapper.
46
+ *
47
+ * @param object object to be mapped considering metadata "src", "transformer" and "reverser"
48
+ */
49
+ export declare function from(object: any): any;