map-transform 0.5.6 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +130 -53
  2. package/dist/index.d.ts +2 -2
  3. package/dist/index.js +2 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/operations/alt.js +21 -23
  6. package/dist/operations/alt.js.map +1 -1
  7. package/dist/operations/apply.js +8 -5
  8. package/dist/operations/apply.js.map +1 -1
  9. package/dist/operations/concat.d.ts +2 -1
  10. package/dist/operations/concat.js +34 -11
  11. package/dist/operations/concat.js.map +1 -1
  12. package/dist/operations/directionals.js +4 -4
  13. package/dist/operations/directionals.js.map +1 -1
  14. package/dist/operations/getSet.js +58 -28
  15. package/dist/operations/getSet.js.map +1 -1
  16. package/dist/operations/lookup.d.ts +3 -1
  17. package/dist/operations/lookup.js +27 -31
  18. package/dist/operations/lookup.js.map +1 -1
  19. package/dist/operations/merge.d.ts +2 -1
  20. package/dist/operations/merge.js +1 -1
  21. package/dist/operations/merge.js.map +1 -1
  22. package/dist/operations/modify.js +4 -7
  23. package/dist/operations/modify.js.map +1 -1
  24. package/dist/operations/pipe.js +4 -4
  25. package/dist/operations/pipe.js.map +1 -1
  26. package/dist/operations/props.js +103 -67
  27. package/dist/operations/props.js.map +1 -1
  28. package/dist/operations/transform.js +6 -12
  29. package/dist/operations/transform.js.map +1 -1
  30. package/dist/transformers/compare.js +17 -8
  31. package/dist/transformers/compare.js.map +1 -1
  32. package/dist/transformers/explode.d.ts +2 -3
  33. package/dist/transformers/explode.js +6 -7
  34. package/dist/transformers/explode.js.map +1 -1
  35. package/dist/transformers/index.d.ts +1 -0
  36. package/dist/transformers/index.js +2 -1
  37. package/dist/transformers/index.js.map +1 -1
  38. package/dist/transformers/merge.d.ts +2 -2
  39. package/dist/transformers/merge.js +9 -4
  40. package/dist/transformers/merge.js.map +1 -1
  41. package/dist/transformers/sort.js +5 -5
  42. package/dist/transformers/sort.js.map +1 -1
  43. package/dist/utils/definitionHelpers.js +4 -4
  44. package/dist/utils/definitionHelpers.js.map +1 -1
  45. package/dist/utils/is.d.ts +2 -0
  46. package/dist/utils/is.js +2 -0
  47. package/dist/utils/is.js.map +1 -1
  48. package/dist/utils/modifyOperationObject.js +9 -10
  49. package/dist/utils/modifyOperationObject.js.map +1 -1
  50. package/dist/utils/stateHelpers.d.ts +26 -3
  51. package/dist/utils/stateHelpers.js +14 -7
  52. package/dist/utils/stateHelpers.js.map +1 -1
  53. package/package.json +4 -4
package/README.md CHANGED
@@ -46,7 +46,7 @@ npm install map-transform
46
46
  - Map objects won't be mapped over an array by default. You have to specify
47
47
  `$iterate: true`
48
48
  - The `alt` operation now accepts any type of pipeline, but not a helper
49
- function, and all alternative pipelines must be given as arguments to `alt()`
49
+ function, and all alternative pipelines must be given as arguments to `alt`
50
50
  - The root path prefix is changed from `$` to `^^`
51
51
  - The `.rev()` method on `mapTransform()` has been removed, and instead you pass
52
52
  in `{ rev: true }` as the second argument to the regular method
@@ -422,7 +422,7 @@ const def6 = [
422
422
  custom transformers for this case, but their implementations are not provided.)
423
423
 
424
424
  **A note on arrays:** In a transform pipeline, the default behavior is to treat
425
- an array as any other data. The array will be passed on to a `transform()`
425
+ an array as any other data. The array will be passed on to a `transform`
426
426
  operation, the entire array will be set on a path, etc. This also means that a
427
427
  mapping object will be applied to the entire array if nothing else is specified.
428
428
  In the example above, we have set `$iterate: true` on the mapping object, to
@@ -551,7 +551,7 @@ Operations may be used as steps in a transform pipeline.
551
551
 
552
552
  #### `transform(transformFn, transformFnRev)` operation
553
553
 
554
- The simple beauty of the `transform()` operation, is that it will apply whatever
554
+ The simple beauty of the `transform` operation, is that it will apply whatever
555
555
  function (transformer) you provide it with to the data at that point in the
556
556
  pipeline. It's up to you to write the function that does the transformation – or
