map-transform 0.4.0-alpha.1 → 0.4.0-alpha.10

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.
Files changed (90) hide show
  1. package/README.md +139 -86
  2. package/ava-dist.config.cjs +3 -0
  3. package/ava.config.cjs +5 -0
  4. package/dist/functions/compare.js +2 -2
  5. package/dist/functions/compare.js.map +1 -1
  6. package/dist/functions/explode.d.ts +2 -1
  7. package/dist/functions/explode.js +13 -5
  8. package/dist/functions/explode.js.map +1 -1
  9. package/dist/functions/get.js +1 -1
  10. package/dist/functions/get.js.map +1 -1
  11. package/dist/functions/index.d.ts +8 -2
  12. package/dist/functions/index.js +10 -4
  13. package/dist/functions/index.js.map +1 -1
  14. package/dist/functions/joinSplit.d.ts +8 -0
  15. package/dist/functions/joinSplit.js +32 -0
  16. package/dist/functions/joinSplit.js.map +1 -0
  17. package/dist/functions/logical.d.ts +7 -0
  18. package/dist/functions/logical.js +24 -0
  19. package/dist/functions/logical.js.map +1 -0
  20. package/dist/functions/sort.d.ts +7 -0
  21. package/dist/functions/sort.js +33 -0
  22. package/dist/functions/sort.js.map +1 -0
  23. package/dist/functions/template.js +8 -6
  24. package/dist/functions/template.js.map +1 -1
  25. package/dist/functions/validate.js +1 -1
  26. package/dist/functions/validate.js.map +1 -1
  27. package/dist/functions/value.js +4 -3
  28. package/dist/functions/value.js.map +1 -1
  29. package/dist/index.js +4 -6
  30. package/dist/index.js.map +1 -1
  31. package/dist/operations/alt.js +3 -3
  32. package/dist/operations/alt.js.map +1 -1
  33. package/dist/operations/apply.js +2 -2
  34. package/dist/operations/apply.js.map +1 -1
  35. package/dist/operations/concat.js +4 -4
  36. package/dist/operations/concat.js.map +1 -1
  37. package/dist/operations/directionals.js +3 -3
  38. package/dist/operations/directionals.js.map +1 -1
  39. package/dist/operations/filter.js +10 -8
  40. package/dist/operations/filter.js.map +1 -1
  41. package/dist/operations/fixed.js +1 -1
  42. package/dist/operations/fixed.js.map +1 -1
  43. package/dist/operations/getSet.js +14 -9
  44. package/dist/operations/getSet.js.map +1 -1
  45. package/dist/operations/ifelse.js +7 -5
  46. package/dist/operations/ifelse.js.map +1 -1
  47. package/dist/operations/iterate.js +7 -9
  48. package/dist/operations/iterate.js.map +1 -1
  49. package/dist/operations/lookup.js +2 -2
  50. package/dist/operations/lookup.js.map +1 -1
  51. package/dist/operations/merge.d.ts +1 -0
  52. package/dist/operations/merge.js +29 -6
  53. package/dist/operations/merge.js.map +1 -1
  54. package/dist/operations/modify.d.ts +1 -1
  55. package/dist/operations/modify.js +6 -6
  56. package/dist/operations/modify.js.map +1 -1
  57. package/dist/operations/mutate.js +53 -29
  58. package/dist/operations/mutate.js.map +1 -1
  59. package/dist/operations/pipe.js +15 -5
  60. package/dist/operations/pipe.js.map +1 -1
  61. package/dist/operations/plug.js +1 -1
  62. package/dist/operations/plug.js.map +1 -1
  63. package/dist/operations/root.js +3 -2
  64. package/dist/operations/root.js.map +1 -1
  65. package/dist/operations/transform.js +3 -3
  66. package/dist/operations/transform.js.map +1 -1
  67. package/dist/operations/value.js +1 -1
  68. package/dist/operations/value.js.map +1 -1
  69. package/dist/types.d.ts +5 -2
  70. package/dist/utils/array.d.ts +2 -0
  71. package/dist/utils/array.js +12 -0
  72. package/dist/utils/array.js.map +1 -0
  73. package/dist/utils/definitionHelpers.d.ts +2 -2
  74. package/dist/utils/definitionHelpers.js +54 -31
  75. package/dist/utils/definitionHelpers.js.map +1 -1
  76. package/dist/utils/functional.d.ts +1 -0
  77. package/dist/utils/functional.js +7 -3
  78. package/dist/utils/functional.js.map +1 -1
  79. package/dist/utils/is.js +2 -1
  80. package/dist/utils/is.js.map +1 -1
  81. package/dist/utils/pathGetter.d.ts +4 -2
  82. package/dist/utils/pathGetter.js +17 -21
  83. package/dist/utils/pathGetter.js.map +1 -1
  84. package/dist/utils/pathSetter.d.ts +4 -3
  85. package/dist/utils/pathSetter.js +34 -39
  86. package/dist/utils/pathSetter.js.map +1 -1
  87. package/dist/utils/stateHelpers.d.ts +2 -1
  88. package/dist/utils/stateHelpers.js +18 -7
  89. package/dist/utils/stateHelpers.js.map +1 -1
  90. 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, each item in the
