map-transform 1.2.0 → 1.3.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
@@ -1452,7 +1452,7 @@ and not the order of the items in the original array. When a path is given, the
1452
1452
  array will be set on this path.
1453
1453
 
1454
1454
  ```javascript
1455
- const def40 = bucket({
1455
+ const def40 = transform(bucket({
1456
1456
  path: 'users[]',
1457
1457
  buckets: [
1458
1458
  {
@@ -1467,7 +1467,7 @@ const def40 = bucket({
1467
1467
  key: 'users',
1468
1468
  },
1469
1469
  ],
1470
- })
1470
+ }))
1471
1471
 
1472
1472
  const data = {
1473
1473
  users: [
@@ -1500,9 +1500,9 @@ const mappedData = await mapper(data)
1500
1500
  Or by size:
1501
1501
 
1502
1502
  ```javascript
1503
- const def41 = bucket({
1503
+ const def41 = transform(bucket({
1504
1504
  buckets: [{ key: 'top3', size: 3 }, { key: 'theOthers' }],
1505
- })
1505
+ }))
1506
1506
 
1507
1507
  const data = ['user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7']
1508
1508
 
@@ -1848,6 +1848,46 @@ const def21b = [
1848
1848
  ]
1849
1849
  ```
1850
1850
 
1851
+ #### `project({include, exclude})` transformer
1852
+
1853
+ Will return an object with only the props specified in `include` or none of the
1854
+ props in `exclude`. Both `include` and `exclude` may be array of strings, and
1855
+ they should not be used in combination. If both are provided, `include` will be
1856
+ used.
1857
+
1858
+ When given an array of object, each object will be projected. When given
1859
+ anything that is not an object, undefined will be returned.
1860
+
1861
+ As we cannot bring back the removed props when mapping in reverse, this
1862
+ transformer will pass on the object data as is in reverse.
1863
+
1864
+ ```javascript
1865
+ import { transform, transformers } from 'map-transform'
1866
+ const { project } = transformers
1867
+
1868
+ const def42 = transform(project({ include: ['id', 'name'] }))
1869
+
1870
+ const data = {
1871
+ id: 'ent1',
1872
+ name: 'Entry 1',
1873
+ text: 'Do not include',
1874
+ created: new Date('2023-12-01T00:00:00Z'),
1875
+ }
1876
+
1877
+ const mapper = mapTransform(def42)
1878
+ const mappedData = await mapper(data)
1879
+ // --> {
1880
+ // id: 'ent1',
1881
+ // name: 'Entry 1',
1882
+ // }
1883
+ ```
1884
+
1885
+ You may also define this as an operation object:
1886
+
1887
+ ```javascript
1888
+ const def42b = { $transform: 'project', include: ['id', 'name'] }
1889
+ ```
1890
+
1851
1891
  #### `sort({asc, path})` transformer
1852
1892
 
1853
1893
  The `sort` transformer will sort the array at the given `path`, in the direction
@@ -12,6 +12,7 @@ declare const _default: {
12
12
  merge: import("../types.js").AsyncTransformer<import("./merge.js").Props>;
13
13
  mergeRev: import("../types.js").AsyncTransformer<import("./merge.js").Props>;
14
14
  not: import("../types.js").AsyncTransformer<import("../types.js").AsyncDataMapperWithOptions | import("../types.js").DataMapperWithOptions | import("./not.js").Props>;
15
+ project: import("../types.js").Transformer<import("./project.js").Props>;
15
16
  sort: import("../types.js").Transformer<import("./sort.js").Props>;
16
17
  value: import("../types.js").Transformer<unknown>;
17
18
  };
@@ -9,6 +9,7 @@ import logical from './logical.js';
9
9
  import map from './map.js';
10
10
  import { merge, mergeRev } from './merge.js';
11
11
  import not from './not.js';
12
+ import project from './project.js';
12
13
  import sort from './sort.js';
13
14
  export default {
14
15
  bucket,
@@ -24,6 +25,7 @@ export default {
24
25
  merge,
25
26
  mergeRev,
26
27
  not,
28
+ project,
27
29
  sort,
28
30
  value,
29
31
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/transformers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,OAAO,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,OAAO,MAAM,cAAc,CAAA;AAClC,OAAO,GAAG,MAAM,UAAU,CAAA;AAC1B,OAAO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ACN,CAAA"}
@@ -0,0 +1,7 @@
1
+ import type { Transformer, TransformerProps } from '../types.js';
2
+ export interface Props extends TransformerProps {
3
+ include?: string[];
4
+ exclude?: string[];
5
+ }
6
+ declare const transformer: Transformer<Props>;
7
+ export default transformer;
@@ -0,0 +1,19 @@
1
+ import mapAny from 'map-any';
2
+ import { isObject, isString, isNonEmptyArray } from '../utils/is.js';
3
+ const projectProps = (rawProps, doInclude) => {
4
+ const props = rawProps.filter(isString);
5
+ const filterFn = doInclude
6
+ ? ([key]) => props.includes(key)
7
+ : ([key]) => !props.includes(key);
8
+ return (obj) => Object.fromEntries(Object.entries(obj).filter(filterFn));
9
+ };
10
+ const transformer = function bucket({ include, exclude }) {
11
+ const projectFn = isNonEmptyArray(include)
12
+ ? projectProps(include, true)
13
+ : isNonEmptyArray(exclude)
14
+ ? projectProps(exclude, false)
15
+ : (obj) => obj;
16
+ return () => (data, state) => mapAny((data) => isObject(data) ? (state.rev ? data : projectFn(data)) : undefined, data);
17
+ };
18
+ export default transformer;
19
+ //# sourceMappingURL=project.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/transformers/project.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAQpE,MAAM,YAAY,GAAG,CAAC,QAAkB,EAAE,SAAkB,EAAE,EAAE;IAC9D,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IACvC,MAAM,QAAQ,GAAG,SAAS;QACxB,CAAC,CAAC,CAAC,CAAC,GAAG,CAAoB,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;QACnD,CAAC,CAAC,CAAC,CAAC,GAAG,CAAoB,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;IACtD,OAAO,CAAC,GAA4B,EAAE,EAAE,CACtC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;AAC5D,CAAC,CAAA;AAED,MAAM,WAAW,GAAuB,SAAS,MAAM,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE;IAE1E,MAAM,SAAS,GAAG,eAAe,CAAC,OAAO,CAAC;QACxC,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC;QAC7B,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC;YACxB,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,KAAK,CAAC;YAC9B,CAAC,CAAC,CAAC,GAA4B,EAAE,EAAE,CAAC,GAAG,CAAA;IAI3C,OAAO,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAC3B,MAAM,CACJ,CAAC,IAAI,EAAE,EAAE,CACP,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EACnE,IAAI,CACL,CAAA;AACL,CAAC,CAAA;AAED,eAAe,WAAW,CAAA"}
@@ -1,5 +1,7 @@
1
1
  export declare const isObject: (value: unknown) => value is Record<string, unknown>;
2
+ export declare const isString: (value: unknown) => value is string;
2
3
  export declare const isPath: (value: unknown) => value is string;
3
4
  export declare const isArrayPath: (value: unknown) => value is string;
4
5
  export declare const isNullOrUndefined: (value: unknown) => value is null | undefined;
5
6
  export declare const isNotNullOrUndefined: <T>(value: T) => value is NonNullable<T>;
7
+ export declare const isNonEmptyArray: <T>(value: unknown) => value is T[];
package/dist/utils/is.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export const isObject = (value) => Object.prototype.toString.call(value) === '[object Object]';
2
+ export const isString = (value) => typeof value === 'string';
2
3
  export const isPath = (value) => typeof value === 'string';
3
4
  export const isArrayPath = (value) => isPath(value) && value.endsWith('[]');
4
5
  export const isNullOrUndefined = (value) => value === null || value === undefined;
5
6
  export const isNotNullOrUndefined = (value) => !isNullOrUndefined(value);
7
+ export const isNonEmptyArray = (value) => Array.isArray(value) && value.length > 0;
6
8
  //# sourceMappingURL=is.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"is.js","sourceRoot":"","sources":["../../src/utils/is.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoC,EAAE,CAC3E,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,iBAAiB,CAAA;AAE7D,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,KAAc,EAAiB,EAAE,CACtD,OAAO,KAAK,KAAK,QAAQ,CAAA;AAE3B,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAc,EAAiB,EAAE,CAC3D,MAAM,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;AAEvC,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAA6B,EAAE,CAC7E,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAA;AAEvC,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAI,KAAQ,EAA2B,EAAE,CAC3E,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAA"}
1
+ {"version":3,"file":"is.js","sourceRoot":"","sources":["../../src/utils/is.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoC,EAAE,CAC3E,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,iBAAiB,CAAA;AAE7D,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAmB,EAAE,CAC1D,OAAO,KAAK,KAAK,QAAQ,CAAA;AAE3B,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,KAAc,EAAiB,EAAE,CACtD,OAAO,KAAK,KAAK,QAAQ,CAAA;AAE3B,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAc,EAAiB,EAAE,CAC3D,MAAM,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;AAEvC,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAA6B,EAAE,CAC7E,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAA;AAEvC,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAI,KAAQ,EAA2B,EAAE,CAC3E,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAA;AAE3B,MAAM,CAAC,MAAM,eAAe,GAAG,CAAI,KAAc,EAAgB,EAAE,CACjE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "map-transform",
3
- "version": "1.2.0",
3
+ "version": "1.3.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",