inferred-types 0.37.0 → 0.37.1
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/dist/index.d.ts +2745 -0
- package/dist/index.js +854 -0
- package/package.json +6 -12
- package/.eslintrc +0 -77
- package/.github/workflows/main.yml +0 -34
- package/.github/workflows/other.yml +0 -15
- package/.nvmrc +0 -1
- package/.prettierrc.js +0 -3
- package/.vscode/launch.json +0 -27
- package/.vscode/settings.json +0 -19
- package/devops/build.js +0 -243
- package/docs/builder-overview.md +0 -45
- package/docs/builder.md +0 -94
- package/src/errors/ReadOnlyViolation.ts +0 -3
- package/src/errors/index.ts +0 -12
- package/src/index.ts +0 -14
- package/src/runtime/README.md +0 -3
- package/src/runtime/api/api.ts +0 -10
- package/src/runtime/api/index.ts +0 -12
- package/src/runtime/builders/Model.ts +0 -25
- package/src/runtime/builders/url.ts +0 -15
- package/src/runtime/combinators/and.ts +0 -15
- package/src/runtime/combinators/filter.ts +0 -267
- package/src/runtime/combinators/index.ts +0 -4
- package/src/runtime/combinators/not.ts +0 -15
- package/src/runtime/combinators/or.ts +0 -5
- package/src/runtime/createFnWithProps.ts +0 -37
- package/src/runtime/dictionary/arrayToKeyLookup.ts +0 -19
- package/src/runtime/dictionary/defineProperties.ts +0 -35
- package/src/runtime/dictionary/dictArr.ts +0 -69
- package/src/runtime/dictionary/dictionaryTransform.ts +0 -22
- package/src/runtime/dictionary/entries.ts +0 -33
- package/src/runtime/dictionary/index.ts +0 -21
- package/src/runtime/dictionary/kv/dictToKv.ts +0 -21
- package/src/runtime/dictionary/kv/filterDictArray.ts +0 -31
- package/src/runtime/dictionary/kv/index.ts +0 -15
- package/src/runtime/dictionary/kv/kv.ts +0 -18
- package/src/runtime/dictionary/kv/kvToDict.ts +0 -24
- package/src/runtime/dictionary/mapTo.ts +0 -206
- package/src/runtime/dictionary/mapValues.ts +0 -23
- package/src/runtime/dictionary/merge.ts +0 -36
- package/src/runtime/dictionary/strArrayToDict.ts +0 -12
- package/src/runtime/index.ts +0 -23
- package/src/runtime/keys.ts +0 -16
- package/src/runtime/lists/asArray.ts +0 -38
- package/src/runtime/lists/createConverter.ts +0 -62
- package/src/runtime/lists/groupBy.ts +0 -15
- package/src/runtime/lists/index.ts +0 -15
- package/src/runtime/literals/ExplicitFunction.ts +0 -10
- package/src/runtime/literals/Suggest.ts +0 -19
- package/src/runtime/literals/arrayToObject.ts +0 -78
- package/src/runtime/literals/box.ts +0 -98
- package/src/runtime/literals/defineType.ts +0 -20
- package/src/runtime/literals/ensureLeading.ts +0 -17
- package/src/runtime/literals/ensureTrailing.ts +0 -17
- package/src/runtime/literals/identity.ts +0 -12
- package/src/runtime/literals/index.ts +0 -23
- package/src/runtime/literals/literal.ts +0 -48
- package/src/runtime/literals/pathJoin.ts +0 -33
- package/src/runtime/literals/stripLeading.ts +0 -15
- package/src/runtime/literals/stripTrailing.ts +0 -15
- package/src/runtime/literals/wide.ts +0 -13
- package/src/runtime/ruleset.ts +0 -42
- package/src/runtime/runtime/README.md +0 -3
- package/src/runtime/runtime/condition.ts +0 -13
- package/src/runtime/runtime/index.ts +0 -16
- package/src/runtime/runtime/type.ts +0 -127
- package/src/runtime/runtime/withValue.ts +0 -39
- package/src/runtime/state/Configurator.ts +0 -76
- package/src/runtime/state/FluentConfigurator.ts +0 -49
- package/src/runtime/state/index.ts +0 -13
- package/src/runtime/type-checks/ifSameType.ts +0 -20
- package/src/runtime/type-checks/ifTypeOf.ts +0 -124
- package/src/runtime/type-checks/index.ts +0 -24
- package/src/runtime/type-checks/isArray.ts +0 -34
- package/src/runtime/type-checks/isBoolean.ts +0 -24
- package/src/runtime/type-checks/isFalse.ts +0 -29
- package/src/runtime/type-checks/isFunction.ts +0 -26
- package/src/runtime/type-checks/isNull.ts +0 -26
- package/src/runtime/type-checks/isNumber.ts +0 -20
- package/src/runtime/type-checks/isObject.ts +0 -22
- package/src/runtime/type-checks/isString.ts +0 -34
- package/src/runtime/type-checks/isSymbol.ts +0 -3
- package/src/runtime/type-checks/isTrue.ts +0 -31
- package/src/runtime/type-checks/isUndefined.ts +0 -33
- package/src/runtime/type-checks/startsWith.ts +0 -107
- package/src/runtime/valueTypes.ts +0 -48
- package/src/types/Api.ts +0 -16
- package/src/types/Constructor.ts +0 -5
- package/src/types/EnumValues.ts +0 -14
- package/src/types/ExpandRecursively.ts +0 -12
- package/src/types/FunctionType.ts +0 -19
- package/src/types/If.ts +0 -10
- package/src/types/Include.ts +0 -18
- package/src/types/KeyedRecord.ts +0 -21
- package/src/types/Keys.ts +0 -26
- package/src/types/Length.ts +0 -8
- package/src/types/Mutable.ts +0 -8
- package/src/types/Narrowable.ts +0 -5
- package/src/types/Not.ts +0 -13
- package/src/types/Numeric.ts +0 -20
- package/src/types/Opaque.ts +0 -11
- package/src/types/README.md +0 -3
- package/src/types/Retain.ts +0 -16
- package/src/types/SimplifyObject.ts +0 -12
- package/src/types/Transformer.ts +0 -15
- package/src/types/TypeGuard.ts +0 -7
- package/src/types/Where.ts +0 -40
- package/src/types/alphabetic/AllCaps.ts +0 -15
- package/src/types/alphabetic/CamelCase.ts +0 -3
- package/src/types/alphabetic/CapFirstAlpha.ts +0 -23
- package/src/types/alphabetic/CapitalizeWords.ts +0 -9
- package/src/types/alphabetic/Cardinality.ts +0 -80
- package/src/types/alphabetic/DashToSnake.ts +0 -3
- package/src/types/alphabetic/DashUppercase.ts +0 -19
- package/src/types/alphabetic/Dasherize.ts +0 -23
- package/src/types/alphabetic/EnsureLeading.ts +0 -24
- package/src/types/alphabetic/EnsureTrailing.ts +0 -24
- package/src/types/alphabetic/HasUppercase.ts +0 -16
- package/src/types/alphabetic/IsCapitalized.ts +0 -18
- package/src/types/alphabetic/KebabCase.ts +0 -13
- package/src/types/alphabetic/LowerAllCaps.ts +0 -5
- package/src/types/alphabetic/PascalCase.ts +0 -29
- package/src/types/alphabetic/PathJoin.ts +0 -54
- package/src/types/alphabetic/Pluralize.ts +0 -112
- package/src/types/alphabetic/SnakeCase.ts +0 -27
- package/src/types/alphabetic/StripLeading.ts +0 -23
- package/src/types/alphabetic/StripTrailing.ts +0 -23
- package/src/types/alphabetic/Url.ts +0 -29
- package/src/types/alphabetic/alpha-characters.ts +0 -81
- package/src/types/alphabetic/index.ts +0 -32
- package/src/types/append-types.ts +0 -10
- package/src/types/boolean-logic/EndsWith.ts +0 -40
- package/src/types/boolean-logic/Extends.ts +0 -22
- package/src/types/boolean-logic/HasParameters.ts +0 -21
- package/src/types/boolean-logic/Includes.ts +0 -33
- package/src/types/boolean-logic/IsLiteral.ts +0 -101
- package/src/types/boolean-logic/IsScalar.ts +0 -20
- package/src/types/boolean-logic/IsUndefined.ts +0 -20
- package/src/types/boolean-logic/Or.ts +0 -12
- package/src/types/boolean-logic/StartsWith.ts +0 -42
- package/src/types/boolean-logic/TypeDefault.ts +0 -58
- package/src/types/boolean-logic/array.ts +0 -57
- package/src/types/boolean-logic/boolean.ts +0 -84
- package/src/types/boolean-logic/equivalency.ts +0 -15
- package/src/types/boolean-logic/index.ts +0 -14
- package/src/types/boolean-logic/object.ts +0 -30
- package/src/types/boolean-logic/string.ts +0 -21
- package/src/types/dictionary/DictChangeValue.ts +0 -34
- package/src/types/dictionary/DictPartialApplication.ts +0 -30
- package/src/types/dictionary/DictPrependWithFn.ts +0 -23
- package/src/types/dictionary/DictReturnValues.ts +0 -27
- package/src/types/dictionary/Get.ts +0 -14
- package/src/types/dictionary/MapTo.ts +0 -428
- package/src/types/dictionary/MutableProps.ts +0 -19
- package/src/types/dictionary/RequireProps.ts +0 -15
- package/src/types/dictionary/SameKeys.ts +0 -11
- package/src/types/dictionary/index.ts +0 -21
- package/src/types/dictionary/props.ts +0 -145
- package/src/types/fluent/fluent.ts +0 -61
- package/src/types/fluent/index.ts +0 -12
- package/src/types/functions/FinalReturn.ts +0 -12
- package/src/types/functions/LogicFunction.ts +0 -4
- package/src/types/functions/index.ts +0 -13
- package/src/types/index.ts +0 -49
- package/src/types/kv/DictFromKv.ts +0 -3
- package/src/types/kv/KeyValue.ts +0 -12
- package/src/types/kv/KvFrom.ts +0 -8
- package/src/types/kv/KvTuple.ts +0 -14
- package/src/types/kv/index.ts +0 -15
- package/src/types/lists/AfterFirst.ts +0 -10
- package/src/types/lists/ConvertAndMap.ts +0 -152
- package/src/types/lists/FilterTuple.ts +0 -20
- package/src/types/lists/First.ts +0 -8
- package/src/types/lists/FirstString.ts +0 -4
- package/src/types/lists/Split.ts +0 -21
- package/src/types/lists/UniqueForProp.ts +0 -18
- package/src/types/lists/index.ts +0 -18
- package/src/types/literal-unions/OptRequired.ts +0 -4
- package/src/types/literal-unions/index.ts +0 -1
- package/src/types/maybe.ts +0 -9
- package/src/types/ruleset-types.ts +0 -76
- package/src/types/runtime.ts +0 -39
- package/src/types/string-literals/Break.ts +0 -22
- package/src/types/string-literals/Concat.ts +0 -9
- package/src/types/string-literals/LeftWhitespace.ts +0 -13
- package/src/types/string-literals/Replace.ts +0 -17
- package/src/types/string-literals/RightWhitespace.ts +0 -13
- package/src/types/string-literals/StringLength.ts +0 -34
- package/src/types/string-literals/Trim.ts +0 -15
- package/src/types/string-literals/TrimLeft.ts +0 -14
- package/src/types/string-literals/TrimRight.ts +0 -14
- package/src/types/string-literals/form-fields.ts +0 -15
- package/src/types/string-literals/index.ts +0 -21
- package/src/types/tuples/DictArray.ts +0 -21
- package/src/types/tuples/FirstKey.ts +0 -10
- package/src/types/tuples/FirstKeyValue.ts +0 -9
- package/src/types/tuples/FirstOfEach.ts +0 -13
- package/src/types/tuples/FromDictArray.ts +0 -14
- package/src/types/tuples/SecondOfEach.ts +0 -13
- package/src/types/tuples/index.ts +0 -17
- package/src/types/type-conversion/TupleToUnion.ts +0 -9
- package/src/types/type-conversion/UnionToIntersection.ts +0 -9
- package/src/types/type-conversion/UnionToTuple.ts +0 -20
- package/src/types/type-conversion/Widen.ts +0 -22
- package/src/types/type-conversion/index.ts +0 -16
- package/src/types/type-testing.ts +0 -14
- package/tests/CamelCase-spec.ts +0 -20
- package/tests/Configurator-spec.hold +0 -106
- package/tests/ExplicitFunction-spec.ts +0 -50
- package/tests/IncludeAndRetain-spec.ts +0 -67
- package/tests/Includes-spec.ts +0 -36
- package/tests/KebabCase-spec.ts +0 -21
- package/tests/PascalCase-spec.ts +0 -22
- package/tests/Pluralize-spec.ts +0 -23
- package/tests/SnakeCase-spec.ts +0 -21
- package/tests/Where.spec.ts +0 -38
- package/tests/arrayToKeyLookup-spec.ts +0 -17
- package/tests/arrayToObject-spec.ts +0 -115
- package/tests/boolean-logic/Contains.test.ts +0 -55
- package/tests/boolean-logic/HasParameters.ts +0 -29
- package/tests/boolean-logic/IsLiteral.spec.ts +0 -65
- package/tests/boolean-logic/boolean.spec.ts +0 -21
- package/tests/boolean-logic/filter.spec.ts +0 -52
- package/tests/constructor.spec.ts +0 -42
- package/tests/createFnWithProps.spec.ts +0 -14
- package/tests/data/SimpleTable.hold +0 -21
- package/tests/data/index.ts +0 -12
- package/tests/data/playlist.hold +0 -25
- package/tests/data/song.hold +0 -23
- package/tests/defineType-spec.ts +0 -57
- package/tests/dictionary/DictChangeValue.test.ts +0 -30
- package/tests/dictionary/DictPartialApplication.test.ts +0 -43
- package/tests/dictionary/DictReturnValues.test.ts +0 -36
- package/tests/dictionary/Get.spec.ts +0 -28
- package/tests/dictionary/IntersectingKeys.test.ts +0 -42
- package/tests/dictionary/MutableProps.test.ts +0 -30
- package/tests/dictionary/OptionalKeys.test.ts +0 -24
- package/tests/dictionary/PrependValuesWithFunction.test.ts +0 -34
- package/tests/dictionary/RequireProps.test.ts +0 -21
- package/tests/dictionary/RequiredKeys.test.ts +0 -53
- package/tests/dictionary/TypeDefault.test.ts +0 -76
- package/tests/dictionary/mapTo.test.ts +0 -388
- package/tests/dictionary/merge.test.ts +0 -41
- package/tests/dictionaryTransform-spec.ts +0 -280
- package/tests/ifTypeOf-spec.ts +0 -191
- package/tests/kv/KeyValue-spec.ts +0 -22
- package/tests/kv/SameKeys-spec.ts +0 -35
- package/tests/kv/dict-to-kv-and-back.spec.ts +0 -169
- package/tests/kv/entries-spec.ts +0 -65
- package/tests/kv/keys.spec.ts +0 -66
- package/tests/kv/kv-spec.ts +0 -20
- package/tests/lists/First.test.ts +0 -43
- package/tests/lists/Length.test.ts +0 -19
- package/tests/lists/Split.test.ts +0 -29
- package/tests/lists/UniqueForProp.spec.ts +0 -59
- package/tests/lists/asArray.test.ts +0 -109
- package/tests/lists/dictArr.test.ts +0 -110
- package/tests/literal-spec.ts +0 -82
- package/tests/literals/AllCaps.spec.ts +0 -33
- package/tests/literals/CamelCase.spec.ts +0 -109
- package/tests/literals/Dasherize.spec.ts +0 -107
- package/tests/literals/EnsureStripLeadingTrailing.test.ts +0 -79
- package/tests/literals/HasUppercase.spec.ts +0 -33
- package/tests/literals/PascalCase.spec.ts +0 -109
- package/tests/literals/PathJoin.test.ts +0 -111
- package/tests/literals/SnakeCase.spec.ts +0 -109
- package/tests/literals/box.test.ts +0 -85
- package/tests/literals/whitespace-capture.spec.ts +0 -60
- package/tests/mapValues-spec.ts +0 -50
- package/tests/props-spec.ts +0 -234
- package/tests/runtime/condition.spec.ts +0 -19
- package/tests/runtime/if-is.spec.ts +0 -390
- package/tests/runtime/map-and-convert.test.ts +0 -31
- package/tests/runtime/type.spec.ts +0 -190
- package/tests/strArrayToDict-spec.ts +0 -40
- package/tests/tuples/TupleToUnion.spec.ts +0 -20
- package/tests/withValue.spec.ts +0 -173
- package/tsconfig.json +0 -36
- package/vitest.config.ts +0 -17
|
@@ -1,388 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FinalizedMapConfig,
|
|
3
|
-
MapCardinality,
|
|
4
|
-
MapFn,
|
|
5
|
-
MapTo,
|
|
6
|
-
AsFinalizedConfig,
|
|
7
|
-
MapConfig,
|
|
8
|
-
ConfiguredMap,
|
|
9
|
-
Mapper,
|
|
10
|
-
MapInputFrom,
|
|
11
|
-
MapCardinalityFrom,
|
|
12
|
-
} from "src/types/dictionary";
|
|
13
|
-
import { describe, expect, it } from "vitest";
|
|
14
|
-
import type { Expect, Equal } from "@type-challenges/utils";
|
|
15
|
-
import {
|
|
16
|
-
DEFAULT_MANY_TO_ONE_MAPPING,
|
|
17
|
-
DEFAULT_ONE_TO_MANY_MAPPING,
|
|
18
|
-
mapTo,
|
|
19
|
-
} from "src/runtime/dictionary/mapTo";
|
|
20
|
-
|
|
21
|
-
type I = { title: string; color: string; products: string[] };
|
|
22
|
-
const i: I = { title: "Test", color: "green", products: ["foo", "bar", "baz"] };
|
|
23
|
-
const i2: I = { title: "i2", color: "green", products: ["foo", "bar", "baz"] };
|
|
24
|
-
|
|
25
|
-
type O = { title: string; count: number; unnecessary?: number };
|
|
26
|
-
|
|
27
|
-
describe("MapTo<I,O> and MapToFn<I,O>", () => {
|
|
28
|
-
it("MapTo with 0:M, 0:M cardinality", () => {
|
|
29
|
-
type C = FinalizedMapConfig<"req", "I -> O[]", "opt">;
|
|
30
|
-
type Fn = MapTo<I, O, C>;
|
|
31
|
-
type WFn = MapFn<I, O, C>;
|
|
32
|
-
|
|
33
|
-
/** userland mapping function */
|
|
34
|
-
type T = (source: I) => O[];
|
|
35
|
-
/** exposed via mapTo() util */
|
|
36
|
-
type WT = <S extends I | I[]>(source: S) => S extends I[] ? O[] : O[] | null;
|
|
37
|
-
|
|
38
|
-
type cases = [Expect<Equal<Fn, T>>, Expect<Equal<WFn, WT>>];
|
|
39
|
-
const cases: cases = [true, true];
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("MapTo<X> with I -> O[] variable config", () => {
|
|
43
|
-
type C0 = FinalizedMapConfig<"req", "I -> O[]", "opt">;
|
|
44
|
-
type C1 = FinalizedMapConfig<"req", "I -> O[]", "req">;
|
|
45
|
-
type C2 = FinalizedMapConfig<"opt", "I -> O[]", "opt">;
|
|
46
|
-
type C3 = FinalizedMapConfig<"opt", "I -> O[]", "req">;
|
|
47
|
-
|
|
48
|
-
type Fn0 = MapTo<I, O, C0>;
|
|
49
|
-
type Fn1 = MapTo<I, O, C1>;
|
|
50
|
-
type Fn2 = MapTo<I, O, C2>;
|
|
51
|
-
type Fn3 = MapTo<I, O, C3>;
|
|
52
|
-
|
|
53
|
-
type T0 = (source: I) => O[];
|
|
54
|
-
type T1 = (source: I) => [O, ...O[]];
|
|
55
|
-
type T2 = (source?: I) => O[];
|
|
56
|
-
type T3 = (source?: I) => [O, ...O[]];
|
|
57
|
-
|
|
58
|
-
type cases = [
|
|
59
|
-
Expect<Equal<Fn0, T0>>, //
|
|
60
|
-
Expect<Equal<Fn1, T1>>, //
|
|
61
|
-
Expect<Equal<Fn2, T2>>, //
|
|
62
|
-
Expect<Equal<Fn3, T3>> //
|
|
63
|
-
];
|
|
64
|
-
const cases: cases = [true, true, true, true];
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("MapTo<X> with I[] -> O variable config", () => {
|
|
68
|
-
type C0 = FinalizedMapConfig<"req", "I[] -> O", "opt">;
|
|
69
|
-
type C1 = FinalizedMapConfig<"req", "I[] -> O", "req">;
|
|
70
|
-
type C2 = FinalizedMapConfig<"opt", "I[] -> O", "opt">;
|
|
71
|
-
type C3 = FinalizedMapConfig<"opt", "I[] -> O", "req">;
|
|
72
|
-
|
|
73
|
-
type Fn0 = MapTo<I, O, C0>;
|
|
74
|
-
type Fn1 = MapTo<I, O, C1>;
|
|
75
|
-
type Fn2 = MapTo<I, O, C2>;
|
|
76
|
-
type Fn3 = MapTo<I, O, C3>;
|
|
77
|
-
|
|
78
|
-
type T0 = (source: I[]) => O | null;
|
|
79
|
-
type T1 = (source: I[]) => O;
|
|
80
|
-
type T2 = (source?: I[]) => O | null;
|
|
81
|
-
type T3 = (source?: I[]) => O;
|
|
82
|
-
|
|
83
|
-
type cases = [
|
|
84
|
-
Expect<Equal<Fn0, T0>>, //
|
|
85
|
-
Expect<Equal<Fn1, T1>>, //
|
|
86
|
-
Expect<Equal<Fn2, T2>>, //
|
|
87
|
-
Expect<Equal<Fn3, T3>> //
|
|
88
|
-
];
|
|
89
|
-
const cases: cases = [true, true, true, true];
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it("MapFn<X> with I -> O[] variable config", () => {
|
|
93
|
-
type C0 = FinalizedMapConfig<"req", "I -> O[]", "opt">;
|
|
94
|
-
type C1 = FinalizedMapConfig<"req", "I -> O[]", "req">;
|
|
95
|
-
type C2 = FinalizedMapConfig<"opt", "I -> O[]", "opt">;
|
|
96
|
-
type C3 = FinalizedMapConfig<"opt", "I -> O[]", "req">;
|
|
97
|
-
|
|
98
|
-
type Fn0 = MapFn<I, O, C0>;
|
|
99
|
-
type Fn1 = MapFn<I, O, C1>;
|
|
100
|
-
type Fn2 = MapFn<I, O, C2>;
|
|
101
|
-
type Fn3 = MapFn<I, O, C3>;
|
|
102
|
-
|
|
103
|
-
type T0 = <S extends I | I[]>(source: S) => S extends I[] ? O[] : O[] | null;
|
|
104
|
-
type T1 = <S extends I | I[]>(source: S) => S extends I[] ? [O, ...O[]] : O[];
|
|
105
|
-
type T2 = <S extends I | I[] | undefined>(source?: S) => S extends I[] ? O[] : O[] | null;
|
|
106
|
-
type T3 = <S extends I | I[] | undefined>(source?: S) => S extends I[] ? [O, ...O[]] : O[];
|
|
107
|
-
|
|
108
|
-
type cases = [
|
|
109
|
-
Expect<Equal<Fn0, T0>>, //
|
|
110
|
-
Expect<Equal<Fn1, T1>>, //
|
|
111
|
-
Expect<Equal<Fn2, T2>>, //
|
|
112
|
-
Expect<Equal<Fn3, T3>> //
|
|
113
|
-
];
|
|
114
|
-
const cases: cases = [true, true, true, true];
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("MapFn<X> with I[] -> O variable config", () => {
|
|
118
|
-
type C0 = FinalizedMapConfig<"req", "I[] -> O", "opt">;
|
|
119
|
-
type C1 = FinalizedMapConfig<"req", "I[] -> O", "req">;
|
|
120
|
-
type C2 = FinalizedMapConfig<"opt", "I[] -> O", "opt">;
|
|
121
|
-
type C3 = FinalizedMapConfig<"opt", "I[] -> O", "req">;
|
|
122
|
-
|
|
123
|
-
type Fn0 = MapFn<I, O, C0>;
|
|
124
|
-
type Fn1 = MapFn<I, O, C1>;
|
|
125
|
-
type Fn2 = MapFn<I, O, C2>;
|
|
126
|
-
type Fn3 = MapFn<I, O, C3>;
|
|
127
|
-
|
|
128
|
-
type T0 = <S extends I[] | I[][]>(source: S) => S extends I[][] ? O[] : O | null;
|
|
129
|
-
type T1 = <S extends I[] | I[][]>(source: S) => S extends I[][] ? [O, ...O[]] : O;
|
|
130
|
-
type T2 = <S extends I[] | I[][] | undefined>(source?: S) => S extends I[][] ? O[] : O | null;
|
|
131
|
-
type T3 = <S extends I[] | I[][] | undefined>(source?: S) => S extends I[][] ? [O, ...O[]] : O;
|
|
132
|
-
|
|
133
|
-
type cases = [
|
|
134
|
-
Expect<Equal<Fn0, T0>>, //
|
|
135
|
-
Expect<Equal<Fn1, T1>>, //
|
|
136
|
-
Expect<Equal<Fn2, T2>>, //
|
|
137
|
-
Expect<Equal<Fn3, T3>> //
|
|
138
|
-
];
|
|
139
|
-
const cases: cases = [true, true, true, true];
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it("ToConfiguredMap<U,D> type utility", () => {
|
|
143
|
-
// userland overrides cardinality
|
|
144
|
-
type U1 = MapConfig<undefined, "I -> O", undefined>;
|
|
145
|
-
type D1 = typeof DEFAULT_ONE_TO_MANY_MAPPING;
|
|
146
|
-
type CD1 = AsFinalizedConfig<U1, D1>;
|
|
147
|
-
type CM1 = ConfiguredMap<CD1>;
|
|
148
|
-
type M1 = CM1["map"];
|
|
149
|
-
type M1P = Parameters<M1>;
|
|
150
|
-
type M1R = ReturnType<M1>;
|
|
151
|
-
|
|
152
|
-
// userland provides nothing; leaves default value
|
|
153
|
-
type U2 = MapConfig<undefined, undefined, undefined>;
|
|
154
|
-
type D2 = typeof DEFAULT_MANY_TO_ONE_MAPPING;
|
|
155
|
-
type C2 = AsFinalizedConfig<U2, D2>;
|
|
156
|
-
type CM2 = ConfiguredMap<C2>;
|
|
157
|
-
type M2 = CM2["map"];
|
|
158
|
-
type M2P = Parameters<M2>;
|
|
159
|
-
type M2R = ReturnType<M2>;
|
|
160
|
-
|
|
161
|
-
type U3 = MapConfig<undefined, undefined, "opt">;
|
|
162
|
-
type D3 = typeof DEFAULT_MANY_TO_ONE_MAPPING;
|
|
163
|
-
type C3 = AsFinalizedConfig<U3, D3>;
|
|
164
|
-
type CM3 = ConfiguredMap<C3>;
|
|
165
|
-
type M3 = CM3["map"];
|
|
166
|
-
type M3P = Parameters<M3>;
|
|
167
|
-
type M3R = ReturnType<M3>;
|
|
168
|
-
|
|
169
|
-
type cases = [
|
|
170
|
-
// merged configuration
|
|
171
|
-
Expect<Equal<CD1, FinalizedMapConfig<"req", "I -> O", "opt">>>,
|
|
172
|
-
// map() function's parameters (unknown is I)
|
|
173
|
-
Expect<Equal<[map: (source: unknown) => unknown], M1P>>,
|
|
174
|
-
// map() function's return value
|
|
175
|
-
Expect<Equal<Mapper<unknown, unknown, FinalizedMapConfig<"req", "I -> O", "opt">>, M1R>>,
|
|
176
|
-
|
|
177
|
-
// merged configuration is same as default for M:1
|
|
178
|
-
Expect<Equal<C2, FinalizedMapConfig<"req", "I[] -> O", "req">>>,
|
|
179
|
-
// map() function's parameters (unknown is I)
|
|
180
|
-
Expect<Equal<[map: (source: unknown[]) => unknown], M2P>>,
|
|
181
|
-
// map() function's return value
|
|
182
|
-
Expect<Equal<Mapper<unknown, unknown, FinalizedMapConfig<"req", "I[] -> O", "req">>, M2R>>,
|
|
183
|
-
|
|
184
|
-
// merged configuration should be M:1 default with an "opt" for output
|
|
185
|
-
Expect<Equal<C3, FinalizedMapConfig<"req", "I[] -> O", "opt">>>,
|
|
186
|
-
// map() function's parameters (unknown is I)
|
|
187
|
-
Expect<Equal<[map: (source: unknown[]) => unknown | null], M3P>>,
|
|
188
|
-
// map() function's return value
|
|
189
|
-
Expect<Equal<Mapper<unknown, unknown, FinalizedMapConfig<"req", "I[] -> O", "opt">>, M3R>>
|
|
190
|
-
];
|
|
191
|
-
|
|
192
|
-
const cases: cases = [true, true, true, true, true, true, true, true, true];
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it("config() matches cardinality configs", () => {
|
|
196
|
-
const a1 = mapTo.oneToMany();
|
|
197
|
-
const b1 = mapTo.config({
|
|
198
|
-
input: "req",
|
|
199
|
-
output: "opt",
|
|
200
|
-
cardinality: "I -> O[]",
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
expect(a1.input).toBe(b1.input);
|
|
204
|
-
expect(a1.output).toBe(b1.output);
|
|
205
|
-
expect(a1.cardinality).toBe(b1.cardinality);
|
|
206
|
-
|
|
207
|
-
const a2 = mapTo.oneToOne();
|
|
208
|
-
const b2 = mapTo.config({
|
|
209
|
-
input: "req",
|
|
210
|
-
output: "req",
|
|
211
|
-
cardinality: "I -> O",
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
expect(a2.input).toBe(b2.input);
|
|
215
|
-
expect(a2.output).toBe(b2.output);
|
|
216
|
-
expect(a2.cardinality).toBe(b2.cardinality);
|
|
217
|
-
|
|
218
|
-
type cases = [
|
|
219
|
-
Expect<Equal<typeof a1, typeof a2>>, //
|
|
220
|
-
Expect<Equal<typeof b1, typeof b2>>
|
|
221
|
-
];
|
|
222
|
-
const cases: cases = [true, true];
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
describe("mapTo() utility function", () => {
|
|
227
|
-
it("Partial application of mapTo utility", () => {
|
|
228
|
-
const m1 = mapTo.manyToOne();
|
|
229
|
-
|
|
230
|
-
expect(m1.input).toBe("req");
|
|
231
|
-
expect(m1.output).toBe("req");
|
|
232
|
-
expect(m1.cardinality).toBe(MapCardinality.ManyToOne);
|
|
233
|
-
|
|
234
|
-
const m2 = mapTo.manyToOne({ output: "opt" });
|
|
235
|
-
|
|
236
|
-
expect(m2.input).toBe("req");
|
|
237
|
-
expect(m2.output).toBe("opt");
|
|
238
|
-
expect(m2.cardinality).toBe(MapCardinality.ManyToOne);
|
|
239
|
-
|
|
240
|
-
const o1 = mapTo.oneToOne();
|
|
241
|
-
|
|
242
|
-
expect(o1.input).toBe("req");
|
|
243
|
-
expect(o1.output).toBe("req");
|
|
244
|
-
expect(o1.cardinality).toBe(MapCardinality.OneToOne);
|
|
245
|
-
|
|
246
|
-
const o2 = mapTo.oneToOne({ output: "opt" });
|
|
247
|
-
|
|
248
|
-
expect(o2.input).toBe("req");
|
|
249
|
-
expect(o2.output).toBe("opt");
|
|
250
|
-
expect(o2.cardinality).toBe(MapCardinality.OneToOne);
|
|
251
|
-
|
|
252
|
-
const c1 = mapTo.config({ input: "opt" });
|
|
253
|
-
|
|
254
|
-
expect(c1.input).toBe("opt");
|
|
255
|
-
expect(c1.output).toBe("opt");
|
|
256
|
-
expect(c1.cardinality).toBe(MapCardinality.OneToMany);
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it("Mapper<I,O,C>", () => {
|
|
260
|
-
const m0 = mapTo.oneToOne();
|
|
261
|
-
const m1 = m0.map<I, I>((i) => i);
|
|
262
|
-
type M1 = MapInputFrom<typeof m1>;
|
|
263
|
-
type MC = MapCardinalityFrom<typeof m1>;
|
|
264
|
-
|
|
265
|
-
expect(m1.input).toBe("req");
|
|
266
|
-
expect(m1.output).toBe("req");
|
|
267
|
-
expect(m1.cardinality).toBe("I -> O");
|
|
268
|
-
expect(m1(i)).toEqual(i);
|
|
269
|
-
|
|
270
|
-
type cases = [
|
|
271
|
-
Expect<Equal<typeof m1["inputType"], I>>, //
|
|
272
|
-
Expect<Equal<typeof m1["outputType"], I>>, //
|
|
273
|
-
Expect<Equal<M1, I>>, //
|
|
274
|
-
Expect<Equal<MC, "I -> O">> //
|
|
275
|
-
];
|
|
276
|
-
const cases: cases = [true, true, true, true];
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it("M:1 conversion", () => {
|
|
280
|
-
const m = mapTo
|
|
281
|
-
.config({
|
|
282
|
-
cardinality: "I[] -> O",
|
|
283
|
-
output: "req",
|
|
284
|
-
})
|
|
285
|
-
.map<I, O>((i) => ({
|
|
286
|
-
title: "count of products",
|
|
287
|
-
count: i.reduce((acc, i) => (acc = acc + i.products.length), 0),
|
|
288
|
-
}));
|
|
289
|
-
|
|
290
|
-
const summary = m([i, i2]);
|
|
291
|
-
expect(summary.title).toBe("count of products");
|
|
292
|
-
expect(summary.count).toBe(6);
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
it("1:1 conversion", () => {
|
|
296
|
-
const m = mapTo.oneToOne().map<I, O>((i) => ({
|
|
297
|
-
title: i.title,
|
|
298
|
-
count: i.products.length,
|
|
299
|
-
}));
|
|
300
|
-
const o = m(i);
|
|
301
|
-
|
|
302
|
-
expect(o?.title).toBe(i.title);
|
|
303
|
-
expect(o?.count).toBe(3);
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
it("1:1 with 1:M conversion setting", () => {
|
|
307
|
-
const mc = mapTo.config({
|
|
308
|
-
output: "req",
|
|
309
|
-
cardinality: "I -> O[]",
|
|
310
|
-
});
|
|
311
|
-
const m = mc.map<I, O>((i) => [{ title: i.title, count: i.products.length }]);
|
|
312
|
-
const o = m(i);
|
|
313
|
-
|
|
314
|
-
expect(o.length).toBe(1);
|
|
315
|
-
expect(o[0]?.title).toBe(i.title);
|
|
316
|
-
expect(o[0]?.count).toBe(3);
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it("1:M conversion ", () => {
|
|
320
|
-
const m = mapTo
|
|
321
|
-
.config({ output: "req", cardinality: "I -> O[]" })
|
|
322
|
-
.map<I, O>((i) => [{ title: i.title, count: i.products.length }]);
|
|
323
|
-
const o = m([i, i2]);
|
|
324
|
-
type RT = ReturnType<typeof m>;
|
|
325
|
-
|
|
326
|
-
expect(o.length).toBe(2);
|
|
327
|
-
o.map((item) => expect("title" in item).toBeTruthy());
|
|
328
|
-
|
|
329
|
-
type cases = [Expect<Equal<RT, O[] | [O, ...O[]]>>];
|
|
330
|
-
const cases: cases = [true];
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
it("1:M conversion with filtering", () => {
|
|
334
|
-
const m = mapTo<I, O>((i) =>
|
|
335
|
-
i.title === "i2" ? [{ title: i.title, count: i.products.length }] : []
|
|
336
|
-
);
|
|
337
|
-
const o = m([i, i2]);
|
|
338
|
-
|
|
339
|
-
expect(o.length).toBe(1);
|
|
340
|
-
o.map((item) => expect(item.title).toBe("i2"));
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
it("1:M conversion with filtering and debugging", () => {
|
|
344
|
-
const m = mapTo
|
|
345
|
-
.config({ debug: true })
|
|
346
|
-
.map<I, O>((i) => (i.title === "i2" ? [{ title: i.title, count: i.products.length }] : []));
|
|
347
|
-
const o = m([i, i2]);
|
|
348
|
-
|
|
349
|
-
expect(o.length).toBe(1);
|
|
350
|
-
o.map((item) => expect(item.title).toBe("i2"));
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
it("M:1 conversion", () => {
|
|
354
|
-
const m = mapTo.config({ output: "req", cardinality: "I[] -> O" }).map<I, O>((i) => {
|
|
355
|
-
return {
|
|
356
|
-
title: "summary",
|
|
357
|
-
count: i.reduce((acc, i) => (acc = acc + i.products.length), 0),
|
|
358
|
-
};
|
|
359
|
-
});
|
|
360
|
-
const o = m([i, i2]);
|
|
361
|
-
|
|
362
|
-
expect(o.title).toBe("summary");
|
|
363
|
-
expect(o.count).toBe(6);
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
it("1:1 conversion using oneToOne() configurator", () => {
|
|
367
|
-
const m = mapTo.oneToOne().map<I, O>((i) => ({ title: i.title, count: i.products.length }));
|
|
368
|
-
const o = m(i);
|
|
369
|
-
|
|
370
|
-
expect(o.title).toBe(i.title);
|
|
371
|
-
expect(o.count).toBe(i.products.length);
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
it("M:1 conversion using manyToOne() configurator", () => {
|
|
375
|
-
const m = mapTo //
|
|
376
|
-
.manyToOne()
|
|
377
|
-
.map<I, O>((i) => {
|
|
378
|
-
return {
|
|
379
|
-
title: "summary",
|
|
380
|
-
count: i.reduce((acc, cur) => (acc = acc + cur.products.length), 0),
|
|
381
|
-
};
|
|
382
|
-
});
|
|
383
|
-
const o = m([i, i2]);
|
|
384
|
-
|
|
385
|
-
expect(o.title).toBe("summary");
|
|
386
|
-
expect(o.count).toBe(6);
|
|
387
|
-
});
|
|
388
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Equal, Expect } from "@type-challenges/utils";
|
|
2
|
-
import { merge } from "src/runtime/dictionary/merge";
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
4
|
-
|
|
5
|
-
describe("merge() utility", () => {
|
|
6
|
-
it("merge to scalars/undefined", () => {
|
|
7
|
-
const m1 = merge(undefined, 6);
|
|
8
|
-
const m2 = merge(42, 6);
|
|
9
|
-
|
|
10
|
-
expect(m1).toBe(6);
|
|
11
|
-
expect(m2).toBe(42);
|
|
12
|
-
|
|
13
|
-
type cases = [
|
|
14
|
-
Expect<Equal<typeof m1, 6>>, //
|
|
15
|
-
Expect<Equal<typeof m2, 42>>
|
|
16
|
-
];
|
|
17
|
-
const cases: cases = [true, true];
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("merge two objects", () => {
|
|
21
|
-
const o1 = merge(
|
|
22
|
-
{ foo: "foo", baz: false, color: { fav: undefined, next: "green" } } as const,
|
|
23
|
-
{ foo: "bar", bar: 42, color: { fav: "red", next: "" as string } } as const
|
|
24
|
-
);
|
|
25
|
-
type O1 = typeof o1;
|
|
26
|
-
|
|
27
|
-
// runtime
|
|
28
|
-
expect(o1.foo).toBe("foo");
|
|
29
|
-
expect(o1.bar).toBe(42);
|
|
30
|
-
expect(o1.color.fav).toBe("red");
|
|
31
|
-
expect(o1.color.next).toBe("green");
|
|
32
|
-
|
|
33
|
-
// design time
|
|
34
|
-
type cases = [
|
|
35
|
-
Expect<Equal<O1["foo"], "foo">>, //
|
|
36
|
-
Expect<Equal<O1["bar"], 42>>,
|
|
37
|
-
Expect<Equal<O1["color"], { fav: "red"; next: "green" }>>
|
|
38
|
-
];
|
|
39
|
-
const cases: cases = [true, true, true];
|
|
40
|
-
});
|
|
41
|
-
});
|