192
- data array will be be transformed with the mapping object.
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
- title: 'heading'
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
- If you want to apply the mapping object to the entire array not each item in
207
- the array set `$iterate: false` on the mapping object or wrap the object in a
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,7 +823,7 @@ 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
 
@@ -830,11 +842,11 @@ pipeline when we're mapping in reverse.
830
842
 
831
843
  ```javascript
832
844
  import { fwd, rev, transform } from 'map-transform'
833
- const increment = data => data + 1
834
- const decrement = data => data - 1
845
+ const increment = (data) => data + 1
846
+ const decrement = (data) => data - 1
835
847
 
836
848
  const def12 = {
837
- order: ['index', fwd(transform(increment)), rev(transform(decrement))]
849
+ order: ['index', fwd(transform(increment)), rev(transform(decrement))],
838
850
  }
839
851
  ```
840
852
 
@@ -887,7 +899,7 @@ This example results in the exact same pipeline as the example above:
887
899
 
888
900
  ```javascript
889
901
  const def14 = {
890
- 'content[]': 'data.items[].content'
902
+ 'content[]': 'data.items[].content',
891
903
  }
892
904
  ```
893
905
 
@@ -916,13 +928,13 @@ const def15 = [
916
928
  {
917
929
  id: 'id',
918
930
  title: 'headline',
919
- section: root('meta.section')
920
- }
931
+ section: root('meta.section'),
932
+ },
921
933
  ]
922
934
 
923
935
  const data = {
924
936
  articles: [{ id: '1', headline: 'An article' } /* ... */],
925
- meta: { section: 'news' }
937
+ meta: { section: 'news' },
926
938
  }
927
939
 
928
940
  mapTransform(def15)(data)
@@ -984,8 +996,8 @@ const data = {
984
996
  users: [
985
997
  { id: 'user1', name: 'User 1' },
986
998
  { id: 'user2', name: 'User 2' },
987
- { id: 'user3', name: 'User 3' }
988
- ]
999
+ { id: 'user3', name: 'User 3' },
1000
+ ],
989
1001
  }
990
1002
  const mapper = mapTransform(def18)
991
1003
  const mappedData = mapper(data)
@@ -1025,9 +1037,9 @@ const { compare } = functions
1025
1037
  const def19 = [
1026
1038
  {
1027
1039
  name: 'name',
1028
- role: 'editor'
1040
+ role: 'editor',
1029
1041
  },
1030
- filter(compare({ path: 'role', operator: '=', match: 'admin' }))
1042
+ filter(compare({ path: 'role', operator: '=', match: 'admin' })),
1031
1043
  ]