557
557
  use one of [the transformers that comes with MapTransform](#transformers).
@@ -559,7 +559,7 @@ use one of [the transformers that comes with MapTransform](#transformers).
559
559
  You may supply a second transformer (`transformFnRev`), that will be used when
560
560
  [reverse mapping](#reverse-mapping). If you only supplies one transformer, it
561
561
  will be used in both directions. You may supply `null` for either of these, to
562
- make it uni-directional, but it might be clearer to use `fwd()` or `rev()`
562
+ make it uni-directional, but it might be clearer to use `fwd` or `rev`
563
563
  operations for this.
564
564
 
565
565
  The transformers you write for the transform operation are a function that
@@ -754,8 +754,8 @@ value from your transformer will simply mean that it is replaced with
754
754
  `undefined`.
755
755
 
756
756
  The filter operation only accepts one argument, which is applied in both
757
- directions through the pipeline. You'll have to use `fwd()` or `rev()`
758
- operations to make it uni-directional.
757
+ directions through the pipeline. You'll have to use `fwd` or `rev` operations to
758
+ make it uni-directional.
759
759
 
760
760
  Transformers passed to the filter operation should also be pure, but
761
761
  could, when it is expected and absolutely necessary, rely on anything outside
@@ -803,14 +803,14 @@ You may also set `$direction: 'fwd'` or `$direction: 'rev'` on the object, to
803
803
  have it filter in one direction only.
804
804
 
805
805
  See
806
- [the `transform()` operation](#transformtransformfn-transformfnrev-operation)
807
- for more on how defining as an object works.
806
+ [the `transform` operation](#transformtransformfn-transformfnrev-operation) for
807
+ more on how defining as an object works.
808
808
 
809
809
  #### `ifelse(conditionFn, truePipeline, falsePipeline)` operation
810
810
 
811
- The `ifelse()` operation will run the `truePipeline` if the `conditionFn`
812
- results in something truthy, JavaScript style, otherwise it will run the
813
- `falsePipeline`. See [the `filter()` operation](#filterconditionFn-operation)
811
+ The `ifelse` operation will run the `truePipeline` if the `conditionFn` results
812
+ in something truthy, JavaScript style, otherwise it will run the
813
+ `falsePipeline`. See [the `filter` operation](#filterconditionFn-operation)
814
814
  for more on the requirements for the `conditionFn`.
815
815
 
816
816
  Both `truePipeline` and `falsePipeline` are optional, in case you only need to
@@ -1015,15 +1015,15 @@ order, with the last being run first. The first pipeline is always run, though,
1015
1015
  as it is common practice to let the first be a `get` that acts like a `set` in
1016
1016
  reverse. This may be confusing, but will usually just be naturally when you
1017
1017
  don't think too much about it. See
1018
- [the `get()` and `set()` operations](#getpath-and-setpath-operation) for more on
1019
- how `get` works in reverse.
1018
+ [the `get` and `set` operations](#getpath-and-setpath-operation) for more on how
1019
+ `get` works in reverse.
1020
1020
 
1021
1021
  `alt` will behave a bit differently when you give only one pipeline: The
1022
1022
  pipeline will be run if the curent value is `undefined`, but skipped otherwise.
1023
1023
  This is different from the multi-pipeline behavor, where the first is always run
1024
1024
  and the rest is only run if the previous returns `undefined`.
1025
1025
 
1026
- You may also define an alt operation as an object:
1026
+ You may also define an alt operation as an operation object:
1027
1027
 
1028
1028
  ```javascript
1029
1029
  const def11asObject = {
@@ -1046,33 +1046,66 @@ direction only.
1046
1046
 
1047
1047
  #### `concat(pipeline, pipeline, ...)` operation
1048
1048
 
1049
- The `concat()` operation will flatten the result of every pipeline it is given
1049
+ The `concat` operation will flatten the result of every pipeline it is given
1050
1050
  into one array. A pipeline that does not return an array will simple have its
1051
- return value appended to the array.
1051
+ return value appended to the array. Even when there's only one pipeline, its
1052
+ value will be forced to an array. `undefined` will be filtered away from the
1053
+ returned array.
1052
1054
 
1053
- This operation will always return an array, even when it is given only one
1054
- pipeline that does not return an array. Pipelines that does not result in a
1055
- value (i.e. return `undefined`) will be filtered away.
1055
+ In reverse, the value (array) will be set on the first pipeline, and the rest of
1056
+ the pipelines will be given an empty array. The results of all the pipelines
1057
+ will be merged.
1058
+
1059
+ If `concat` is not given any pipelines, it will return an empty array going
1060
+ forward, and an empty object in reverse. The reason for the empty object is that
1061
+ the normal behavior for concat is to get with paths from an object, and with
1062
+ no paths, we can't set any props, so an empty object is the best we can do.
1063
+
1064
+ > **Note:** This operation is destructive, in that the result from running it
1065
+ > forward cannot reproduce the original data when run in reverse. Only the data
1066
+ > fetched by the given pipelines will be preserved, and the merged arrays cannot
1067
+ > be unmerged.
1056
1068
 
1057
1069
  ```javascript
1058
- {
1059
- $concat: ['data.users', 'data.admins']
1070
+ import { concat } from 'map-transform'
1071
+
1072
+ const def39 = {
1073
+ id: 'data.id',
1074
+ users: concat('data.users', 'data.admins'),
1060
1075
  }
1061
1076
  ```
1062
1077
 
1063
- > Editors note: We need more examples here.
1078
+ You may also define a concat operation as an operation object:
1079
+
1080
+ ```javascript
1081
+ const def39asObject = {
1082
+ id: 'data.id',
1083
+ users: { $concat: ['data.users', 'data.admins'] },
1084
+ }
1085
+ ```
1086
+
1087
+ #### `concatRev(pipeline, pipeline, ...)` operation
1088
+
1089
+ The `concatRev` operation is the exact oposite of the `concat` operation,
1090
+ meaning that it will exhibit the same behavior in reverse as `concat` does
1091
+ going forward, and vice versa. See the description of
1092
+ [the `concat` operation](#concatpipeline-pipeline--operation) for more
1093
+ details.
1094
+
1095
+ Note that `concatRev` does not have an operation object notation, but `concat`
1096
+ will honor the [flipped mode](#flipping-a-transform-object).
1064
1097
 
1065
1098
  #### `merge(pipeline, pipeline, ...)` operation
1066
1099
 
1067
- `merge()` will run all given pipelines and deep merge their results. Conflicts
1068
- are resolved by prioritizing results from the rightmost of the conflicting
1100
+ `merge` will run all given pipelines and deep merge their results. Conflicts are
1101
+ resolved by prioritizing results from the rightmost of the conflicting
1069
1102
  pipelines.
1070
1103
 
1071
1104
  > Editors note: We need examples here.
1072
1105
 
1073
1106
  #### `modify(pipeline)` operation
1074
1107
 
1075
- Use the `modify()` operation when you want the pipeline to modify an object,
1108
+ Use the `modify` operation when you want the pipeline to modify an object,
1076
1109
  instead of replacing it.
1077
1110
 
1078
1111
  Example:
@@ -1104,7 +1137,7 @@ const response = {
1104
1137
  }
1105
1138
  ```
1106
1139
 
1107
- Had we ran this without the `modify()` operation, the returned object would only
1140
+ Had we ran this without the `modify` operation, the returned object would only
1108
1141
  have the `data` prop, as no props from the source data will be set in the target
1109
1142
  data, unless they are "picked up" by dot notation paths.
1110
1143
 
@@ -1129,6 +1162,24 @@ const def34c = {
1129
1162
  }
1130
1163
  ```
1131
1164
 
1165
+ The `$modify` flag may also be set on a path:
1166
+
1167
+ ```javascript
1168
+ const def34d = {
1169
+ 'content.$modify': 'response',
1170
+ 'content.data': 'response.data.deeply.placed.items',
1171
+ }
1172
+ ```
1173
+
1174
+ This is the way to set it for reverse direction:
1175
+
1176
+ ```javascript
1177
+ const def34e = {
1178
+ response: '$modify',
1179
+ 'response.data.deeply.placed.items': 'data',
1180
+ }
1181
+ ```
1182
+
1132
1183
  Note that setting a path like this, is only available when the `modify`
1133
1184
  operation is defined as an operation object.
1134
1185
 
@@ -1136,10 +1187,10 @@ operation is defined as an operation object.
1136
1187
 
1137
1188
  All operations in MapTransform will apply in both directions, although some of
1138
1189
  them may behave a bit different dependending on the direction. If you want an
1139
- operation to only apply in one direction, you need to wrap it in a `fwd()` or
1140
- `rev()` operation. The `fwd()` operation will only apply its pipeline when we're
1190
+ operation to only apply in one direction, you need to wrap it in a `fwd` or
1191
+ `rev` operation. The `fwd` operation will only apply its pipeline when we're
1141
1192
  going forward, i.e. mapping in the normal direction, and its pipeline will be
1142
- skipped when we're mapping in reverse. The `rev()` operation will only apply its
1193
+ skipped when we're mapping in reverse. The `rev` operation will only apply its
1143
1194
  pipeline when we're mapping in reverse.
1144
1195
 
1145
1196
  The value in the pipeline will be untouched when we are encountering an
@@ -1174,15 +1225,15 @@ at the description of
1174
1225
 
1175
1226
  #### `divide(fwdPipeline, revPipeline)` operation
1176
1227
 
1177
- `divide()` is `fwd()` and `rev()` operations combined, where the first argument
1178
- is a pipeline to use when going forward and the second when going in reverse.
1228
+ `divide` is `fwd` and `rev` operations combined, where the first argument is a
1229
+ pipeline to use when going forward and the second when going in reverse.
1179
1230
 
1180
- See [`fwd()` and `rev()`](#fwdpipeline-and-revpipeline-operation) for more details.
1231
+ See [`fwd` and `rev`](#fwdpipeline-and-revpipeline-operation) for more details.
1181
1232
 
1182
1233
  #### `get(path)` and `set(path)` operation
1183
1234
 
1184
- Both the `get()` and `set()` operations accepts a dot notation path to act on.
1185
- The get operation will pull the data at the path from the data currently in the
1235
+ Both the `get` and `set` operations accepts a dot notation path to act on. The
1236
+ get operation will pull the data at the path from the data currently in the
1186
1237
  pipeline, and replace the value in the pipeline with it. The set operation will
1187
1238
  take what ever's in the pipeline and set it on the given path at a new object.
1188
1239
 
@@ -1285,23 +1336,33 @@ const def16b = divide(root('meta.section'), plug())
1285
1336
 
1286
1337
  #### `plug()` operation
1287
1338
 
1288
- The `plug()` operation simply clears the value in the pipeline - it plugs it.
1339
+ The `plug` operation simply clears the value in the pipeline - it plugs it.
1289
1340
  The value will be set to `undefined` regardless of what has happened before that
1290
- point. Any `alt()` operations etc. coming after the plug will still have an
1341
+ point. Any `alt` operations etc. coming after the plug will still have an
1291
1342
  effect.
1292
1343
 
1293
1344
  This main use case for this is to clear the value going one way. E.g. if you
1294
1345
  need a value when you map in reverse, but don't want it going forward, plug it
1295
1346
  with `fwd(plug())`. You will also need it in a pipeline where the only operation
1296
- is uni-directional (i.e. using `fwd()` or `rev()`). An empty pipeline (which is
1297
- what a uni-directional pipeline will be in the other direction), will return
1298
- the data you give it, which is usually not what you want in these cases.
1299
- The solution is to plug it in the other direction.
1347
+ is uni-directional (i.e. using `fwd` or `rev`). An empty pipeline (which is what
1348
+ a uni-directional pipeline will be in the other direction), will return the data
1349
+ you give it, which is usually not what you want in these cases. The solution is
1350
+ to plug it in the other direction.
1300
1351
 
1301
1352
  You could have accomplished the same with `transform(value(undefined))`, but
1302
- this will not work when `state.noDefaults` is `true`. `plug()` will do its trick
1353
+ this will not work when `state.noDefaults` is `true`. `plug` will do its trick
1303
1354
  in all cases.
1304
1355
 
1356
+ #### `lookdown({ arrayPath, propPath, matchSeveral })` operation
1357
+
1358
+ The `lookdown` operation is the exact oposite of `lookup`, and the name is
1359
+ marelly word-play on that. See
1360
+ [the `lookup` operation](#lookup-arraypath-proppath-matchseveral-operation)
1361
+ for more on how it works, just reverse the directions.
1362
+
1363
+ Note that `lookdown` does not have an operation object notation, but `lookup`
1364
+ will honor the [flipped mode](#flipping-a-transform-object).
1365
+
1305
1366
  #### `lookup({ arrayPath, propPath, matchSeveral })` operation
1306
1367
 
1307
1368
  `lookup` will take the value in the pipeline and replace it with the first
@@ -1312,10 +1373,11 @@ returned. Default is `false`.
1312
1373
 
1313
1374
  In reverse, the `propPath` will simply be used as a get path, getting the prop
1314
1375
  of the objects out of the objects, so to speak. (In the future, MapTransform
1315
- _might_ support setting the items back on the `arrayPath` in reverse.) The
1316
- exception to this rule, is that `lookup` will behave as if in forward mode, when
1317
- it's called in a transform object in
1318
- [flipped mode](#flipping-a-transform-object).
1376
+ _might_ support setting the items back on the `arrayPath` in reverse.)
1377
+
1378
+ > **Note:** When `lookup` is called within a transform object in
1379
+ > [flipped mode](#flipping-a-transform-object), it will behave in the opposite
1380
+ > way, looking up in reverse mode and extracting `propPath` going forward.
1319
1381
 
1320
1382
  Example:
1321
1383
 
@@ -1362,8 +1424,8 @@ values in the pipeline.
1362
1424
 
1363
1425
  This is a transformer intended for use with the `filter` operation. You
1364
1426
  pass a dot notation `path` and a `match` value (string, number, boolean, `null`
1365
- or `undefined`) to `compare()`, and it returns a function that you can pass to
1366
- `filter()` for filtering away data that does not not have the value set at the
1427
+ or `undefined`) to `compare`, and it returns a function that you can pass to
1428
+ `filter` for filtering away data that does not not have the value set at the
1367
1429
  provided path.
1368
1430
 
1369
1431
  As an alternative to `match`, you may specify a `matchPath`, which is a dot
@@ -1483,7 +1545,7 @@ When used outside of an iteration, it always returns `0`.
1483
1545
 
1484
1546
  This is the exact opposite of the `explode` helper, imploding going forward and
1485
1547
  exploding in reverse. See
1486
- [the documentation for `explode()`](#explode-transformer) for how this works.
1548
+ [the documentation for `explode`](#explode-transformer) for how this works.
1487
1549
 
1488
1550
  #### `logical({ path, operator })` transformer
1489
1551
 
@@ -1590,6 +1652,15 @@ one object. Merging happens from left to right, so the props of the last object
1590
1652
  will have priority. However, `undefined` values will never overwrite another
1591
1653
  value.
1592
1654
 
1655
+ In reverse, the pipeline data will be provided to every pipeline in `path`, as
1656
+ there is no way of splitting up the "original" data. In most cases the pipeline
1657
+ data will be set on the props they were "originally" fetched and merged from.
1658
+
1659
+ > **Note:** This transformer is destructive, in that the result from running it
1660
+ > forward cannot reproduce the original data when run in reverse. Only the data
1661
+ > fetched by the given pipelines will be preserved, and the merged object cannot
1662
+ > be unmerged.
1663
+
1593
1664
  ```javascript
1594
1665
  import mapTransform, { transform, transformers } from 'map-transform'
1595
1666
  const { merge } = transformers
@@ -1616,10 +1687,16 @@ const def38b = {
1616
1687
  }
1617
1688
  ```
1618
1689
 
1690
+ #### `mergeRev({path})` transformer
1691
+
1692
+ The `mergeRev` transformer has the opposite behavior of the `merge` transformer,
1693
+ in that it will do forward what `merge` does in reverse, and vice versa. See
1694
+ [the `merge` transformer](#mergepath-transformer) for more details.
1695
+
1619
1696
  #### `not(value)` transformer
1620
1697
 
1621
- `not()` will return `false` when the value in the pipeline is truthy, and `true`
1622
- when value is falsy. This is useful for making the `filter()` operation do the
1698
+ `not` will return `false` when the value in the pipeline is truthy, and `true`
1699
+ when value is falsy. This is useful for making the `filter` operation do the
1623
1700
  opposite of what the filter transformer implies.
1624
1701
 
1625
1702
  Here we filter _away_ all data where role is set to 'admin':
@@ -1878,13 +1955,13 @@ same goes for flipped transform objects if you want to forward transform.
1878
1955
 
1879
1956
  MapTransform will try its best to map the data to the shape you want, and will
1880
1957
  always set all properties, even though the mapping you defined result in
1881
- `undefined`. You may include `alt()` operations to provide default or fallback
1958
+ `undefined`. You may include `alt` operations to provide default or fallback
1882
1959
  values for these cases.
1883
1960
 
1884
1961
  But sometimes, you only want the data that is actually present in the source
1885
1962
  data, without defaults or properties set to `undefined`. You may accomplish this
1886
1963
  by setting `state.noDefaults` to true, either by setting in on the initial state
1887
- given to `mapTransform()()` or by setting the `$noDefaults` flag on a transform
1964
+ given to `mapTransform()` or by setting the `$noDefaults` flag on a transform
1888
1965
  object (will set `noDefaults` on the state for everything happening within that
1889
1966
  transform object).
1890
1967
 
@@ -1966,7 +2043,7 @@ versions, and should not be used in custom transformers:
1966
2043
  - `target`: The target object at the current point. When setting on a path, the
1967
2044
  setting will happen on this target.
1968
2045
 
1969
- Note that you may provide the `mapTransform()()` function with an initial state
2046
+ Note that you may provide the `mapTransform()` function with an initial state
1970
2047
  object as its second argument. Only `rev`, `noDefaults`, and `target` will be
1971
2048
  passed on from the state object you provide.
1972
2049
 
package/dist/index.d.ts CHANGED
@@ -2,13 +2,13 @@ import type { TransformDefinition, DataMapper, Options, InitialState } from './t
2
2
  import transformers from './transformers/index.js';
3
3
  import iterate from './operations/iterate.js';
4
4
  export { default as apply } from './operations/apply.js';
5
- export { default as concat } from './operations/concat.js';
5
+ export { concat, concatRev } from './operations/concat.js';
6
6
  export { default as alt } from './operations/alt.js';
7
7
  export { fwd, rev, divide } from './operations/directionals.js';
8
8
  export { default as filter } from './operations/filter.js';
9
9
  export { get, set } from './operations/getSet.js';
10
10
  export { default as ifelse } from './operations/ifelse.js';
11
- export { default as lookup } from './operations/lookup.js';
11
+ export { lookup, lookdown } from './operations/lookup.js';
12
12
  export { default as merge } from './operations/merge.js';
13
13
  export { default as modify } from './operations/modify.js';
14
14
  export { default as plug } from './operations/plug.js';
package/dist/index.js CHANGED
@@ -3,13 +3,13 @@ import { populateState, getStateValue, noopNext } from './utils/stateHelpers.js'
3
3
  import transformers from './transformers/index.js';
4
4
  import iterate from './operations/iterate.js';
5
5
  export { default as apply } from './operations/apply.js';
6
- export { default as concat } from './operations/concat.js';
6
+ export { concat, concatRev } from './operations/concat.js';
7
7
  export { default as alt } from './operations/alt.js';
8
8
  export { fwd, rev, divide } from './operations/directionals.js';
9
9
  export { default as filter } from './operations/filter.js';
10
10
  export { get, set } from './operations/getSet.js';
11
11
  export { default as ifelse } from './operations/ifelse.js';
12
- export { default as lookup } from './operations/lookup.js';
12
+ export { lookup, lookdown } from './operations/lookup.js';
13
13
  export { default as merge } from './operations/merge.js';
14
14
  export { default as modify } from './operations/modify.js';
15
15
  export { default as plug } from './operations/plug.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[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AA;AAC1D,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,CAAA;AAEhC,MAAM,YAAY,GAAG,CAAC,OAAgB,EAAE,EAAE,CAAC,CAAC;IAC1C,GAAG,OAAO;IACV,YAAY,EAAE;QACZ,GAAG,YAAY;QACf,GAAG,CAAC,OAAO,CAAC,YAAY,IAAI,EAAE,CAAC;KAChC;CACF,CAAC,CAAA;AAQF,MAAM,CAAC,OAAO,UAAU,YAAY,CAClC,GAAwB,EACxB,UAAmB,EAAE;IAErB,MAAM,eAAe,GAAG,YAAY,CAAC,OAAO,CAAC,CAAA;IAC7C,MAAM,WAAW,GAAG,cAAc,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,eAAe,CAAC,CAAC,QAAQ,CAAC,CAAA;IAE3E,OAAO,KAAK,UAAU,SAAS,CAAC,IAAI,EAAE,YAAY;QAChD,MAAM,SAAS,GAAG,MAAM,WAAW,CAAC,aAAa,CAAC,IAAI,EAAE,YAAY,IAAI,EAAE,CAAC,CAAC,CAAA;QAC5E,OAAO,aAAa,CAAC,SAAS,CAAC,CAAA;IACjC,CAAC,CAAA;AACH,CAAC"}
@@ -1,37 +1,35 @@
1
1
  import pipe from './pipe.js';
2
- import { setStateValue, getLastContext, isNonvalueState, setValueFromState, removeLastContext, } from '../utils/stateHelpers.js';
2
+ import { setValueFromState, popContext, isNonvalueState, setStateValue, } from '../utils/stateHelpers.js';
3
3
  import { defToOperations } from '../utils/definitionHelpers.js';
4
4
  import { noopNext } from '../utils/stateHelpers.js';
5
5
  const pipeIfArray = (def) => Array.isArray(def) ? pipe(def, true) : def;
6
- function createOneAltOperation(def, index, isSingleMode) {
6
+ const isUntouched = (afterState, beforeState) => beforeState.value === afterState.value;
7
+ async function runAltPipeline(pipeline, state) {
8
+ const afterState = await pipeline(state);
9
+ return isUntouched(afterState, state)
10
+ ? setStateValue(afterState, undefined)
11
+ : afterState;
12
+ }
13
+ function createOneAltPipeline(def, index, hasOnlyOneAlt) {
7
14
  return (options) => {
8
- const operation = pipeIfArray(defToOperations(def, options));
15
+ const pipeline = pipeIfArray(defToOperations(def, options))(options)(noopNext);
16
+ const isFirst = !hasOnlyOneAlt && index === 0;
17
+ const { nonvalues } = options;
9
18
  return (next) => async (state) => {
10
19
  const nextState = await next(state);
11
- const { nonvalues } = options;
12
- const isFirst = !isSingleMode && index === 0;
13
- if (isFirst) {
14
- const thisState = await operation(options)(noopNext)(nextState);
15
- return isNonvalueState(thisState, nonvalues)
16
- ? { ...thisState, context: [...nextState.context, nextState.value] }
17
- : thisState;
18
- }
19
- else {
20
- if (isNonvalueState(nextState, nonvalues)) {
21
- const thisState = await operation(options)(noopNext)(removeLastContext(setStateValue(nextState, getLastContext(nextState))));
22
- return isNonvalueState(thisState, nonvalues)
23
- ? setValueFromState(nextState, thisState)
24
- : thisState;
25
- }
26
- else {
27
- return nextState;
28
- }
20
+ if (!isFirst && !isNonvalueState(nextState, nonvalues)) {
21
+ return nextState;
29
22
  }
23
+ const beforeState = isFirst ? nextState : popContext(nextState);
24
+ const afterState = await runAltPipeline(pipeline, beforeState);
25
+ return isNonvalueState(afterState, nonvalues)
26
+ ? setValueFromState(nextState, afterState, isFirst)
27
+ : afterState;
30
28
  };
31
29
  };
32
30
  }
33
31
  export default function alt(...defs) {
34
- const isSingleMode = defs.length === 1;
35
- return defs.map((def, index) => createOneAltOperation(def, index, isSingleMode));
32
+ const hasOnlyOneAlt = defs.length === 1;
33
+ return defs.map((def, index) => createOneAltPipeline(def, index, hasOnlyOneAlt));
36
34
  }
37
35
  //# sourceMappingURL=alt.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"alt.js","sourceRoot":"","sources":["../../src/operations/alt.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EACL,aAAa,EACb,cAAc,EACd,eAAe,EACf,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAKnD,MAAM,WAAW,GAAG,CAAC,GAA4B,EAAE,EAAE,CACnD,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;AAE5C,SAAS,qBAAqB,CAC5B,GAAwB,EACxB,KAAa,EACb,YAAqB;IAErB,OAAO,CAAC,OAAO,EAAE,EAAE;QAEjB,MAAM,SAAS,GAAG,WAAW,CAAC,eAAe,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAA;QAE5D,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YAC/B,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,CAAA;YACnC,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;YAC7B,MAAM,OAAO,GAAG,CAAC,YAAY,IAAI,KAAK,KAAK,CAAC,CAAA;YAE5C,IAAI,OAAO,EAAE;gBACX,MAAM,SAAS,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAA;gBAC/D,OAAO,eAAe,CAAC,SAAS,EAAE,SAAS,CAAC;oBAC1C,CA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CAAC,CAAA;YAC9D,OAAO,eAAe,CAAC,UAAU,EAAE,SAAS,CAAC;gBAC3C,CAAC,CAAC,iBAAiB,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC;gBACnD,CAAC,CAAC,UAAU,CAAA;QAChB,CAAC,CAAA;IACH,CAAC,CAAA;AACH,CAAC;AASD,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,GAAG,IAA2B;IACxD,MAAM,aAAa,GAAG,IAAI,CAAC,MAAM,KAAK,CAAC,CAAA;IACvC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAC7B,oBAAoB,CAAC,GAAG,EAAE,KAAK,EAAE,aAAa,CAAC,CAChD,CAAA;AACH,CAAC"}
@@ -10,6 +10,13 @@ function setPipeline(id, operation, options) {
10
10
  }
11
11
  }
12
12
  const removeFlip = ({ flip, ...state }) => state;
13
+ function prepareAndSetPipeline(pipelineId, pipeline, options) {
14
+ if (typeof pipeline !== 'function' && pipeline) {
15
+ setPipeline(pipelineId, () => () => noopNext, options);
16
+ const operation = defToOperation(pipeline, options)(options);
17
+ setPipeline(pipelineId, () => operation, options);
18
+ }
19
+ }
13
20
  export default function apply(pipelineId) {
14
21
  return (options) => {
15
22
  const pipeline = getPipeline(pipelineId, options);
@@ -19,11 +26,7 @@ export default function apply(pipelineId) {
19
26
  : 'Failed to apply pipeline. No id provided';
20
27
  throw new Error(message);
21
28
  }
22
- if (typeof pipeline !== 'function' && pipeline) {
23
- setPipeline(pipelineId, () => () => noopNext, options);
24
- const operation = defToOperation(pipeline, options)(options);
25
- setPipeline(pipelineId, () => operation, options);
26
- }
29
+ prepareAndSetPipeline(pipelineId, pipeline, options);
27
30
  return (next) => {
28
31
  const operation = getPipeline(pipelineId, options);
29
32
  const fn = typeof operation === 'function'
@@ -1 +1 @@
1
- {"version":3,"file":"apply.js","sourceRoot":"","sources":["../../src/operations/apply.ts"],"names":[],"mappings":"AACA,OAAO,EAAE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IN,WAAW,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,OAAO,CAAC,CAAA;aACjD;YAED,OAAO,KAAK,EAAE,KAAK,EAAE,EAAE;gBACrB,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,CAAA;gBACnC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;YACnD,CAAC,CAAA;QACH,CAAC,CAAA;IACH,CAAC,CAAA;AACH,CAAC"}
@@ -1,2 +1,3 @@
1
1
  import type { Operation, TransformDefinition } from '../types.js';
2
- export default function concat(...defs: TransformDefinition[]): Operation;
2
+ export declare function concat(...defs: TransformDefinition[]): Operation;
3
+ export declare function concatRev(...defs: TransformDefinition[]): Operation;
@@ -1,19 +1,42 @@
1
- import { setStateValue, getStateValue } from '../utils/stateHelpers.js';
1
+ import { mergeStates } from './merge.js';
2
+ import { setStateValue, getStateValue, revFromState, flipState, } from '../utils/stateHelpers.js';
2
3
  import { defToOperation } from '../utils/definitionHelpers.js';
3
4
  import { noopNext } from '../utils/stateHelpers.js';
4
5
  const merge = (left, right) => Array.isArray(right) ? [...left, ...right] : [...left, right];
5
- export default function concat(...defs) {
6
- return (options) => (next) => {
6
+ async function getAndMergeArrays(state, fns) {
7
+ let nextValue = [];
8
+ for (const fn of fns) {
9
+ const value = getStateValue(await fn(state));
10
+ nextValue = merge(nextValue, value);
11
+ }
12
+ return setStateValue(state, nextValue.filter((val) => val !== undefined));
13
+ }
14
+ async function setArrayOnFirstOperation(state, fns) {
15
+ let valueState = await fns[0](state);
16
+ for (const fn of fns.slice(1)) {
17
+ const thisState = await fn(setStateValue(state, []));
18
+ valueState = mergeStates(valueState, thisState);
19
+ }
20
+ return valueState;
21
+ }
22
+ function concatPipelines(defs, flip) {
23
+ return (options) => {
7
24
  const fns = defs.map((def) => defToOperation(def, options)(options)(noopNext));
8
- return async function doConcat(state) {
9
- const nextState = await next(state);
10
- let nextValue = [];
11
- for (const fn of fns) {
12
- const value = getStateValue(await fn(nextState));
13
- nextValue = merge(nextValue, value);
14
- }
15
- return setStateValue(nextState, nextValue.filter((val) => val !== undefined));
25
+ if (fns.length === 0) {
26
+ return (next) => async (state) => setStateValue(await next(state), revFromState(state, flip) ? {} : []);
27
+ }
28
+ return (next) => async function doConcat(state) {
29
+ const nextState = flipState(await next(state), flip);
30
+ return revFromState(nextState)
31
+ ? setArrayOnFirstOperation(nextState, fns)
32
+ : getAndMergeArrays(nextState, fns);
16
33
  };
17
34
  };
18
35
  }
36
+ export function concat(...defs) {
37
+ return concatPipelines(defs, false);
38
+ }
39
+ export function concatRev(...defs) {
40
+ return concatPipelines(defs, true);
41
+ }
19
42
  //# sourceMappingURL=concat.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"concat.js","sourceRoot":"","sources":["../../src/operations/concat.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAEnD,MAAM,KAAK,GAAG,CAAO,IAAS,EAAE,KAAc,EAAE,EAAE,CAChD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,CAAC,CAAA;AAE/D,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,GAAG,IAA2B;IAC3D,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAC3B,cAAc,CAAC,GAAG,EAAE,OAAO,CAAC,CA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CAAC,KAAK;YAC3B,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,CAAA;YACpD,OAAO,YAAY,CAAC,SAAS,CAAC;gBAC5B,CAAC,CAAC,wBAAwB,CAAC,SAAS,EAAE,GAAG,CAAC;gBAC1C,CAAC,CAAC,iBAAiB,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;QACvC,CAAC,CAAA;IACL,CAAC,CAAA;AACH,CAAC;AAYD,MAAM,UAAU,MAAM,CAAC,GAAG,IAA2B;IACnD,OAAO,eAAe,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;AACrC,CAAC;AAGD,MAAM,UAAU,SAAS,CAAC,GAAG,IAA2B;IACtD,OAAO,eAAe,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;AACpC,CAAC"}
@@ -1,8 +1,8 @@
1
1
  import { defToOperation } from '../utils/definitionHelpers.js';
2
- import xor from '../utils/xor.js';
3
- const applyInDirection = (def, rev) => (options) => (next) => {
2
+ import { revFromState } from '../utils/stateHelpers.js';
3
+ const applyInDirection = (def, shouldRunRev) => (options) => (next) => {
4
4
  const fn = defToOperation(def, options)(options)(next);
5
- return async (state) => xor(rev, !state.rev) ? await fn(state) : await next(state);
5
+ return async (state) => revFromState(state) === shouldRunRev ? await fn(state) : await next(state);
6
6
  };
7
7
  export function fwd(def) {
8
8
  return applyInDirection(def, false);
@@ -14,7 +14,7 @@ export function divide(fwdDef, revDef) {
14
14
  return (options) => (next) => {
15
15
  const fwdFn = defToOperation(fwdDef, options)(options)(next);
16
16
  const revFn = defToOperation(revDef, options)(options)(next);
17
- return async (state) => state.rev ? await revFn(state) : await fwdFn(state);
17
+ return async (state) => revFromState(state) ? await revFn(state) : await fwdFn(state);
18
18
  };
19
19
  }
20
20
  //# sourceMappingURL=directionals.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"directionals.js","sourceRoot":"","sources":["../../src/operations/directionals.ts"],"names":[],"mappings":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