map-transform 1.3.0 → 1.4.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.
package/README.md
CHANGED
|
@@ -1855,6 +1855,12 @@ props in `exclude`. Both `include` and `exclude` may be array of strings, and
|
|
|
1855
1855
|
they should not be used in combination. If both are provided, `include` will be
|
|
1856
1856
|
used.
|
|
1857
1857
|
|
|
1858
|
+
You may also specify an `includePath` or `excludePath`. These are dot notation
|
|
1859
|
+
paths to arrays of strings, and will be used instead of `include` or `exclude`.
|
|
1860
|
+
If `include` or `exclude` are also provided, they will be used as default
|
|
1861
|
+
values when the corresponding path yields no value. Note that "no value" here
|
|
1862
|
+
means `undefined`, and we don't support custom nonvalues here yet.
|
|
1863
|
+
|
|
1858
1864
|
When given an array of object, each object will be projected. When given
|
|
1859
1865
|
anything that is not an object, undefined will be returned.
|
|
1860
1866
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { Transformer, TransformerProps } from '../types.js';
|
|
2
2
|
export interface Props extends TransformerProps {
|
|
3
3
|
include?: string[];
|
|
4
|
+
includePath?: string;
|
|
4
5
|
exclude?: string[];
|
|
6
|
+
excludePath?: string;
|
|
5
7
|
}
|
|
6
8
|
declare const transformer: Transformer<Props>;
|
|
7
9
|
export default transformer;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import mapAny from 'map-any';
|
|
2
|
+
import { pathGetter } from '../operations/getSet.js';
|
|
2
3
|
import { isObject, isString, isNonEmptyArray } from '../utils/is.js';
|
|
4
|
+
import { ensureArray } from '../utils/array.js';
|
|
3
5
|
const projectProps = (rawProps, doInclude) => {
|
|
4
6
|
const props = rawProps.filter(isString);
|
|
5
7
|
const filterFn = doInclude
|
|
@@ -7,13 +9,43 @@ const projectProps = (rawProps, doInclude) => {
|
|
|
7
9
|
: ([key]) => !props.includes(key);
|
|
8
10
|
return (obj) => Object.fromEntries(Object.entries(obj).filter(filterFn));
|
|
9
11
|
};
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
const projectPropsFromPath = (path, rawProps, doInclude) => {
|
|
13
|
+
const getFn = pathGetter(path);
|
|
14
|
+
return (obj, state) => {
|
|
15
|
+
let props = getFn(obj, state);
|
|
16
|
+
if (props === undefined) {
|
|
17
|
+
props = rawProps;
|
|
18
|
+
}
|
|
19
|
+
if (!props) {
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
return projectProps(ensureArray(props), doInclude)(obj);
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
function prepareProjectFn(include, includePath, exclude, excludePath) {
|
|
26
|
+
if (typeof includePath === 'string') {
|
|
27
|
+
return projectPropsFromPath(includePath, include, true);
|
|
28
|
+
}
|
|
29
|
+
else if (typeof excludePath === 'string') {
|
|
30
|
+
return projectPropsFromPath(excludePath, exclude, false);
|
|
31
|
+
}
|
|
32
|
+
else if (isNonEmptyArray(include)) {
|
|
33
|
+
return projectProps(include, true);
|
|
34
|
+
}
|
|
35
|
+
else if (isNonEmptyArray(exclude)) {
|
|
36
|
+
return projectProps(exclude, false);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return (obj) => obj;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const transformer = function bucket({ include, includePath, exclude, excludePath, }) {
|
|
43
|
+
const projectFn = prepareProjectFn(include, includePath, exclude, excludePath);
|
|
44
|
+
return () => (data, state) => mapAny((data) => isObject(data)
|
|
45
|
+
? state.rev
|
|
46
|
+
? data
|
|
47
|
+
: projectFn(data, state)
|
|
48
|
+
: undefined, data);
|
|
17
49
|
};
|
|
18
50
|
export default transformer;
|
|
19
51
|
//# sourceMappingURL=project.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/transformers/project.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM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AG;YACT,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC;QAC1B,CAAC,CAAC,SAAS,EACf,IAAI,CACL,CAAA;AACL,CAAC,CAAA;AAED,eAAe,WAAW,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "map-transform",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Map and transform objects with mapping definitions",
|
|
5
5
|
"author": "Kjell-Morten Bratsberg Thorsen <kjellmorten@integreat.io>",
|
|
6
6
|
"license": "ISC",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@integreat/ts-dev-setup": "^5.0.4",
|
|
57
57
|
"@types/deep-freeze": "^0.1.5",
|
|
58
|
-
"@types/sinon": "^17.0.
|
|
58
|
+
"@types/sinon": "^17.0.3",
|
|
59
59
|
"deep-freeze": "0.0.1",
|
|
60
60
|
"sinon": "^17.0.1"
|
|
61
61
|
}
|