1032
1044
  ```
1033
1045
 
@@ -1037,9 +1049,9 @@ You may also define this with a transform object:
1037
1049
  const def19o = [
1038
1050
  {
1039
1051
  name: 'name',
1040
- role: 'editor'
1052
+ role: 'editor',
1041
1053
  },
1042
- { $filter: 'compare', path: 'role', operator: '=', match: 'admin' }
1054
+ { $filter: 'compare', path: 'role', operator: '=', match: 'admin' },
1043
1055
  ]
1044
1056
  ```
1045
1057
 
@@ -1065,7 +1077,7 @@ import { mapTransform, transform, functions } from 'map-transform'
1065
1077
  const { explode } = functions
1066
1078
 
1067
1079
  const data = {
1068
- currencies: { NOK: 1, USD: 0.125, EUR: 0.1 }
1080
+ currencies: { NOK: 1, USD: 0.125, EUR: 0.1 },
1069
1081
  }
1070
1082
 
1071
1083
  const def32 = ['currencies', transform(explode())]
@@ -1081,6 +1093,13 @@ Or as a transform object:
1081
1093
  const def32o = ['currencies', { $transform: 'explode' }]
1082
1094
  ```
1083
1095
 
1096
+ #### `implode()` function
1097
+
1098
+ This is the exact opposite of the `explode` helper, imploding from a service and
1099
+ explode in reverse (to a service). See
1100
+ [the documentation for `explode()`](#explode-function), but remember that the
1101
+ directions will be reversed for `implode()`.
1102
+
1084
1103
  #### `map(dictionary)` function
1085
1104
 
1086
1105
  This helper function accepts a dictionary described as an array of tuples, where
@@ -1138,6 +1157,40 @@ const def29 = {
1138
1157
  const mapper = mapTransform(def29, { dictionaries: { statusCodes: dictionary } })
1139
1158
  ```
1140
1159
 
1160
+ #### `sort({asc, path})` function
1161
+
1162
+ The `sort` helper function will sort the given array of items in ascending or
1163
+ descending (depending on whether `asc` is `true` or `false`). Ascending is the
1164
+ default. When a `path` is given, the sort is performed on the value at that path
1165
+ on each object in the array. With no `path`, the values in the array are sorted
1166
+ directly.
1167
+
1168
+ Example:
1169
+
1170
+ ```javascript
1171
+ import { mapTransform, transform, functions } from 'map-transform'
1172
+ const { sort } = functions
1173
+
1174
+ const data = {
1175
+ items: [{ id: 'ent5' }, { id: 'ent1' }, { id: 'ent3' }],
1176
+ }
1177
+
1178
+ const def35 = {
1179
+ data: ['items', transform(sort({ asc: true, path: 'id' }))],
1180
+ }
1181
+
1182
+ const ret = mapTransform(def35)(data)
1183
+ // --> { caption: 'Bergen by night. By John F.' }
1184
+ ```
1185
+
1186
+ The `sort` function is also available through a transform object:
1187
+
1188
+ ```javascript
1189
+ const def35o = {
1190
+ data: ['items', { $transform: 'sort', asc: true, path: 'id' }],
1191
+ }
1192
+ ```
1193
+
1141
1194
  #### `template(template)` function
1142
1195
 
1143
1196
  The `template` helper function takes a [handlebars] template and applies the
@@ -1154,11 +1207,11 @@ import { mapTransform, transform, functions } from 'map-transform'
1154
1207
  const { template } = functions
1155
1208
 
1156
1209
  const data = {
1157
- content: { description: 'Bergen by night', artist: 'John F.' }
1210
+ content: { description: 'Bergen by night', artist: 'John F.' },
1158
1211
  }
1159
1212
 
1160
1213
  const def30 = {
1161
- caption: ['content', transform(template('{{description}}. By {{artist}}'))]
1214
+ caption: ['content', transform(template('{{description}}. By {{artist}}'))],
1162
1215
  }
