map-transform 0.4.0-alpha.3 → 0.4.0-alpha.30
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 +206 -46
- package/dist/functions/compare.d.ts +2 -1
- package/dist/functions/compare.js +26 -5
- package/dist/functions/compare.js.map +1 -1
- package/dist/functions/explode.d.ts +2 -1
- package/dist/functions/explode.js +12 -4
- package/dist/functions/explode.js.map +1 -1
- package/dist/functions/index.d.ts +4 -1
- package/dist/functions/index.js +4 -1
- package/dist/functions/index.js.map +1 -1
- package/dist/functions/joinSplit.js +4 -5
- package/dist/functions/joinSplit.js.map +1 -1
- package/dist/functions/logical.js +1 -5
- package/dist/functions/logical.js.map +1 -1
- package/dist/functions/map.js +5 -4
- package/dist/functions/map.js.map +1 -1
- package/dist/functions/not.js +1 -1
- package/dist/functions/not.js.map +1 -1
- 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 +7 -5
- package/dist/functions/template.js.map +1 -1
- package/dist/functions/validate.js +2 -2
- package/dist/functions/validate.js.map +1 -1
- package/dist/functions/value.js +2 -2
- package/dist/functions/value.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +9 -6
- package/dist/index.js.map +1 -1
- package/dist/operations/alt.d.ts +1 -1
- package/dist/operations/alt.js +28 -6
- package/dist/operations/alt.js.map +1 -1
- package/dist/operations/apply.js +10 -5
- package/dist/operations/apply.js.map +1 -1
- package/dist/operations/concat.js +9 -5
- package/dist/operations/concat.js.map +1 -1
- package/dist/operations/directionals.js +7 -6
- package/dist/operations/directionals.js.map +1 -1
- package/dist/operations/filter.js +10 -9
- 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 +3 -3
- package/dist/operations/getSet.js +137 -25
- package/dist/operations/getSet.js.map +1 -1
- package/dist/operations/ifelse.d.ts +1 -1
- package/dist/operations/ifelse.js +21 -8
- package/dist/operations/ifelse.js.map +1 -1
- package/dist/operations/iterate.d.ts +2 -1
- package/dist/operations/iterate.js +14 -12
- package/dist/operations/iterate.js.map +1 -1
- package/dist/operations/logical.d.ts +3 -0
- package/dist/operations/logical.js +17 -0
- package/dist/operations/logical.js.map +1 -0
- package/dist/operations/lookup.js +9 -5
- package/dist/operations/lookup.js.map +1 -1
- package/dist/operations/merge.d.ts +1 -0
- package/dist/operations/merge.js +34 -9
- package/dist/operations/merge.js.map +1 -1
- package/dist/operations/modify.d.ts +1 -1
- package/dist/operations/modify.js +15 -8
- package/dist/operations/modify.js.map +1 -1
- package/dist/operations/pipe.js +18 -10
- 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/props.d.ts +2 -0
- package/dist/operations/props.js +110 -0
- package/dist/operations/props.js.map +1 -0
- package/dist/operations/root.js +3 -4
- package/dist/operations/root.js.map +1 -1
- package/dist/operations/transform.js +6 -4
- 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 +42 -13
- package/dist/utils/array.d.ts +3 -0
- package/dist/utils/array.js +14 -0
- package/dist/utils/array.js.map +1 -0
- package/dist/utils/definitionHelpers.d.ts +3 -1
- package/dist/utils/definitionHelpers.js +95 -24
- package/dist/utils/definitionHelpers.js.map +1 -1
- package/dist/utils/escape.d.ts +2 -0
- package/dist/utils/escape.js +8 -0
- package/dist/utils/escape.js.map +1 -0
- package/dist/utils/functional.d.ts +1 -0
- package/dist/utils/functional.js +3 -1
- package/dist/utils/functional.js.map +1 -1
- package/dist/utils/is.d.ts +2 -0
- package/dist/utils/is.js +5 -1
- package/dist/utils/is.js.map +1 -1
- package/dist/utils/pathGetter.d.ts +4 -2
- package/dist/utils/pathGetter.js +24 -19
- package/dist/utils/pathGetter.js.map +1 -1
- package/dist/utils/pathSetter.d.ts +5 -3
- package/dist/utils/pathSetter.js +48 -43
- package/dist/utils/pathSetter.js.map +1 -1
- package/dist/utils/stateHelpers.d.ts +19 -6
- package/dist/utils/stateHelpers.js +28 -12
- package/dist/utils/stateHelpers.js.map +1 -1
- package/dist/utils/xor.d.ts +1 -0
- package/dist/utils/xor.js +7 -0
- package/dist/utils/xor.js.map +1 -0
- package/package.json +8 -10
- package/dist/functions/join.d.ts +0 -7
- package/dist/functions/join.js +0 -12
- package/dist/functions/join.js.map +0 -1
- package/dist/operations/mutate.d.ts +0 -2
- package/dist/operations/mutate.js +0 -55
- package/dist/operations/mutate.js.map +0 -1
package/README.md
CHANGED
|
@@ -148,8 +148,8 @@ npm install map-transform --save
|
|
|
148
148
|
- Map objects won't be mapped over an array by default. You have to specify
|
|
149
149
|
`$iterate: true`
|
|
150
150
|
- The `alt` operation now accepts any type of pipeline, but not a helper
|
|
151
|
-
function
|
|
152
|
-
- The root path prefix is changed from `$` to
|
|
151
|
+
function, and all alternative pipelines must be given as arguments to `alt()`
|
|
152
|
+
- The root path prefix is changed from `$` to `^^`
|
|
153
153
|
|
|
154
154
|
## Usage
|
|
155
155
|
|
|
@@ -188,11 +188,14 @@ 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 = {
|
|
198
|
+
$iterate: true,
|
|
196
199
|
title: 'heading',
|
|
197
200
|
}
|
|
198
201
|
|
|
@@ -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
|
|
@@ -306,15 +305,18 @@ below.
|
|
|
306
305
|
|
|
307
306
|
MapTransform will treat `undefined` as a value that is not set, and so a
|
|
308
307
|
transform object will not be applied to it. So if an `undefined` value meets
|
|
309
|
-
a transform object, it will produce `undefined`,
|
|
308
|
+
a transform object, it will produce `undefined`, regardless of the shape of the
|
|
310
309
|
transform object.
|
|
311
310
|
|
|
312
311
|
This is not the case for `null`, though. MapTransform treats `null` as a value,
|
|
313
312
|
an intended nothing, and will apply a transform object to it, even though it
|
|
314
313
|
will most likely produce nothing but default values. To change this behavior,
|
|
315
|
-
set `
|
|
316
|
-
will essentially make MapTransform treat `null` the same way
|
|
317
|
-
it
|
|
314
|
+
set `noneValues: [undefined, null]` on the `options` object passed to
|
|
315
|
+
MapTransform. This will essentially make MapTransform treat `null` the same way
|
|
316
|
+
as `undefined` when it meets to the transform object.
|
|
317
|
+
|
|
318
|
+
The example above sets `undefined` and `null` as none-values, but you could in
|
|
319
|
+
principle set any primitive values here.
|
|
318
320
|
|
|
319
321
|
#### Directional transform objects
|
|
320
322
|
|
|
@@ -326,6 +328,17 @@ When running a forward transformation, transform objects marked with
|
|
|
326
328
|
`$direction: 'rev'` will be skipped. The same goes for `$direction: 'fwd'` in
|
|
327
329
|
reverse.
|
|
328
330
|
|
|
331
|
+
You may specify aliases for `fwd` and `rev` in the `mapTransform` options:
|
|
332
|
+
|
|
333
|
+
```javascript
|
|
334
|
+
const options = { fwdAlias: 'from', revAlias: 'to' }
|
|
335
|
+
const mapper = mapTransform(def, options)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
In this case, `from` and `to` may be used to specify forward and reverse
|
|
339
|
+
direction respectively. `fwd` and `rev` will still work in addition to the
|
|
340
|
+
aliases.
|
|
341
|
+
|
|
329
342
|
### Transform pipeline
|
|
330
343
|
|
|
331
344
|
The idea of the transform pipeline, is that you describe a set of
|
|
@@ -553,10 +566,10 @@ See the `transform()` operation for more on how defining as an object works.
|
|
|
553
566
|
|
|
554
567
|
#### `ifelse(conditionFn, truePipeline, falsePipeline)` operation
|
|
555
568
|
|
|
556
|
-
The `ifelse()` operation will run the `truePipeline` if the `conditionFn`
|
|
557
|
-
in
|
|
558
|
-
[the `filter()` operation](#filterconditionFn-operation)
|
|
559
|
-
requirements for the `conditionFn`.
|
|
569
|
+
The `ifelse()` operation will run the `truePipeline` if the `conditionFn`
|
|
570
|
+
results in something truthy, JavaScript tstyle, otherwise it will run the
|
|
571
|
+
`falsePipeline`. See [the `filter()` operation](#filterconditionFn-operation)
|
|
572
|
+
for more on the requirements for the `conditionFn`.
|
|
560
573
|
|
|
561
574
|
Both `truePipeline` and `falsePipeline` are optional, in case you only need to
|
|
562
575
|
apply a pipeline in one of the cases.
|
|
@@ -577,6 +590,29 @@ const def31 = [
|
|
|
577
590
|
]
|
|
578
591
|
```
|
|
579
592
|
|
|
593
|
+
Defining an if operation as an object:
|
|
594
|
+
|
|
595
|
+
```javascript
|
|
596
|
+
import { mapTransform } from 'map-transform'
|
|
597
|
+
|
|
598
|
+
const def31b = [
|
|
599
|
+
'members'
|
|
600
|
+
{
|
|
601
|
+
name: 'name',
|
|
602
|
+
active: 'hasPayed'
|
|
603
|
+
},
|
|
604
|
+
{
|
|
605
|
+
$if: 'active',
|
|
606
|
+
then: set('active[]'),
|
|
607
|
+
else: set('inactive[]')
|
|
608
|
+
}
|
|
609
|
+
]
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
Note that `$if`, `then`, and `else` in the object notation may be any type of
|
|
613
|
+
pipeline definition. The only gotcha is that if `$if` is a function, it is
|
|
614
|
+
treated as a `conditionFn`, like in `def31`, not as a state mapper.
|
|
615
|
+
|
|
580
616
|
#### `iterate(pipeline)` operation
|
|
581
617
|
|
|
582
618
|
If you want something to be mapped over the items of an array, the `iterate`
|
|
@@ -612,6 +648,9 @@ When an id is passed to the apply operation as `pipelinedId`, the pipeline will
|
|
|
612
648
|
be applied in the place of the apply operation and executed as if it was part of
|
|
613
649
|
the pipeline definition in the first place.
|
|
614
650
|
|
|
651
|
+
When no pipeline is provided, e.g. because the id is unknown, no pipeline will
|
|
652
|
+
be applied and the data will pass through untouched.
|
|
653
|
+
|
|
615
654
|
```javascript
|
|
616
655
|
import { mapTransform, apply, transform } from 'map-transform'
|
|
617
656
|
|
|
@@ -680,20 +719,20 @@ import { value, alt } from 'map-transform'
|
|
|
680
719
|
const def10 = {
|
|
681
720
|
id: 'data.customerNo',
|
|
682
721
|
type: value('customer'),
|
|
683
|
-
name:
|
|
722
|
+
name: alt('data.name', value('Anonymous')),
|
|
684
723
|
}
|
|
685
724
|
```
|
|
686
725
|
|
|
687
726
|
The operation will not set anything when mapping with `.onlyMappedValues()`.
|
|
688
727
|
|
|
689
728
|
If you want to define the `value` operation as an operation object, use
|
|
690
|
-
`$transform` or `$
|
|
729
|
+
`$transform` or the short form `$value`:
|
|
691
730
|
|
|
692
731
|
```javascript
|
|
693
732
|
const def10asObject = {
|
|
694
733
|
id: 'data.customerNo',
|
|
695
734
|
type: { $transform: 'value', value: 'customer' },
|
|
696
|
-
name: ['data.name', { $
|
|
735
|
+
name: { $alt: ['data.name', { $value: 'Anonymous' }] },
|
|
697
736
|
}
|
|
698
737
|
```
|
|
699
738
|
|
|
@@ -708,14 +747,15 @@ will be included when mapping with `.onlyMappedValues()` as well.
|
|
|
708
747
|
|
|
709
748
|
#### `alt(pipeline)` operation
|
|
710
749
|
|
|
711
|
-
The alt operation will apply the
|
|
712
|
-
the pipeline
|
|
713
|
-
|
|
714
|
-
|
|
750
|
+
The alt operation will apply the pipelines in turn until it gets a value,
|
|
751
|
+
meaning that if the first pipeline returns `undefined`, it will try the next and
|
|
752
|
+
so on. This is how you provide default values in MapTransform. The pipeline may
|
|
753
|
+
be as simple as a `value()` operation, a dot notation path into the source data,
|
|
754
|
+
or a full pipeline of several operations.
|
|
715
755
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
756
|
+
Note that when the return value is an array, it is treated as a value, as it is
|
|
757
|
+
not an `undefined` value. To provide the `alt` operation to every item in the
|
|
758
|
+
array, use the `iterate` operation.
|
|
719
759
|
|
|
720
760
|
```javascript
|
|
721
761
|
import { alt, transform, functions } from 'map-transform'
|
|
@@ -727,11 +767,9 @@ const formatDate = (data) => {
|
|
|
727
767
|
|
|
728
768
|
const def11 = {
|
|
729
769
|
id: 'data.id',
|
|
730
|
-
name:
|
|
770
|
+
name: alt('data.name', value('Anonymous')),
|
|
731
771
|
updatedAt: [
|
|
732
|
-
'data.updateDate',
|
|
733
|
-
alt('data.createDate'),
|
|
734
|
-
alt(transform(currentDate)),
|
|
772
|
+
alt('data.updateDate', 'data.createDate', transform(currentDate)),
|
|
735
773
|
transform(formatDate),
|
|
736
774
|
],
|
|
737
775
|
}
|
|
@@ -745,16 +783,24 @@ we still have `undefined`, the second alt will call the customer function
|
|
|
745
783
|
another transform operation pipes whatever data we get from all of this through
|
|
746
784
|
the `formatDate` function.
|
|
747
785
|
|
|
786
|
+
When `alt` is run in reverse, the alternative pipelines are run in the oposite
|
|
787
|
+
order, with the last being run first. The first pipeline is always run, though,
|
|
788
|
+
as it is common practice to let the first be a `get` that acts like a `set` in
|
|
789
|
+
reverse.
|
|
790
|
+
|
|
791
|
+
With only one pipeline, `alt` will behave a bit differently. A single pipeline
|
|
792
|
+
will be run if the state has an `undefined` value, but skipped otherwise. This
|
|
793
|
+
is different from the multi-pipeline behavor, where the first is always run and
|
|
794
|
+
the rest is only run if the previous returns `undefined`.
|
|
795
|
+
|
|
748
796
|
You may also define an alt operation as an object:
|
|
749
797
|
|
|
750
798
|
```javascript
|
|
751
799
|
const def11asObject = {
|
|
752
800
|
id: 'data.id',
|
|
753
|
-
name: ['data.name', { $
|
|
801
|
+
name: { $alt: ['data.name', { $value: 'Anonymous' }] },
|
|
754
802
|
updatedAt: [
|
|
755
|
-
'data.updateDate',
|
|
756
|
-
{ $alt: 'get', path: 'data.createDate' },
|
|
757
|
-
{ $alt: 'currentDate' },
|
|
803
|
+
{ $alt: ['data.updateDate', 'data.createDate', 'currentDate'] },
|
|
758
804
|
{ $transform: 'formatDate' },
|
|
759
805
|
],
|
|
760
806
|
}
|
|
@@ -818,6 +864,27 @@ const response = {
|
|
|
818
864
|
Had we ran this without the `modify()` operation, the returned object would only
|
|
819
865
|
have the `data` prop.
|
|
820
866
|
|
|
867
|
+
This is equivalent to setting the `$modify` property to `true` on the object
|
|
868
|
+
mutation object:
|
|
869
|
+
|
|
870
|
+
```javascript
|
|
871
|
+
const def34b = {
|
|
872
|
+
$modify: true,
|
|
873
|
+
data: 'data.deeply.placed.items',
|
|
874
|
+
}
|
|
875
|
+
```
|
|
876
|
+
|
|
877
|
+
Note that `$modify` may also be set further down in the object structure. Also,
|
|
878
|
+
in some cases it may make more sense to specify a path in the source data to
|
|
879
|
+
merge with:
|
|
880
|
+
|
|
881
|
+
```javascript
|
|
882
|
+
const def34c = {
|
|
883
|
+
$modify: 'response',
|
|
884
|
+
data: 'response.data.deeply.placed.items',
|
|
885
|
+
}
|
|
886
|
+
```
|
|
887
|
+
|
|
821
888
|
#### `fwd(pipeline)` and `rev(pipeline)` operation
|
|
822
889
|
|
|
823
890
|
All operations in MapTransform will apply in both directions, although some of
|
|
@@ -998,7 +1065,51 @@ mapper.rev(mappedData)
|
|
|
998
1065
|
// --> { content: { meta: { authors: ['user1', 'user3'] } } }
|
|
999
1066
|
```
|
|
1000
1067
|
|
|
1001
|
-
|
|
1068
|
+
You may also define this as a transform object:
|
|
1069
|
+
|
|
1070
|
+
```javascript
|
|
1071
|
+
const def18o = ['content.meta.authors[]', { $lookup: '$users[]', path: 'id' }]
|
|
1072
|
+
```
|
|
1073
|
+
|
|
1074
|
+
#### `and(pipeline, pipeline, ...)` operation
|
|
1075
|
+
|
|
1076
|
+
Will run all provided pipelines, force their return values to boolean, according
|
|
1077
|
+
to JavaScript rules, and return `true` if they are all `true`, otherwise
|
|
1078
|
+
`false`.
|
|
1079
|
+
|
|
1080
|
+
Typically used together with [`ifelse` operation](#ifelseconditionFn-truePipeline-falsePipeline-operation),
|
|
1081
|
+
to support AND logic:
|
|
1082
|
+
|
|
1083
|
+
```javascript
|
|
1084
|
+
const def36 = [
|
|
1085
|
+
{
|
|
1086
|
+
$if: { $and: ['active', 'authorized'] },
|
|
1087
|
+
then: 'content',
|
|
1088
|
+
else: { $value: undefined },
|
|
1089
|
+
},
|
|
1090
|
+
]
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
#### `or(pipeline, pipeline, ...)` operation
|
|
1094
|
+
|
|
1095
|
+
Will run all provided pipelines, force their return values to boolean, according
|
|
1096
|
+
to JavaScript rules, and return `true` if any of the are `true`, otherwise
|
|
1097
|
+
`false`.
|
|
1098
|
+
|
|
1099
|
+
Typically used together with [`ifelse` operation](#ifelseconditionFn-truePipeline-falsePipeline-operation),
|
|
1100
|
+
to support OR logic:
|
|
1101
|
+
|
|
1102
|
+
```javascript
|
|
1103
|
+
const def37 = [
|
|
1104
|
+
{
|
|
1105
|
+
$if: { $or: ['active', 'draft'] },
|
|
1106
|
+
then: 'content',
|
|
1107
|
+
else: { $value: undefined },
|
|
1108
|
+
},
|
|
1109
|
+
]
|
|
1110
|
+
```
|
|
1111
|
+
|
|
1112
|
+
#### `compare({ path, operator, match, matchPath, not })` function
|
|
1002
1113
|
|
|
1003
1114
|
This is a helper function intended for use with the `filter()` operation. You
|
|
1004
1115
|
pass a dot notation `path` and a `match` value (string, number, boolean) to
|
|
@@ -1011,10 +1122,14 @@ data.
|
|
|
1011
1122
|
|
|
1012
1123
|
The default is to compare the values resulting from `path` and `match` or
|
|
1013
1124
|
`matchPath` with equality, but other operations may be set on the `operator`
|
|
1014
|
-
property. Alternatives: `'='`, `'!='`, `'>'`, `'>='`, `'<'`, or `'<='
|
|
1125
|
+
property. Alternatives: `'='`, `'!='`, `'>'`, `'>='`, `'<'`, or `'<='`, `in`, or
|
|
1126
|
+
`exists`. `in` requires equality to at least one of the elements in an array,
|
|
1127
|
+
and `exists` requires any value besides `undefined`.
|
|
1128
|
+
|
|
1129
|
+
If the `path` points to an array, the value is expected to be one of the values
|
|
1130
|
+
in the array.
|
|
1015
1131
|
|
|
1016
|
-
|
|
1017
|
-
the array.
|
|
1132
|
+
Set `not` to `true` to reverse the result of the comparison.
|
|
1018
1133
|
|
|
1019
1134
|
Here's an example where only data where role is set to 'admin' will be kept:
|
|
1020
1135
|
|
|
@@ -1043,6 +1158,9 @@ const def19o = [
|
|
|
1043
1158
|
]
|
|
1044
1159
|
```
|
|
1045
1160
|
|
|
1161
|
+
When you define the `compare` function as a transform object in JSON and need to
|
|
1162
|
+
compare to `undefined`, use `**undefined**` instead.
|
|
1163
|
+
|
|
1046
1164
|
#### `explode()` function
|
|
1047
1165
|
|
|
1048
1166
|
Given an object, the `explode` helper function will return an array with one
|
|
@@ -1081,6 +1199,13 @@ Or as a transform object:
|
|
|
1081
1199
|
const def32o = ['currencies', { $transform: 'explode' }]
|
|
1082
1200
|
```
|
|
1083
1201
|
|
|
1202
|
+
#### `implode()` function
|
|
1203
|
+
|
|
1204
|
+
This is the exact opposite of the `explode` helper, imploding from a service and
|
|
1205
|
+
explode in reverse (to a service). See
|
|
1206
|
+
[the documentation for `explode()`](#explode-function), but remember that the
|
|
1207
|
+
directions will be reversed for `implode()`.
|
|
1208
|
+
|
|
1084
1209
|
#### `map(dictionary)` function
|
|
1085
1210
|
|
|
1086
1211
|
This helper function accepts a dictionary described as an array of tuples, where
|
|
@@ -1096,7 +1221,8 @@ is returned instead.
|
|
|
1096
1221
|
|
|
1097
1222
|
The `map` function only supports primitive values, so any object will be mapped to
|
|
1098
1223
|
`undefined` or the value given by the wildcard in the dictionary. Arrays will be
|
|
1099
|
-
iterated to map each value in the array.
|
|
1224
|
+
iterated to map each value in the array. To map to or from `undefined` with a
|
|
1225
|
+
dictionary defined in JSON, use the value `**undefined**`.
|
|
1100
1226
|
|
|
1101
1227
|
Example:
|
|
1102
1228
|
|
|
@@ -1115,7 +1241,7 @@ const def28 = {
|
|
|
1115
1241
|
}
|
|
1116
1242
|
```
|
|
1117
1243
|
|
|
1118
|
-
When using `map` in an operation object, you may
|
|
1244
|
+
When using `map` in an operation object, you may provide a dictionary array
|
|
1119
1245
|
or a named dictionary on the `dictionary` property. An example of with a named
|
|
1120
1246
|
dictionary:
|
|
1121
1247
|
|
|
@@ -1138,6 +1264,40 @@ const def29 = {
|
|
|
1138
1264
|
const mapper = mapTransform(def29, { dictionaries: { statusCodes: dictionary } })
|
|
1139
1265
|
```
|
|
1140
1266
|
|
|
1267
|
+
#### `sort({asc, path})` function
|
|
1268
|
+
|
|
1269
|
+
The `sort` helper function will sort the given array of items in ascending or
|
|
1270
|
+
descending (depending on whether `asc` is `true` or `false`). Ascending is the
|
|
1271
|
+
default. When a `path` is given, the sort is performed on the value at that path
|
|
1272
|
+
on each object in the array. With no `path`, the values in the array are sorted
|
|
1273
|
+
directly.
|
|
1274
|
+
|
|
1275
|
+
Example:
|
|
1276
|
+
|
|
1277
|
+
```javascript
|
|
1278
|
+
import { mapTransform, transform, functions } from 'map-transform'
|
|
1279
|
+
const { sort } = functions
|
|
1280
|
+
|
|
1281
|
+
const data = {
|
|
1282
|
+
items: [{ id: 'ent5' }, { id: 'ent1' }, { id: 'ent3' }],
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const def35 = {
|
|
1286
|
+
data: ['items', transform(sort({ asc: true, path: 'id' }))],
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
const ret = mapTransform(def35)(data)
|
|
1290
|
+
// --> { caption: 'Bergen by night. By John F.' }
|
|
1291
|
+
```
|
|
1292
|
+
|
|
1293
|
+
The `sort` function is also available through a transform object:
|
|
1294
|
+
|
|
1295
|
+
```javascript
|
|
1296
|
+
const def35o = {
|
|
1297
|
+
data: ['items', { $transform: 'sort', asc: true, path: 'id' }],
|
|
1298
|
+
}
|
|
1299
|
+
```
|
|
1300
|
+
|
|
1141
1301
|
#### `template(template)` function
|
|
1142
1302
|
|
|
1143
1303
|
The `template` helper function takes a [handlebars] template and applies the
|
|
@@ -1249,7 +1409,7 @@ const def22 = [
|
|
|
1249
1409
|
'data.customers[]',
|
|
1250
1410
|
{
|
|
1251
1411
|
id: 'customerNo',
|
|
1252
|
-
name: ['fullname',
|
|
1412
|
+
name: [alt('fullname', value('Anonymous'))],
|
|
1253
1413
|
},
|
|
1254
1414
|
]
|
|
1255
1415
|
|
|
@@ -1363,8 +1523,8 @@ same goes for flipped transform objects if you want to forward transform.
|
|
|
1363
1523
|
|
|
1364
1524
|
MapTransform will try its best to map the data it gets to the state you want,
|
|
1365
1525
|
and will always set all properties, even though the mapping you defined result
|
|
1366
|
-
in `undefined`. You may include `alt()` operations to provide default or
|
|
1367
|
-
values for these cases.
|
|
1526
|
+
in `undefined`. You may include `alt()` operations to provide default or
|
|
1527
|
+
fallback values for these cases.
|
|
1368
1528
|
|
|
1369
1529
|
But sometimes, you want just the data that is actually present in the source
|
|
1370
1530
|
data, without defaults or properties set to `undefined`. MapTransform's
|
|
@@ -1380,7 +1540,7 @@ import { mapTransform, alt, value } from 'map-transform'
|
|
|
1380
1540
|
|
|
1381
1541
|
const def24 = {
|
|
1382
1542
|
id: 'customerNo',
|
|
1383
|
-
name:
|
|
1543
|
+
name: alt('fullname', value('Anonymous')),
|
|
1384
1544
|
}
|
|
1385
1545
|
|
|
1386
1546
|
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 {};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const mapAny = require("map-any");
|
|
3
4
|
const pathGetter_1 = require("../utils/pathGetter");
|
|
5
|
+
const escape_1 = require("../utils/escape");
|
|
6
|
+
const stateHelpers_1 = require("../utils/stateHelpers");
|
|
4
7
|
const not = (comparer) => (value, match) => !comparer(value, match);
|
|
5
8
|
const compareArrayOrValue = (comparer) => (value, match) => Array.isArray(value)
|
|
6
9
|
? value.some((value) => comparer(value, match))
|
|
@@ -8,6 +11,10 @@ const compareArrayOrValue = (comparer) => (value, match) => Array.isArray(value)
|
|
|
8
11
|
const isNumeric = (value) => typeof value === 'number';
|
|
9
12
|
const compareArrayOrValueNumeric = (comparer) => compareArrayOrValue((value, match) => isNumeric(value) && isNumeric(match) && comparer(value, match));
|
|
10
13
|
const compareEqual = compareArrayOrValue((value, match) => value === match);
|
|
14
|
+
const compareIn = (value, match) => Array.isArray(match)
|
|
15
|
+
? match.some((item) => compareEqual(value, item))
|
|
16
|
+
: compareEqual(value, match);
|
|
17
|
+
const exists = (value) => value !== undefined;
|
|
11
18
|
function createComparer(operator) {
|
|
12
19
|
switch (operator) {
|
|
13
20
|
case '=':
|
|
@@ -22,18 +29,32 @@ function createComparer(operator) {
|
|
|
22
29
|
return compareArrayOrValueNumeric((value, match) => value < match);
|
|
23
30
|
case '<=':
|
|
24
31
|
return compareArrayOrValueNumeric((value, match) => value <= match);
|
|
32
|
+
case 'in':
|
|
33
|
+
return compareIn;
|
|
34
|
+
case 'exists':
|
|
35
|
+
return exists;
|
|
25
36
|
default:
|
|
26
37
|
return (_value, _match) => false;
|
|
27
38
|
}
|
|
28
39
|
}
|
|
29
|
-
function compare({ path = '.', operator = '=', match, matchPath, }) {
|
|
40
|
+
function compare({ path = '.', operator = '=', match, matchPath, not = false, }) {
|
|
30
41
|
const getValue = (0, pathGetter_1.default)(path);
|
|
31
|
-
const
|
|
42
|
+
const useRoot = typeof matchPath === 'string' &&
|
|
43
|
+
matchPath[0] === '^' &&
|
|
44
|
+
matchPath[1] !== '.';
|
|
45
|
+
const realMatchPath = useRoot
|
|
46
|
+
? matchPath.slice(matchPath[1] === '^' ? 2 : 1)
|
|
47
|
+
: matchPath;
|
|
48
|
+
const realMatchValue = mapAny(escape_1.unescapeValue, match);
|
|
49
|
+
const getMatch = typeof realMatchPath === 'string'
|
|
50
|
+
? (0, pathGetter_1.default)(realMatchPath)
|
|
51
|
+
: () => realMatchValue;
|
|
32
52
|
const comparer = createComparer(operator);
|
|
33
|
-
return (data) => {
|
|
53
|
+
return (data, state) => {
|
|
34
54
|
const value = getValue(data);
|
|
35
|
-
const match = getMatch(data);
|
|
36
|
-
|
|
55
|
+
const match = getMatch(useRoot ? (0, stateHelpers_1.getRootFromState)(state) : data);
|
|
56
|
+
const result = comparer(value, match);
|
|
57
|
+
return not ? !result : result;
|
|
37
58
|
};
|
|
38
59
|
}
|
|
39
60
|
exports.default = compare;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/functions/compare.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/functions/compare.ts"],"names":[],"mappings":";;AAAA,kCAAkC;AAElC,oDAAwC;AACxC,4CAA+C;AAC/C,wDAAwD;AAkBxD,MAAM,GAAG,GAAG,CAAC,QAAkB,EAAE,EAAE,CAAC,CAAC,KAAc,EAAE,KAAc,EAAE,EAAE,CACrE,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;AAEzB,MAAM,mBAAmB,GACvB,CAAC,QAAkB,EAAE,EAAE,CAAC,CAAC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B,CAAC,CAAC,IAAA,oBAAM,EAAC,aAAa,CAAC;QACvB,CAAC,CAAC,GAAG,EAAE,CAAC,cAAc,CAAA;IAC1B,MAAM,QAAQ,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAA;IAEzC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;QACrB,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;QAC5B,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,IAAA,+BAAgB,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;QAChE,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QACrC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAA;IAC/B,CAAC,CAAA;AACH,CAAC;AA5BD,0BA4BC"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.implode = exports.explode = void 0;
|
|
3
4
|
const is_1 = require("../utils/is");
|
|
4
|
-
const isExplodedArray = (data) => data.
|
|
5
|
+
const isExplodedArray = (data) => data.length > 0 &&
|
|
6
|
+
data.every((item) => (0, is_1.isObject)(item) && typeof item.key === 'number');
|
|
5
7
|
const setValueOnKey = (target, keyValue) => {
|
|
6
8
|
if ((0, is_1.isObject)(keyValue)) {
|
|
7
9
|
const { key, value } = keyValue;
|
|
@@ -26,7 +28,9 @@ function doImplode(data) {
|
|
|
26
28
|
}
|
|
27
29
|
function doExplode(data) {
|
|
28
30
|
if ((0, is_1.isObject)(data)) {
|
|
29
|
-
return Object.entries(data)
|
|
31
|
+
return Object.entries(data)
|
|
32
|
+
.filter(([, value]) => value !== undefined)
|
|
33
|
+
.map(([key, value]) => ({
|
|
30
34
|
key,
|
|
31
35
|
value,
|
|
32
36
|
}));
|
|
@@ -39,7 +43,11 @@ function doExplode(data) {
|
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
function explode() {
|
|
42
|
-
return (data,
|
|
46
|
+
return (data, state) => (state.rev ? doImplode(data) : doExplode(data));
|
|
43
47
|
}
|
|
44
|
-
exports.
|
|
48
|
+
exports.explode = explode;
|
|
49
|
+
function implode() {
|
|
50
|
+
return (data, state) => (state.rev ? doExplode(data) : doImplode(data));
|
|
51
|
+
}
|
|
52
|
+
exports.implode = implode;
|
|
45
53
|
//# sourceMappingURL=explode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"explode.js","sourceRoot":"","sources":["../../src/functions/explode.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"explode.js","sourceRoot":"","sources":["../../src/functions/explode.ts"],"names":[],"mappings":";;;AAEA,oCAAsC;AAOtC,MAAM,eAAe,GAAG,CAAC,IAAe,EAAE,EAAE,CAC1C,IAAI,CAAC,MAAM,GAAG,CAAC;IACf,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAA,aAAQ,EAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,GAAG,KAAK,QAAQ,CAAC,CAAA;AAEtE,MAAM,aAAa,GAAG,CACpB,MAA2C,EAC3C,QAAiB,EACjB,EAAE;IACF,IAAI,IAAA,aAAQ,EAAC,QAAQ,CAAC,EAAE;QACtB,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,QAAQ,CAAA;QAC/B,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACzB,MAAM,CAAC,GAAa,CAAC,GAAG,KAAK,CAAA;SAC9B;aAAM;YACL,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAA;SAC5B;KACF;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAED,SAAS,SAAS,CAAC,IAAa;IAC9B,IAAI,KAAK,CAAC,OAAO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"}
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { value, fixed } from './value';
|
|
2
2
|
import compare from './compare';
|
|
3
|
-
import explode from './explode';
|
|
3
|
+
import { explode, implode } from './explode';
|
|
4
4
|
import get from './get';
|
|
5
5
|
import { join, split } from './joinSplit';
|
|
6
6
|
import logical from './logical';
|
|
7
7
|
import map from './map';
|
|
8
|
+
import sort from './sort';
|
|
8
9
|
import template from './template';
|
|
9
10
|
declare const _default: {
|
|
10
11
|
compare: typeof compare;
|
|
11
12
|
explode: typeof explode;
|
|
12
13
|
fixed: typeof fixed;
|
|
13
14
|
get: typeof get;
|
|
15
|
+
implode: typeof implode;
|
|
14
16
|
join: typeof join;
|
|
15
17
|
logical: typeof logical;
|
|
16
18
|
map: typeof map;
|
|
19
|
+
sort: typeof sort;
|
|
17
20
|
split: typeof split;
|
|
18
21
|
template: typeof template;
|
|
19
22
|
value: typeof value;
|
package/dist/functions/index.js
CHANGED
|
@@ -7,15 +7,18 @@ const get_1 = require("./get");
|
|
|
7
7
|
const joinSplit_1 = require("./joinSplit");
|
|
8
8
|
const logical_1 = require("./logical");
|
|
9
9
|
const map_1 = require("./map");
|
|
10
|
+
const sort_1 = require("./sort");
|
|
10
11
|
const template_1 = require("./template");
|
|
11
12
|
exports.default = {
|
|
12
13
|
compare: compare_1.default,
|
|
13
|
-
explode: explode_1.
|
|
14
|
+
explode: explode_1.explode,
|
|
14
15
|
fixed: value_1.fixed,
|
|
15
16
|
get: get_1.default,
|
|
17
|
+
implode: explode_1.implode,
|
|
16
18
|
join: joinSplit_1.join,
|
|
17
19
|
logical: logical_1.default,
|
|
18
20
|
map: map_1.default,
|
|
21
|
+
sort: sort_1.default,
|
|
19
22
|
split: joinSplit_1.split,
|
|
20
23
|
template: template_1.default,
|
|
21
24
|
value: value_1.value,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":";;AAAA,mCAAsC;AACtC,uCAA+B;AAC/B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":";;AAAA,mCAAsC;AACtC,uCAA+B;AAC/B,uCAA4C;AAC5C,+BAAuB;AACvB,2CAAyC;AACzC,uCAA+B;AAC/B,+BAAuB;AACvB,iCAAyB;AACzB,yCAAiC;AAEjC,kBAAe;IACb,OAAO,EAAP,iBAAO;IACP,OAAO,EAAP,iBAAO;IACP,KAAK,EAAL,aAAK;IACL,GAAG,EAAH,aAAG;IACH,OAAO,EAAP,iBAAO;IACP,IAAI,EAAJ,gBAAI;IACJ,OAAO,EAAP,iBAAO;IACP,GAAG,EAAH,aAAG;IACH,IAAI,EAAJ,cAAI;IACJ,KAAK,EAAL,iBAAK;IACL,QAAQ,EAAR,kBAAQ;IACR,KAAK,EAAL,aAAK;CACN,CAAA"}
|
|
@@ -5,16 +5,15 @@ const pathGetter_1 = require("../utils/pathGetter");
|
|
|
5
5
|
const pathSetter_1 = require("../utils/pathSetter");
|
|
6
6
|
function joinSplit({ path = [], sep = ' ' }, split) {
|
|
7
7
|
const pathArr = [].concat(path);
|
|
8
|
+
if (pathArr.length === 0) {
|
|
9
|
+
return (_data, _context) => undefined;
|
|
10
|
+
}
|
|
8
11
|
const getFns = pathArr.map(pathGetter_1.default);
|
|
9
12
|
const setFns = pathArr.map(pathSetter_1.default);
|
|
10
13
|
return (data, { rev }) => {
|
|
11
14
|
if (split ? !rev : rev) {
|
|
12
15
|
const values = typeof data === 'string' ? data.split(sep) : [];
|
|
13
|
-
|
|
14
|
-
setFns.forEach((setFn, index) => {
|
|
15
|
-
ret = setFn(values[index], ret);
|
|
16
|
-
});
|
|
17
|
-
return ret;
|
|
16
|
+
return setFns.reduce((obj, setFn, index) => setFn(values[index], obj), undefined);
|
|
18
17
|
}
|
|
19
18
|
else {
|
|
20
19
|
const values = getFns.map((fn) => fn(data));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"joinSplit.js","sourceRoot":"","sources":["../../src/functions/joinSplit.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"joinSplit.js","sourceRoot":"","sources":["../../src/functions/joinSplit.ts"],"names":[],"mappings":";;;AAEA,oDAAwC;AACxC,oDAAwC;AAOxC,SAAS,SAAS,CAChB,EAAE,IAAI,GAAG,EAAE,EAAE,GAAG,GAAG,GAAG,EAAW,EACjC,KAAc;IAEd,MAAM,OAAO,GAAI,EAAe,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC7C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;QACxB,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,SAAS,CAAA;KACtC;IAED,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,CAAA;IAClC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAM,CAAC,CAAA;IAElC,OAAO,CAAC,IAAI,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE;QACvB,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE;YACtB,MAAM,MAAM,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;YAC9D,OAAO,MAAM,CAAC,MAAM,CAClB,CAAC,GAAY,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,EACzD,SAAS,CACV,CAAA;SACF;aAAM;YACL,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAA;YAC3C,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK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