map-transform 0.4.0-alpha.1 → 0.4.0-alpha.12
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 +167 -90
- package/dist/functions/compare.d.ts +2 -1
- package/dist/functions/compare.js +10 -4
- package/dist/functions/compare.js.map +1 -1
- package/dist/functions/explode.d.ts +2 -1
- package/dist/functions/explode.js +13 -5
- package/dist/functions/explode.js.map +1 -1
- package/dist/functions/get.js +1 -1
- package/dist/functions/get.js.map +1 -1
- package/dist/functions/index.d.ts +8 -2
- package/dist/functions/index.js +10 -4
- package/dist/functions/index.js.map +1 -1
- package/dist/functions/joinSplit.d.ts +8 -0
- package/dist/functions/joinSplit.js +32 -0
- package/dist/functions/joinSplit.js.map +1 -0
- package/dist/functions/logical.d.ts +7 -0
- package/dist/functions/logical.js +24 -0
- package/dist/functions/logical.js.map +1 -0
- package/dist/functions/sort.d.ts +7 -0
- package/dist/functions/sort.js +33 -0
- package/dist/functions/sort.js.map +1 -0
- package/dist/functions/template.js +8 -6
- package/dist/functions/template.js.map +1 -1
- package/dist/functions/validate.js +3 -3
- package/dist/functions/validate.js.map +1 -1
- package/dist/functions/value.js +4 -3
- package/dist/functions/value.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -6
- package/dist/index.js.map +1 -1
- package/dist/operations/alt.js +3 -3
- package/dist/operations/alt.js.map +1 -1
- package/dist/operations/apply.js +2 -2
- package/dist/operations/apply.js.map +1 -1
- package/dist/operations/concat.js +4 -4
- package/dist/operations/concat.js.map +1 -1
- package/dist/operations/directionals.js +3 -3
- package/dist/operations/directionals.js.map +1 -1
- package/dist/operations/filter.js +10 -8
- package/dist/operations/filter.js.map +1 -1
- package/dist/operations/fixed.js +1 -1
- package/dist/operations/fixed.js.map +1 -1
- package/dist/operations/getSet.d.ts +2 -1
- package/dist/operations/getSet.js +17 -11
- package/dist/operations/getSet.js.map +1 -1
- package/dist/operations/ifelse.js +7 -5
- package/dist/operations/ifelse.js.map +1 -1
- package/dist/operations/iterate.js +7 -9
- package/dist/operations/iterate.js.map +1 -1
- package/dist/operations/lookup.js +2 -2
- package/dist/operations/lookup.js.map +1 -1
- package/dist/operations/merge.d.ts +1 -0
- package/dist/operations/merge.js +29 -6
- package/dist/operations/merge.js.map +1 -1
- package/dist/operations/modify.d.ts +1 -1
- package/dist/operations/modify.js +6 -6
- package/dist/operations/modify.js.map +1 -1
- package/dist/operations/mutate.js +87 -32
- package/dist/operations/mutate.js.map +1 -1
- package/dist/operations/pipe.js +15 -5
- package/dist/operations/pipe.js.map +1 -1
- package/dist/operations/plug.js +1 -1
- package/dist/operations/plug.js.map +1 -1
- package/dist/operations/root.js +3 -2
- package/dist/operations/root.js.map +1 -1
- package/dist/operations/transform.js +3 -3
- package/dist/operations/transform.js.map +1 -1
- package/dist/operations/value.js +1 -1
- package/dist/operations/value.js.map +1 -1
- package/dist/types.d.ts +6 -2
- package/dist/utils/array.d.ts +2 -0
- package/dist/utils/array.js +12 -0
- package/dist/utils/array.js.map +1 -0
- package/dist/utils/definitionHelpers.d.ts +2 -2
- package/dist/utils/definitionHelpers.js +54 -31
- package/dist/utils/definitionHelpers.js.map +1 -1
- package/dist/utils/functional.d.ts +1 -0
- package/dist/utils/functional.js +7 -3
- package/dist/utils/functional.js.map +1 -1
- package/dist/utils/is.d.ts +2 -0
- package/dist/utils/is.js +7 -2
- package/dist/utils/is.js.map +1 -1
- package/dist/utils/pathGetter.d.ts +4 -2
- package/dist/utils/pathGetter.js +17 -21
- package/dist/utils/pathGetter.js.map +1 -1
- package/dist/utils/pathSetter.d.ts +4 -3
- package/dist/utils/pathSetter.js +34 -39
- package/dist/utils/pathSetter.js.map +1 -1
- package/dist/utils/stateHelpers.d.ts +2 -1
- package/dist/utils/stateHelpers.js +18 -7
- package/dist/utils/stateHelpers.js.map +1 -1
- package/package.json +15 -30
package/README.md
CHANGED
|
@@ -85,8 +85,8 @@ const def2 = [
|
|
|
85
85
|
{
|
|
86
86
|
title: 'name',
|
|
87
87
|
author: 'meta.author',
|
|
88
|
-
date: 'meta.date'
|
|
89
|
-
}
|
|
88
|
+
date: 'meta.date',
|
|
89
|
+
},
|
|
90
90
|
]
|
|
91
91
|
|
|
92
92
|
const target2 = mapTransform(def2)(source)
|
|
@@ -105,15 +105,15 @@ const { mapTransform, transform } = require('map-transform')
|
|
|
105
105
|
// ....
|
|
106
106
|
|
|
107
107
|
// Write a transform function, that accepts a value and returns a value
|
|
108
|
-
const msToDate = ms => new Date(ms).toISOString()
|
|
108
|
+
const msToDate = (ms) => new Date(ms).toISOString()
|
|
109
109
|
|
|
110
110
|
const def3 = [
|
|
111
111
|
'data[0].content',
|
|
112
112
|
{
|
|
113
113
|
title: 'name',
|
|
114
114
|
author: 'meta.author',
|
|
115
|
-
date: ['meta.date', transform(msToDate)]
|
|
116
|
-
}
|
|
115
|
+
date: ['meta.date', transform(msToDate)],
|
|
116
|
+
},
|
|
117
117
|
]
|
|
118
118
|
|
|
119
119
|
const target3 = mapTransform(def3)(source)
|
|
@@ -167,15 +167,15 @@ taste.
|
|
|
167
167
|
|
|
168
168
|
```javascript
|
|
169
169
|
const def1 = {
|
|
170
|
-
'data.entry.title': 'heading'
|
|
170
|
+
'data.entry.title': 'heading',
|
|
171
171
|
}
|
|
172
172
|
|
|
173
173
|
const def2 = {
|
|
174
174
|
data: {
|
|
175
175
|
entry: {
|
|
176
|
-
title: 'heading'
|
|
177
|
-
}
|
|
178
|
-
}
|
|
176
|
+
title: 'heading',
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
// def1 and def2 are identical, and will result in an object like this:
|
|
@@ -188,12 +188,15 @@ const def2 = {
|
|
|
188
188
|
// }
|
|
189
189
|
```
|
|
190
190
|
|
|
191
|
-
When you transform an array of data with a mapping object,
|
|
192
|
-
data array
|
|
191
|
+
When you transform an array of data with a mapping object, you'll have to set
|
|
192
|
+
`$iterate: true` to have each item in the data array be transformed with the
|
|
193
|
+
mapping object. If you don't the entire array will be passed to the mapping
|
|
194
|
+
object.
|
|
193
195
|
|
|
194
196
|
```javascript
|
|
195
197
|
const def3 = {
|
|
196
|
-
|
|
198
|
+
$iterate: true,
|
|
199
|
+
title: 'heading',
|
|
197
200
|
}
|
|
198
201
|
|
|
199
202
|
// -->
|
|
@@ -203,12 +206,8 @@ const def3 = {
|
|
|
203
206
|
// ]
|
|
204
207
|
```
|
|
205
208
|
|
|
206
|
-
|
|
207
|
-
the
|
|
208
|
-
transform pipeline.
|
|
209
|
-
|
|
210
|
-
**Note:** When a mapping object is part of a transform pipeline, the default is
|
|
211
|
-
to not iterate. See [**transform pipeline**](#transform-pipeline) for more.
|
|
209
|
+
**Note:** Iterating used to be the default behavior, but it has been changed due
|
|
210
|
+
to the contradiction with how the mapping object behaves everywhere else.
|
|
212
211
|
|
|
213
212
|
A key will set whatever is returned by the pipeline (see
|
|
214
213
|
[next section](#values-on-the-transform-object)), whether it is a string, a
|
|
@@ -252,7 +251,7 @@ is at this path on the source object.
|
|
|
252
251
|
|
|
253
252
|
```javascript
|
|
254
253
|
const def4 = {
|
|
255
|
-
title: 'data.item.heading'
|
|
254
|
+
title: 'data.item.heading',
|
|
256
255
|
}
|
|
257
256
|
|
|
258
257
|
const source1 = {
|
|
@@ -260,9 +259,9 @@ const source1 = {
|
|
|
260
259
|
item: {
|
|
261
260
|
id: 'item1',
|
|
262
261
|
heading: 'The actual heading',
|
|
263
|
-
intro: 'The actual intro'
|
|
264
|
-
}
|
|
265
|
-
}
|
|
262
|
+
intro: 'The actual intro',
|
|
263
|
+
},
|
|
264
|
+
},
|
|
266
265
|
}
|
|
267
266
|
|
|
268
267
|
// `mapTransform(def4)(source1)` will transform to:
|
|
@@ -286,7 +285,7 @@ an array by indicating the array index in the brackets.
|
|
|
286
285
|
|
|
287
286
|
```javascript
|
|
288
287
|
const def5 = {
|
|
289
|
-
title: 'data.items[0].heading'
|
|
288
|
+
title: 'data.items[0].heading',
|
|
290
289
|
}
|
|
291
290
|
|
|
292
291
|
// def5 will pull the heading from the first item in the `items` array, and will
|
|
@@ -326,6 +325,17 @@ When running a forward transformation, transform objects marked with
|
|
|
326
325
|
`$direction: 'rev'` will be skipped. The same goes for `$direction: 'fwd'` in
|
|
327
326
|
reverse.
|
|
328
327
|
|
|
328
|
+
You may specify aliases for `fwd` and `rev` in the `mapTransform` options:
|
|
329
|
+
|
|
330
|
+
```javascript
|
|
331
|
+
const options = { fwdAlias: 'from', revAlias: 'to' }
|
|
332
|
+
const mapper = mapTransform(def, options)
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
In this case, `from` and `to` may be used to specify forward and reverse
|
|
336
|
+
direction respectively. `fwd` and `rev` will still work in addition to the
|
|
337
|
+
aliases.
|
|
338
|
+
|
|
329
339
|
### Transform pipeline
|
|
330
340
|
|
|
331
341
|
The idea of the transform pipeline, is that you describe a set of
|
|
@@ -361,9 +371,9 @@ const def6 = [
|
|
|
361
371
|
$iterate: true,
|
|
362
372
|
id: 'articleNo',
|
|
363
373
|
title: ['headline', transform(maxLength(20))],
|
|
364
|
-
sections: 'meta.sections[].id'
|
|
374
|
+
sections: 'meta.sections[].id',
|
|
365
375
|
},
|
|
366
|
-
filter(onlyItemsWithSection)
|
|
376
|
+
filter(onlyItemsWithSection),
|
|
367
377
|
]
|
|
368
378
|
```
|
|
369
379
|
|
|
@@ -404,16 +414,16 @@ on an object. You would probably just not get the end result you expected.
|
|
|
404
414
|
```javascript
|
|
405
415
|
import { mapTransform, transform } from 'map-transform'
|
|
406
416
|
|
|
407
|
-
const ensureInteger = data => Number.parseInt(data, 10) || 0
|
|
417
|
+
const ensureInteger = (data) => Number.parseInt(data, 10) || 0
|
|
408
418
|
const def7 = {
|
|
409
|
-
count: ['statistics.views', transform(ensureInteger)]
|
|
419
|
+
count: ['statistics.views', transform(ensureInteger)],
|
|
410
420
|
}
|
|
411
421
|
|
|
412
422
|
const data = {
|
|
413
423
|
statistics: {
|
|
414
|
-
view: '18'
|
|
424
|
+
view: '18',
|
|
415
425
|
// ...
|
|
416
|
-
}
|
|
426
|
+
},
|
|
417
427
|
}
|
|
418
428
|
|
|
419
429
|
mapTransform(def7)(data)
|
|
@@ -449,7 +459,7 @@ Example transformation pipeline with a `yearsSince` function:
|
|
|
449
459
|
|
|
450
460
|
```javascript
|
|
451
461
|
const def8 = {
|
|
452
|
-
age: ['birthyear', yearsSince(new Date())]
|
|
462
|
+
age: ['birthyear', yearsSince(new Date())],
|
|
453
463
|
}
|
|
454
464
|
```
|
|
455
465
|
|
|
@@ -462,17 +472,17 @@ You may also define a transform operation as an object:
|
|
|
462
472
|
```javascript
|
|
463
473
|
import { mapTransform } from 'map-transform'
|
|
464
474
|
|
|
465
|
-
const ensureInteger = operands => data => Number.parseInt(data, 10) || 0
|
|
475
|
+
const ensureInteger = (operands) => (data) => Number.parseInt(data, 10) || 0
|
|
466
476
|
const functions = { ensureInteger }
|
|
467
477
|
const def7asObject = {
|
|
468
|
-
count: ['statistics.views', { $transform: 'ensureInteger' }]
|
|
478
|
+
count: ['statistics.views', { $transform: 'ensureInteger' }],
|
|
469
479
|
}
|
|
470
480
|
|
|
471
481
|
const data = {
|
|
472
482
|
statistics: {
|
|
473
|
-
view: '18'
|
|
483
|
+
view: '18',
|
|
474
484
|
// ...
|
|
475
|
-
}
|
|
485
|
+
},
|
|
476
486
|
}
|
|
477
487
|
|
|
478
488
|
mapTransform(def7asObject, { functions })(data)
|
|
@@ -615,26 +625,26 @@ the pipeline definition in the first place.
|
|
|
615
625
|
```javascript
|
|
616
626
|
import { mapTransform, apply, transform } from 'map-transform'
|
|
617
627
|
|
|
618
|
-
const ensureInteger = data => Number.parseInt(data, 10) || 0
|
|
628
|
+
const ensureInteger = (data) => Number.parseInt(data, 10) || 0
|
|
619
629
|
const pipelines = {
|
|
620
630
|
castEntry: {
|
|
621
631
|
title: ['title', transform(String)],
|
|
622
|
-
count: ['count', transform(ensureInteger)]
|
|
623
|
-
}
|
|
632
|
+
count: ['count', transform(ensureInteger)],
|
|
633
|
+
},
|
|
624
634
|
}
|
|
625
635
|
const def25 = [
|
|
626
636
|
{
|
|
627
637
|
title: 'heading',
|
|
628
|
-
count: 'statistics.views'
|
|
638
|
+
count: 'statistics.views',
|
|
629
639
|
},
|
|
630
|
-
apply('castEntry')
|
|
640
|
+
apply('castEntry'),
|
|
631
641
|
]
|
|
632
642
|
|
|
633
643
|
const data = {
|
|
634
644
|
heading: 'Entry 1',
|
|
635
645
|
statistics: {
|
|
636
|
-
view: '18'
|
|
637
|
-
}
|
|
646
|
+
view: '18',
|
|
647
|
+
},
|
|
638
648
|
}
|
|
639
649
|
|
|
640
650
|
mapTransform(def7)(data)
|
|
@@ -650,9 +660,9 @@ You may also define the apply operation as an operation object:
|
|
|
650
660
|
const def25 = [
|
|
651
661
|
{
|
|
652
662
|
title: 'heading',
|
|
653
|
-
count: 'statistics.views'
|
|
663
|
+
count: 'statistics.views',
|
|
654
664
|
},
|
|
655
|
-
{ $apply: 'castEntry' }
|
|
665
|
+
{ $apply: 'castEntry' },
|
|
656
666
|
]
|
|
657
667
|
```
|
|
658
668
|
|
|
@@ -680,7 +690,7 @@ import { value, alt } from 'map-transform'
|
|
|
680
690
|
const def10 = {
|
|
681
691
|
id: 'data.customerNo',
|
|
682
692
|
type: value('customer'),
|
|
683
|
-
name: ['data.name', alt(value('Anonymous'))]
|
|
693
|
+
name: ['data.name', alt(value('Anonymous'))],
|
|
684
694
|
}
|
|
685
695
|
```
|
|
686
696
|
|
|
@@ -693,10 +703,12 @@ If you want to define the `value` operation as an operation object, use
|
|
|
693
703
|
const def10asObject = {
|
|
694
704
|
id: 'data.customerNo',
|
|
695
705
|
type: { $transform: 'value', value: 'customer' },
|
|
696
|
-
name: ['data.name', { $alt: 'value', value: 'Anonymous' }]
|
|
706
|
+
name: ['data.name', { $alt: 'value', value: 'Anonymous' }],
|
|
697
707
|
}
|
|
698
708
|
```
|
|
699
709
|
|
|
710
|
+
There is also a shortcut for `{ $transform: 'value', value: 'customer' }`: `{ $value: 'customer' }`, which might be useful when typing definitions by hand.
|
|
711
|
+
|
|
700
712
|
#### `fixed(data)` operation
|
|
701
713
|
|
|
702
714
|
The data given to the fixed operation, will be inserted in the pipeline in place
|
|
@@ -720,8 +732,8 @@ every item in the array, please use the `iterate` operation.
|
|
|
720
732
|
```javascript
|
|
721
733
|
import { alt, transform, functions } from 'map-transform'
|
|
722
734
|
const { value } = functions
|
|
723
|
-
const currentDate = data => new Date()
|
|
724
|
-
const formatDate = data => {
|
|
735
|
+
const currentDate = (data) => new Date()
|
|
736
|
+
const formatDate = (data) => {
|
|
725
737
|
/* implementation not included */
|
|
726
738
|
}
|
|
727
739
|
|
|
@@ -732,8 +744,8 @@ const def11 = {
|
|
|
732
744
|
'data.updateDate',
|
|
733
745
|
alt('data.createDate'),
|
|
734
746
|
alt(transform(currentDate)),
|
|
735
|
-
transform(formatDate)
|
|
736
|
-
]
|
|
747
|
+
transform(formatDate),
|
|
748
|
+
],
|
|
737
749
|
}
|
|
738
750
|
```
|
|
739
751
|
|
|
@@ -755,8 +767,8 @@ const def11asObject = {
|
|
|
755
767
|
'data.updateDate',
|
|
756
768
|
{ $alt: 'get', path: 'data.createDate' },
|
|
757
769
|
{ $alt: 'currentDate' },
|
|
758
|
-
{ $transform: 'formatDate' }
|
|
759
|
-
]
|
|
770
|
+
{ $transform: 'formatDate' },
|
|
771
|
+
],
|
|
760
772
|
}
|
|
761
773
|
```
|
|
762
774
|
|
|
@@ -792,7 +804,7 @@ Example:
|
|
|
792
804
|
import { modify } from 'map-transform'
|
|
793
805
|
|
|
794
806
|
const def34 = modify({
|
|
795
|
-
data: 'data.deeply.placed.items'
|
|
807
|
+
data: 'data.deeply.placed.items',
|
|
796
808
|
})
|
|
797
809
|
```
|
|
798
810
|
|
|
@@ -802,7 +814,7 @@ prop. Giving this an object like:
|
|
|
802
814
|
```javascript
|
|
803
815
|
const response = {
|
|
804
816
|
status: 'ok',
|
|
805
|
-
data: { deeply: { placed: { items: [{ id: 'ent1' }] } } }
|
|
817
|
+
data: { deeply: { placed: { items: [{ id: 'ent1' }] } } },
|
|
806
818
|
}
|
|
807
819
|
```
|
|
808
820
|
|
|
@@ -811,13 +823,34 @@ const response = {
|
|
|
811
823
|
```javascript
|
|
812
824
|
const response = {
|
|
813
825
|
status: 'ok',
|
|
814
|
-
data: [{ id: 'ent1' }]
|
|
826
|
+
data: [{ id: 'ent1' }],
|
|
815
827
|
}
|
|
816
828
|
```
|
|
817
829
|
|
|
818
830
|
Had we ran this without the `modify()` operation, the returned object would only
|
|
819
831
|
have the `data` prop.
|
|
820
832
|
|
|
833
|
+
This is equivalent to setting the `$modify` property to `true` on the object
|
|
834
|
+
mutation object:
|
|
835
|
+
|
|
836
|
+
```javascript
|
|
837
|
+
const def34b = {
|
|
838
|
+
$modify: true,
|
|
839
|
+
data: 'data.deeply.placed.items',
|
|
840
|
+
}
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
Note that `$modify` may also be set further down in the object structure. Also,
|
|
844
|
+
in some cases it may make more sense to specify a path in the source data to
|
|
845
|
+
merge with:
|
|
846
|
+
|
|
847
|
+
```javascript
|
|
848
|
+
const def34c = {
|
|
849
|
+
$modify: 'response',
|
|
850
|
+
data: 'response.data.deeply.placed.items',
|
|
851
|
+
}
|
|
852
|
+
```
|
|
853
|
+
|
|
821
854
|
#### `fwd(pipeline)` and `rev(pipeline)` operation
|
|
822
855
|
|
|
823
856
|
All operations in MapTransform will apply in both directions, although some of
|
|
@@ -830,11 +863,11 @@ pipeline when we're mapping in reverse.
|
|
|
830
863
|
|
|
831
864
|
```javascript
|
|
832
865
|
import { fwd, rev, transform } from 'map-transform'
|
|
833
|
-
const increment = data => data + 1
|
|
834
|
-
const decrement = data => data - 1
|
|
866
|
+
const increment = (data) => data + 1
|
|
867
|
+
const decrement = (data) => data - 1
|
|
835
868
|
|
|
836
869
|
const def12 = {
|
|
837
|
-
order: ['index', fwd(transform(increment)), rev(transform(decrement))]
|
|
870
|
+
order: ['index', fwd(transform(increment)), rev(transform(decrement))],
|
|
838
871
|
}
|
|
839
872
|
```
|
|
840
873
|
|
|
@@ -887,7 +920,7 @@ This example results in the exact same pipeline as the example above:
|
|
|
887
920
|
|
|
888
921
|
```javascript
|
|
889
922
|
const def14 = {
|
|
890
|
-
'content[]': 'data.items[].content'
|
|
923
|
+
'content[]': 'data.items[].content',
|
|
891
924
|
}
|
|
892
925
|
```
|
|
893
926
|
|
|
@@ -916,13 +949,13 @@ const def15 = [
|
|
|
916
949
|
{
|
|
917
950
|
id: 'id',
|
|
918
951
|
title: 'headline',
|
|
919
|
-
section: root('meta.section')
|
|
920
|
-
}
|
|
952
|
+
section: root('meta.section'),
|
|
953
|
+
},
|
|
921
954
|
]
|
|
922
955
|
|
|
923
956
|
const data = {
|
|
924
957
|
articles: [{ id: '1', headline: 'An article' } /* ... */],
|
|
925
|
-
meta: { section: 'news' }
|
|
958
|
+
meta: { section: 'news' },
|
|
926
959
|
}
|
|
927
960
|
|
|
928
961
|
mapTransform(def15)(data)
|
|
@@ -984,8 +1017,8 @@ const data = {
|
|
|
984
1017
|
users: [
|
|
985
1018
|
{ id: 'user1', name: 'User 1' },
|
|
986
1019
|
{ id: 'user2', name: 'User 2' },
|
|
987
|
-
{ id: 'user3', name: 'User 3' }
|
|
988
|
-
]
|
|
1020
|
+
{ id: 'user3', name: 'User 3' },
|
|
1021
|
+
],
|
|
989
1022
|
}
|
|
990
1023
|
const mapper = mapTransform(def18)
|
|
991
1024
|
const mappedData = mapper(data)
|
|
@@ -998,7 +1031,7 @@ mapper.rev(mappedData)
|
|
|
998
1031
|
// --> { content: { meta: { authors: ['user1', 'user3'] } } }
|
|
999
1032
|
```
|
|
1000
1033
|
|
|
1001
|
-
#### `compare({ path, operator, match, matchPath })` function
|
|
1034
|
+
#### `compare({ path, operator, match, matchPath, not })` function
|
|
1002
1035
|
|
|
1003
1036
|
This is a helper function intended for use with the `filter()` operation. You
|
|
1004
1037
|
pass a dot notation `path` and a `match` value (string, number, boolean) to
|
|
@@ -1011,10 +1044,13 @@ data.
|
|
|
1011
1044
|
|
|
1012
1045
|
The default is to compare the values resulting from `path` and `match` or
|
|
1013
1046
|
`matchPath` with equality, but other operations may be set on the `operator`
|
|
1014
|
-
property. Alternatives: `'='`, `'!='`, `'>'`, `'>='`, `'<'`, or `'<='
|
|
1047
|
+
property. Alternatives: `'='`, `'!='`, `'>'`, `'>='`, `'<'`, or `'<='`, or `in`
|
|
1048
|
+
(equality to at least one of the elements in an array).
|
|
1049
|
+
|
|
1050
|
+
If the `path` points to an array, the value is expected to be one of the values
|
|
1051
|
+
in the array.
|
|
1015
1052
|
|
|
1016
|
-
|
|
1017
|
-
the array.
|
|
1053
|
+
Set `not` to `true` to reverse the result of the comparison.
|
|
1018
1054
|
|
|
1019
1055
|
Here's an example where only data where role is set to 'admin' will be kept:
|
|
1020
1056
|
|
|
@@ -1025,9 +1061,9 @@ const { compare } = functions
|
|
|
1025
1061
|
const def19 = [
|
|
1026
1062
|
{
|
|
1027
1063
|
name: 'name',
|
|
1028
|
-
role: 'editor'
|
|
1064
|
+
role: 'editor',
|
|
1029
1065
|
},
|
|
1030
|
-
filter(compare({ path: 'role', operator: '=', match: 'admin' }))
|
|
1066
|
+
filter(compare({ path: 'role', operator: '=', match: 'admin' })),
|
|
1031
1067
|
]
|
|
1032
1068
|
```
|
|
1033
1069
|
|
|
@@ -1037,9 +1073,9 @@ You may also define this with a transform object:
|
|
|
1037
1073
|
const def19o = [
|
|
1038
1074
|
{
|
|
1039
1075
|
name: 'name',
|
|
1040
|
-
role: 'editor'
|
|
1076
|
+
role: 'editor',
|
|
1041
1077
|
},
|
|
1042
|
-
{ $filter: 'compare', path: 'role', operator: '=', match: 'admin' }
|
|
1078
|
+
{ $filter: 'compare', path: 'role', operator: '=', match: 'admin' },
|
|
1043
1079
|
]
|
|
1044
1080
|
```
|
|
1045
1081
|
|
|
@@ -1065,7 +1101,7 @@ import { mapTransform, transform, functions } from 'map-transform'
|
|
|
1065
1101
|
const { explode } = functions
|
|
1066
1102
|
|
|
1067
1103
|
const data = {
|
|
1068
|
-
currencies: { NOK: 1, USD: 0.125, EUR: 0.1 }
|
|
1104
|
+
currencies: { NOK: 1, USD: 0.125, EUR: 0.1 },
|
|
1069
1105
|
}
|
|
1070
1106
|
|
|
1071
1107
|
const def32 = ['currencies', transform(explode())]
|
|
@@ -1081,6 +1117,13 @@ Or as a transform object:
|
|
|
1081
1117
|
const def32o = ['currencies', { $transform: 'explode' }]
|
|
1082
1118
|
```
|
|
1083
1119
|
|
|
1120
|
+
#### `implode()` function
|
|
1121
|
+
|
|
1122
|
+
This is the exact opposite of the `explode` helper, imploding from a service and
|
|
1123
|
+
explode in reverse (to a service). See
|
|
1124
|
+
[the documentation for `explode()`](#explode-function), but remember that the
|
|
1125
|
+
directions will be reversed for `implode()`.
|
|
1126
|
+
|
|
1084
1127
|
#### `map(dictionary)` function
|
|
1085
1128
|
|
|
1086
1129
|
This helper function accepts a dictionary described as an array of tuples, where
|
|
@@ -1138,6 +1181,40 @@ const def29 = {
|
|
|
1138
1181
|
const mapper = mapTransform(def29, { dictionaries: { statusCodes: dictionary } })
|
|
1139
1182
|
```
|
|
1140
1183
|
|
|
1184
|
+
#### `sort({asc, path})` function
|
|
1185
|
+
|
|
1186
|
+
The `sort` helper function will sort the given array of items in ascending or
|
|
1187
|
+
descending (depending on whether `asc` is `true` or `false`). Ascending is the
|
|
1188
|
+
default. When a `path` is given, the sort is performed on the value at that path
|
|
1189
|
+
on each object in the array. With no `path`, the values in the array are sorted
|
|
1190
|
+
directly.
|
|
1191
|
+
|
|
1192
|
+
Example:
|
|
1193
|
+
|
|
1194
|
+
```javascript
|
|
1195
|
+
import { mapTransform, transform, functions } from 'map-transform'
|
|
1196
|
+
const { sort } = functions
|
|
1197
|
+
|
|
1198
|
+
const data = {
|
|
1199
|
+
items: [{ id: 'ent5' }, { id: 'ent1' }, { id: 'ent3' }],
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
const def35 = {
|
|
1203
|
+
data: ['items', transform(sort({ asc: true, path: 'id' }))],
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
const ret = mapTransform(def35)(data)
|
|
1207
|
+
// --> { caption: 'Bergen by night. By John F.' }
|
|
1208
|
+
```
|
|
1209
|
+
|
|
1210
|
+
The `sort` function is also available through a transform object:
|
|
1211
|
+
|
|
1212
|
+
```javascript
|
|
1213
|
+
const def35o = {
|
|
1214
|
+
data: ['items', { $transform: 'sort', asc: true, path: 'id' }],
|
|
1215
|
+
}
|
|
1216
|
+
```
|
|
1217
|
+
|
|
1141
1218
|
#### `template(template)` function
|
|
1142
1219
|
|
|
1143
1220
|
The `template` helper function takes a [handlebars] template and applies the
|
|
@@ -1154,11 +1231,11 @@ import { mapTransform, transform, functions } from 'map-transform'
|
|
|
1154
1231
|
const { template } = functions
|
|
1155
1232
|
|
|
1156
1233
|
const data = {
|
|
1157
|
-
content: { description: 'Bergen by night', artist: 'John F.' }
|
|
1234
|
+
content: { description: 'Bergen by night', artist: 'John F.' },
|
|
1158
1235
|
}
|
|
1159
1236
|
|
|
1160
1237
|
const def30 = {
|
|
1161
|
-
caption: ['content', transform(template('{{description}}. By {{artist}}'))]
|
|
1238
|
+
caption: ['content', transform(template('{{description}}. By {{artist}}'))],
|
|
1162
1239
|
}
|
|
1163
1240
|
|
|
1164
1241
|
const ret = mapTransform(def30)(data)
|
|
@@ -1171,8 +1248,8 @@ The `template` function is also available through a transform object:
|
|
|
1171
1248
|
const def30o = {
|
|
1172
1249
|
caption: [
|
|
1173
1250
|
'content',
|
|
1174
|
-
{ $transform: 'template', template: '{{description}}. By {{artist}}' }
|
|
1175
|
-
]
|
|
1251
|
+
{ $transform: 'template', template: '{{description}}. By {{artist}}' },
|
|
1252
|
+
],
|
|
1176
1253
|
}
|
|
1177
1254
|
```
|
|
1178
1255
|
|
|
@@ -1200,8 +1277,8 @@ const def20 = [
|
|
|
1200
1277
|
'items',
|
|
1201
1278
|
filter(validate('draft', { const: false })),
|
|
1202
1279
|
{
|
|
1203
|
-
title: 'heading'
|
|
1204
|
-
}
|
|
1280
|
+
title: 'heading',
|
|
1281
|
+
},
|
|
1205
1282
|
]
|
|
1206
1283
|
```
|
|
1207
1284
|
|
|
@@ -1220,9 +1297,9 @@ const { compare } = functions
|
|
|
1220
1297
|
const def21 = [
|
|
1221
1298
|
{
|
|
1222
1299
|
name: 'name',
|
|
1223
|
-
role: 'role'
|
|
1300
|
+
role: 'role',
|
|
1224
1301
|
},
|
|
1225
|
-
filter(not(compare('role', 'admin')))
|
|
1302
|
+
filter(not(compare('role', 'admin'))),
|
|
1226
1303
|
]
|
|
1227
1304
|
```
|
|
1228
1305
|
|
|
@@ -1249,14 +1326,14 @@ const def22 = [
|
|
|
1249
1326
|
'data.customers[]',
|
|
1250
1327
|
{
|
|
1251
1328
|
id: 'customerNo',
|
|
1252
|
-
name: ['fullname', alt(value('Anonymous'))]
|
|
1253
|
-
}
|
|
1329
|
+
name: ['fullname', alt(value('Anonymous'))],
|
|
1330
|
+
},
|
|
1254
1331
|
]
|
|
1255
1332
|
|
|
1256
1333
|
const dataInTargetState = [
|
|
1257
1334
|
{ id: 'cust1', name: 'Fred Johnsen' },
|
|
1258
1335
|
// { id: 'cust2', name: 'Lucy Knight' },
|
|
1259
|
-
{ id: 'cust3' }
|
|
1336
|
+
{ id: 'cust3' },
|
|
1260
1337
|
]
|
|
1261
1338
|
|
|
1262
1339
|
const dataInSourceState = mapTransform(def22).rev(dataInTargetState)
|
|
@@ -1282,15 +1359,15 @@ For example:
|
|
|
1282
1359
|
```javascript
|
|
1283
1360
|
import { mapTransform, transform } from 'map-transform'
|
|
1284
1361
|
|
|
1285
|
-
const username = name => name.replace(/\s+/, '.').toLowerCase()
|
|
1362
|
+
const username = (name) => name.replace(/\s+/, '.').toLowerCase()
|
|
1286
1363
|
|
|
1287
1364
|
const def23 = [
|
|
1288
1365
|
'data.customers[]',
|
|
1289
1366
|
{
|
|
1290
1367
|
id: 'customerNo',
|
|
1291
1368
|
name: 'fullname',
|
|
1292
|
-
'name/1': ['username', rev(transform(username))]
|
|
1293
|
-
}
|
|
1369
|
+
'name/1': ['username', rev(transform(username))],
|
|
1370
|
+
},
|
|
1294
1371
|
]
|
|
1295
1372
|
|
|
1296
1373
|
const dataInTargetState = [{ id: 'cust1', name: 'Fred Johnsen' }]
|
|
@@ -1329,11 +1406,11 @@ const def33flipped = {
|
|
|
1329
1406
|
id: 'key',
|
|
1330
1407
|
attributes: {
|
|
1331
1408
|
title: ['headline', transform(threeLetters)],
|
|
1332
|
-
age: ['unknown']
|
|
1409
|
+
age: ['unknown'],
|
|
1333
1410
|
},
|
|
1334
1411
|
relationships: {
|
|
1335
|
-
author: value('johnf')
|
|
1336
|
-
}
|
|
1412
|
+
author: value('johnf'),
|
|
1413
|
+
},
|
|
1337
1414
|
}
|
|
1338
1415
|
```
|
|
1339
1416
|
|
|
@@ -1380,7 +1457,7 @@ import { mapTransform, alt, value } from 'map-transform'
|
|
|
1380
1457
|
|
|
1381
1458
|
const def24 = {
|
|
1382
1459
|
id: 'customerNo',
|
|
1383
|
-
name: ['fullname', alt(value('Anonymous'))]
|
|
1460
|
+
name: ['fullname', alt(value('Anonymous'))],
|
|
1384
1461
|
}
|
|
1385
1462
|
|
|
1386
1463
|
const mapper = mapTransform(def24)
|
|
@@ -4,6 +4,7 @@ interface CompareOperands extends Operands {
|
|
|
4
4
|
operator?: string;
|
|
5
5
|
match?: unknown;
|
|
6
6
|
matchPath?: Path;
|
|
7
|
+
not?: boolean;
|
|
7
8
|
}
|
|
8
|
-
export default function compare({ path, operator, match, matchPath, }: CompareOperands): DataMapper;
|
|
9
|
+
export default function compare({ path, operator, match, matchPath, not, }: CompareOperands): DataMapper;
|
|
9
10
|
export {};
|
|
@@ -8,6 +8,9 @@ const compareArrayOrValue = (comparer) => (value, match) => Array.isArray(value)
|
|
|
8
8
|
const isNumeric = (value) => typeof value === 'number';
|
|
9
9
|
const compareArrayOrValueNumeric = (comparer) => compareArrayOrValue((value, match) => isNumeric(value) && isNumeric(match) && comparer(value, match));
|
|
10
10
|
const compareEqual = compareArrayOrValue((value, match) => value === match);
|
|
11
|
+
const compareIn = (value, match) => Array.isArray(match)
|
|
12
|
+
? match.some((item) => compareEqual(value, item))
|
|
13
|
+
: compareEqual(value, match);
|
|
11
14
|
function createComparer(operator) {
|
|
12
15
|
switch (operator) {
|
|
13
16
|
case '=':
|
|
@@ -22,18 +25,21 @@ function createComparer(operator) {
|
|
|
22
25
|
return compareArrayOrValueNumeric((value, match) => value < match);
|
|
23
26
|
case '<=':
|
|
24
27
|
return compareArrayOrValueNumeric((value, match) => value <= match);
|
|
28
|
+
case 'in':
|
|
29
|
+
return compareIn;
|
|
25
30
|
default:
|
|
26
31
|
return (_value, _match) => false;
|
|
27
32
|
}
|
|
28
33
|
}
|
|
29
|
-
function compare({ path = '.', operator = '=', match, matchPath, }) {
|
|
30
|
-
const getValue = pathGetter_1.default(path);
|
|
31
|
-
const getMatch = matchPath ? pathGetter_1.default(matchPath) : () => match;
|
|
34
|
+
function compare({ path = '.', operator = '=', match, matchPath, not = false, }) {
|
|
35
|
+
const getValue = (0, pathGetter_1.default)(path);
|
|
36
|
+
const getMatch = matchPath ? (0, pathGetter_1.default)(matchPath) : () => match;
|
|
32
37
|
const comparer = createComparer(operator);
|
|
33
38
|
return (data) => {
|
|
34
39
|
const value = getValue(data);
|
|
35
40
|
const match = getMatch(data);
|
|
36
|
-
|
|
41
|
+
const result = comparer(value, match);
|
|
42
|
+
return not ? !result : result;
|
|
37
43
|
};
|
|
38
44
|
}
|
|
39
45
|
exports.default = compare;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/functions/compare.ts"],"names":[],"mappings":";;AACA,oDAAwC;
|
|
1
|
+
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/functions/compare.ts"],"names":[],"mappings":";;A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