1163
1216
 
1164
1217
  const ret = mapTransform(def30)(data)
@@ -1171,8 +1224,8 @@ The `template` function is also available through a transform object:
1171
1224
  const def30o = {
1172
1225
  caption: [
1173
1226
  'content',
1174
- { $transform: 'template', template: '{{description}}. By {{artist}}' }
1175
- ]
1227
+ { $transform: 'template', template: '{{description}}. By {{artist}}' },
1228
+ ],
1176
1229
  }
1177
1230
  ```
1178
1231
 
@@ -1200,8 +1253,8 @@ const def20 = [
1200
1253
  'items',
1201
1254
  filter(validate('draft', { const: false })),
1202
1255
  {
1203
- title: 'heading'
1204
- }
1256
+ title: 'heading',
1257
+ },
1205
1258
  ]
1206
1259
  ```
1207
1260
 
@@ -1220,9 +1273,9 @@ const { compare } = functions
1220
1273
  const def21 = [
1221
1274
  {
1222
1275
  name: 'name',
1223
- role: 'role'
1276
+ role: 'role',
1224
1277
  },
1225
- filter(not(compare('role', 'admin')))
1278
+ filter(not(compare('role', 'admin'))),
1226
1279
  ]
1227
1280
  ```
1228
1281
 
@@ -1249,14 +1302,14 @@ const def22 = [
1249
1302
  'data.customers[]',
1250
1303
  {
1251
1304
  id: 'customerNo',
1252
- name: ['fullname', alt(value('Anonymous'))]
1253
- }
1305
+ name: ['fullname', alt(value('Anonymous'))],
1306
+ },
1254
1307
  ]
1255
1308
 
1256
1309
  const dataInTargetState = [
1257
1310
  { id: 'cust1', name: 'Fred Johnsen' },
1258
1311
  // { id: 'cust2', name: 'Lucy Knight' },
1259
- { id: 'cust3' }
1312
+ { id: 'cust3' },
1260
1313
  ]
1261
1314
 
1262
1315
  const dataInSourceState = mapTransform(def22).rev(dataInTargetState)
@@ -1282,15 +1335,15 @@ For example:
1282
1335
  ```javascript
1283
1336
  import { mapTransform, transform } from 'map-transform'
1284
1337
 
1285
- const username = name => name.replace(/\s+/, '.').toLowerCase()
1338
+ const username = (name) => name.replace(/\s+/, '.').toLowerCase()
1286
1339
 
1287
1340
  const def23 = [
1288
1341
  'data.customers[]',
1289
1342
  {
1290
1343
  id: 'customerNo',
1291
1344
  name: 'fullname',
1292
- 'name/1': ['username', rev(transform(username))]
1293
- }
1345
+ 'name/1': ['username', rev(transform(username))],
1346
+ },
1294
1347
  ]
1295
1348
 
1296
1349
  const dataInTargetState = [{ id: 'cust1', name: 'Fred Johnsen' }]
@@ -1329,11 +1382,11 @@ const def33flipped = {
1329
1382
  id: 'key',
1330
1383
  attributes: {
1331
1384
  title: ['headline', transform(threeLetters)],
1332
- age: ['unknown']
1385
+ age: ['unknown'],
1333
1386
  },
1334
1387
  relationships: {
1335
- author: value('johnf')
1336
- }
1388
+ author: value('johnf'),
1389
+ },
1337
1390
  }
