map-transform 0.4.0-alpha.20 → 0.4.0-alpha.21
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 +39 -30
- package/dist/functions/compare.js +8 -3
- package/dist/functions/compare.js.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/operations/alt.d.ts +1 -1
- package/dist/operations/alt.js +24 -8
- package/dist/operations/alt.js.map +1 -1
- package/dist/operations/apply.js +4 -2
- 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 +5 -6
- 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 -4
- package/dist/operations/getSet.js +136 -32
- package/dist/operations/getSet.js.map +1 -1
- package/dist/operations/ifelse.js +14 -8
- package/dist/operations/ifelse.js.map +1 -1
- package/dist/operations/iterate.d.ts +2 -1
- package/dist/operations/iterate.js +14 -10
- package/dist/operations/iterate.js.map +1 -1
- package/dist/operations/logical.js +6 -6
- package/dist/operations/logical.js.map +1 -1
- package/dist/operations/lookup.js +9 -5
- package/dist/operations/lookup.js.map +1 -1
- package/dist/operations/merge.js +9 -7
- package/dist/operations/merge.js.map +1 -1
- package/dist/operations/modify.js +9 -6
- package/dist/operations/modify.js.map +1 -1
- package/dist/operations/pipe.js +14 -16
- 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 +112 -0
- package/dist/operations/props.js.map +1 -0
- package/dist/operations/root.js +3 -5
- package/dist/operations/root.js.map +1 -1
- package/dist/operations/transform.js +3 -1
- 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 +13 -8
- package/dist/utils/array.d.ts +1 -0
- package/dist/utils/array.js +3 -1
- package/dist/utils/array.js.map +1 -1
- package/dist/utils/definitionHelpers.d.ts +3 -1
- package/dist/utils/definitionHelpers.js +46 -24
- package/dist/utils/definitionHelpers.js.map +1 -1
- package/dist/utils/stateHelpers.d.ts +18 -5
- package/dist/utils/stateHelpers.js +26 -10
- 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 +1 -1
- 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 -111
- 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
|
|
|
@@ -305,15 +305,18 @@ below.
|
|
|
305
305
|
|
|
306
306
|
MapTransform will treat `undefined` as a value that is not set, and so a
|
|
307
307
|
transform object will not be applied to it. So if an `undefined` value meets
|
|
308
|
-
a transform object, it will produce `undefined`,
|
|
308
|
+
a transform object, it will produce `undefined`, regardless of the shape of the
|
|
309
309
|
transform object.
|
|
310
310
|
|
|
311
311
|
This is not the case for `null`, though. MapTransform treats `null` as a value,
|
|
312
312
|
an intended nothing, and will apply a transform object to it, even though it
|
|
313
313
|
will most likely produce nothing but default values. To change this behavior,
|
|
314
|
-
set `
|
|
315
|
-
will essentially make MapTransform treat `null` the same way
|
|
316
|
-
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.
|
|
317
320
|
|
|
318
321
|
#### Directional transform objects
|
|
319
322
|
|
|
@@ -716,25 +719,23 @@ import { value, alt } from 'map-transform'
|
|
|
716
719
|
const def10 = {
|
|
717
720
|
id: 'data.customerNo',
|
|
718
721
|
type: value('customer'),
|
|
719
|
-
name:
|
|
722
|
+
name: alt('data.name', value('Anonymous')),
|
|
720
723
|
}
|
|
721
724
|
```
|
|
722
725
|
|
|
723
726
|
The operation will not set anything when mapping with `.onlyMappedValues()`.
|
|
724
727
|
|
|
725
728
|
If you want to define the `value` operation as an operation object, use
|
|
726
|
-
`$transform` or `$
|
|
729
|
+
`$transform` or the short form `$value`:
|
|
727
730
|
|
|
728
731
|
```javascript
|
|
729
732
|
const def10asObject = {
|
|
730
733
|
id: 'data.customerNo',
|
|
731
734
|
type: { $transform: 'value', value: 'customer' },
|
|
732
|
-
name: ['data.name', { $
|
|
735
|
+
name: { $alt: ['data.name', { $value: 'Anonymous' }] },
|
|
733
736
|
}
|
|
734
737
|
```
|
|
735
738
|
|
|
736
|
-
There is also a shortcut for `{ $transform: 'value', value: 'customer' }`: `{ $value: 'customer' }`, which might be useful when typing definitions by hand.
|
|
737
|
-
|
|
738
739
|
#### `fixed(data)` operation
|
|
739
740
|
|
|
740
741
|
The data given to the fixed operation, will be inserted in the pipeline in place
|
|
@@ -746,14 +747,15 @@ will be included when mapping with `.onlyMappedValues()` as well.
|
|
|
746
747
|
|
|
747
748
|
#### `alt(pipeline)` operation
|
|
748
749
|
|
|
749
|
-
The alt operation will apply the
|
|
750
|
-
the pipeline
|
|
751
|
-
|
|
752
|
-
|
|
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.
|
|
753
755
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
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.
|
|
757
759
|
|
|
758
760
|
```javascript
|
|
759
761
|
import { alt, transform, functions } from 'map-transform'
|
|
@@ -765,11 +767,9 @@ const formatDate = (data) => {
|
|
|
765
767
|
|
|
766
768
|
const def11 = {
|
|
767
769
|
id: 'data.id',
|
|
768
|
-
name:
|
|
770
|
+
name: alt('data.name', value('Anonymous')),
|
|
769
771
|
updatedAt: [
|
|
770
|
-
'data.updateDate',
|
|
771
|
-
alt('data.createDate'),
|
|
772
|
-
alt(transform(currentDate)),
|
|
772
|
+
alt('data.updateDate', 'data.createDate', transform(currentDate)),
|
|
773
773
|
transform(formatDate),
|
|
774
774
|
],
|
|
775
775
|
}
|
|
@@ -783,16 +783,19 @@ we still have `undefined`, the second alt will call the customer function
|
|
|
783
783
|
another transform operation pipes whatever data we get from all of this through
|
|
784
784
|
the `formatDate` function.
|
|
785
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
|
+
|
|
786
791
|
You may also define an alt operation as an object:
|
|
787
792
|
|
|
788
793
|
```javascript
|
|
789
794
|
const def11asObject = {
|
|
790
795
|
id: 'data.id',
|
|
791
|
-
name: ['data.name', { $
|
|
796
|
+
name: { $alt: ['data.name', { $value: 'Anonymous' }] },
|
|
792
797
|
updatedAt: [
|
|
793
|
-
'data.updateDate',
|
|
794
|
-
{ $alt: 'get', path: 'data.createDate' },
|
|
795
|
-
{ $alt: 'currentDate' },
|
|
798
|
+
{ $alt: ['data.updateDate', 'data.createDate', 'currentDate'] },
|
|
796
799
|
{ $transform: 'formatDate' },
|
|
797
800
|
],
|
|
798
801
|
}
|
|
@@ -1057,6 +1060,12 @@ mapper.rev(mappedData)
|
|
|
1057
1060
|
// --> { content: { meta: { authors: ['user1', 'user3'] } } }
|
|
1058
1061
|
```
|
|
1059
1062
|
|
|
1063
|
+
You may also define this as a transform object:
|
|
1064
|
+
|
|
1065
|
+
```javascript
|
|
1066
|
+
const def18o = ['content.meta.authors[]', { $lookup: '$users[]', path: 'id' }]
|
|
1067
|
+
```
|
|
1068
|
+
|
|
1060
1069
|
#### `and(pipeline, pipeline, ...)` operation
|
|
1061
1070
|
|
|
1062
1071
|
Will run all provided pipelines, force their return values to boolean, according
|
|
@@ -1395,7 +1404,7 @@ const def22 = [
|
|
|
1395
1404
|
'data.customers[]',
|
|
1396
1405
|
{
|
|
1397
1406
|
id: 'customerNo',
|
|
1398
|
-
name: ['fullname',
|
|
1407
|
+
name: [alt('fullname', value('Anonymous'))],
|
|
1399
1408
|
},
|
|
1400
1409
|
]
|
|
1401
1410
|
|
|
@@ -1509,8 +1518,8 @@ same goes for flipped transform objects if you want to forward transform.
|
|
|
1509
1518
|
|
|
1510
1519
|
MapTransform will try its best to map the data it gets to the state you want,
|
|
1511
1520
|
and will always set all properties, even though the mapping you defined result
|
|
1512
|
-
in `undefined`. You may include `alt()` operations to provide default or
|
|
1513
|
-
values for these cases.
|
|
1521
|
+
in `undefined`. You may include `alt()` operations to provide default or
|
|
1522
|
+
fallback values for these cases.
|
|
1514
1523
|
|
|
1515
1524
|
But sometimes, you want just the data that is actually present in the source
|
|
1516
1525
|
data, without defaults or properties set to `undefined`. MapTransform's
|
|
@@ -1526,7 +1535,7 @@ import { mapTransform, alt, value } from 'map-transform'
|
|
|
1526
1535
|
|
|
1527
1536
|
const def24 = {
|
|
1528
1537
|
id: 'customerNo',
|
|
1529
|
-
name:
|
|
1538
|
+
name: alt('fullname', value('Anonymous')),
|
|
1530
1539
|
}
|
|
1531
1540
|
|
|
1532
1541
|
const mapper = mapTransform(def24)
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const mapAny = require("map-any");
|
|
4
4
|
const pathGetter_1 = require("../utils/pathGetter");
|
|
5
5
|
const escape_1 = require("../utils/escape");
|
|
6
|
+
const stateHelpers_1 = require("../utils/stateHelpers");
|
|
6
7
|
const not = (comparer) => (value, match) => !comparer(value, match);
|
|
7
8
|
const compareArrayOrValue = (comparer) => (value, match) => Array.isArray(value)
|
|
8
9
|
? value.some((value) => comparer(value, match))
|
|
@@ -38,8 +39,12 @@ function createComparer(operator) {
|
|
|
38
39
|
}
|
|
39
40
|
function compare({ path = '.', operator = '=', match, matchPath, not = false, }) {
|
|
40
41
|
const getValue = (0, pathGetter_1.default)(path);
|
|
41
|
-
const useRoot = typeof matchPath === 'string' &&
|
|
42
|
-
|
|
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;
|
|
43
48
|
const realMatchValue = mapAny(escape_1.unescapeValue, match);
|
|
44
49
|
const getMatch = typeof realMatchPath === 'string'
|
|
45
50
|
? (0, pathGetter_1.default)(realMatchPath)
|
|
@@ -47,7 +52,7 @@ function compare({ path = '.', operator = '=', match, matchPath, not = false, })
|
|
|
47
52
|
const comparer = createComparer(operator);
|
|
48
53
|
return (data, state) => {
|
|
49
54
|
const value = getValue(data);
|
|
50
|
-
const match = getMatch(useRoot ? state
|
|
55
|
+
const match = getMatch(useRoot ? (0, stateHelpers_1.getRootFromState)(state) : data);
|
|
51
56
|
const result = comparer(value, match);
|
|
52
57
|
return not ? !result : result;
|
|
53
58
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../../src/functions/compare.ts"],"names":[],"mappings":";;AAAA,kCAAkC;AAElC,oDAAwC;AACxC,4CAA+C;
|
|
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,KAAc,EAAE,KAAc,EAAE,EAAE,CACzD,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;AAE9B,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,MAAM,SAAS,G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require("./operations/iterate");
|
|
9
9
|
exports.iterate = iterate_1.default;
|
|
10
|
+
const functional_1 = require("./utils/functional");
|
|
10
11
|
var getSet_1 = require("./operations/getSet");
|
|
11
12
|
Object.defineProperty(exports, "get", { enumerable: true, get: function () { return getSet_1.get; } });
|
|
12
13
|
Object.defineProperty(exports, "set", { enumerable: true, get: function () { return getSet_1.set; } });
|
|
@@ -61,7 +62,7 @@ const mergeOptions = (options) => ({
|
|
|
61
62
|
});
|
|
62
63
|
function mapTransform(def, options = {}) {
|
|
63
64
|
const completeOptions = mergeOptions(options);
|
|
64
|
-
const mapFn = (0, definitionHelpers_1.
|
|
65
|
+
const mapFn = (0, definitionHelpers_1.operationFromDef)(def)(completeOptions)(functional_1.identity);
|
|
65
66
|
return Object.assign(composeMapFunction(mapFn, {}), {
|
|
66
67
|
onlyMappedValues: composeMapFunction(mapFn, { onlyMapped: true }),
|
|
67
68
|
rev: Object.assign(composeMapFunction(mapFn, { rev: true }), {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAOA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAOA,iEAA4D;AAC5D,uDAAmE;AACnE,2CAAmC;AAqBjB,oBArBX,mBAAS,CAqBW;AApB3B,kDAA0C;AAoBjC,kBApBF,iBAAO,CAoBE;AAnBhB,mDAA6C;AAE7C,8CAA8C;AAArC,6FAAA,GAAG,OAAA;AAAE,6FAAA,GAAG,OAAA;AACjB,0CAAmD;AAA1C,4FAAA,OAAO,OAAQ;AACxB,wCAAiD;AAAxC,0FAAA,OAAO,OAAO;AACvB,4CAAqD;AAA5C,8FAAA,OAAO,OAAS;AACzB,4CAAqD;AAA5C,8FAAA,OAAO,OAAS;AACzB,4CAAqD;AAA5C,8FAAA,OAAO,OAAS;AACzB,8CAAuD;AAA9C,gGAAA,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,GAAG,CAAC,CAAC,eAAe,CAAC,CAAC,qBAAQ,CAAC,CAAA;IAE9D,OAAO,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE;QAClD,gBAAgB,EAAE,kBAAkB,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QACjE,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,KAAK,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE;YAC3D,gBAAgB,EAAE,kBAAkB,CAAC,KAAK,EAAE;gBAC1C,GAAG,EAAE,IAAI;gBACT,UAAU,EAAE,IAAI;aACjB,CAAC;SACH,CAAC;KACH,CAAC,CAAA;AACJ,CAAC;AAhBD,oCAgBC"}
|
package/dist/operations/alt.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Operation, MapDefinition } from '../types';
|
|
2
|
-
export default function alt(
|
|
2
|
+
export default function alt(...defs: MapDefinition[]): Operation[];
|
package/dist/operations/alt.js
CHANGED
|
@@ -2,15 +2,31 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const stateHelpers_1 = require("../utils/stateHelpers");
|
|
4
4
|
const definitionHelpers_1 = require("../utils/definitionHelpers");
|
|
5
|
-
const
|
|
6
|
-
function
|
|
7
|
-
return (options) => {
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
const functional_1 = require("../utils/functional");
|
|
6
|
+
function runAlt(operation, index) {
|
|
7
|
+
return (options) => (next) => (state) => {
|
|
8
|
+
const nextState = next(state);
|
|
9
|
+
const { noneValues } = options;
|
|
10
|
+
const isFirst = index === 0;
|
|
11
|
+
if (isFirst) {
|
|
12
|
+
const thisState = operation(options)(functional_1.identity)(nextState);
|
|
13
|
+
return (0, stateHelpers_1.isNoneValueState)(thisState, noneValues)
|
|
14
|
+
? { ...thisState, context: [...nextState.context, nextState.value] }
|
|
15
|
+
: thisState;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
if ((0, stateHelpers_1.isNoneValueState)(nextState, noneValues)) {
|
|
19
|
+
return operation(options)(functional_1.identity)((0, stateHelpers_1.removeLastContext)((0, stateHelpers_1.setStateValue)(nextState, (0, stateHelpers_1.getLastContext)(nextState))));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return nextState;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
13
25
|
};
|
|
14
26
|
}
|
|
27
|
+
function alt(...defs) {
|
|
28
|
+
const altOperations = defs.map((def) => (0, definitionHelpers_1.operationFromDef)(def));
|
|
29
|
+
return altOperations.map(runAlt);
|
|
30
|
+
}
|
|
15
31
|
exports.default = alt;
|
|
16
32
|
//# sourceMappingURL=alt.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alt.js","sourceRoot":"","sources":["../../src/operations/alt.ts"],"names":[],"mappings":";;AACA,
|
|
1
|
+
{"version":3,"file":"alt.js","sourceRoot":"","sources":["../../src/operations/alt.ts"],"names":[],"mappings":";;AACA,wDAK8B;AAC9B,kEAA6D;AAC7D,oDAA8C;AAE9C,SAAS,MAAM,CAAC,SAAoB,EAAE,KAAa;IACjD,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,EAAE;QACtC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7B,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,CAAA;QAC9B,MAAM,OAAO,GAAG,KAAK,KAAK,CAAC,CAAA;QAE3B,IAAI,OAAO,EAAE;YACX,MAAM,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC,qBAAQ,CAAC,CAAC,SAAS,CAAC,CAAA;YACzD,OAAO,IAAA,+BAAgB,EAAC,SAAS,EAAE,UAAU,CAAC;gBAC5C,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE,OAAO,EAAE,CAAC,GAAG,SAAS,CAAC,OAAO,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE;gBACpE,CAAC,CAAC,SAAS,CAAA;SACd;aAAM;YACL,IAAI,IAAA,+BAAgB,EAAC,SAAS,EAAE,UAAU,CAAC,EAAE;gBAC3C,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC,qBAAQ,CAAC,CACjC,IAAA,gCAAiB,EAAC,IAAA,4BAAa,EAAC,SAAS,EAAE,IAAA,6BAAc,EAAC,SAAS,CAAC,CAAC,CAAC,CACvE,CAAA;aACF;iBAAM;gBACL,OAAO,SAAS,CAAA;aACjB;SACF;IACH,CAAC,CAAA;AACH,CAAC;AAED,SAAwB,GAAG,CAAC,GAAG,IAAqB;IAElD,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAA,oCAAgB,EAAC,GAAG,CAAC,CAAC,CAAA;IAK9D,OAAO,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAClC,CAAC;AARD,sBAQC"}
|
package/dist/operations/apply.js
CHANGED
|
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const definitionHelpers_1 = require("../utils/definitionHelpers");
|
|
4
4
|
const extractPipeline = (pipelineId, { pipelines }) => pipelineId && pipelines ? pipelines[pipelineId] : undefined;
|
|
5
5
|
function apply(pipelineId) {
|
|
6
|
-
return (options) => {
|
|
6
|
+
return (options) => (next) => {
|
|
7
7
|
const pipeline = extractPipeline(pipelineId, options);
|
|
8
|
-
return pipeline
|
|
8
|
+
return pipeline
|
|
9
|
+
? (0, definitionHelpers_1.operationFromDef)(pipeline)(options)(next)
|
|
10
|
+
: (state) => next(state);
|
|
9
11
|
};
|
|
10
12
|
}
|
|
11
13
|
exports.default = apply;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apply.js","sourceRoot":"","sources":["../../src/operations/apply.ts"],"names":[],"mappings":";;AACA,
|
|
1
|
+
{"version":3,"file":"apply.js","sourceRoot":"","sources":["../../src/operations/apply.ts"],"names":[],"mappings":";;AACA,kEAA6D;AAE7D,MAAM,eAAe,GAAG,CAAC,UAAkB,EAAE,EAAE,SAAS,EAAW,EAAE,EAAE,CACrE,UAAU,IAAI,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AAE7D,SAAwB,KAAK,CAAC,UAAkB;IAC9C,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE;QAC3B,MAAM,QAAQ,GAAG,eAAe,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;QACrD,OAAO,QAAQ;YACb,CAAC,CAAC,IAAA,oCAAgB,EAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC;YAC3C,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC,CAAA;AACH,CAAC;AAPD,wBAOC"}
|
|
@@ -2,13 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const stateHelpers_1 = require("../utils/stateHelpers");
|
|
4
4
|
const definitionHelpers_1 = require("../utils/definitionHelpers");
|
|
5
|
+
const functional_1 = require("../utils/functional");
|
|
5
6
|
const merge = (left, right) => Array.isArray(right) ? [...left, ...right] : [...left, right];
|
|
6
7
|
function concat(...defs) {
|
|
7
|
-
return (options) => {
|
|
8
|
-
const fns = defs.map((def) => (0, definitionHelpers_1.
|
|
9
|
-
return (state)
|
|
10
|
-
|
|
11
|
-
.
|
|
8
|
+
return (options) => (next) => {
|
|
9
|
+
const fns = defs.map((def) => (0, definitionHelpers_1.operationFromDef)(def)(options)(functional_1.identity));
|
|
10
|
+
return function doConcat(state) {
|
|
11
|
+
const nextState = next(state);
|
|
12
|
+
return (0, stateHelpers_1.setStateValue)(nextState, fns
|
|
13
|
+
.reduce((value, fn) => merge(value, (0, stateHelpers_1.getStateValue)(fn(nextState))), [])
|
|
14
|
+
.filter((val) => val !== undefined));
|
|
15
|
+
};
|
|
12
16
|
};
|
|
13
17
|
}
|
|
14
18
|
exports.default = concat;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concat.js","sourceRoot":"","sources":["../../src/operations/concat.ts"],"names":[],"mappings":";;AACA,wDAAoE;AACpE,
|
|
1
|
+
{"version":3,"file":"concat.js","sourceRoot":"","sources":["../../src/operations/concat.ts"],"names":[],"mappings":";;AACA,wDAAoE;AACpE,kEAA6D;AAC7D,oDAA8C;AAE9C,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,SAAwB,MAAM,CAAC,GAAG,IAAqB;IACrD,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAA,oCAAgB,EAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,qBAAQ,CAAC,CAAC,CAAA;QAEvE,OAAO,SAAS,QAAQ,CAAC,KAAK;YAC5B,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;YAC7B,OAAO,IAAA,4BAAa,EAClB,SAAS,EACT,GAAG;iBACA,MAAM,CACL,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,IAAA,4BAAa,EAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,EACzD,EAAe,CAChB;iBACA,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,CAAC,CACtC,CAAA;QACH,CAAC,CAAA;IACH,CAAC,CAAA;AACH,CAAC;AAjBD,yBAiBC"}
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.divide = exports.rev = exports.fwd = void 0;
|
|
4
4
|
const definitionHelpers_1 = require("../utils/definitionHelpers");
|
|
5
|
+
const xor_1 = require("../utils/xor");
|
|
5
6
|
const applyInDirection = (def, rev) => {
|
|
6
|
-
return (options) => {
|
|
7
|
-
const fn = (0, definitionHelpers_1.
|
|
8
|
-
return (state) => ((
|
|
7
|
+
return (options) => (next) => {
|
|
8
|
+
const fn = (0, definitionHelpers_1.operationFromDef)(def)(options)(next);
|
|
9
|
+
return (state) => ((0, xor_1.default)(rev, !state.rev) ? fn(state) : next(state));
|
|
9
10
|
};
|
|
10
11
|
};
|
|
11
12
|
function fwd(def) {
|
|
@@ -17,9 +18,9 @@ function rev(def) {
|
|
|
17
18
|
}
|
|
18
19
|
exports.rev = rev;
|
|
19
20
|
function divide(fwdDef, revDef) {
|
|
20
|
-
return (options) => {
|
|
21
|
-
const fwdFn = (0, definitionHelpers_1.
|
|
22
|
-
const revFn = (0, definitionHelpers_1.
|
|
21
|
+
return (options) => (next) => {
|
|
22
|
+
const fwdFn = (0, definitionHelpers_1.operationFromDef)(fwdDef)(options)(next);
|
|
23
|
+
const revFn = (0, definitionHelpers_1.operationFromDef)(revDef)(options)(next);
|
|
23
24
|
return (state) => (state.rev ? revFn(state) : fwdFn(state));
|
|
24
25
|
};
|
|
25
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"directionals.js","sourceRoot":"","sources":["../../src/operations/directionals.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"directionals.js","sourceRoot":"","sources":["../../src/operations/directionals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,CAAA;AACH,CAAC;AATD,wBASC"}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const stateHelpers_1 = require("../utils/stateHelpers");
|
|
4
|
-
const functional_1 = require("../utils/functional");
|
|
5
4
|
const filterValue = (value, filterFn) => Array.isArray(value)
|
|
6
5
|
? value.filter(filterFn)
|
|
7
6
|
: filterFn(value)
|
|
8
7
|
? value
|
|
9
8
|
: undefined;
|
|
10
9
|
function filter(fn) {
|
|
11
|
-
return (_options) => {
|
|
10
|
+
return (_options) => (next) => {
|
|
12
11
|
if (typeof fn !== 'function') {
|
|
13
|
-
return
|
|
12
|
+
return (state) => next(state);
|
|
14
13
|
}
|
|
15
14
|
return (state) => {
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
return (0, stateHelpers_1.setStateValue)(
|
|
15
|
+
const nextState = next(state);
|
|
16
|
+
const filterFn = (data) => !!fn(data, nextState);
|
|
17
|
+
return (0, stateHelpers_1.setStateValue)(nextState, filterValue((0, stateHelpers_1.getStateValue)(nextState), filterFn));
|
|
19
18
|
};
|
|
20
19
|
};
|
|
21
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter.js","sourceRoot":"","sources":["../../src/operations/filter.ts"],"names":[],"mappings":";;AACA,wDAAoE;
|
|
1
|
+
{"version":3,"file":"filter.js","sourceRoot":"","sources":["../../src/operations/filter.ts"],"names":[],"mappings":";;AACA,wDAAoE;AAEpE,MAAM,WAAW,GAAG,CAAC,KAAc,EAAE,QAAoC,EAAE,EAAE,CAC3E,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;IAClB,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC;IACxB,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;QACjB,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,SAAS,CAAA;AAEf,SAAwB,MAAM,CAAC,EAAc;IAC3C,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE;QAC5B,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE;YAC5B,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;SAC9B;QAED,OAAO,CAAC,KAAK,EAAE,EAAE;YACf,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;YAC7B,MAAM,QAAQ,GAAG,CAAC,IAAa,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;YACzD,OAAO,IAAA,4BAAa,EAClB,SAAS,EACT,WAAW,CAAC,IAAA,4BAAa,EAAC,SAAS,CAAC,EAAE,QAAQ,CAAC,CAChD,CAAA;QACH,CAAC,CAAA;IACH,CAAC,CAAA;AACH,CAAC;AAfD,yBAeC"}
|
package/dist/operations/fixed.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixed.js","sourceRoot":"","sources":["../../src/operations/fixed.ts"],"names":[],"mappings":";;AACA,8CAAiD;AAEjD,SAAwB,KAAK,CAAC,GAA4B;IACxD,OAAO,GAAG,EAAE,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"fixed.js","sourceRoot":"","sources":["../../src/operations/fixed.ts"],"names":[],"mappings":";;AACA,8CAAiD;AAEjD,SAAwB,KAAK,CAAC,GAA4B;IACxD,OAAO,GAAG,EAAE,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAClC,GAAG,KAAK;QACR,KAAK,EAAE,IAAA,oBAAY,EAAC,GAAG,CAAC;KACzB,CAAC,CAAA;AACJ,CAAC;AALD,wBAKC"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const set: (path: Path) => Operation;
|
|
1
|
+
import { Path, Operation } from '../types';
|
|
2
|
+
export declare const get: (path: Path) => Operation[];
|
|
3
|
+
export declare const set: (path: Path) => Operation[];
|
|
@@ -1,37 +1,141 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.set = exports.get =
|
|
4
|
-
const
|
|
5
|
-
const pathSetter_1 = require("../utils/pathSetter");
|
|
3
|
+
exports.set = exports.get = void 0;
|
|
4
|
+
const mapAny = require("map-any");
|
|
6
5
|
const stateHelpers_1 = require("../utils/stateHelpers");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
6
|
+
const is_1 = require("../utils/is");
|
|
7
|
+
const array_1 = require("../utils/array");
|
|
8
|
+
const functional_1 = require("../utils/functional");
|
|
9
|
+
const xor_1 = require("../utils/xor");
|
|
10
|
+
const adjustIsSet = (isSet, { rev = false, flip = false }) => (0, xor_1.default)(isSet, (0, xor_1.default)(rev, flip));
|
|
11
|
+
function flatMapAny(fn) {
|
|
12
|
+
return (value, target) => Array.isArray(value)
|
|
13
|
+
? value.flatMap((value) => fn(value, target))
|
|
14
|
+
: fn(value, target);
|
|
15
|
+
}
|
|
16
|
+
function preparePath(path) {
|
|
17
|
+
if (typeof path === 'string' && path.includes('[')) {
|
|
18
|
+
const pos = path.indexOf('[');
|
|
19
|
+
if (path[pos - 1] === '\\') {
|
|
20
|
+
return [path.replace('\\[', '['), false];
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
const isArr = path[pos + 1] === ']';
|
|
24
|
+
return [path.slice(0, pos), isArr];
|
|
25
|
+
}
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
return [path, false];
|
|
28
|
+
}
|
|
29
|
+
function getSetProp(path) {
|
|
30
|
+
if (path === '') {
|
|
31
|
+
return functional_1.identity;
|
|
32
|
+
}
|
|
33
|
+
const getFn = flatMapAny((value) => (0, is_1.isObject)(value) ? value[path] : undefined);
|
|
34
|
+
const setFn = (value, target) => (0, is_1.isObject)(target) ? { ...target, [path]: value } : { [path]: value };
|
|
35
|
+
return (value, isSet, target) => {
|
|
36
|
+
if (isSet) {
|
|
37
|
+
return setFn(value, target);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return getFn(value);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function getSetIndex(index) {
|
|
45
|
+
return (value, isSet, target) => {
|
|
46
|
+
if (isSet) {
|
|
47
|
+
const arr = Array.isArray(target) ? [...target] : [];
|
|
48
|
+
arr[index] = value;
|
|
49
|
+
return arr;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return Array.isArray(value) ? value[index] : undefined;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function getParent(state) {
|
|
57
|
+
const nextValue = (0, stateHelpers_1.getLastContext)(state);
|
|
58
|
+
const nextContext = state.context.slice(0, -1);
|
|
59
|
+
return { ...state, context: nextContext, value: nextValue };
|
|
60
|
+
}
|
|
61
|
+
function getRoot(state) {
|
|
62
|
+
const nextValue = (0, stateHelpers_1.getRootFromState)(state);
|
|
63
|
+
const nextContext = [];
|
|
64
|
+
return { ...state, context: nextContext, value: nextValue };
|
|
65
|
+
}
|
|
66
|
+
function getSet(isSet = false) {
|
|
67
|
+
return (path) => {
|
|
68
|
+
if (typeof path === 'string' && path[0] === '^') {
|
|
69
|
+
const getFn = path[1] === '^' ? getRoot : getParent;
|
|
70
|
+
return () => (next) => (state) => {
|
|
71
|
+
if (adjustIsSet(isSet, state)) {
|
|
72
|
+
return (0, stateHelpers_1.setStateValue)(next(state), undefined);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
return next(getFn(state));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const [basePath, isArr] = preparePath(path);
|
|
80
|
+
const getSetFn = typeof basePath === 'number'
|
|
81
|
+
? getSetIndex(basePath)
|
|
82
|
+
: getSetProp(basePath);
|
|
83
|
+
const getValue = isArr ? (0, functional_1.compose)(array_1.ensureArray, stateHelpers_1.getStateValue) : stateHelpers_1.getStateValue;
|
|
84
|
+
return (_options) => (next) => (state) => {
|
|
85
|
+
if (adjustIsSet(isSet, state)) {
|
|
86
|
+
const target = (0, stateHelpers_1.getTargetFromState)(state);
|
|
87
|
+
const nextTarget = getSetFn(target, false);
|
|
88
|
+
const nextState = next((0, stateHelpers_1.setTargetOnState)({ ...state, iterate: state.iterate || isArr }, nextTarget));
|
|
89
|
+
const setIt = (value, index) => getSetFn(value, true, (0, array_1.indexOfIfArray)(target, index));
|
|
90
|
+
const nextValue = state.iterate
|
|
91
|
+
? mapAny(setIt, getValue(nextState))
|
|
92
|
+
: setIt(getValue(nextState));
|
|
93
|
+
return (0, stateHelpers_1.setStateValue)(state, nextValue);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const nextState = next(state);
|
|
97
|
+
const nextValue = getSetFn(getValue(nextState), false);
|
|
98
|
+
return (0, stateHelpers_1.setStateValue)(nextState, nextValue, true);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function dividePath(path) {
|
|
104
|
+
const pos = path.indexOf('[');
|
|
105
|
+
if (pos > -1 && path[pos - 1] !== '\\') {
|
|
106
|
+
const index = Number.parseInt(path.slice(pos + 1), 10);
|
|
107
|
+
if (!Number.isNaN(index)) {
|
|
108
|
+
const basePath = path.slice(0, pos).trim();
|
|
109
|
+
return basePath ? [basePath, index] : index;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (path.startsWith('^')) {
|
|
113
|
+
if (path.startsWith('^^') && path.length > 2) {
|
|
114
|
+
return ['^^', path.slice(2)];
|
|
115
|
+
}
|
|
116
|
+
else if (path.length > 1 && path !== '^^') {
|
|
117
|
+
return ['^^', path.slice(1)];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return path.trim();
|
|
121
|
+
}
|
|
122
|
+
function pathToNextOperations(path, isSet = false) {
|
|
123
|
+
if (!path || path === '.') {
|
|
124
|
+
return [() => (next) => (state) => next(state)];
|
|
125
|
+
}
|
|
126
|
+
if (path[0] === '>') {
|
|
127
|
+
path = path.slice(1);
|
|
128
|
+
isSet = true;
|
|
129
|
+
}
|
|
130
|
+
const parts = path.split('.').flatMap(dividePath);
|
|
131
|
+
const operations = parts.map(getSet(isSet));
|
|
132
|
+
if (isSet) {
|
|
133
|
+
operations.reverse();
|
|
134
|
+
}
|
|
135
|
+
return operations;
|
|
136
|
+
}
|
|
137
|
+
const get = (path) => pathToNextOperations(path, false);
|
|
138
|
+
exports.get = get;
|
|
139
|
+
const set = (path) => pathToNextOperations(path, true);
|
|
140
|
+
exports.set = set;
|
|
37
141
|
//# sourceMappingURL=getSet.js.map
|