1
1
# @cookbook/mapper-js
2
+
2
3
> Fast, reliable and intuitive object mapping.
3
4
4
5
[ ![ NPM Version] [ npm-image ]] [ npm-url ]
@@ -41,7 +42,7 @@ const source = {
41
42
person: {
42
43
name: {
43
44
firstName: ' John' ,
44
- lastName: ' Doe'
45
+ lastName: ' Doe' ,
45
46
},
46
47
age: 32 ,
47
48
drinks: [' beer' , ' whiskey' ],
@@ -51,7 +52,7 @@ const source = {
51
52
city: ' Cupertino' ,
52
53
state: ' CA' ,
53
54
postalCode: 95014 ,
54
- country: ' United States'
55
+ country: ' United States' ,
55
56
},
56
57
{
57
58
street: ' 1600 Amphitheatre' ,
@@ -60,12 +61,11 @@ const source = {
60
61
postalCode: 94043 ,
61
62
country: ' United States' ,
62
63
},
63
- ]
64
- }
65
- }
64
+ ],
65
+ },
66
+ };
66
67
```
67
68
68
-
69
69
### 2) Create your mapping using dot notation
70
70
71
71
At this step, we need to create our ` mapping ` against our data ` source ` .
@@ -81,14 +81,14 @@ For that, `map()` accepts `single dot notation` path or
81
81
` an array of dot notation paths ` . E.g.: ` map('person.name.firstName') ` , ` map([person.name.firstName, person.name.lastName]); ` '
82
82
83
83
Those values can be _ transformed_ by using the ` .transform() ` method, which expects a ` function ` as argument and provides
84
- the selected values as array in the ` parameter ` .
84
+ the selected values as array in the ` parameter ` .
85
85
86
86
> For more information about the usage, check the [ API Documentation] ( #api-documentation ) .
87
87
88
-
89
88
Now let's create our ` mapping ` !
90
89
91
90
``` js
91
+ // mapping.ts
92
92
import mapper from ' @cookbook/mapper-js' ;
93
93
94
94
...
@@ -106,15 +106,14 @@ const mapping = mapper((map) => ({
106
106
}));
107
107
```
108
108
109
-
110
109
### 3) Create your mapped object
111
110
112
111
``` js
113
- import mapper from ' @cookbook/mapper-js ' ;
112
+ import mapping from ' ./mapping ' ;
114
113
...
115
114
116
115
const result = mapping (source);
117
- /* outputs
116
+ /* outputs
118
117
{
119
118
person: {
120
119
name: 'John Doe',
@@ -151,11 +150,10 @@ const result = mapping(source);
151
150
** Return:** ` <T>(source: object | object[], options?: Options) => T extends [] ? T[] : T ` ,
152
151
** Signature:** ` (mapping: Mapping) => <T>(source: object | object[], options?: Options) => T extends [] ? T[] : T `
153
152
154
-
155
153
** Description:**
156
154
157
- ` mapper() ` is the main method and responsible for mapping the _ values_ from your _ data source_ against the _ mapping instructions_ .
158
- It accepts ` dot notation ` path(s) as ` key(s) ` .
155
+ ` mapper() ` is the main method and responsible for mapping the _ values_ from your _ data source_ against the _ mapping instructions_ .
156
+ It accepts ` dot notation ` path(s) as ` key(s) ` .
159
157
160
158
Example:
161
159
@@ -173,9 +171,9 @@ const mapping = mapper((map) => ({
173
171
}));
174
172
```
175
173
176
- As a result from the above implementation, ` mapper() ` return a new ` function ` to map and compile your _ source data_ against your _ mapping_ .
174
+ As a result from the above implementation, ` mapper() ` return a new ` function ` to map and compile your _ source data_ against your _ mapping_ .
177
175
178
- It accepts an extra (_ optional_ ) argument defining the [ _ global mapping options_ ] ( #mapper-options ) .
176
+ It accepts an extra (_ optional_ ) argument defining the [ _ global mapping options_ ] ( #mapper-options ) .
179
177
180
178
Example:
181
179
@@ -184,7 +182,7 @@ Example:
184
182
185
183
mapping (source , options );
186
184
187
- /* outputs
185
+ /* outputs
188
186
{
189
187
employee: {
190
188
name: 'John',
@@ -203,77 +201,72 @@ mapping(source, options);
203
201
}
204
202
*/
205
203
```
206
- ___
207
204
205
+ ---
208
206
209
207
## map
210
208
211
209
** Type:** ` function `
212
- ** Parameter:** ` keys: string | string[], options?: Options `
210
+ ** Parameter:** ` keys: string | string[], options?: Options `
213
211
** Return:** ` MapMethods<T> ` ,
214
212
** Signature:** ` <T = unknown>(keys: string | string[], options?: Options) => MapMethods<T> `
215
213
216
- ** Description:**
214
+ ** Description:**
217
215
218
- ` root ` method retrieves values from your _ source data_ using ` dot notation ` path, it accepts a string or array of string.
216
+ ` root ` method retrieves values from your _ source data_ using ` dot notation ` path, it accepts a string or array of string.
219
217
220
- It accepts an extra (_ optional_ ) argument to define the [ _ mapping options for current entry_ ] ( #mapper-options ) , _ overriding_ the _ global mapping options_ .
218
+ It accepts an extra (_ optional_ ) argument to define the [ _ mapping options for current entry_ ] ( #mapper-options ) , _ overriding_ the _ global mapping options_ .
221
219
222
220
Example:
221
+
223
222
``` ts
224
223
map (' person.name.firstName' );
225
224
map ([' person.name.firstName' , ' person.name.lastName' ]);
226
225
map ([' person.name.firstName' , ' person.name.lastName' ], options );
227
226
```
228
227
229
-
230
228
#### ` transform `
231
229
232
230
** Type:** ` function `
233
231
** Parameter:** ` ...unknown[] `
234
232
** Return:** ` unknown | unknown[] ` ,
235
233
** Signature:** ` (...args: unknown[]) => unknown | unknown[] `
236
234
237
- ** Description:**
235
+ ** Description:**
238
236
239
- ` .transform ` method provides you the ability to _ transform_ the retrieved value(s) from ` map() ` according to your needs, and for that, it expects a return value.
237
+ ` .transform ` method provides you the ability to _ transform_ the retrieved value(s) from ` map() ` according to your needs, and for that, it expects a return value.
240
238
241
- ` .transform ` provides you as _ parameter_ , the retrieved value(s) in the ** same order** as defined in the ` map() ` method, otherwise
239
+ ` .transform ` provides you as _ parameter_ , the retrieved value(s) in the ** same order** as defined in the ` map() ` method, otherwise
242
240
243
241
Example:
242
+
244
243
``` ts
245
244
// single value
246
- map (' person.name.firstName' )
247
- .transform ((firstName ) => firstName .toLoweCase ());
245
+ map (' person.name.firstName' ).transform ((firstName ) => firstName .toLoweCase ());
248
246
249
247
// multiple values
250
- map ([' person.name.firstName' , ' person.name.lastName' ])
251
- .transform ((firstName , lastName ) => ` ${firstName } ${lastName } ` );
248
+ map ([' person.name.firstName' , ' person.name.lastName' ]).transform ((firstName , lastName ) => ` ${firstName } ${lastName } ` );
252
249
```
253
250
254
-
255
251
#### ` value `
256
252
257
253
** Type:** ` readonly `
258
254
** Return:** ` T `
259
- ** Description:**
255
+ ** Description:**
260
256
261
- ` .value ` returns the value of your ` dot notation ` query. If transformed, returns the transformed value.
257
+ ` .value ` returns the value of your ` dot notation ` query. If transformed, returns the transformed value.
262
258
263
259
Example:
260
+
264
261
``` ts
265
262
// single value
266
- map (' person.name.firstName' )
267
- .transform ((firstName ) => firstName .toLoweCase ())
268
- .value ;
263
+ map (' person.name.firstName' ).transform ((firstName ) => firstName .toLoweCase ()).value ;
269
264
270
265
// multiple values
271
- map ([' person.name.firstName' , ' person.name.lastName' ])
272
- .transform ((firstName , lastName ) => ` ${firstName } ${lastName } ` )
273
- .value ;
266
+ map ([' person.name.firstName' , ' person.name.lastName' ]).transform ((firstName , lastName ) => ` ${firstName } ${lastName } ` )
267
+ .value ;
274
268
```
275
269
276
-
277
270
## Mapper Options
278
271
279
272
### defaults
@@ -292,9 +285,9 @@ map(['person.name.firstName', 'person.name.lastName'])
292
285
** Type:** ` boolean `
293
286
** default value:** ` false `
294
287
295
- ** Description:**
288
+ ** Description:**
296
289
297
- Removes ` null ` or ` undefined ` entries from the _ mapped_ object.
290
+ Removes ` null ` or ` undefined ` entries from the _ mapped_ object.
298
291
299
292
Example:
300
293
@@ -309,11 +302,11 @@ Example:
309
302
}
310
303
*/
311
304
const mapping = mapper ((map ) => ({
312
- ' name' : map (' person.name' ).value ,
313
- ' age' : map (' person.age' ).value ,
314
- // source doesn't have property 'address',
315
- // therefore will return "undefined"
316
- ' address' : map (' person.address' ).value ,
305
+ name: map (' person.name' ).value ,
306
+ age: map (' person.age' ).value ,
307
+ // source doesn't have property 'address',
308
+ // therefore will return "undefined"
309
+ address: map (' person.address' ).value ,
317
310
}));
318
311
319
312
mapping (source , { omitNullUndefined: true });
@@ -323,7 +316,6 @@ mapping(source, { omitNullUndefined: true });
323
316
age: 32,
324
317
}
325
318
*/
326
-
327
319
```
328
320
329
321
** ` omitStrategy ` **
@@ -333,11 +325,12 @@ mapping(source, { omitNullUndefined: true });
333
325
** Return:** ` boolean `
334
326
** Signature:** ` (value: unknown | unknown[]) => boolean `
335
327
336
- ** Description:**
328
+ ** Description:**
337
329
338
- Defines a _ custom strategy_ to omit (_ suppress_ ) entries from the _ mapped object_ .
330
+ Defines a _ custom strategy_ to omit (_ suppress_ ) entries from the _ mapped object_ .
339
331
340
332
Example:
333
+
341
334
``` tsx
342
335
/* source object
343
336
{
@@ -359,9 +352,9 @@ Example:
359
352
const customOmitStrategy = (address : Record <string , string >): boolean => address && address .city === ' Cupertino' ;
360
353
361
354
const mapping = mapper ((map ) => ({
362
- ' name' : map (' person.name' ).value ,
363
- ' age' : map (' person.age' ).value ,
364
- ' address' : map (' person.address' ).value ,
355
+ name: map (' person.name' ).value ,
356
+ age: map (' person.age' ).value ,
357
+ address: map (' person.address' ).value ,
365
358
}));
366
359
367
360
mapping (source , { omitStrategy: customOmitStrategy });
@@ -371,10 +364,10 @@ mapping(source, { omitStrategy: customOmitStrategy });
371
364
age: 32,
372
365
}
373
366
*/
374
-
375
367
```
376
368
377
369
<!-- Markdown link & img dfn's -->
370
+
378
371
[ npm-image ] : https://img.shields.io/npm/v/@cookbook/mapper-js.svg?style=flat-square
379
372
[ npm-url ] : https://npmjs.org/package/@cookbook/mapper-js
380
373
[ npm-downloads ] : https://img.shields.io/npm/dm/@cookbook/mapper-js.svg?style=flat-square
0 commit comments