1338
1391
  ```
1339
1392
 
@@ -1380,7 +1433,7 @@ import { mapTransform, alt, value } from 'map-transform'
1380
1433
 
1381
1434
  const def24 = {
1382
1435
  id: 'customerNo',
1383
- name: ['fullname', alt(value('Anonymous'))]
1436
+ name: ['fullname', alt(value('Anonymous'))],
1384
1437
  }
1385
1438
 
1386
1439
  const mapper = mapTransform(def24)
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ files: ['dist/**/*.test.js'],
3
+ }
package/ava.config.cjs ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ extensions: ['ts'],
3
+ require: ['ts-node/register/transpile-only'],
4
+ files: ['src/**/*.test.ts']
5
+ }
@@ -27,8 +27,8 @@ function createComparer(operator) {
27
27
  }
28
28
  }
29
29
  function compare({ path = '.', operator = '=', match, matchPath, }) {
30
- const getValue = pathGetter_1.default(path);
31
- const getMatch = matchPath ? pathGetter_1.default(matchPath) : () => match;
30
+ const getValue = (0, pathGetter_1.default)(path);
31
+ const getMatch = matchPath ? (0, pathGetter_1.default)(matchPath) : () => match;
32
32
  const comparer = createComparer(operator);
33
33
  return (data) => {
34
34
  const value = getValue(data);
@@ -1 +1 @@
1
- {"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/functions/compare.ts"],"names":[],"mappings":";;AACA,oDAAwC;AAiBxC,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,GAAG,CAAC,QAAkB,EAAE,EAAE,CAAC,CAClD,KAAc,EACd,KAAc,EACd,EAAE,CACF,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;IAClB,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAc,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxD,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;AAE5B,MAAM,SAAS,GAAG,CAAC,KAAc,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAA;AAEhF,MAAM,0BAA0B,GAAG,CAAC,QAAyB,EAAE,EAAE,CAC/D,mBAAmB,CACjB,CAAC,KAAc,EAAE,KAAc,EAAE,EAAE,CACjC,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,IAAI,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CACjE,CAAA;AAEH,MAAM,YAAY,GAAG,mBAAmB,CACtC,CAAC,KAAc,EAAE,KAAc,EAAE,EAAE,CAAC,KAAK,KAAK,KAAK,CACpD,CAAA;AAED,SAAS,cAAc,CAAC,QAAgB;IACtC,QAAQ,QAAQ,EAAE;QAChB,KAAK,GAAG;YACN,OAAO,YAAY,CAAA;QACrB,KAAK,IAAI;YACP,OAAO,GAAG,CAAC,YAAY,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AAE,EAAE,CAAC,KAAK,IAAI,KAAK,CACjD,CAAA;QACH,KAAK,GAAG;YACN,OAAO,0BAA0B,CAC/B,CAAC,KAAa,EAAE,KAAa,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,CAChD,CAAA;QACH,KAAK,IAAI;YACP,OAAO,0BAA0B,CAC/B,CAAC,KAAa,EAAE,KAAa,EAAE,EAAE,CAAC,KAAK,IAAI,KAAK,CACjD,CAAA;QACH;YACE,OAAO,CAAC,MAAe,EAAE,MAAe,EAAE,EAAE,CAAC,KAAK,CAAA;KACrD;AACH,CAAC;AAED,SAAwB,OAAO,CAAC,EAC9B,IAAI,GAAG,GAAG,EACV,QAAQ,GAAG,GAAG,EACd,KAAK,EACL,SAAS,GACO;IAChB,MAAM,QAAQ,GAAG,IAAA,oBAAM,EAAC,IAAI,CAAC,CAAA;IAC7B,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,IAAA,oBAAM,EAAC,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAA;IAE5D,MAAM,QAAQ,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAA;IAEzC,OAAO,CAAC,IAAI,EAAE,EAAE;QACd,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;QAC5B,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;QAC5B,OAAO,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IAC/B,CAAC,CAAA;AACH,CAAC;AAhBD,0BAgBC"}
@@ -3,4 +3,5 @@ export interface KeyValue {
3
3
  key: string | number;
4
4
  value: unknown;
5
5
  }
6
- export default function explode(): DataMapper;
6
+ export declare function explode(): DataMapper;
7
+ export declare function implode(): DataMapper;
@@ -1,9 +1,11 @@
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.every((item) => is_1.isObject(item) && typeof item.key === 'number');
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
- if (is_1.isObject(keyValue)) {
8
+ if ((0, is_1.isObject)(keyValue)) {
7
9
  const { key, value } = keyValue;
8
10
  if (Array.isArray(target)) {
9
11
  target[key] = value;
@@ -25,8 +27,10 @@ function doImplode(data) {
25
27
  }
26
28
  }
27
29
  function doExplode(data) {
28
- if (is_1.isObject(data)) {
29
- return Object.entries(data).map(([key, value]) => ({
30
+ if ((0, is_1.isObject)(data)) {
31
+ return Object.entries(data)
32
+ .filter(([, value]) => value !== undefined)
33
+ .map(([key, value]) => ({
30
34
  key,
31
35
  value,
32
36
  }));
@@ -41,5 +45,9 @@ function doExplode(data) {
41
45
  function explode() {
42
46
  return (data, context) => (context.rev ? doImplode(data) : doExplode(data));
43
47
  }
44
- exports.default = explode;
48
+ exports.explode = explode;
49
+ function implode() {
50
+ return (data, context) => (context.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":";;AAEA,oCAAsC;AAOtC,MAAM,eAAe,GAAG,CAAC,IAAe,EAAE,EAAE,CAC1C,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAQ,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,GAAG,KAAK,QAAQ,CAAC,CAAA;AAEtE,MAAM,aAAa,GAAG,CACpB,MAA2C,EAC3C,QAAiB,EACjB,EAAE;IACF,IAAI,aAAQ,CAAC,QAAQ,CAAC,EAAE;QACtB,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,QAAQ,CAAA;QAC/B,IAAI,KAAK,CAAC,OAAO,CAAC,M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,CAAC;YACzC,GAAG;YACH,KAAK;SACN,CAAC,CAAC,CAAA;KACN;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QAC9B,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,KAAc,EAAE,GAAW,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC,CAAA;KACnE;SAAM;QACL,OAAO,SAAS,CAAA;KACjB;AACH,CAAC;AAED,SAAgB,OAAO;IACrB,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;AAC7E,CAAC;AAFD,0BAEC;AAED,SAAgB,OAAO;IACrB,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;AAC7E,CAAC;AAFD,0BAEC"}
@@ -4,7 +4,7 @@ const pathGetter_1 = require("../utils/pathGetter");
4
4
  const extractPath = (path) => typeof path === 'string' ? path : path.path;
5
5
  function get(options) {
6
6
  const path = extractPath(options) || '.';
7
- const getFn = pathGetter_1.default(path);
7
+ const getFn = (0, pathGetter_1.default)(path);
8
8
  return (data) => getFn(data);
9
9
  }
10
10
  exports.default = get;
@@ -1 +1 @@
1
- {"version":3,"file":"get.js","sourceRoot":"","sources":["../../src/functions/get.ts"],"names":[],"mappings":";;AACA,oDAAwC;AAMxC,MAAM,WAAW,GAAG,CAAC,IAAsB,EAAE,EAAE,CAC7C,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;AAE7C,SAAwB,GAAG,CAAC,OAAyB;IACnD,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,GAAG,CAAA;IACxC,MAAM,KAAK,GAAG,oBAAM,CAAC,IAAI,CAAC,CAAA;IAE1B,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AAC9B,CAAC;AALD,sBAKC"}
1
+ {"version":3,"file":"get.js","sourceRoot":"","sources":["../../src/functions/get.ts"],"names":[],"mappings":";;AACA,oDAAwC;AAMxC,MAAM,WAAW,GAAG,CAAC,IAAsB,EAAE,EAAE,CAC7C,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;AAE7C,SAAwB,GAAG,CAAC,OAAyB;IACnD,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,GAAG,CAAA;IACxC,MAAM,KAAK,GAAG,IAAA,oBAAM,EAAC,IAAI,CAAC,CAAA;IAE1B,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AAC9B,CAAC;AALD,sBAKC"}