@transcend-io/type-utils 1.8.5 → 2.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/build/aggregateObjects.js +1 -5
- package/build/aggregateObjects.js.map +1 -1
- package/build/apply.d.ts +1 -1
- package/build/apply.d.ts.map +1 -1
- package/build/apply.js +7 -13
- package/build/apply.js.map +1 -1
- package/build/codecTools/FixedLengthArray.js +2 -29
- package/build/codecTools/FixedLengthArray.js.map +1 -1
- package/build/codecTools/FixedLengthString.js +2 -29
- package/build/codecTools/FixedLengthString.js.map +1 -1
- package/build/codecTools/NonEmptyString.js +2 -28
- package/build/codecTools/NonEmptyString.js.map +1 -1
- package/build/codecTools/createDefaultCodec.js +8 -35
- package/build/codecTools/createDefaultCodec.js.map +1 -1
- package/build/codecTools/decodeCodec.js +8 -12
- package/build/codecTools/decodeCodec.js.map +1 -1
- package/build/codecTools/dictionary.js +3 -30
- package/build/codecTools/dictionary.js.map +1 -1
- package/build/codecTools/index.d.ts +9 -9
- package/build/codecTools/index.d.ts.map +1 -1
- package/build/codecTools/index.js +9 -25
- package/build/codecTools/index.js.map +1 -1
- package/build/codecTools/isCodecError.js +1 -5
- package/build/codecTools/isCodecError.js.map +1 -1
- package/build/codecTools/partialRecord.js +2 -29
- package/build/codecTools/partialRecord.js.map +1 -1
- package/build/codecTools/toJsonSchema.js +10 -14
- package/build/codecTools/toJsonSchema.js.map +1 -1
- package/build/enum.d.ts +1 -1
- package/build/enum.d.ts.map +1 -1
- package/build/enum.js +4 -11
- package/build/enum.js.map +1 -1
- package/build/enums.js +2 -5
- package/build/enums.js.map +1 -1
- package/build/findAllWithRegex.js +1 -5
- package/build/findAllWithRegex.js.map +1 -1
- package/build/flattenObject.js +5 -9
- package/build/flattenObject.js.map +1 -1
- package/build/fromEntries.js +1 -5
- package/build/fromEntries.js.map +1 -1
- package/build/getEntries.d.ts +1 -1
- package/build/getEntries.d.ts.map +1 -1
- package/build/getEntries.js +1 -5
- package/build/getEntries.js.map +1 -1
- package/build/getKeys.d.ts +1 -1
- package/build/getKeys.d.ts.map +1 -1
- package/build/getKeys.js +2 -7
- package/build/getKeys.js.map +1 -1
- package/build/getValues.js +1 -5
- package/build/getValues.js.map +1 -1
- package/build/gql.js +4 -8
- package/build/gql.js.map +1 -1
- package/build/groupBy.js +1 -5
- package/build/groupBy.js.map +1 -1
- package/build/index.d.ts +18 -17
- package/build/index.d.ts.map +1 -1
- package/build/index.js +18 -33
- package/build/index.js.map +1 -1
- package/build/invert.js +5 -10
- package/build/invert.js.map +1 -1
- package/build/noExcess.d.ts +33 -0
- package/build/noExcess.d.ts.map +1 -0
- package/build/noExcess.js +153 -0
- package/build/noExcess.js.map +1 -0
- package/build/tests/FixedLengthArray.test.js +6 -31
- package/build/tests/FixedLengthArray.test.js.map +1 -1
- package/build/tests/aggregateObjects.test.js +20 -22
- package/build/tests/aggregateObjects.test.js.map +1 -1
- package/build/tests/createDefaultCodec.test.js +39 -67
- package/build/tests/createDefaultCodec.test.js.map +1 -1
- package/build/tests/decodeCodec.test.js +7 -32
- package/build/tests/decodeCodec.test.js.map +1 -1
- package/build/tests/enum.test.js +7 -9
- package/build/tests/enum.test.js.map +1 -1
- package/build/tests/flattenObject.test.js +16 -18
- package/build/tests/flattenObject.test.js.map +1 -1
- package/build/tests/gql.test.js +6 -8
- package/build/tests/gql.test.js.map +1 -1
- package/build/tests/groupBy.test.js +3 -5
- package/build/tests/groupBy.test.js.map +1 -1
- package/build/tests/invert.test.js +6 -8
- package/build/tests/invert.test.js.map +1 -1
- package/build/tests/noExcess.test.d.ts +2 -0
- package/build/tests/noExcess.test.d.ts.map +1 -0
- package/build/tests/noExcess.test.js +65 -0
- package/build/tests/noExcess.test.js.map +1 -0
- package/build/tests/transposeObjectArray.test.js +22 -24
- package/build/tests/transposeObjectArray.test.js.map +1 -1
- package/build/transposeObjectArray.js +1 -5
- package/build/transposeObjectArray.js.map +1 -1
- package/build/tsbuildinfo +1 -1
- package/build/types.js +1 -2
- package/build/valuesOf.js +4 -31
- package/build/valuesOf.js.map +1 -1
- package/package.json +14 -8
package/build/invert.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.invertSafe = exports.invert = void 0;
|
|
4
1
|
// local
|
|
5
|
-
|
|
2
|
+
import { getEntries } from './getEntries.js';
|
|
6
3
|
/**
|
|
7
4
|
* Invert an object so that the values look up the keys.
|
|
8
5
|
* If the object has an array as the value, each item in the array will be inverted.
|
|
@@ -10,11 +7,11 @@ const getEntries_1 = require("./getEntries");
|
|
|
10
7
|
* @param throwOnDuplicate - When true, throw error if duplicate key detected
|
|
11
8
|
* @returns The inverted object
|
|
12
9
|
*/
|
|
13
|
-
function invert(obj, throwOnDuplicate = true) {
|
|
10
|
+
export function invert(obj, throwOnDuplicate = true) {
|
|
14
11
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
12
|
const result = {};
|
|
16
13
|
// Invert
|
|
17
|
-
|
|
14
|
+
getEntries(obj).forEach(([key, instance]) => {
|
|
18
15
|
// Ensure no undefined values
|
|
19
16
|
if (instance === undefined) {
|
|
20
17
|
throw new Error('inverse found undefined value, this is not supported');
|
|
@@ -42,17 +39,16 @@ function invert(obj, throwOnDuplicate = true) {
|
|
|
42
39
|
});
|
|
43
40
|
return result;
|
|
44
41
|
}
|
|
45
|
-
exports.invert = invert;
|
|
46
42
|
/**
|
|
47
43
|
* Safely invert an object to be { [key in TValue]: TKey[] }
|
|
48
44
|
* @param obj - The object to invert
|
|
49
45
|
* @returns The inverted object
|
|
50
46
|
*/
|
|
51
|
-
function invertSafe(obj) {
|
|
47
|
+
export function invertSafe(obj) {
|
|
52
48
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
49
|
const result = {};
|
|
54
50
|
// Invert
|
|
55
|
-
|
|
51
|
+
getEntries(obj).forEach(([key, instance]) => {
|
|
56
52
|
// Ensure no undefined values
|
|
57
53
|
if (instance === undefined) {
|
|
58
54
|
throw new Error('inverse found undefined value, this is not supported');
|
|
@@ -80,5 +76,4 @@ function invertSafe(obj) {
|
|
|
80
76
|
});
|
|
81
77
|
return result;
|
|
82
78
|
}
|
|
83
|
-
exports.invertSafe = invertSafe;
|
|
84
79
|
//# sourceMappingURL=invert.js.map
|
package/build/invert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invert.js","sourceRoot":"","sources":["../src/invert.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"invert.js","sourceRoot":"","sources":["../src/invert.ts"],"names":[],"mappings":"AAAA,QAAQ;AACR,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;GAMG;AACH,MAAM,UAAU,MAAM,CACpB,GAA+B,EAC/B,gBAAgB,GAAG,IAAI;IAOvB,8DAA8D;IAC9D,MAAM,MAAM,GAAQ,EAAS,CAAC;IAE9B,SAAS;IACT,UAAU,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,QAAQ,CAA6B,EAAE,EAAE;QACtE,6BAA6B;QAC7B,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,CAAC;QAED,oBAAoB;QACpB,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC5B,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBAC3B,qBAAqB;gBACrB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;oBACrB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBAC1B,CAAC;qBAAM,CAAC;oBACN,kBAAkB;oBAClB,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAA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G,CAAC,GAAG,CAAC,CAAC;QAC3B,CAAC;aAAM,CAAC;YACN,kBAAkB;YAClB,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as t from 'io-ts';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an enhanced codec that validates objects with strict property checking.
|
|
4
|
+
*
|
|
5
|
+
* The enhanced codec performs validation in three steps:
|
|
6
|
+
* 1. Validates structure using the original codec
|
|
7
|
+
* 2. Checks for excess properties using stripKeys
|
|
8
|
+
* 3. Returns validation errors for any unexpected keys with the message "excess key "{key}" found"
|
|
9
|
+
*
|
|
10
|
+
* @template C - The codec type that extends HasProps
|
|
11
|
+
* @param codec - The base codec to enhance with excess property checking
|
|
12
|
+
* @param name - Optional custom name for the enhanced type (auto-generated if not provided)
|
|
13
|
+
* @returns A new NoExcessType that wraps the original codec with strict validation
|
|
14
|
+
*/
|
|
15
|
+
export declare const noExcess: <C extends t.HasProps>(codec: C, name?: string) => NoExcessType<C, C["_A"], C["_A"], unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* The NoExcess Type - a wrapper type to be used with io-ts to ensure there aren't any excess keys
|
|
18
|
+
*/
|
|
19
|
+
export declare class NoExcessType<C extends t.Any, A = C['_A'], O = A, I = unknown> extends t.Type<A, O, I> {
|
|
20
|
+
readonly type: C;
|
|
21
|
+
readonly _tag: "NoExcessType";
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new NoExcessType instance that wraps another codec with excess property validation.
|
|
24
|
+
*
|
|
25
|
+
* @param name - The name of this type for debugging and error messages
|
|
26
|
+
* @param is - Type guard function that checks if a value is of this type
|
|
27
|
+
* @param validate - Validation function that decodes input and validates structure
|
|
28
|
+
* @param encode - Encoding function that converts the type back to its output representation
|
|
29
|
+
* @param type - The original wrapped codec that this NoExcessType enhances
|
|
30
|
+
*/
|
|
31
|
+
constructor(name: string, is: NoExcessType<C, A, O, I>['is'], validate: NoExcessType<C, A, O, I>['validate'], encode: NoExcessType<C, A, O, I>['encode'], type: C);
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=noExcess.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noExcess.d.ts","sourceRoot":"","sources":["../src/noExcess.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AA6H3B;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,QAAQ,0CAEb,MAAM,+CAsBb,CAAC;AAEF;;GAEG;AACH,qBAAa,YAAY,CACvB,CAAC,SAAS,CAAC,CAAC,GAAG,EACf,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EACX,CAAC,GAAG,CAAC,EACL,CAAC,GAAG,OAAO,CACX,SAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;aAiBL,IAAI,EAAE,CAAC;IAhBzB,SAAgB,IAAI,iBAA2B;IAE/C;;;;;;;;OAQG;gBAED,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAClC,QAAQ,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,EAC9C,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAC1B,IAAI,EAAE,CAAC;CAI1B"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { either, isRight, left, right } from 'fp-ts/Either';
|
|
2
|
+
import * as t from 'io-ts';
|
|
3
|
+
/**
|
|
4
|
+
* Yanked from https://github.com/gcanti/io-ts/issues/322#issuecomment-584658211
|
|
5
|
+
*
|
|
6
|
+
* The code creates a way to reject objects that have extra/unexpected properties
|
|
7
|
+
* beyond what's defined in a type schema. This is useful for strict validation
|
|
8
|
+
* where you want to ensure incoming data contains only expected fields.
|
|
9
|
+
* @example usage
|
|
10
|
+
* ```
|
|
11
|
+
* const UserCodec = t.type({
|
|
12
|
+
* name: t.string,
|
|
13
|
+
* age: t.number
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* const StrictUserCodec = noExcess(UserCodec);
|
|
17
|
+
* // This would pass
|
|
18
|
+
* StrictUserCodec.decode({ name: "Kirby", age: 33 })
|
|
19
|
+
*
|
|
20
|
+
* // This would fail with "excess key 'email' found"
|
|
21
|
+
* StrictUserCodec.decode({ name: "Kirby", age: 33, email: "kirby@transcend.io" })
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Creates a type guard function that checks if a codec matches a specific tag.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The expected codec type
|
|
28
|
+
* @param tag - The tag string to match against (e.g., 'InterfaceType', 'PartialType')
|
|
29
|
+
* @returns A type guard function that checks if a codec is of type T
|
|
30
|
+
*/
|
|
31
|
+
const getIsCodec = (tag) => (codec) =>
|
|
32
|
+
/* eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-explicit-any */
|
|
33
|
+
codec._tag === tag;
|
|
34
|
+
const isInterfaceCodec = getIsCodec('InterfaceType');
|
|
35
|
+
const isPartialCodec = getIsCodec('PartialType');
|
|
36
|
+
/**
|
|
37
|
+
* Extracts property definitions from various codec types, handling
|
|
38
|
+
* nested structures like intersections and refinements.
|
|
39
|
+
*
|
|
40
|
+
* @param codec - The codec to extract properties from
|
|
41
|
+
* @returns The props object containing all property definitions
|
|
42
|
+
*/
|
|
43
|
+
const getProps = (codec) => {
|
|
44
|
+
/* eslint-disable-next-line no-underscore-dangle */
|
|
45
|
+
switch (codec._tag) {
|
|
46
|
+
case 'RefinementType':
|
|
47
|
+
case 'ReadonlyType':
|
|
48
|
+
return getProps(codec.type);
|
|
49
|
+
case 'InterfaceType':
|
|
50
|
+
case 'StrictType':
|
|
51
|
+
case 'PartialType':
|
|
52
|
+
return codec.props;
|
|
53
|
+
case 'IntersectionType':
|
|
54
|
+
return codec.types.reduce((props, type) => Object.assign(props, getProps(type)), {});
|
|
55
|
+
default:
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Generates a string representation of props for type naming.
|
|
61
|
+
*
|
|
62
|
+
* @param props - The props object to convert to string
|
|
63
|
+
* @returns A comma-separated string of "key: type" pairs
|
|
64
|
+
*/
|
|
65
|
+
const getNameFromProps = (props) => Object.keys(props)
|
|
66
|
+
.map((k) => `${k}: ${props[k].name}`)
|
|
67
|
+
.join(', ');
|
|
68
|
+
/**
|
|
69
|
+
* Wraps a type name with Partial<> syntax for partial types.
|
|
70
|
+
*
|
|
71
|
+
* @param inner - The inner type string to wrap
|
|
72
|
+
* @returns The wrapped type string in Partial<> format
|
|
73
|
+
*/
|
|
74
|
+
const getPartialTypeName = (inner) => `Partial<${inner}>`;
|
|
75
|
+
/**
|
|
76
|
+
* Generates a human-readable type name for the noExcess wrapper.
|
|
77
|
+
*
|
|
78
|
+
* @param codec - The codec to generate a name for
|
|
79
|
+
* @returns A string representation of the excess-checked type
|
|
80
|
+
*/
|
|
81
|
+
const getNoExcessTypeName = (codec) => {
|
|
82
|
+
if (isInterfaceCodec(codec)) {
|
|
83
|
+
return `{| ${getNameFromProps(codec.props)} |}`;
|
|
84
|
+
}
|
|
85
|
+
if (isPartialCodec(codec)) {
|
|
86
|
+
return getPartialTypeName(`{| ${getNameFromProps(codec.props)} |}`);
|
|
87
|
+
}
|
|
88
|
+
return `Excess<${codec.name}>`;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Compares an object's keys against expected properties and returns either
|
|
92
|
+
* the object (if valid) or a list of excess keys.
|
|
93
|
+
*
|
|
94
|
+
* @template T - The type of the object being checked
|
|
95
|
+
* @param o - The object to check for excess keys
|
|
96
|
+
* @param props - The expected properties definition
|
|
97
|
+
* @returns Either a Left with excess keys array or Right with the valid object
|
|
98
|
+
*/
|
|
99
|
+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
100
|
+
const stripKeys = (o, props) => {
|
|
101
|
+
const keys = Object.getOwnPropertyNames(o);
|
|
102
|
+
const propsKeys = Object.getOwnPropertyNames(props);
|
|
103
|
+
propsKeys.forEach((pk) => {
|
|
104
|
+
const index = keys.indexOf(pk);
|
|
105
|
+
if (index !== -1) {
|
|
106
|
+
keys.splice(index, 1);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return keys.length ? left(keys) : right(o);
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Creates an enhanced codec that validates objects with strict property checking.
|
|
113
|
+
*
|
|
114
|
+
* The enhanced codec performs validation in three steps:
|
|
115
|
+
* 1. Validates structure using the original codec
|
|
116
|
+
* 2. Checks for excess properties using stripKeys
|
|
117
|
+
* 3. Returns validation errors for any unexpected keys with the message "excess key "{key}" found"
|
|
118
|
+
*
|
|
119
|
+
* @template C - The codec type that extends HasProps
|
|
120
|
+
* @param codec - The base codec to enhance with excess property checking
|
|
121
|
+
* @param name - Optional custom name for the enhanced type (auto-generated if not provided)
|
|
122
|
+
* @returns A new NoExcessType that wraps the original codec with strict validation
|
|
123
|
+
*/
|
|
124
|
+
export const noExcess = (codec, name = getNoExcessTypeName(codec)) => {
|
|
125
|
+
const props = getProps(codec);
|
|
126
|
+
return new NoExcessType(name, (u) => isRight(stripKeys(u, props)) && codec.is(u), (u, c) => either.chain(t.UnknownRecord.validate(u, c), () => either.chain(codec.validate(u, c), (a) => either.mapLeft(stripKeys(a, props), (keys) => keys.map((k) => ({
|
|
127
|
+
value: a[k],
|
|
128
|
+
context: c,
|
|
129
|
+
message: `excess key "${k}" found`,
|
|
130
|
+
}))))),
|
|
131
|
+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
132
|
+
(a) => codec.encode(stripKeys(a, props).right), codec);
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* The NoExcess Type - a wrapper type to be used with io-ts to ensure there aren't any excess keys
|
|
136
|
+
*/
|
|
137
|
+
export class NoExcessType extends t.Type {
|
|
138
|
+
/**
|
|
139
|
+
* Creates a new NoExcessType instance that wraps another codec with excess property validation.
|
|
140
|
+
*
|
|
141
|
+
* @param name - The name of this type for debugging and error messages
|
|
142
|
+
* @param is - Type guard function that checks if a value is of this type
|
|
143
|
+
* @param validate - Validation function that decodes input and validates structure
|
|
144
|
+
* @param encode - Encoding function that converts the type back to its output representation
|
|
145
|
+
* @param type - The original wrapped codec that this NoExcessType enhances
|
|
146
|
+
*/
|
|
147
|
+
constructor(name, is, validate, encode, type) {
|
|
148
|
+
super(name, is, validate, encode);
|
|
149
|
+
this.type = type;
|
|
150
|
+
this._tag = 'NoExcessType';
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=noExcess.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noExcess.js","sourceRoot":"","sources":["../src/noExcess.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;;;;GAMG;AACH,MAAM,UAAU,GACd,CAAkB,GAAW,EAAE,EAAE,CACjC,CAAC,KAAY,EAAc,EAAE;AAC3B,uFAAuF;AACtF,KAAa,CAAC,IAAI,KAAK,GAAG,CAAC;AAChC,MAAM,gBAAgB,GAAG,UAAU,CAA2B,eAAe,CAAC,CAAC;AAC/E,MAAM,cAAc,GAAG,UAAU,CAAyB,aAAa,CAAC,CAAC;AAEzE;;;;;;GAMG;AACH,MAAM,QAAQ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ChD,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CACvC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAI,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE,CAC9C,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACf,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;QACX,OAAO,EAAE,CAAC;QACV,OAAO,EAAE,eAAe,CAAC,SAAS;KACnC,CAAC,CAAC,CACJ,CACF,CACF;IACH,iEAAiE;IACjE,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAE,SAAS,CAAC,CAAC,EAAE,KAAK,CAAgB,CAAC,KAAK,CAAC,EAC9D,KAAK,CACN,CAAC;AACJ,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,OAAO,YAKX,SAAQ,CAAC,CAAC,IAAa;IAGvB;;;;;;;;OAQG;IACH,YACE,IAAY,EACZ,EAAkC,EAClC,QAA8C,EAC9C,MAA0C,EAC1B,IAAO;QAEvB,KAAK,CAAC,IAAI,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;QAFlB,SAAI,GAAJ,IAAI,CAAG;QAhBT,SAAI,GAAG,cAAuB,CAAC;IAmB/C,CAAC;CACF"}
|
|
@@ -1,40 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
1
|
// external
|
|
27
|
-
|
|
28
|
-
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import * as t from 'io-ts';
|
|
29
4
|
// global
|
|
30
|
-
|
|
5
|
+
import { decodeCodec, FixedLengthArray } from '../codecTools/index.js';
|
|
31
6
|
describe('FixedLengthArray', () => {
|
|
32
|
-
const TwoItemArray =
|
|
7
|
+
const TwoItemArray = FixedLengthArray(2, 2, t.number);
|
|
33
8
|
it('decode a fixed length array successfully', () => {
|
|
34
|
-
|
|
9
|
+
expect(() => decodeCodec(TwoItemArray, [1, 2])).to.not.throw();
|
|
35
10
|
});
|
|
36
11
|
it('error if the array does not have the expected length', () => {
|
|
37
|
-
|
|
12
|
+
expect(() => decodeCodec(TwoItemArray, [1, 2, 3])).to.throw();
|
|
38
13
|
});
|
|
39
14
|
});
|
|
40
15
|
//# sourceMappingURL=FixedLengthArray.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FixedLengthArray.test.js","sourceRoot":"","sources":["../../src/tests/FixedLengthArray.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FixedLengthArray.test.js","sourceRoot":"","sources":["../../src/tests/FixedLengthArray.test.ts"],"names":[],"mappings":"AAAA,WAAW;AACX,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,SAAS;AACT,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAEvE,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAChC,MAAM,YAAY,GAAG,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IAEtD,EAAE,CAAC,0CAA0C,EAAE,GAAG,EAAE;QAClD,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;IACjE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAC9D,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IAChE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const aggregateObjects_1 = require("../aggregateObjects");
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { aggregateObjects } from '../aggregateObjects.js';
|
|
5
3
|
describe('aggregateObjects', () => {
|
|
6
4
|
it('should return empty object for empty input array', () => {
|
|
7
|
-
const result =
|
|
8
|
-
|
|
5
|
+
const result = aggregateObjects({ objs: [] });
|
|
6
|
+
expect(result).to.deep.equal({});
|
|
9
7
|
});
|
|
10
8
|
it('should aggregate objects with same keys', () => {
|
|
11
9
|
const objs = [
|
|
@@ -13,8 +11,8 @@ describe('aggregateObjects', () => {
|
|
|
13
11
|
{ name: 'Jane', age: 25 },
|
|
14
12
|
{ name: 'Bob', age: 35 },
|
|
15
13
|
];
|
|
16
|
-
const result =
|
|
17
|
-
|
|
14
|
+
const result = aggregateObjects({ objs });
|
|
15
|
+
expect(result).to.deep.equal({
|
|
18
16
|
name: 'John,Jane,Bob',
|
|
19
17
|
age: '30,25,35',
|
|
20
18
|
});
|
|
@@ -25,8 +23,8 @@ describe('aggregateObjects', () => {
|
|
|
25
23
|
{ name: 'Jane' },
|
|
26
24
|
{ name: 'Bob', age: 35 },
|
|
27
25
|
];
|
|
28
|
-
const result =
|
|
29
|
-
|
|
26
|
+
const result = aggregateObjects({ objs });
|
|
27
|
+
expect(result).to.deep.equal({
|
|
30
28
|
name: 'John,Jane,Bob',
|
|
31
29
|
age: '30,,35',
|
|
32
30
|
});
|
|
@@ -37,8 +35,8 @@ describe('aggregateObjects', () => {
|
|
|
37
35
|
{ name: undefined, hobby: 'reading' },
|
|
38
36
|
{ name: 'Bob', hobby: null },
|
|
39
37
|
];
|
|
40
|
-
const result =
|
|
41
|
-
|
|
38
|
+
const result = aggregateObjects({ objs });
|
|
39
|
+
expect(result).to.deep.equal({
|
|
42
40
|
name: 'John,,Bob',
|
|
43
41
|
age: ',,',
|
|
44
42
|
hobby: ',reading,',
|
|
@@ -50,8 +48,8 @@ describe('aggregateObjects', () => {
|
|
|
50
48
|
{ name: 'Jane' },
|
|
51
49
|
{ name: 'Bob', age: 35 },
|
|
52
50
|
];
|
|
53
|
-
const result =
|
|
54
|
-
|
|
51
|
+
const result = aggregateObjects({ objs, wrap: true });
|
|
52
|
+
expect(result).to.deep.equal({
|
|
55
53
|
name: '[John],[Jane],[Bob]',
|
|
56
54
|
age: '[30],[],[35]',
|
|
57
55
|
});
|
|
@@ -62,8 +60,8 @@ describe('aggregateObjects', () => {
|
|
|
62
60
|
{ city: 'NY', country: 'USA' },
|
|
63
61
|
{ name: 'Bob', country: 'UK' },
|
|
64
62
|
];
|
|
65
|
-
const result =
|
|
66
|
-
|
|
63
|
+
const result = aggregateObjects({ objs });
|
|
64
|
+
expect(result).to.deep.equal({
|
|
67
65
|
name: 'John,,Bob',
|
|
68
66
|
age: '30,,',
|
|
69
67
|
city: ',NY,',
|
|
@@ -72,15 +70,15 @@ describe('aggregateObjects', () => {
|
|
|
72
70
|
});
|
|
73
71
|
it('should handle empty objects', () => {
|
|
74
72
|
const objs = [{ name: 'John' }, {}, { name: 'Bob' }];
|
|
75
|
-
const result =
|
|
76
|
-
|
|
73
|
+
const result = aggregateObjects({ objs });
|
|
74
|
+
expect(result).to.deep.equal({
|
|
77
75
|
name: 'John,,Bob',
|
|
78
76
|
});
|
|
79
77
|
});
|
|
80
78
|
it('should handle array with null or undefined objects', () => {
|
|
81
79
|
const objs = [{ name: 'John' }, null, undefined, { name: 'Bob' }];
|
|
82
|
-
const result =
|
|
83
|
-
|
|
80
|
+
const result = aggregateObjects({ objs });
|
|
81
|
+
expect(result).to.deep.equal({
|
|
84
82
|
name: 'John,,,Bob',
|
|
85
83
|
});
|
|
86
84
|
});
|
|
@@ -90,8 +88,8 @@ describe('aggregateObjects', () => {
|
|
|
90
88
|
{ count: 2, active: false },
|
|
91
89
|
{ count: 3, active: true },
|
|
92
90
|
];
|
|
93
|
-
const result =
|
|
94
|
-
|
|
91
|
+
const result = aggregateObjects({ objs });
|
|
92
|
+
expect(result).to.deep.equal({
|
|
95
93
|
count: '1,2,3',
|
|
96
94
|
active: 'true,false,true',
|
|
97
95
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregateObjects.test.js","sourceRoot":"","sources":["../../src/tests/aggregateObjects.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"aggregateObjects.test.js","sourceRoot":"","sources":["../../src/tests/aggregateObjects.test.ts"],"names":[],"mappings":"AAAA,OAAO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D,MAAM,IAAI,GAAG;YACX,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE;YAC1B,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE;YAC3B,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE;SAC3B,CAAC;QACF,MAAM,MAAM,GAAG,gBAAgB,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1C,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC;YAC3B,KAAK,EAAE,OAAO;YACd,MAAM,EAAE,iBAAiB;SAC1B,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,132 +1,104 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
const t = __importStar(require("io-ts"));
|
|
30
|
-
const chai_1 = __importStar(require("chai"));
|
|
31
|
-
const deep_equal_in_any_order_1 = __importDefault(require("deep-equal-in-any-order"));
|
|
32
|
-
const codecTools_1 = require("../codecTools");
|
|
33
|
-
chai_1.default.use(deep_equal_in_any_order_1.default);
|
|
1
|
+
import * as t from 'io-ts';
|
|
2
|
+
import chai, { expect } from 'chai';
|
|
3
|
+
import deepEqualInAnyOrder from 'deep-equal-in-any-order';
|
|
4
|
+
import { createDefaultCodec } from '../codecTools/index.js';
|
|
5
|
+
chai.use(deepEqualInAnyOrder);
|
|
34
6
|
describe('buildDefaultCodec', () => {
|
|
35
7
|
it('should correctly build a default codec for null', () => {
|
|
36
|
-
const result =
|
|
37
|
-
|
|
8
|
+
const result = createDefaultCodec(t.null);
|
|
9
|
+
expect(result).to.equal(null);
|
|
38
10
|
});
|
|
39
11
|
it('should correctly build a default codec for number', () => {
|
|
40
|
-
const result =
|
|
41
|
-
|
|
12
|
+
const result = createDefaultCodec(t.number);
|
|
13
|
+
expect(result).to.equal(0);
|
|
42
14
|
});
|
|
43
15
|
it('should correctly build a default codec for boolean', () => {
|
|
44
|
-
const result =
|
|
45
|
-
|
|
16
|
+
const result = createDefaultCodec(t.boolean);
|
|
17
|
+
expect(result).to.equal(false);
|
|
46
18
|
});
|
|
47
19
|
it('should correctly build a default codec for undefined', () => {
|
|
48
|
-
const result =
|
|
49
|
-
|
|
20
|
+
const result = createDefaultCodec(t.undefined);
|
|
21
|
+
expect(result).to.equal(undefined);
|
|
50
22
|
});
|
|
51
23
|
it('should correctly build a default codec for string', () => {
|
|
52
|
-
const result =
|
|
53
|
-
|
|
24
|
+
const result = createDefaultCodec(t.string);
|
|
25
|
+
expect(result).to.equal('');
|
|
54
26
|
});
|
|
55
27
|
it('should correctly build a default codec for a union of literals', () => {
|
|
56
28
|
const codec = t.union([t.literal('A'), t.literal('B')]);
|
|
57
|
-
const defaultCodec =
|
|
58
|
-
|
|
29
|
+
const defaultCodec = createDefaultCodec(codec);
|
|
30
|
+
expect(defaultCodec).to.equal('A');
|
|
59
31
|
});
|
|
60
32
|
it('should correctly build a default codec for an object', () => {
|
|
61
33
|
const codec = t.object;
|
|
62
|
-
const defaultCodec =
|
|
63
|
-
|
|
34
|
+
const defaultCodec = createDefaultCodec(codec);
|
|
35
|
+
expect(defaultCodec).to.deep.equal({});
|
|
64
36
|
});
|
|
65
37
|
it('should correctly build a default codec for a union with null', () => {
|
|
66
|
-
const result =
|
|
38
|
+
const result = createDefaultCodec(t.union([t.string, t.null]));
|
|
67
39
|
// should default to null if the union contains null
|
|
68
|
-
|
|
40
|
+
expect(result).to.equal(null);
|
|
69
41
|
});
|
|
70
42
|
it('should correctly build a default codec for a union with object', () => {
|
|
71
|
-
const result =
|
|
43
|
+
const result = createDefaultCodec(t.union([t.string, t.null, t.type({ name: t.string })]));
|
|
72
44
|
// should default to the type if the union contains an object
|
|
73
|
-
|
|
45
|
+
expect(result).to.deep.equal({ name: '' });
|
|
74
46
|
});
|
|
75
47
|
it('should correctly build a default codec for a union with array of type', () => {
|
|
76
|
-
const result =
|
|
48
|
+
const result = createDefaultCodec(t.union([
|
|
77
49
|
t.string,
|
|
78
50
|
t.null,
|
|
79
51
|
t.type({ name: t.string }),
|
|
80
52
|
t.array(t.string),
|
|
81
53
|
]));
|
|
82
54
|
// should default to the empty array if the union contains an array of type
|
|
83
|
-
|
|
55
|
+
expect(result).to.deep.equal([]);
|
|
84
56
|
});
|
|
85
57
|
it('should correctly build a default codec for a union with array of object', () => {
|
|
86
|
-
const result =
|
|
58
|
+
const result = createDefaultCodec(t.union([
|
|
87
59
|
t.string,
|
|
88
60
|
t.null,
|
|
89
61
|
t.type({ name: t.string }),
|
|
90
62
|
t.array(t.type({ age: t.number })),
|
|
91
63
|
]));
|
|
92
64
|
// should default to the array with object if the union contains an array of objects
|
|
93
|
-
|
|
65
|
+
expect(result).to.deep.equal([{ age: 0 }]);
|
|
94
66
|
});
|
|
95
67
|
it('should correctly build a default codec for a union without null', () => {
|
|
96
|
-
const result =
|
|
68
|
+
const result = createDefaultCodec(t.union([t.string, t.number]));
|
|
97
69
|
// should default to the first value if the union does not contains null
|
|
98
|
-
|
|
70
|
+
expect(result).to.equal('');
|
|
99
71
|
});
|
|
100
72
|
it('should correctly build a default codec for an array of object types', () => {
|
|
101
|
-
const result =
|
|
73
|
+
const result = createDefaultCodec(t.array(t.type({ name: t.string, age: t.number })));
|
|
102
74
|
// should default to the first value if the union does not contains null
|
|
103
|
-
|
|
75
|
+
expect(result).to.deep.equalInAnyOrder([{ name: '', age: 0 }]);
|
|
104
76
|
});
|
|
105
77
|
it('should correctly build a default codec for an array of object partials', () => {
|
|
106
|
-
const result =
|
|
78
|
+
const result = createDefaultCodec(t.array(t.partial({ name: t.string, age: t.number })));
|
|
107
79
|
// should default to the first value if the union does not contains null
|
|
108
|
-
|
|
80
|
+
expect(result).to.deep.equalInAnyOrder([{ name: '', age: 0 }]);
|
|
109
81
|
});
|
|
110
82
|
it('should correctly build a default codec for an array of object intersections', () => {
|
|
111
|
-
const result =
|
|
83
|
+
const result = createDefaultCodec(t.array(t.intersection([
|
|
112
84
|
t.partial({ name: t.string, age: t.number }),
|
|
113
85
|
t.type({ city: t.string }),
|
|
114
86
|
])));
|
|
115
87
|
// should default to the first value if the union does not contains null
|
|
116
|
-
|
|
88
|
+
expect(result).to.deep.equalInAnyOrder([{ name: '', age: 0, city: '' }]);
|
|
117
89
|
});
|
|
118
90
|
it('should correctly build a default codec for an array of strings', () => {
|
|
119
|
-
const result =
|
|
91
|
+
const result = createDefaultCodec(t.array(t.string));
|
|
120
92
|
// should default to the first value if the union does not contains null
|
|
121
|
-
|
|
93
|
+
expect(result).to.deep.equal([]);
|
|
122
94
|
});
|
|
123
95
|
it('should correctly build a default codec for an intersection', () => {
|
|
124
|
-
const result =
|
|
96
|
+
const result = createDefaultCodec(t.intersection([
|
|
125
97
|
t.type({ id: t.string, name: t.string }),
|
|
126
98
|
t.partial({ age: t.number }),
|
|
127
99
|
]));
|
|
128
100
|
// should default to the first value if the union does not contains null
|
|
129
|
-
|
|
101
|
+
expect(result).to.deep.equalInAnyOrder({ id: '', name: '', age: 0 });
|
|
130
102
|
});
|
|
131
103
|
});
|
|
132
104
|
//# sourceMappingURL=createDefaultCodec.test.js.map
|