@transcend-io/type-utils 2.0.1 → 3.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.
- package/LICENSE +201 -21
- package/dist/index.d.mts +591 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +529 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +33 -43
- package/.yarn/sdks/eslint/package.json +0 -14
- package/.yarn/sdks/prettier/package.json +0 -7
- package/.yarn/sdks/typescript/package.json +0 -10
- package/README.md +0 -37
- package/build/aggregateObjects.d.ts +0 -27
- package/build/aggregateObjects.d.ts.map +0 -1
- package/build/aggregateObjects.js +0 -34
- package/build/aggregateObjects.js.map +0 -1
- package/build/apply.d.ts +0 -35
- package/build/apply.d.ts.map +0 -1
- package/build/apply.js +0 -47
- package/build/apply.js.map +0 -1
- package/build/codecTools/FixedLengthArray.d.ts +0 -20
- package/build/codecTools/FixedLengthArray.d.ts.map +0 -1
- package/build/codecTools/FixedLengthArray.js +0 -11
- package/build/codecTools/FixedLengthArray.js.map +0 -1
- package/build/codecTools/FixedLengthString.d.ts +0 -17
- package/build/codecTools/FixedLengthString.d.ts.map +0 -1
- package/build/codecTools/FixedLengthString.js +0 -10
- package/build/codecTools/FixedLengthString.js.map +0 -1
- package/build/codecTools/NonEmptyString.d.ts +0 -12
- package/build/codecTools/NonEmptyString.d.ts.map +0 -1
- package/build/codecTools/NonEmptyString.js +0 -6
- package/build/codecTools/NonEmptyString.js.map +0 -1
- package/build/codecTools/createDefaultCodec.d.ts +0 -39
- package/build/codecTools/createDefaultCodec.d.ts.map +0 -1
- package/build/codecTools/createDefaultCodec.js +0 -114
- package/build/codecTools/createDefaultCodec.js.map +0 -1
- package/build/codecTools/decodeCodec.d.ts +0 -12
- package/build/codecTools/decodeCodec.d.ts.map +0 -1
- package/build/codecTools/decodeCodec.js +0 -47
- package/build/codecTools/decodeCodec.js.map +0 -1
- package/build/codecTools/dictionary.d.ts +0 -18
- package/build/codecTools/dictionary.d.ts.map +0 -1
- package/build/codecTools/dictionary.js +0 -13
- package/build/codecTools/dictionary.js.map +0 -1
- package/build/codecTools/index.d.ts +0 -10
- package/build/codecTools/index.d.ts.map +0 -1
- package/build/codecTools/index.js +0 -10
- package/build/codecTools/index.js.map +0 -1
- package/build/codecTools/isCodecError.d.ts +0 -7
- package/build/codecTools/isCodecError.d.ts.map +0 -1
- package/build/codecTools/isCodecError.js +0 -9
- package/build/codecTools/isCodecError.js.map +0 -1
- package/build/codecTools/partialRecord.d.ts +0 -16
- package/build/codecTools/partialRecord.d.ts.map +0 -1
- package/build/codecTools/partialRecord.js +0 -11
- package/build/codecTools/partialRecord.js.map +0 -1
- package/build/codecTools/toJsonSchema.d.ts +0 -11
- package/build/codecTools/toJsonSchema.d.ts.map +0 -1
- package/build/codecTools/toJsonSchema.js +0 -115
- package/build/codecTools/toJsonSchema.js.map +0 -1
- package/build/enum.d.ts +0 -47
- package/build/enum.d.ts.map +0 -1
- package/build/enum.js +0 -44
- package/build/enum.js.map +0 -1
- package/build/enums.d.ts +0 -20
- package/build/enums.d.ts.map +0 -1
- package/build/enums.js +0 -18
- package/build/enums.js.map +0 -1
- package/build/findAllWithRegex.d.ts +0 -41
- package/build/findAllWithRegex.d.ts.map +0 -1
- package/build/findAllWithRegex.js +0 -41
- package/build/findAllWithRegex.js.map +0 -1
- package/build/flattenObject.d.ts +0 -33
- package/build/flattenObject.d.ts.map +0 -1
- package/build/flattenObject.js +0 -72
- package/build/flattenObject.js.map +0 -1
- package/build/fromEntries.d.ts +0 -9
- package/build/fromEntries.d.ts.map +0 -1
- package/build/fromEntries.js +0 -9
- package/build/fromEntries.js.map +0 -1
- package/build/getEntries.d.ts +0 -8
- package/build/getEntries.d.ts.map +0 -1
- package/build/getEntries.js +0 -10
- package/build/getEntries.js.map +0 -1
- package/build/getKeys.d.ts +0 -14
- package/build/getKeys.d.ts.map +0 -1
- package/build/getKeys.js +0 -19
- package/build/getKeys.js.map +0 -1
- package/build/getValues.d.ts +0 -9
- package/build/getValues.d.ts.map +0 -1
- package/build/getValues.js +0 -9
- package/build/getValues.js.map +0 -1
- package/build/gql.d.ts +0 -22
- package/build/gql.d.ts.map +0 -1
- package/build/gql.js +0 -32
- package/build/gql.js.map +0 -1
- package/build/groupBy.d.ts +0 -11
- package/build/groupBy.d.ts.map +0 -1
- package/build/groupBy.js +0 -20
- package/build/groupBy.js.map +0 -1
- package/build/index.d.ts +0 -19
- package/build/index.d.ts.map +0 -1
- package/build/index.js +0 -19
- package/build/index.js.map +0 -1
- package/build/invert.d.ts +0 -23
- package/build/invert.d.ts.map +0 -1
- package/build/invert.js +0 -79
- package/build/invert.js.map +0 -1
- package/build/noExcess.d.ts +0 -33
- package/build/noExcess.d.ts.map +0 -1
- package/build/noExcess.js +0 -153
- package/build/noExcess.js.map +0 -1
- package/build/tests/FixedLengthArray.test.d.ts +0 -2
- package/build/tests/FixedLengthArray.test.d.ts.map +0 -1
- package/build/tests/FixedLengthArray.test.js +0 -15
- package/build/tests/FixedLengthArray.test.js.map +0 -1
- package/build/tests/aggregateObjects.test.d.ts +0 -2
- package/build/tests/aggregateObjects.test.d.ts.map +0 -1
- package/build/tests/aggregateObjects.test.js +0 -98
- package/build/tests/aggregateObjects.test.js.map +0 -1
- package/build/tests/createDefaultCodec.test.d.ts +0 -2
- package/build/tests/createDefaultCodec.test.d.ts.map +0 -1
- package/build/tests/createDefaultCodec.test.js +0 -104
- package/build/tests/createDefaultCodec.test.js.map +0 -1
- package/build/tests/decodeCodec.test.d.ts +0 -2
- package/build/tests/decodeCodec.test.d.ts.map +0 -1
- package/build/tests/decodeCodec.test.js +0 -43
- package/build/tests/decodeCodec.test.js.map +0 -1
- package/build/tests/enum.test.d.ts +0 -2
- package/build/tests/enum.test.d.ts.map +0 -1
- package/build/tests/enum.test.js +0 -21
- package/build/tests/enum.test.js.map +0 -1
- package/build/tests/flattenObject.test.d.ts +0 -2
- package/build/tests/flattenObject.test.d.ts.map +0 -1
- package/build/tests/flattenObject.test.js +0 -115
- package/build/tests/flattenObject.test.js.map +0 -1
- package/build/tests/gql.test.d.ts +0 -2
- package/build/tests/gql.test.d.ts.map +0 -1
- package/build/tests/gql.test.js +0 -44
- package/build/tests/gql.test.js.map +0 -1
- package/build/tests/groupBy.test.d.ts +0 -2
- package/build/tests/groupBy.test.d.ts.map +0 -1
- package/build/tests/groupBy.test.js +0 -13
- package/build/tests/groupBy.test.js.map +0 -1
- package/build/tests/invert.test.d.ts +0 -2
- package/build/tests/invert.test.d.ts.map +0 -1
- package/build/tests/invert.test.js +0 -50
- package/build/tests/invert.test.js.map +0 -1
- package/build/tests/noExcess.test.d.ts +0 -2
- package/build/tests/noExcess.test.d.ts.map +0 -1
- package/build/tests/noExcess.test.js +0 -65
- package/build/tests/noExcess.test.js.map +0 -1
- package/build/tests/transposeObjectArray.test.d.ts +0 -2
- package/build/tests/transposeObjectArray.test.d.ts.map +0 -1
- package/build/tests/transposeObjectArray.test.js +0 -155
- package/build/tests/transposeObjectArray.test.js.map +0 -1
- package/build/transposeObjectArray.d.ts +0 -67
- package/build/transposeObjectArray.d.ts.map +0 -1
- package/build/transposeObjectArray.js +0 -39
- package/build/transposeObjectArray.js.map +0 -1
- package/build/tsbuildinfo +0 -1
- package/build/types.d.ts +0 -150
- package/build/types.d.ts.map +0 -1
- package/build/types.js +0 -2
- package/build/types.js.map +0 -1
- package/build/valuesOf.d.ts +0 -12
- package/build/valuesOf.d.ts.map +0 -1
- package/build/valuesOf.js +0 -14
- package/build/valuesOf.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,62 +1,52 @@
|
|
|
1
1
|
{
|
|
2
|
-
"author": "Transcend Inc.",
|
|
3
2
|
"name": "@transcend-io/type-utils",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"homepage": "https://github.com/transcend-io/type-utils",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Typed runtime and compile-time utilities for the Transcend developer tools monorepo.",
|
|
5
|
+
"homepage": "https://github.com/transcend-io/tools/tree/main/packages/type-utils",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "Transcend Inc.",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
9
|
-
"url": "https://github.com/transcend-io/
|
|
10
|
+
"url": "https://github.com/transcend-io/tools.git",
|
|
11
|
+
"directory": "packages/type-utils"
|
|
10
12
|
},
|
|
11
|
-
"
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
12
16
|
"type": "module",
|
|
13
|
-
"
|
|
14
|
-
"types": "./
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
15
19
|
"exports": {
|
|
16
20
|
".": {
|
|
17
|
-
"
|
|
18
|
-
"types": "./
|
|
19
|
-
"
|
|
20
|
-
"default": "./build/index.js"
|
|
21
|
+
"@transcend-io/source": "./src/index.ts",
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
21
24
|
}
|
|
22
25
|
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"package.json"
|
|
26
|
-
],
|
|
27
|
-
"scripts": {
|
|
28
|
-
"update:sdks": "yarn set version from sources && yarn dlx @yarnpkg/sdks base",
|
|
29
|
-
"update:deps": "yarn upgrade-interactive && yarn update:sdks",
|
|
30
|
-
"build": "yarn tsc --build",
|
|
31
|
-
"clean": "yarn tsc --build --clean",
|
|
32
|
-
"lint": "yarn eslint ./src --ext .js --ext .ts --ext .tsx --ext .jsx",
|
|
33
|
-
"prepublish": "yarn build",
|
|
34
|
-
"test": "yarn mocha './src/**/*.test.ts'"
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
35
28
|
},
|
|
36
29
|
"dependencies": {
|
|
37
30
|
"fp-ts": "^2.16.1",
|
|
38
31
|
"io-ts": "^2.2.21"
|
|
39
32
|
},
|
|
40
33
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"@types/deep-equal-in-any-order": "1.0.1",
|
|
34
|
+
"@arethetypeswrong/cli": "^0.18.2",
|
|
43
35
|
"@types/json-schema": "^7.0.15",
|
|
44
|
-
"@types/
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"depcheck": "^1.4.7",
|
|
50
|
-
"eslint": "^8.55.0",
|
|
51
|
-
"eslint-config-airbnb-base": "^15.0.0",
|
|
52
|
-
"eslint-import-resolver-typescript": "^3.6.1",
|
|
53
|
-
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
54
|
-
"eslint-plugin-import": "2.29.0",
|
|
55
|
-
"eslint-plugin-jsdoc": "^46.9.0",
|
|
56
|
-
"mocha": "^11.0.0",
|
|
57
|
-
"prettier": "^2.7.1",
|
|
58
|
-
"tsx": "^4.21.0",
|
|
59
|
-
"typescript": "^5.3.2"
|
|
36
|
+
"@types/node": "^22.19.15",
|
|
37
|
+
"publint": "^0.3.18",
|
|
38
|
+
"tsdown": "^0.21.2",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"vitest": "^4.0.18"
|
|
60
41
|
},
|
|
61
|
-
"
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22.12.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
49
|
+
"check:exports": "attw --pack . --ignore-rules cjs-resolves-to-esm",
|
|
50
|
+
"check:publint": "publint --level warning --strict --pack pnpm"
|
|
51
|
+
}
|
|
62
52
|
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "eslint",
|
|
3
|
-
"version": "8.55.0-sdk",
|
|
4
|
-
"main": "./lib/api.js",
|
|
5
|
-
"type": "commonjs",
|
|
6
|
-
"bin": {
|
|
7
|
-
"eslint": "./bin/eslint.js"
|
|
8
|
-
},
|
|
9
|
-
"exports": {
|
|
10
|
-
"./package.json": "./package.json",
|
|
11
|
-
".": "./lib/api.js",
|
|
12
|
-
"./use-at-your-own-risk": "./lib/unsupported-api.js"
|
|
13
|
-
}
|
|
14
|
-
}
|
package/README.md
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
2
|
-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
3
|
-
|
|
4
|
-
## Table of Contents
|
|
5
|
-
|
|
6
|
-
- [Overview](#overview)
|
|
7
|
-
- [Typescript Build](#typescript-build)
|
|
8
|
-
- [Lint](#lint)
|
|
9
|
-
|
|
10
|
-
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
11
|
-
|
|
12
|
-
## Overview
|
|
13
|
-
|
|
14
|
-
This package contains utilities for overriding require statement to support aliases, functions like `apply` and `decodeCodec` as well as generic typescript types useful in any typescript project.
|
|
15
|
-
|
|
16
|
-
## Typescript Build
|
|
17
|
-
|
|
18
|
-
Build this package only:
|
|
19
|
-
|
|
20
|
-
```sh
|
|
21
|
-
yarn run tsc
|
|
22
|
-
yarn run tsc --watch # Watch mode
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
Create a fresh build:
|
|
26
|
-
|
|
27
|
-
```sh
|
|
28
|
-
yarn clean && yarn run tsc
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Lint
|
|
32
|
-
|
|
33
|
-
Lint the typescript files in this package:
|
|
34
|
-
|
|
35
|
-
```sh
|
|
36
|
-
yarn lint
|
|
37
|
-
```
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* Aggregates multiple objects into a single object by combining values of matching keys.
|
|
4
|
-
* For each key present in any of the input objects, creates a comma-separated string
|
|
5
|
-
* of values from all objects.
|
|
6
|
-
* @param param - the objects to aggregate and the aggregation method
|
|
7
|
-
* @returns a single object containing all unique keys with aggregated values
|
|
8
|
-
* @example
|
|
9
|
-
* const obj1 = { name: 'John', age: 30 };
|
|
10
|
-
* const obj2 = { name: 'Jane', city: 'NY' };
|
|
11
|
-
* const obj3 = { name: 'Bob', age: 25 };
|
|
12
|
-
*
|
|
13
|
-
* // Without wrap
|
|
14
|
-
* aggregateObjects({ objs: [obj1, obj2, obj3] })
|
|
15
|
-
* // Returns: { name: 'John,Jane,Bob', age: '30,,25', city: ',NY,' }
|
|
16
|
-
*
|
|
17
|
-
* // With wrap
|
|
18
|
-
* aggregateObjects({ objs: [obj1, obj2, obj3], wrap: true })
|
|
19
|
-
* // Returns: { name: '[John],[Jane],[Bob]', age: '[30],[],[25]', city: '[],[NY],[]' }
|
|
20
|
-
*/
|
|
21
|
-
export declare const aggregateObjects: ({ objs, wrap, }: {
|
|
22
|
-
/** the objects to aggregate in a single one */
|
|
23
|
-
objs: any[];
|
|
24
|
-
/** whether to wrap the concatenated values in a [] */
|
|
25
|
-
wrap?: boolean | undefined;
|
|
26
|
-
}) => any;
|
|
27
|
-
//# sourceMappingURL=aggregateObjects.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"aggregateObjects.d.ts","sourceRoot":"","sources":["../src/aggregateObjects.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,gBAAgB;IAI3B,+CAA+C;UACzC,GAAG,EAAE;IACX,sDAAsD;;MAEpD,GAkBH,CAAC"}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* Aggregates multiple objects into a single object by combining values of matching keys.
|
|
6
|
-
* For each key present in any of the input objects, creates a comma-separated string
|
|
7
|
-
* of values from all objects.
|
|
8
|
-
* @param param - the objects to aggregate and the aggregation method
|
|
9
|
-
* @returns a single object containing all unique keys with aggregated values
|
|
10
|
-
* @example
|
|
11
|
-
* const obj1 = { name: 'John', age: 30 };
|
|
12
|
-
* const obj2 = { name: 'Jane', city: 'NY' };
|
|
13
|
-
* const obj3 = { name: 'Bob', age: 25 };
|
|
14
|
-
*
|
|
15
|
-
* // Without wrap
|
|
16
|
-
* aggregateObjects({ objs: [obj1, obj2, obj3] })
|
|
17
|
-
* // Returns: { name: 'John,Jane,Bob', age: '30,,25', city: ',NY,' }
|
|
18
|
-
*
|
|
19
|
-
* // With wrap
|
|
20
|
-
* aggregateObjects({ objs: [obj1, obj2, obj3], wrap: true })
|
|
21
|
-
* // Returns: { name: '[John],[Jane],[Bob]', age: '[30],[],[25]', city: '[],[NY],[]' }
|
|
22
|
-
*/
|
|
23
|
-
export const aggregateObjects = ({ objs, wrap = false, }) => {
|
|
24
|
-
const allKeys = Array.from(new Set(objs.flatMap((a) => (a && typeof a === 'object' ? Object.keys(a) : []))));
|
|
25
|
-
// Reduce into a single object, where each key contains concatenated values from all input objects
|
|
26
|
-
return allKeys.reduce((acc, key) => {
|
|
27
|
-
const values = objs
|
|
28
|
-
.map((o) => { var _a, _b; return (wrap ? `[${(_a = o === null || o === void 0 ? void 0 : o[key]) !== null && _a !== void 0 ? _a : ''}]` : (_b = o === null || o === void 0 ? void 0 : o[key]) !== null && _b !== void 0 ? _b : ''); })
|
|
29
|
-
.join(',');
|
|
30
|
-
acc[key] = values;
|
|
31
|
-
return acc;
|
|
32
|
-
}, {});
|
|
33
|
-
};
|
|
34
|
-
//# sourceMappingURL=aggregateObjects.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"aggregateObjects.js","sourceRoot":"","sources":["../src/aggregateObjects.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,uDAAuD;AAEvD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,EAC/B,IAAI,EACJ,IAAI,GAAG,KAAK,GAMb,EAAO,EAAE;IACR,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CACxB,IAAI,GAAG,CACL,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CACxE,CACF,CAAC;IAEF,kGAAkG;IAClG,OAAO,OAAO,CAAC,MAAM,CACnB,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACX,MAAM,MAAM,GAAG,IAAI;aAChB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,eAAC,OAAA,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,MAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAG,GAAG,CAAC,mCAAI,EAAE,GAAG,CAAC,CAAC,CAAC,MAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAG,GAAG,CAAC,mCAAI,EAAE,CAAC,CAAA,EAAA,CAAC;aAC3D,IAAI,CAAC,GAAG,CAAC,CAAC;QACb,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;QAClB,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAyB,CAC1B,CAAC;AACJ,CAAC,CAAC"}
|
package/build/apply.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { ObjByString, StringKeys } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Apply a function to each value of an object. Similar to lodash.mapValues but should preserve the typing of the keys.
|
|
4
|
-
*
|
|
5
|
-
* This allows one to define an object keys in an enum and then the resulting map should keep the same typing
|
|
6
|
-
* @param obj - The object to apply the function to
|
|
7
|
-
* @param applyFunc - The function to apply
|
|
8
|
-
* @returns The updated object
|
|
9
|
-
*/
|
|
10
|
-
export declare function apply<TInput extends ObjByString, TOutput>(obj: TInput, applyFunc: (value: TInput[keyof TInput], key: StringKeys<TInput>, fullObj: typeof obj, index: number) => TOutput): {
|
|
11
|
-
[key in keyof TInput]: TOutput;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Apply a function to each value of an object. Similar to lodash.mapValues but should preserve the typing of the keys.
|
|
15
|
-
*
|
|
16
|
-
* This allows one to define an object keys in an enum and then the resulting map should keep the same typing
|
|
17
|
-
* @param obj - The object to apply the function to
|
|
18
|
-
* @param applyFunc - The function to apply
|
|
19
|
-
* @returns The updated object
|
|
20
|
-
*/
|
|
21
|
-
export declare function asyncApply<TInput extends ObjByString, TOutput>(obj: TInput, applyFunc: (value: TInput[keyof TInput], key: StringKeys<TInput>, fullObj: typeof obj, index: number) => Promise<TOutput>): Promise<{
|
|
22
|
-
[key in keyof TInput]: TOutput;
|
|
23
|
-
}>;
|
|
24
|
-
/**
|
|
25
|
-
* Convert a typescript enum to a mapping of [value]: [value] and then call apply on that
|
|
26
|
-
* @param enm - The object to apply the function to
|
|
27
|
-
* @param applyFunc - The function to apply
|
|
28
|
-
* @returns The updated object
|
|
29
|
-
*/
|
|
30
|
-
export declare function applyEnum<TEnum extends string, TOutput>(enm: {
|
|
31
|
-
[k in string]: TEnum;
|
|
32
|
-
}, applyFunc: (value: TEnum, key: TEnum, fullObj: typeof enm, index: number) => TOutput): {
|
|
33
|
-
[key in TEnum]: TOutput;
|
|
34
|
-
};
|
|
35
|
-
//# sourceMappingURL=apply.d.ts.map
|
package/build/apply.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../src/apply.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE1D;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,MAAM,SAAS,WAAW,EAAE,OAAO,EACvD,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,CACT,KAAK,EAAE,MAAM,CAAC,MAAM,MAAM,CAAC,EAC3B,GAAG,EAAE,UAAU,CAAC,MAAM,CAAC,EACvB,OAAO,EAAE,OAAO,GAAG,EACnB,KAAK,EAAE,MAAM,KACV,OAAO,GACX;KAAG,GAAG,IAAI,MAAM,MAAM,GAAG,OAAO;CAAE,CASpC;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,MAAM,SAAS,WAAW,EAAE,OAAO,EAClE,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,CACT,KAAK,EAAE,MAAM,CAAC,MAAM,MAAM,CAAC,EAC3B,GAAG,EAAE,UAAU,CAAC,MAAM,CAAC,EACvB,OAAO,EAAE,OAAO,GAAG,EACnB,KAAK,EAAE,MAAM,KACV,OAAO,CAAC,OAAO,CAAC,GACpB,OAAO,CAAC;KAAG,GAAG,IAAI,MAAM,MAAM,GAAG,OAAO;CAAE,CAAC,CAiB7C;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,OAAO,EACrD,GAAG,EAAE;KAAG,CAAC,IAAI,MAAM,GAAG,KAAK;CAAE,EAC7B,SAAS,EAAE,CACT,KAAK,EAAE,KAAK,EACZ,GAAG,EAAE,KAAK,EACV,OAAO,EAAE,OAAO,GAAG,EACnB,KAAK,EAAE,MAAM,KACV,OAAO,GACX;KAAG,GAAG,IAAI,KAAK,GAAG,OAAO;CAAE,CAI7B"}
|
package/build/apply.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
// local
|
|
2
|
-
import { createEnum } from './enum.js';
|
|
3
|
-
import { getKeys } from './getKeys.js';
|
|
4
|
-
/**
|
|
5
|
-
* Apply a function to each value of an object. Similar to lodash.mapValues but should preserve the typing of the keys.
|
|
6
|
-
*
|
|
7
|
-
* This allows one to define an object keys in an enum and then the resulting map should keep the same typing
|
|
8
|
-
* @param obj - The object to apply the function to
|
|
9
|
-
* @param applyFunc - The function to apply
|
|
10
|
-
* @returns The updated object
|
|
11
|
-
*/
|
|
12
|
-
export function apply(obj, applyFunc) {
|
|
13
|
-
const result = Object.keys(obj).reduce((acc, key, ind) => Object.assign(acc, {
|
|
14
|
-
[key]: applyFunc(obj[key], key, obj, ind),
|
|
15
|
-
}), {});
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Apply a function to each value of an object. Similar to lodash.mapValues but should preserve the typing of the keys.
|
|
20
|
-
*
|
|
21
|
-
* This allows one to define an object keys in an enum and then the resulting map should keep the same typing
|
|
22
|
-
* @param obj - The object to apply the function to
|
|
23
|
-
* @param applyFunc - The function to apply
|
|
24
|
-
* @returns The updated object
|
|
25
|
-
*/
|
|
26
|
-
export async function asyncApply(obj, applyFunc) {
|
|
27
|
-
const entries = await Promise.all(getKeys(obj).map(async (key, ind) => ({
|
|
28
|
-
key,
|
|
29
|
-
value: await applyFunc(obj[key], key, obj, ind),
|
|
30
|
-
})));
|
|
31
|
-
const result = entries.reduce((acc, { key, value }) => Object.assign(acc, {
|
|
32
|
-
[key]: value,
|
|
33
|
-
}), {});
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Convert a typescript enum to a mapping of [value]: [value] and then call apply on that
|
|
38
|
-
* @param enm - The object to apply the function to
|
|
39
|
-
* @param applyFunc - The function to apply
|
|
40
|
-
* @returns The updated object
|
|
41
|
-
*/
|
|
42
|
-
export function applyEnum(enm, applyFunc) {
|
|
43
|
-
const obj = createEnum(Object.values(enm));
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
-
return apply(obj, applyFunc);
|
|
46
|
-
}
|
|
47
|
-
//# sourceMappingURL=apply.js.map
|
package/build/apply.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"apply.js","sourceRoot":"","sources":["../src/apply.ts"],"names":[],"mappings":"AAAA,QAAQ;AACR,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGvC;;;;;;;GAOG;AACH,MAAM,UAAU,KAAK,CACnB,GAAW,EACX,SAKY;IAEZ,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,CACpC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,CAChB,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE;QACjB,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAyB,EAAE,GAAG,EAAE,GAAG,CAAC;KAChE,CAAC,EACJ,EAAE,CACH,CAAC;IACF,OAAO,MAA4C,CAAC;AACtD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAW,EACX,SAKqB;IAErB,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAC/B,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QACpC,GAAG;QACH,KAAK,EAAE,MAAM,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAyB,EAAE,GAAG,EAAE,GAAG,CAAC;KACtE,CAAC,CAAC,CACJ,CAAC;IAEF,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAC3B,CAAC,GAAG,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,CACtB,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE;QACjB,CAAC,GAAG,CAAC,EAAE,KAAK;KACb,CAAC,EACJ,EAAE,CACH,CAAC;IAEF,OAAO,MAA4C,CAAC;AACtD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CACvB,GAA6B,EAC7B,SAKY;IAEZ,MAAM,GAAG,GAAG,UAAU,CAAe,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IACzD,8DAA8D;IAC9D,OAAO,KAAK,CAAC,GAAG,EAAE,SAAS,CAAQ,CAAC;AACtC,CAAC"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import * as t from 'io-ts';
|
|
2
|
-
export interface FixedLengthArrayBrand<T> extends Array<T> {
|
|
3
|
-
/** Unique symbol to ensure uniqueness of this type across modules/packages */
|
|
4
|
-
readonly fixedLengthArray: unique symbol;
|
|
5
|
-
/** The min length of the array */
|
|
6
|
-
readonly min: number;
|
|
7
|
-
/** The max of the array */
|
|
8
|
-
readonly max: number;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* The refinement for encoding/decoding strings of fixed length
|
|
12
|
-
* @param min - The array min length
|
|
13
|
-
* @param max - The array max length
|
|
14
|
-
* @param a - The array type
|
|
15
|
-
* @returns The fixed length array codec
|
|
16
|
-
*/
|
|
17
|
-
export declare const FixedLengthArray: <C extends t.Mixed>(min: number, max: number, a: C) => t.BrandC<t.ArrayC<C>, FixedLengthArrayBrand<C>>;
|
|
18
|
-
/** Override types */
|
|
19
|
-
export type FixedLengthArray<C extends t.Mixed> = t.TypeOf<t.BrandC<t.ArrayC<C>, FixedLengthArrayBrand<C>>>;
|
|
20
|
-
//# sourceMappingURL=FixedLengthArray.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FixedLengthArray.d.ts","sourceRoot":"","sources":["../../src/codecTools/FixedLengthArray.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,MAAM,WAAW,qBAAqB,CAAC,CAAC,CAAE,SAAQ,KAAK,CAAC,CAAC,CAAC;IACxD,8EAA8E;IAC9E,QAAQ,CAAC,gBAAgB,EAAE,OAAO,MAAM,CAAC;IAEzC,kCAAkC;IAClC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB,2BAA2B;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,2BACtB,MAAM,OACN,MAAM,0DAQV,CAAC;AAEJ,qBAAqB;AACrB,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,MAAM,CACxD,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAChD,CAAC"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// external
|
|
2
|
-
import * as t from 'io-ts';
|
|
3
|
-
/**
|
|
4
|
-
* The refinement for encoding/decoding strings of fixed length
|
|
5
|
-
* @param min - The array min length
|
|
6
|
-
* @param max - The array max length
|
|
7
|
-
* @param a - The array type
|
|
8
|
-
* @returns The fixed length array codec
|
|
9
|
-
*/
|
|
10
|
-
export const FixedLengthArray = (min, max, a) => t.brand(t.array(a), (n) => min <= n.length && n.length <= max, 'fixedLengthArray');
|
|
11
|
-
//# sourceMappingURL=FixedLengthArray.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FixedLengthArray.js","sourceRoot":"","sources":["../../src/codecTools/FixedLengthArray.ts"],"names":[],"mappings":"AAAA,WAAW;AACX,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAa3B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,GAAW,EACX,GAAW,EACX,CAAI,EAC6C,EAAE,CACnD,CAAC,CAAC,KAAK,CACL,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EACV,CAAC,CAAW,EAAsD,EAAE,CAClE,GAAG,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,IAAI,GAAG,EACpC,kBAAkB,CACnB,CAAC"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as t from 'io-ts';
|
|
2
|
-
/** The type refinement for encoding/decoding strings of fixed length */
|
|
3
|
-
export type FixedLengthStringBrand<N extends number> = {
|
|
4
|
-
/** The length of the string */
|
|
5
|
-
readonly length: N;
|
|
6
|
-
/** Unique symbol to ensure uniqueness of this type across modules/packages */
|
|
7
|
-
readonly FixedLengthString: unique symbol;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* The refinement for encoding/decoding strings of fixed length
|
|
11
|
-
* @param len - The string's length
|
|
12
|
-
* @returns The fixed length string codec
|
|
13
|
-
*/
|
|
14
|
-
export declare const FixedLengthString: <N extends number>(len: N) => t.BrandC<t.StringC, FixedLengthStringBrand<N>>;
|
|
15
|
-
/** Override types */
|
|
16
|
-
export type FixedLengthString<N extends number> = t.TypeOf<t.BrandC<t.StringC, FixedLengthStringBrand<N>>>;
|
|
17
|
-
//# sourceMappingURL=FixedLengthString.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FixedLengthString.d.ts","sourceRoot":"","sources":["../../src/codecTools/FixedLengthString.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,wEAAwE;AACxE,MAAM,MAAM,sBAAsB,CAAC,CAAC,SAAS,MAAM,IAAI;IACrD,+BAA+B;IAC/B,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IACnB,8EAA8E;IAC9E,QAAQ,CAAC,iBAAiB,EAAE,OAAO,MAAM,CAAC;CAC3C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,8EAO3B,CAAC;AAEJ,qBAAqB;AACrB,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,MAAM,CACxD,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAC/C,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// external
|
|
2
|
-
import * as t from 'io-ts';
|
|
3
|
-
/**
|
|
4
|
-
* The refinement for encoding/decoding strings of fixed length
|
|
5
|
-
* @param len - The string's length
|
|
6
|
-
* @returns The fixed length string codec
|
|
7
|
-
*/
|
|
8
|
-
export const FixedLengthString = (len) => t.brand(t.string, // a codec representing the type to be refined
|
|
9
|
-
(s) => s.length === len, 'FixedLengthString');
|
|
10
|
-
//# sourceMappingURL=FixedLengthString.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FixedLengthString.js","sourceRoot":"","sources":["../../src/codecTools/FixedLengthString.ts"],"names":[],"mappings":"AAAA,WAAW;AACX,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAU3B;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,GAAM,EAC0C,EAAE,CAClD,CAAC,CAAC,KAAK,CACL,CAAC,CAAC,MAAM,EAAE,8CAA8C;AACxD,CAAC,CAAC,EAAqD,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,GAAG,EAC1E,mBAAmB,CACpB,CAAC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as t from 'io-ts';
|
|
2
|
-
/** The type refinement for encoding/decoding non-empty strings */
|
|
3
|
-
type NonEmptyStringBrand = {
|
|
4
|
-
/** Unique symbol to ensure uniqueness of this type across modules/packages */
|
|
5
|
-
readonly NonEmptyString: symbol;
|
|
6
|
-
};
|
|
7
|
-
/** The refinement for encoding/decoding non-empty strings */
|
|
8
|
-
export declare const NonEmptyString: t.BrandC<t.StringC, NonEmptyStringBrand>;
|
|
9
|
-
/** Override types */
|
|
10
|
-
export type NonEmptyString = t.TypeOf<typeof NonEmptyString>;
|
|
11
|
-
export {};
|
|
12
|
-
//# sourceMappingURL=NonEmptyString.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NonEmptyString.d.ts","sourceRoot":"","sources":["../../src/codecTools/NonEmptyString.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,kEAAkE;AAClE,KAAK,mBAAmB,GAAG;IACzB,8EAA8E;IAC9E,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;CACjC,CAAC;AAEF,6DAA6D;AAC7D,eAAO,MAAM,cAAc,0CAI1B,CAAC;AAEF,qBAAqB;AACrB,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
// external
|
|
2
|
-
import * as t from 'io-ts';
|
|
3
|
-
/** The refinement for encoding/decoding non-empty strings */
|
|
4
|
-
export const NonEmptyString = t.brand(t.string, // a codec representing the type to be refined
|
|
5
|
-
(s) => s.trim().length !== 0, 'NonEmptyString');
|
|
6
|
-
//# sourceMappingURL=NonEmptyString.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NonEmptyString.js","sourceRoot":"","sources":["../../src/codecTools/NonEmptyString.ts"],"names":[],"mappings":"AAAA,WAAW;AACX,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAQ3B,6DAA6D;AAC7D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CACnC,CAAC,CAAC,MAAM,EAAE,8CAA8C;AACxD,CAAC,CAAC,EAA+C,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EACzE,gBAAgB,CACjB,CAAC"}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import * as t from 'io-ts';
|
|
2
|
-
/**
|
|
3
|
-
* Creates a default value for an io-ts codec.
|
|
4
|
-
* @param codec - the codec whose default we want to create
|
|
5
|
-
* @returns an object honoring the io-ts codec
|
|
6
|
-
*
|
|
7
|
-
* Use Case: CSV Generation from Typed Objects
|
|
8
|
-
* Consider the following codec:
|
|
9
|
-
*
|
|
10
|
-
* const UserCodec = t.intersection([
|
|
11
|
-
* t.type({
|
|
12
|
-
* id: t.string,
|
|
13
|
-
* name: t.string,
|
|
14
|
-
* }),
|
|
15
|
-
* t.partial({
|
|
16
|
-
* email: t.string,
|
|
17
|
-
* })
|
|
18
|
-
* ])
|
|
19
|
-
*
|
|
20
|
-
* Suppose we want to generate a CSV file from the objects below with UserCodec type:
|
|
21
|
-
*
|
|
22
|
-
* const users = [
|
|
23
|
-
* { id: '1', name: 'Alice', email: 'alice@email.com' },
|
|
24
|
-
* { id: '2', name: 'Bob' },
|
|
25
|
-
* ]
|
|
26
|
-
*
|
|
27
|
-
* To generate a CSV, we need a consistent set of columns (headers) that includes
|
|
28
|
-
* ALL possible fields, even if some objects don't have all properties. In the example
|
|
29
|
-
* above, though, notice that the second user does not have the email property.
|
|
30
|
-
*
|
|
31
|
-
* In such a case, createDefaultCodec(UserCodec) is useful to construct an object
|
|
32
|
-
* with all possible fields, which can then be used to generate the complete CSV header:
|
|
33
|
-
* "id,name,email".
|
|
34
|
-
*
|
|
35
|
-
* The example above is pretty simple, but things get very compliated the more complex
|
|
36
|
-
* is the codec. In any case, createDefaultCodec is useful for outputting the expected CSV header.
|
|
37
|
-
*/
|
|
38
|
-
export declare const createDefaultCodec: <C extends t.Mixed>(codec: C) => t.TypeOf<C>;
|
|
39
|
-
//# sourceMappingURL=createDefaultCodec.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createDefaultCodec.d.ts","sourceRoot":"","sources":["../../src/codecTools/createDefaultCodec.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,kBAAkB,8CAkG9B,CAAC"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
import * as t from 'io-ts';
|
|
4
|
-
/**
|
|
5
|
-
* Creates a default value for an io-ts codec.
|
|
6
|
-
* @param codec - the codec whose default we want to create
|
|
7
|
-
* @returns an object honoring the io-ts codec
|
|
8
|
-
*
|
|
9
|
-
* Use Case: CSV Generation from Typed Objects
|
|
10
|
-
* Consider the following codec:
|
|
11
|
-
*
|
|
12
|
-
* const UserCodec = t.intersection([
|
|
13
|
-
* t.type({
|
|
14
|
-
* id: t.string,
|
|
15
|
-
* name: t.string,
|
|
16
|
-
* }),
|
|
17
|
-
* t.partial({
|
|
18
|
-
* email: t.string,
|
|
19
|
-
* })
|
|
20
|
-
* ])
|
|
21
|
-
*
|
|
22
|
-
* Suppose we want to generate a CSV file from the objects below with UserCodec type:
|
|
23
|
-
*
|
|
24
|
-
* const users = [
|
|
25
|
-
* { id: '1', name: 'Alice', email: 'alice@email.com' },
|
|
26
|
-
* { id: '2', name: 'Bob' },
|
|
27
|
-
* ]
|
|
28
|
-
*
|
|
29
|
-
* To generate a CSV, we need a consistent set of columns (headers) that includes
|
|
30
|
-
* ALL possible fields, even if some objects don't have all properties. In the example
|
|
31
|
-
* above, though, notice that the second user does not have the email property.
|
|
32
|
-
*
|
|
33
|
-
* In such a case, createDefaultCodec(UserCodec) is useful to construct an object
|
|
34
|
-
* with all possible fields, which can then be used to generate the complete CSV header:
|
|
35
|
-
* "id,name,email".
|
|
36
|
-
*
|
|
37
|
-
* The example above is pretty simple, but things get very compliated the more complex
|
|
38
|
-
* is the codec. In any case, createDefaultCodec is useful for outputting the expected CSV header.
|
|
39
|
-
*/
|
|
40
|
-
export const createDefaultCodec = (codec) => {
|
|
41
|
-
// If the codec is an union
|
|
42
|
-
if (codec instanceof t.UnionType) {
|
|
43
|
-
// The default for a union containing arrays is a default array
|
|
44
|
-
const arrayType = codec.types.find((type) => type instanceof t.ArrayType);
|
|
45
|
-
if (arrayType) {
|
|
46
|
-
return createDefaultCodec(arrayType);
|
|
47
|
-
}
|
|
48
|
-
// If the union does not have an array, but have objects, the default is the object
|
|
49
|
-
const objectType = codec.types.find((type) => type instanceof t.InterfaceType ||
|
|
50
|
-
type instanceof t.PartialType ||
|
|
51
|
-
type instanceof t.IntersectionType ||
|
|
52
|
-
type instanceof t.ArrayType);
|
|
53
|
-
if (objectType) {
|
|
54
|
-
return createDefaultCodec(objectType);
|
|
55
|
-
}
|
|
56
|
-
// Otherwise, null is next in priority
|
|
57
|
-
const hasNull = codec.types.some((type) => type instanceof t.NullType || type.name === 'null');
|
|
58
|
-
if (hasNull) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
// If no null type found, default to first type
|
|
62
|
-
return createDefaultCodec(codec.types[0]);
|
|
63
|
-
}
|
|
64
|
-
// The default of an interface or partial type, is an object with that interface or partial type
|
|
65
|
-
if (codec instanceof t.InterfaceType || codec instanceof t.PartialType) {
|
|
66
|
-
const defaults = {};
|
|
67
|
-
Object.entries(codec.props).forEach(([key, type]) => {
|
|
68
|
-
defaults[key] = createDefaultCodec(type);
|
|
69
|
-
});
|
|
70
|
-
return defaults;
|
|
71
|
-
}
|
|
72
|
-
// The default of an intersection, is the merged defaults of the intersection values
|
|
73
|
-
if (codec instanceof t.IntersectionType) {
|
|
74
|
-
return codec.types.reduce((acc, type) => ({
|
|
75
|
-
...acc,
|
|
76
|
-
...createDefaultCodec(type),
|
|
77
|
-
}), {});
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* The default of an Array is an empty array, unless it is an array of objects, in which
|
|
81
|
-
* case the default is an array of one default object.
|
|
82
|
-
*/
|
|
83
|
-
if (codec instanceof t.ArrayType) {
|
|
84
|
-
const elementType = codec.type;
|
|
85
|
-
const isObjectType = elementType instanceof t.InterfaceType ||
|
|
86
|
-
elementType instanceof t.PartialType ||
|
|
87
|
-
elementType instanceof t.IntersectionType;
|
|
88
|
-
return (isObjectType ? [createDefaultCodec(elementType)] : []);
|
|
89
|
-
}
|
|
90
|
-
// The default of a literal type is its value
|
|
91
|
-
if (codec instanceof t.LiteralType) {
|
|
92
|
-
return codec.value;
|
|
93
|
-
}
|
|
94
|
-
// The default of an object type is an empty object
|
|
95
|
-
if (codec instanceof t.ObjectType) {
|
|
96
|
-
return {};
|
|
97
|
-
}
|
|
98
|
-
// Handle primitive and common types
|
|
99
|
-
switch (codec.name) {
|
|
100
|
-
case 'string':
|
|
101
|
-
return '';
|
|
102
|
-
case 'number':
|
|
103
|
-
return 0;
|
|
104
|
-
case 'boolean':
|
|
105
|
-
return false;
|
|
106
|
-
case 'null':
|
|
107
|
-
return null;
|
|
108
|
-
case 'undefined':
|
|
109
|
-
return undefined;
|
|
110
|
-
default:
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
//# sourceMappingURL=createDefaultCodec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createDefaultCodec.js","sourceRoot":"","sources":["../../src/codecTools/createDefaultCodec.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,uDAAuD;AACvD,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,KAAQ,EACK,EAAE;IACf,2BAA2B;IAC3B,IAAI,KAAK,YAAY,CAAC,CAAC,SAAS,EAAE,CAAC;QACjC,+DAA+D;QAC/D,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAChC,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,CAAC,SAAS,CAC3C,CAAC;QACF,IAAI,SAAS,EAAE,CAAC;YACd,OAAO,kBAAkB,CAAC,SAAS,CAAC,CAAC;QACvC,CAAC;QAED,mFAAmF;QACnF,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CACjC,CAAC,IAAS,EAAE,EAAE,CACZ,IAAI,YAAY,CAAC,CAAC,aAAa;YAC/B,IAAI,YAAY,CAAC,CAAC,WAAW;YAC7B,IAAI,YAAY,CAAC,CAAC,gBAAgB;YAClC,IAAI,YAAY,CAAC,CAAC,SAAS,CAC9B,CAAC;QACF,IAAI,UAAU,EAAE,CAAC;YACf,OAAO,kBAAkB,CAAC,UAAU,CAAC,CAAC;QACxC,CAAC;QAED,sCAAsC;QACtC,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAC9B,CAAC,IAAS,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAClE,CAAC;QACF,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,IAAmB,CAAC;QAC7B,CAAC;QAED,+CAA+C;QAC/C,OAAO,kBAAkB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED,gGAAgG;IAChG,IAAI,KAAK,YAAY,CAAC,CAAC,aAAa,IAAI,KAAK,YAAY,CAAC,CAAC,WAAW,EAAE,CAAC;QACvE,MAAM,QAAQ,GAAwB,EAAE,CAAC;QACzC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE;YAClD,QAAQ,CAAC,GAAG,CAAC,GAAG,kBAAkB,CAAC,IAAW,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;QACH,OAAO,QAAuB,CAAC;IACjC,CAAC;IAED,oFAAoF;IACpF,IAAI,KAAK,YAAY,CAAC,CAAC,gBAAgB,EAAE,CAAC;QACxC,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,CACvB,CAAC,GAAgB,EAAE,IAAS,EAAE,EAAE,CAAC,CAAC;YAChC,GAAG,GAAG;YACN,GAAG,kBAAkB,CAAC,IAAI,CAAC;SAC5B,CAAC,EACF,EAAE,CACH,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,IAAI,KAAK,YAAY,CAAC,CAAC,SAAS,EAAE,CAAC;QACjC,MAAM,WAAW,GAAG,KAAK,CAAC,IAAI,CAAC;QAC/B,MAAM,YAAY,GAChB,WAAW,YAAY,CAAC,CAAC,aAAa;YACtC,WAAW,YAAY,CAAC,CAAC,WAAW;YACpC,WAAW,YAAY,CAAC,CAAC,gBAAgB,CAAC;QAE5C,OAAO,CACL,YAAY,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CACvC,CAAC;IACnB,CAAC;IAED,6CAA6C;IAC7C,IAAI,KAAK,YAAY,CAAC,CAAC,WAAW,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC,KAAoB,CAAC;IACpC,CAAC;IAED,mDAAmD;IACnD,IAAI,KAAK,YAAY,CAAC,CAAC,UAAU,EAAE,CAAC;QAClC,OAAO,EAAiB,CAAC;IAC3B,CAAC;IAED,oCAAoC;IACpC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,QAAQ;YACX,OAAO,EAAiB,CAAC;QAC3B,KAAK,QAAQ;YACX,OAAO,CAAgB,CAAC;QAC1B,KAAK,SAAS;YACZ,OAAO,KAAoB,CAAC;QAC9B,KAAK,MAAM;YACT,OAAO,IAAmB,CAAC;QAC7B,KAAK,WAAW;YACd,OAAO,SAAwB,CAAC;QAClC;YACE,OAAO,IAAmB,CAAC;IAC/B,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as t from 'io-ts';
|
|
2
|
-
export declare const CODEC_ERROR_MESSAGE = "Failed to decode codec:";
|
|
3
|
-
/**
|
|
4
|
-
* Decode a codec, returning the value if contents are validated, else throws an error
|
|
5
|
-
* @param codec - The codec to decode with
|
|
6
|
-
* @param txt - The text to JSON parse
|
|
7
|
-
* @param parse - If a string is provided, assume it is JSON and call JSON.parse on it
|
|
8
|
-
* @param customErrorFromContext - a function to custom process validation contexts to error messages
|
|
9
|
-
* @returns The result of the function or the object if it was not a function
|
|
10
|
-
*/
|
|
11
|
-
export declare function decodeCodec<TCodec extends t.Any>(codec: TCodec, txt: string | object | object[] | null | unknown, parse?: boolean, customErrorFromContext?: ((validationContext: t.Context) => string) | undefined): t.TypeOf<TCodec>;
|
|
12
|
-
//# sourceMappingURL=decodeCodec.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decodeCodec.d.ts","sourceRoot":"","sources":["../../src/codecTools/decodeCodec.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AA0C3B,eAAO,MAAM,mBAAmB,4BAA4B,CAAC;AAE7D;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,MAAM,SAAS,CAAC,CAAC,GAAG,EAC9C,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,GAAG,OAAO,EAChD,KAAK,UAAO,EACZ,sBAAsB,GAClB,CAAC,CAAC,iBAAiB,EAAE,CAAC,CAAC,OAAO,KAAK,MAAM,CAAC,GAC1C,SAAqB,GACxB,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAyBlB"}
|