@ddd-ts/shape 0.0.37 → 0.0.39
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 +21 -0
- package/dist/_.d.ts +39 -0
- package/dist/_.d.ts.map +1 -0
- package/dist/_.js +38 -0
- package/dist/_.mjs +36 -0
- package/dist/addons/microsecond-timestamp.d.ts +24 -0
- package/dist/addons/microsecond-timestamp.d.ts.map +1 -0
- package/dist/addons/microsecond-timestamp.js +62 -0
- package/dist/addons/microsecond-timestamp.mjs +61 -0
- package/dist/choice.d.ts +30 -0
- package/dist/choice.d.ts.map +1 -0
- package/dist/choice.js +44 -0
- package/dist/choice.mjs +44 -0
- package/dist/choice.spec.d.ts +2 -0
- package/dist/choice.spec.d.ts.map +1 -0
- package/dist/class.d.ts +23 -0
- package/dist/class.d.ts.map +1 -0
- package/dist/class.js +30 -0
- package/dist/class.mjs +30 -0
- package/dist/class.spec.d.ts +2 -0
- package/dist/class.spec.d.ts.map +1 -0
- package/dist/dict.d.ts +43 -0
- package/dist/dict.d.ts.map +1 -0
- package/dist/dict.js +41 -0
- package/dist/dict.mjs +41 -0
- package/dist/dict.spec.d.ts +2 -0
- package/dist/dict.spec.d.ts.map +1 -0
- package/dist/discriminated-union.d.ts +74 -0
- package/dist/discriminated-union.d.ts.map +1 -0
- package/dist/discriminated-union.js +66 -0
- package/dist/discriminated-union.mjs +64 -0
- package/dist/discriminated-union.spec.d.ts +2 -0
- package/dist/discriminated-union.spec.d.ts.map +1 -0
- package/dist/either.d.ts +84 -0
- package/dist/either.d.ts.map +1 -0
- package/dist/either.js +51 -0
- package/dist/either.mjs +51 -0
- package/dist/either.spec.d.ts +2 -0
- package/dist/either.spec.d.ts.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.mjs +15 -0
- package/dist/literal.d.ts +15 -0
- package/dist/literal.d.ts.map +1 -0
- package/dist/literal.js +27 -0
- package/dist/literal.mjs +27 -0
- package/dist/literal.spec.d.ts +2 -0
- package/dist/literal.spec.d.ts.map +1 -0
- package/dist/mapping.d.ts +43 -0
- package/dist/mapping.d.ts.map +1 -0
- package/dist/mapping.js +45 -0
- package/dist/mapping.mjs +45 -0
- package/dist/mapping.spec.d.ts +2 -0
- package/dist/mapping.spec.d.ts.map +1 -0
- package/dist/multiple.d.ts +20 -0
- package/dist/multiple.d.ts.map +1 -0
- package/dist/multiple.js +119 -0
- package/dist/multiple.mjs +119 -0
- package/dist/multiple.spec.d.ts +2 -0
- package/dist/multiple.spec.d.ts.map +1 -0
- package/dist/nothing.d.ts +14 -0
- package/dist/nothing.d.ts.map +1 -0
- package/dist/nothing.js +19 -0
- package/dist/nothing.mjs +19 -0
- package/dist/nothing.spec.d.ts +2 -0
- package/dist/nothing.spec.d.ts.map +1 -0
- package/dist/optional.d.ts +26 -0
- package/dist/optional.d.ts.map +1 -0
- package/dist/optional.js +34 -0
- package/dist/optional.mjs +34 -0
- package/dist/optional.spec.d.ts +2 -0
- package/dist/optional.spec.d.ts.map +1 -0
- package/dist/primitive.d.ts +38 -0
- package/dist/primitive.d.ts.map +1 -0
- package/dist/primitive.js +30 -0
- package/dist/primitive.mjs +30 -0
- package/dist/primitive.spec.d.ts +2 -0
- package/dist/primitive.spec.d.ts.map +1 -0
- package/dist/test.d.ts +36 -0
- package/dist/test.d.ts.map +1 -0
- package/package.json +17 -15
package/dist/dict.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Empty, Shape } from "./_.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/dict.ts
|
|
4
|
+
const Dict = (of, base = Empty) => {
|
|
5
|
+
class $Dict extends base {
|
|
6
|
+
static $shape = "dict";
|
|
7
|
+
static $of = of;
|
|
8
|
+
constructor(...args) {
|
|
9
|
+
super();
|
|
10
|
+
Object.assign(this, args[0]);
|
|
11
|
+
}
|
|
12
|
+
serialize() {
|
|
13
|
+
return $Dict.$serialize(this);
|
|
14
|
+
}
|
|
15
|
+
static deserialize(value) {
|
|
16
|
+
const runtime = this.$deserialize(value);
|
|
17
|
+
return new this(runtime);
|
|
18
|
+
}
|
|
19
|
+
static $deserialize(value) {
|
|
20
|
+
const transform = Object.entries(of).map(([key, child]) => {
|
|
21
|
+
return [key, Shape(child).$deserialize(value[key])];
|
|
22
|
+
});
|
|
23
|
+
return Object.fromEntries(transform);
|
|
24
|
+
}
|
|
25
|
+
static $serialize(value) {
|
|
26
|
+
const transform = Object.entries(of).map(([key, child]) => {
|
|
27
|
+
return [key, Shape(child).$serialize(value[key])];
|
|
28
|
+
});
|
|
29
|
+
const merge = Object.fromEntries(transform);
|
|
30
|
+
if ("$name" in base) return {
|
|
31
|
+
...merge,
|
|
32
|
+
$name: base.$name
|
|
33
|
+
};
|
|
34
|
+
return merge;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return $Dict;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
export { Dict };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dict.spec.d.ts","sourceRoot":"","sources":["../src/dict.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Expand, type DefinitionOf, type AbstractConstructor, Empty, type Shorthand, type Definition } from "./_";
|
|
2
|
+
import type { ClassShorthand } from "./class";
|
|
3
|
+
import { type DictShorthand } from "./dict";
|
|
4
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
5
|
+
type PopUnion<U> = UnionToOvlds<U> extends (a: infer A) => void ? A : never;
|
|
6
|
+
type UnionToArray<T, A extends unknown[] = []> = IsUnion<T> extends true ? UnionToArray<Exclude<T, PopUnion<T>>, [PopUnion<T>, ...A]> : [T, ...A];
|
|
7
|
+
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
|
|
8
|
+
type CountUnion<T> = UnionToArray<T> extends infer U extends any[] ? U["length"] : never;
|
|
9
|
+
type UnshiftUnion<U> = UnionToArray<U> extends [infer F, ...any[]] ? F : never;
|
|
10
|
+
type UnionToOvlds<U> = UnionToIntersection<U extends any ? (f: U) => void : never>;
|
|
11
|
+
type IsStringLiteral<T> = T extends string ? string extends T ? false : true : false;
|
|
12
|
+
type FindBestKeyForMatching<FromUnion> = UnshiftUnion<keyof FromUnion extends infer K ? K extends keyof FromUnion ? CountUnion<Pick<FromUnion, K>[K]> extends CountUnion<FromUnion> ? [IsStringLiteral<FromUnion[K]>] extends [true] ? K : never : never : never : never>;
|
|
13
|
+
type MatcherConfig = {
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
};
|
|
16
|
+
type ExhaustiveMatcher<C> = C extends MatcherConfig ? {
|
|
17
|
+
[key in keyof C]: (value: Expand<DefinitionOf<C[key]>["$inline"]>) => any;
|
|
18
|
+
} : never;
|
|
19
|
+
type UnsafeFallthroughMatcher<C> = C extends MatcherConfig ? {
|
|
20
|
+
[key in keyof C]?: (value: Expand<DefinitionOf<C[key]>["$inline"]>) => any;
|
|
21
|
+
} & {
|
|
22
|
+
_: (value: Expand<DefinitionOf<C[keyof C]>["$inline"]>) => any;
|
|
23
|
+
} : never;
|
|
24
|
+
type PartialMatcher<C> = C extends MatcherConfig ? {
|
|
25
|
+
[key in keyof C]?: (value: Expand<DefinitionOf<C[key]>["$inline"]>) => any;
|
|
26
|
+
} : never;
|
|
27
|
+
type Matcher<C> = C extends MatcherConfig ? ExhaustiveMatcher<C> | UnsafeFallthroughMatcher<C> | PartialMatcher<C> : never;
|
|
28
|
+
type Config = DictShorthandInput | ClassInput | ClassDictInput | DictInput;
|
|
29
|
+
type ClassInput = ClassShorthand;
|
|
30
|
+
type ClassDictInput = ClassShorthand & {
|
|
31
|
+
$of: {};
|
|
32
|
+
};
|
|
33
|
+
type DictInput = {
|
|
34
|
+
$of: {};
|
|
35
|
+
};
|
|
36
|
+
type DictShorthandInput = DictShorthand;
|
|
37
|
+
type Access<T, K> = K extends keyof T ? T[K] : never;
|
|
38
|
+
type Entries<T extends readonly any[], K> = UnionToIntersection<{
|
|
39
|
+
[i in keyof T]: Access<GetShape<T[i]>, K> extends infer U extends string ? IsStringLiteral<U> extends true ? {
|
|
40
|
+
[key in U]: T[i] extends DictShorthandInput ? DefinitionOf<T[i]> : T[i];
|
|
41
|
+
} : never : never;
|
|
42
|
+
}[number]>;
|
|
43
|
+
type GetShape<S extends Config> = S extends DictInput ? S["$of"] : S extends ClassInput ? Access<S, "prototype"> : S;
|
|
44
|
+
export type BestKey<S extends readonly Config[]> = {
|
|
45
|
+
[key in keyof S]: GetShape<S[key]>;
|
|
46
|
+
}[number] extends infer U ? FindBestKeyForMatching<U> : never;
|
|
47
|
+
export type DiscriminatedUnionConfiguration = readonly Config[];
|
|
48
|
+
export declare function findBestKey(config: DiscriminatedUnionConfiguration): string;
|
|
49
|
+
export declare function prepareShapeMap(config: DiscriminatedUnionConfiguration, key: string): {
|
|
50
|
+
[key: string]: Definition;
|
|
51
|
+
};
|
|
52
|
+
type Internal<S extends DiscriminatedUnionConfiguration, K extends BestKey<S>> = {
|
|
53
|
+
Map: Entries<S, K>;
|
|
54
|
+
Serialized: ReturnType<DefinitionOf<S[number]>["$serialize"]>;
|
|
55
|
+
Inline: DefinitionOf<S[number]>["$inline"];
|
|
56
|
+
};
|
|
57
|
+
export declare const DiscriminatedUnion: <S extends DiscriminatedUnionConfiguration, K extends BestKey<S>, const B extends AbstractConstructor = typeof Empty>(of: S, base?: B | undefined) => IDiscriminatedUnion<S, K, B>;
|
|
58
|
+
export type IDiscriminatedUnion<S extends DiscriminatedUnionConfiguration, K extends BestKey<S>, B extends AbstractConstructor = typeof Empty> = Omit<B, "prototype"> & {
|
|
59
|
+
$shape: "discriminated-union";
|
|
60
|
+
$of: S;
|
|
61
|
+
serialized: Internal<S, K>["Serialized"];
|
|
62
|
+
deserialize<T>(this: T, value: Expand<Internal<S, K>["Serialized"]>): T extends AbstractConstructor ? InstanceType<T> : any;
|
|
63
|
+
$deserialize<T>(this: T, value: Internal<S, K>["Serialized"]): Internal<S, K>["Inline"];
|
|
64
|
+
$serialize<T extends AbstractConstructor>(this: T, value: InstanceType<T>): Internal<S, K>["Serialized"];
|
|
65
|
+
$inline: Internal<S, K>["Inline"];
|
|
66
|
+
} & (abstract new (value: Internal<S, K>["Inline"]) => InstanceType<B> & {
|
|
67
|
+
value: Internal<S, K>["Inline"];
|
|
68
|
+
serialize(): Internal<S, K>["Serialized"];
|
|
69
|
+
match<M extends Matcher<Internal<S, K>["Map"]>, F extends M extends ExhaustiveMatcher<Internal<S, K>["Map"]> ? [] : M extends UnsafeFallthroughMatcher<Internal<S, K>["Map"]> ? [] : M extends PartialMatcher<Internal<S, K>["Map"]> ? [
|
|
70
|
+
fallback: (value: Omit<Internal<S, K>["Map"], keyof M>[keyof Omit<Internal<S, K>["Map"], keyof M>] extends infer U extends Shorthand ? Expand<DefinitionOf<U>["$inline"]> : never) => any
|
|
71
|
+
] : []>(matcher: M, ...fallback_n: F): (M[keyof M] extends (...args: any[]) => any ? ReturnType<M[keyof M]> : never) | (F[0] extends (...args: any[]) => any ? ReturnType<F[0]> : never);
|
|
72
|
+
});
|
|
73
|
+
export {};
|
|
74
|
+
//# sourceMappingURL=discriminated-union.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discriminated-union.d.ts","sourceRoot":"","sources":["../src/discriminated-union.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,EACL,KAAK,SAAS,EAEd,KAAK,UAAU,EAChB,MAAM,KAAK,CAAC;AACb,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5C,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAC5B,CAAC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|
4
|
+
function findBestKey(config) {
|
|
5
|
+
const hash = {};
|
|
6
|
+
for (const c of config) {
|
|
7
|
+
const shape = "$of" in c ? c.$of : c;
|
|
8
|
+
for (const key in shape) {
|
|
9
|
+
const k = key;
|
|
10
|
+
if (hash[key]) hash[key].add(shape[k]);
|
|
11
|
+
else hash[key] = new Set([shape[k]]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const key = Object.entries(hash).map(([key, value]) => [key, value.size]).filter(([_, value]) => value === config.length).sort((a, b) => b[1] - a[1])?.[0]?.[0];
|
|
15
|
+
if (!key) throw new Error("Could not find key for DiscriminatedUnion");
|
|
16
|
+
return key;
|
|
17
|
+
}
|
|
18
|
+
function prepareShapeMap(config, key) {
|
|
19
|
+
return config.reduce((acc, c) => {
|
|
20
|
+
const shape = require__.Shape(c);
|
|
21
|
+
const discriminator = key in c ? c[key] : "$of" in c ? c.$of[key] : shape.$of[key];
|
|
22
|
+
acc[discriminator] = shape;
|
|
23
|
+
return acc;
|
|
24
|
+
}, {});
|
|
25
|
+
}
|
|
26
|
+
const DiscriminatedUnion = (of, ...args) => {
|
|
27
|
+
const base = args[0] || require__.Empty;
|
|
28
|
+
const key = findBestKey(of);
|
|
29
|
+
const map = prepareShapeMap(of, key);
|
|
30
|
+
class $DiscriminatedUnion extends base {
|
|
31
|
+
constructor(value) {
|
|
32
|
+
super();
|
|
33
|
+
this.value = value;
|
|
34
|
+
}
|
|
35
|
+
static $of = of;
|
|
36
|
+
static $shape = "discriminated-union";
|
|
37
|
+
serialize() {
|
|
38
|
+
return $DiscriminatedUnion.$serialize(this.value);
|
|
39
|
+
}
|
|
40
|
+
match(matcher, fallback) {
|
|
41
|
+
const element = this.value;
|
|
42
|
+
const handler = matcher[element[key]];
|
|
43
|
+
if (handler) return handler(element);
|
|
44
|
+
if (fallback) return fallback(element);
|
|
45
|
+
if (matcher._) return matcher._(element);
|
|
46
|
+
throw new Error("Non-exhaustive match");
|
|
47
|
+
}
|
|
48
|
+
static deserialize(value) {
|
|
49
|
+
return new this(this.$deserialize(value));
|
|
50
|
+
}
|
|
51
|
+
static $deserialize(value) {
|
|
52
|
+
const definition = map[value[key]];
|
|
53
|
+
if (!definition) throw new Error("Cannot deserialize DiscriminatedUnion");
|
|
54
|
+
return definition.$deserialize(value);
|
|
55
|
+
}
|
|
56
|
+
static $serialize(value) {
|
|
57
|
+
return map[value[key]].$serialize(value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return $DiscriminatedUnion;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
exports.DiscriminatedUnion = DiscriminatedUnion;
|
|
65
|
+
exports.findBestKey = findBestKey;
|
|
66
|
+
exports.prepareShapeMap = prepareShapeMap;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Empty, Shape } from "./_.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/discriminated-union.ts
|
|
4
|
+
function findBestKey(config) {
|
|
5
|
+
const hash = {};
|
|
6
|
+
for (const c of config) {
|
|
7
|
+
const shape = "$of" in c ? c.$of : c;
|
|
8
|
+
for (const key in shape) {
|
|
9
|
+
const k = key;
|
|
10
|
+
if (hash[key]) hash[key].add(shape[k]);
|
|
11
|
+
else hash[key] = new Set([shape[k]]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const key = Object.entries(hash).map(([key, value]) => [key, value.size]).filter(([_, value]) => value === config.length).sort((a, b) => b[1] - a[1])?.[0]?.[0];
|
|
15
|
+
if (!key) throw new Error("Could not find key for DiscriminatedUnion");
|
|
16
|
+
return key;
|
|
17
|
+
}
|
|
18
|
+
function prepareShapeMap(config, key) {
|
|
19
|
+
return config.reduce((acc, c) => {
|
|
20
|
+
const shape = Shape(c);
|
|
21
|
+
const discriminator = key in c ? c[key] : "$of" in c ? c.$of[key] : shape.$of[key];
|
|
22
|
+
acc[discriminator] = shape;
|
|
23
|
+
return acc;
|
|
24
|
+
}, {});
|
|
25
|
+
}
|
|
26
|
+
const DiscriminatedUnion = (of, ...args) => {
|
|
27
|
+
const base = args[0] || Empty;
|
|
28
|
+
const key = findBestKey(of);
|
|
29
|
+
const map = prepareShapeMap(of, key);
|
|
30
|
+
class $DiscriminatedUnion extends base {
|
|
31
|
+
constructor(value) {
|
|
32
|
+
super();
|
|
33
|
+
this.value = value;
|
|
34
|
+
}
|
|
35
|
+
static $of = of;
|
|
36
|
+
static $shape = "discriminated-union";
|
|
37
|
+
serialize() {
|
|
38
|
+
return $DiscriminatedUnion.$serialize(this.value);
|
|
39
|
+
}
|
|
40
|
+
match(matcher, fallback) {
|
|
41
|
+
const element = this.value;
|
|
42
|
+
const handler = matcher[element[key]];
|
|
43
|
+
if (handler) return handler(element);
|
|
44
|
+
if (fallback) return fallback(element);
|
|
45
|
+
if (matcher._) return matcher._(element);
|
|
46
|
+
throw new Error("Non-exhaustive match");
|
|
47
|
+
}
|
|
48
|
+
static deserialize(value) {
|
|
49
|
+
return new this(this.$deserialize(value));
|
|
50
|
+
}
|
|
51
|
+
static $deserialize(value) {
|
|
52
|
+
const definition = map[value[key]];
|
|
53
|
+
if (!definition) throw new Error("Cannot deserialize DiscriminatedUnion");
|
|
54
|
+
return definition.$deserialize(value);
|
|
55
|
+
}
|
|
56
|
+
static $serialize(value) {
|
|
57
|
+
return map[value[key]].$serialize(value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return $DiscriminatedUnion;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { DiscriminatedUnion, findBestKey, prepareShapeMap };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discriminated-union.spec.d.ts","sourceRoot":"","sources":["../src/discriminated-union.spec.ts"],"names":[],"mappings":""}
|
package/dist/either.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type Constructor, type Expand, type DefinitionOf, type AbstractConstructor, Empty } from "./_";
|
|
2
|
+
import type { ClassShorthand } from "./class";
|
|
3
|
+
type Config = {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
type ExhaustiveMatcher<C extends Config> = {
|
|
7
|
+
[key in keyof C]: (value: InstanceType<C[key]>) => any;
|
|
8
|
+
};
|
|
9
|
+
type UnsafeFallthroughMatcher<C extends Config> = {
|
|
10
|
+
[key in keyof C]?: (value: InstanceType<C[key]>) => any;
|
|
11
|
+
} & {
|
|
12
|
+
_: (value: InstanceType<C[keyof C]>) => any;
|
|
13
|
+
};
|
|
14
|
+
type PartialMatcher<C extends Config> = {
|
|
15
|
+
[key in keyof C]?: (value: InstanceType<C[key]>) => any;
|
|
16
|
+
};
|
|
17
|
+
type Matcher<C extends Config> = ExhaustiveMatcher<C> | UnsafeFallthroughMatcher<C> | PartialMatcher<C>;
|
|
18
|
+
export type EitherConfiguration = {
|
|
19
|
+
[key: string]: ClassShorthand;
|
|
20
|
+
};
|
|
21
|
+
type Internal<S extends EitherConfiguration, B extends AbstractConstructor<{}>> = {
|
|
22
|
+
Definition: DefinitionOf<S[keyof S]>;
|
|
23
|
+
Serialized: (B extends {
|
|
24
|
+
$name: infer U;
|
|
25
|
+
} ? {
|
|
26
|
+
$name: U;
|
|
27
|
+
} : {}) & {
|
|
28
|
+
_key: keyof S;
|
|
29
|
+
} & ReturnType<DefinitionOf<S[keyof S]>["$serialize"]>;
|
|
30
|
+
Deserializing: (B extends {
|
|
31
|
+
$name: infer U;
|
|
32
|
+
} ? {
|
|
33
|
+
$name: U;
|
|
34
|
+
} : {}) & {
|
|
35
|
+
_key: keyof S;
|
|
36
|
+
} & Parameters<DefinitionOf<S[keyof S]>["$deserialize"]>[0];
|
|
37
|
+
Inline: DefinitionOf<S[keyof S]>["$inline"];
|
|
38
|
+
};
|
|
39
|
+
export declare const Either: <const S extends EitherConfiguration, const B extends AbstractConstructor<{}> = typeof Empty>(of: S, base?: B) => Omit<B, "prototype"> & Omit<(abstract new (value: DefinitionOf<S[keyof S]>["$inline"]) => {
|
|
40
|
+
value: DefinitionOf<S[keyof S], typeof Empty>["$inline"];
|
|
41
|
+
serialize(): Expand<{ [K in keyof S]: {
|
|
42
|
+
_key: K;
|
|
43
|
+
} & ReturnType<DefinitionOf<S[K]>["$serialize"]>; }[keyof S]>;
|
|
44
|
+
match<M extends Matcher<S>, F extends M extends ExhaustiveMatcher<S> ? [] : M extends UnsafeFallthroughMatcher<S> ? [] : M extends PartialMatcher<S> ? [fallback: (value: InstanceType<Omit<S, keyof M>[keyof Omit<S, keyof M>]>) => any] : []>(matcher: M, ...fallback_n: F): (M[keyof M] extends (...args: any[]) => any ? ReturnType<M[keyof M]> : never) | (F[0] extends (...args: any[]) => any ? ReturnType<F[0]> : never);
|
|
45
|
+
}) & {
|
|
46
|
+
serialized: { [K in keyof S]: {
|
|
47
|
+
_key: K;
|
|
48
|
+
} & ReturnType<DefinitionOf<S[K]>["$serialize"]>; }[keyof S];
|
|
49
|
+
of: S;
|
|
50
|
+
$shape: "either";
|
|
51
|
+
deserialize<T extends /*elided*/ any>(this: T, value: Expand<{ [K in keyof S]: {
|
|
52
|
+
_key: K;
|
|
53
|
+
} & ReturnType<DefinitionOf<S[K]>["$serialize"]>; }[keyof S]>): InstanceType<T>;
|
|
54
|
+
$deserialize<T extends /*elided*/ any>(this: T, value: { [K in keyof S]: {
|
|
55
|
+
_key: K;
|
|
56
|
+
} & ReturnType<DefinitionOf<S[K]>["$serialize"]>; }[keyof S]): DefinitionOf<S[keyof S]>["$inline"];
|
|
57
|
+
$serialize<T extends /*elided*/ any>(this: T, value: DefinitionOf<S[keyof S]>["$inline"]): { [K in keyof S]: {
|
|
58
|
+
_key: K;
|
|
59
|
+
} & ReturnType<DefinitionOf<S[K]>["$serialize"]>; }[keyof S];
|
|
60
|
+
$inline: DefinitionOf<S[keyof S]>["$inline"];
|
|
61
|
+
}, "prototype"> & (abstract new (value: Expand<DefinitionOf<S[keyof S]>["$inline"]>) => InstanceType<B> & {
|
|
62
|
+
value: DefinitionOf<S[keyof S], typeof Empty>["$inline"];
|
|
63
|
+
serialize(): Expand<{ [K in keyof S]: {
|
|
64
|
+
_key: K;
|
|
65
|
+
} & ReturnType<DefinitionOf<S[K]>["$serialize"]>; }[keyof S]>;
|
|
66
|
+
match<M extends Matcher<S>, F extends M extends ExhaustiveMatcher<S> ? [] : M extends UnsafeFallthroughMatcher<S> ? [] : M extends PartialMatcher<S> ? [fallback: (value: InstanceType<Omit<S, keyof M>[keyof Omit<S, keyof M>]>) => any] : []>(matcher: M, ...fallback_n: F): (M[keyof M] extends (...args: any[]) => any ? ReturnType<M[keyof M]> : never) | (F[0] extends (...args: any[]) => any ? ReturnType<F[0]> : never);
|
|
67
|
+
});
|
|
68
|
+
export type Either<S extends EitherConfiguration, B extends AbstractConstructor<{}> = typeof Empty> = Omit<B, "prototype"> & (abstract new (value: Internal<S, B>["Inline"]) => {
|
|
69
|
+
value: Internal<S, B>["Inline"];
|
|
70
|
+
serialize(): Internal<S, B>["Serialized"];
|
|
71
|
+
match<M extends Matcher<S>, F extends M extends ExhaustiveMatcher<S> ? [] : M extends UnsafeFallthroughMatcher<S> ? [] : M extends PartialMatcher<S> ? [
|
|
72
|
+
fallback: (value: InstanceType<Omit<S, keyof M>[keyof Omit<S, keyof M>]>) => any
|
|
73
|
+
] : []>(matcher: M, ...fallback_n: F): (M[keyof M] extends (...args: any[]) => any ? ReturnType<M[keyof M]> : never) | (F[0] extends (...args: any[]) => any ? ReturnType<F[0]> : never);
|
|
74
|
+
}) & {
|
|
75
|
+
serialized: Internal<S, B>["Serialized"];
|
|
76
|
+
of: S;
|
|
77
|
+
$shape: "either";
|
|
78
|
+
deserialize<T extends Constructor>(this: T, value: Internal<S, B>["Serialized"]): InstanceType<T>;
|
|
79
|
+
$deserialize<T>(this: T, value: Internal<S, B>["Serialized"]): Internal<S, B>["Inline"];
|
|
80
|
+
$serialize<T>(this: T, value: Internal<S, B>["Inline"]): Internal<S, B>["Serialized"];
|
|
81
|
+
$inline: Internal<S, B>["Inline"];
|
|
82
|
+
};
|
|
83
|
+
export {};
|
|
84
|
+
//# sourceMappingURL=either.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"either.d.ts","sourceRoot":"","sources":["../src/either.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,MAAM,EACX,KAAK,YAAY,EAEjB,KAAK,mBAAmB,EACxB,KAAK,EACN,MAAM,KAAK,CAAC;AACb,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C,KAAK,MAAM,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,CAAC;AAErC,KAAK,iBAAiB,CAAC,CAAC,SAAS,MAAM,IAAI;KACxC,GAAG,IAAI,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG;CACvD,CAAC;AAEF,KAAK,wBAAwB,CAAC,CAAC,SAAS,MAAM,IAAI;KAC/C,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG;CACxD,GAAG;IACF,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC;CAC7C,CAAC;AAEF,KAAK,cAAc,CAAC,CAAC,SAAS,MAAM,IAAI;KACrC,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG;CACxD,CAAC;AAEF,KAAK,OAAO,CAAC,CAAC,SAAS,MAAM,IACzB,iBAAiB,CAAC,CAAC,CAAC,GACpB,wBAAwB,CAAC,CAAC,CAAC,GAC3B,cAAc,CAAC,CAAC,CAAC,CAAC;AAE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AQ,CAAC,CAAC;CACnC,CAAC"}
|
package/dist/either.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const require__ = require('./_.js');
|
|
2
|
+
|
|
3
|
+
//#region src/either.ts
|
|
4
|
+
const Either = (of, base = require__.Empty) => {
|
|
5
|
+
const definitions = Object.fromEntries(Object.entries(of).map(([key, value]) => {
|
|
6
|
+
return [key, require__.Shape(value)];
|
|
7
|
+
}));
|
|
8
|
+
class $Either extends base {
|
|
9
|
+
constructor(value) {
|
|
10
|
+
super();
|
|
11
|
+
this.value = value;
|
|
12
|
+
}
|
|
13
|
+
static serialized;
|
|
14
|
+
static of = of;
|
|
15
|
+
static $shape = "either";
|
|
16
|
+
serialize() {
|
|
17
|
+
return $Either.$serialize(this.value);
|
|
18
|
+
}
|
|
19
|
+
match(...[matcher, fallback]) {
|
|
20
|
+
const handler = matcher[Object.entries(of).find(([_, v]) => v === this.value.constructor)?.[0]];
|
|
21
|
+
if (handler) return handler(this.value);
|
|
22
|
+
if (fallback) return fallback(this.value);
|
|
23
|
+
if (matcher._) return matcher._(this.value);
|
|
24
|
+
throw new Error("Non-exhaustive match");
|
|
25
|
+
}
|
|
26
|
+
static deserialize(value) {
|
|
27
|
+
return new this(this.$deserialize(value));
|
|
28
|
+
}
|
|
29
|
+
static $deserialize(value) {
|
|
30
|
+
const { _key: key, ...serialized } = value;
|
|
31
|
+
const definition = definitions[key];
|
|
32
|
+
if (!definition) throw new Error("Cannot deserialize Either");
|
|
33
|
+
return definition.$deserialize(serialized);
|
|
34
|
+
}
|
|
35
|
+
static $serialize(value) {
|
|
36
|
+
const key = Object.entries(of).find(([_, v]) => v === value.constructor)?.[0];
|
|
37
|
+
if (!key) throw new Error("Cannot serialize Either, no matching key");
|
|
38
|
+
const definition = definitions[key];
|
|
39
|
+
if (!definition) throw new Error("Cannot serialize Either");
|
|
40
|
+
return {
|
|
41
|
+
...definition.$serialize(value),
|
|
42
|
+
_key: key
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
static $inline;
|
|
46
|
+
}
|
|
47
|
+
return $Either;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.Either = Either;
|
package/dist/either.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Empty, Shape } from "./_.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/either.ts
|
|
4
|
+
const Either = (of, base = Empty) => {
|
|
5
|
+
const definitions = Object.fromEntries(Object.entries(of).map(([key, value]) => {
|
|
6
|
+
return [key, Shape(value)];
|
|
7
|
+
}));
|
|
8
|
+
class $Either extends base {
|
|
9
|
+
constructor(value) {
|
|
10
|
+
super();
|
|
11
|
+
this.value = value;
|
|
12
|
+
}
|
|
13
|
+
static serialized;
|
|
14
|
+
static of = of;
|
|
15
|
+
static $shape = "either";
|
|
16
|
+
serialize() {
|
|
17
|
+
return $Either.$serialize(this.value);
|
|
18
|
+
}
|
|
19
|
+
match(...[matcher, fallback]) {
|
|
20
|
+
const handler = matcher[Object.entries(of).find(([_, v]) => v === this.value.constructor)?.[0]];
|
|
21
|
+
if (handler) return handler(this.value);
|
|
22
|
+
if (fallback) return fallback(this.value);
|
|
23
|
+
if (matcher._) return matcher._(this.value);
|
|
24
|
+
throw new Error("Non-exhaustive match");
|
|
25
|
+
}
|
|
26
|
+
static deserialize(value) {
|
|
27
|
+
return new this(this.$deserialize(value));
|
|
28
|
+
}
|
|
29
|
+
static $deserialize(value) {
|
|
30
|
+
const { _key: key, ...serialized } = value;
|
|
31
|
+
const definition = definitions[key];
|
|
32
|
+
if (!definition) throw new Error("Cannot deserialize Either");
|
|
33
|
+
return definition.$deserialize(serialized);
|
|
34
|
+
}
|
|
35
|
+
static $serialize(value) {
|
|
36
|
+
const key = Object.entries(of).find(([_, v]) => v === value.constructor)?.[0];
|
|
37
|
+
if (!key) throw new Error("Cannot serialize Either, no matching key");
|
|
38
|
+
const definition = definitions[key];
|
|
39
|
+
if (!definition) throw new Error("Cannot serialize Either");
|
|
40
|
+
return {
|
|
41
|
+
...definition.$serialize(value),
|
|
42
|
+
_key: key
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
static $inline;
|
|
46
|
+
}
|
|
47
|
+
return $Either;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { Either };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"either.spec.d.ts","sourceRoot":"","sources":["../src/either.spec.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./_";
|
|
2
|
+
export * from "./choice";
|
|
3
|
+
export * from "./class";
|
|
4
|
+
export * from "./dict";
|
|
5
|
+
export * from "./either";
|
|
6
|
+
export * from "./primitive";
|
|
7
|
+
export * from "./literal";
|
|
8
|
+
export * from "./discriminated-union";
|
|
9
|
+
export * from "./mapping";
|
|
10
|
+
export * from "./multiple";
|
|
11
|
+
export * from "./nothing";
|
|
12
|
+
export * from "./optional";
|
|
13
|
+
export * from "./addons/microsecond-timestamp";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,KAAK,CAAC;AACpB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,uBAAuB,CAAC;AACtC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gCAAgC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_class = require('./class.js');
|
|
3
|
+
const require_dict = require('./dict.js');
|
|
4
|
+
const require_primitive = require('./primitive.js');
|
|
5
|
+
const require_multiple = require('./multiple.js');
|
|
6
|
+
const require_nothing = require('./nothing.js');
|
|
7
|
+
const require_literal = require('./literal.js');
|
|
8
|
+
const require__ = require('./_.js');
|
|
9
|
+
const require_choice = require('./choice.js');
|
|
10
|
+
const require_either = require('./either.js');
|
|
11
|
+
const require_discriminated_union = require('./discriminated-union.js');
|
|
12
|
+
const require_mapping = require('./mapping.js');
|
|
13
|
+
const require_optional = require('./optional.js');
|
|
14
|
+
const require_microsecond_timestamp = require('./addons/microsecond-timestamp.js');
|
|
15
|
+
|
|
16
|
+
exports.Choice = require_choice.Choice;
|
|
17
|
+
exports.Class = require_class.Class;
|
|
18
|
+
exports.Dict = require_dict.Dict;
|
|
19
|
+
exports.DiscriminatedUnion = require_discriminated_union.DiscriminatedUnion;
|
|
20
|
+
exports.Either = require_either.Either;
|
|
21
|
+
exports.Empty = require__.Empty;
|
|
22
|
+
exports.Literal = require_literal.Literal;
|
|
23
|
+
exports.Mapping = require_mapping.Mapping;
|
|
24
|
+
exports.MicrosecondTimestamp = require_microsecond_timestamp.MicrosecondTimestamp;
|
|
25
|
+
exports.Multiple = require_multiple.Multiple;
|
|
26
|
+
exports.Nothing = require_nothing.Nothing;
|
|
27
|
+
exports.Optional = require_optional.Optional;
|
|
28
|
+
exports.Primitive = require_primitive.Primitive;
|
|
29
|
+
exports.Shape = require__.Shape;
|
|
30
|
+
exports.findBestKey = require_discriminated_union.findBestKey;
|
|
31
|
+
exports.forward = require__.forward;
|
|
32
|
+
exports.prepareShapeMap = require_discriminated_union.prepareShapeMap;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Class } from "./class.mjs";
|
|
2
|
+
import { Dict } from "./dict.mjs";
|
|
3
|
+
import { Primitive } from "./primitive.mjs";
|
|
4
|
+
import { Multiple } from "./multiple.mjs";
|
|
5
|
+
import { Nothing } from "./nothing.mjs";
|
|
6
|
+
import { Literal } from "./literal.mjs";
|
|
7
|
+
import { Empty, Shape, forward } from "./_.mjs";
|
|
8
|
+
import { Choice } from "./choice.mjs";
|
|
9
|
+
import { Either } from "./either.mjs";
|
|
10
|
+
import { DiscriminatedUnion, findBestKey, prepareShapeMap } from "./discriminated-union.mjs";
|
|
11
|
+
import { Mapping } from "./mapping.mjs";
|
|
12
|
+
import { Optional } from "./optional.mjs";
|
|
13
|
+
import { MicrosecondTimestamp } from "./addons/microsecond-timestamp.mjs";
|
|
14
|
+
|
|
15
|
+
export { Choice, Class, Dict, DiscriminatedUnion, Either, Empty, Literal, Mapping, MicrosecondTimestamp, Multiple, Nothing, Optional, Primitive, Shape, findBestKey, forward, prepareShapeMap };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type AbstractConstructor, type Constructor, Empty, type Expand } from "./_";
|
|
2
|
+
export type LiteralShorthand = string | number;
|
|
3
|
+
export declare const Literal: <const S extends LiteralShorthand, B extends AbstractConstructor<{}> = typeof Empty>(of: S, base?: B) => ILiteral<S, B>;
|
|
4
|
+
export type ILiteral<S extends LiteralShorthand, B extends AbstractConstructor<{}> = typeof Empty> = Omit<B, "prototype"> & {
|
|
5
|
+
value: S;
|
|
6
|
+
$shape: "literal";
|
|
7
|
+
deserialize<T extends Constructor>(this: T, value: S): InstanceType<T>;
|
|
8
|
+
$serialize(value: S): S;
|
|
9
|
+
$deserialize(value: S): S;
|
|
10
|
+
$inline: S;
|
|
11
|
+
} & (abstract new (value: Expand<S>) => InstanceType<B> & {
|
|
12
|
+
readonly value: S;
|
|
13
|
+
serialize(): S;
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=literal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../src/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,KAAK,WAAW,EAAE,KAAK,EAAE,KAAK,MAAM,EAAE,MAAM,KAAK,CAAC;AAErF,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,CAAC;AAE/C,eAAO,MAAM,OAAO,SACZ,CAAC,SAAS,gBAAgB,EAChC,CAAC,SAAS,mBAAmB,CAAC,EAAE,CAAC,qBAE7B,CAAC,SACC,CAAC,KACN,QAAQ,CAAC,CAAC,EAAE,CAAC,CA8Bf,CAAC;AAEF,MAAM,MAAM,QAAQ,CAClB,CAAC,SAAS,gBAAgB,EAC1B,CAAC,SAAS,mBAAmB,CAAC,EAAE,CAAC,GAAG,OAAO,KAAK,IAC9C,IAAI,CAAC,CAAC,EAAE,WAAW,CAAC,GAAG;IACzB,KAAK,EAAE,CAAC,CAAC;IACT,MAAM,EAAE,SAAS,CAAC;IAClB,WAAW,CAAC,CAAC,SAAS,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;IACvE,UAAU,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACxB,YAAY,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAC1B,OAAO,EAAE,CAAC,CAAC;CACZ,GAAG,CAAC,QAAQ,MACT,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,KACb,YAAY,CAAC,CAAC,CAAC,GAAG;IACrB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB,SAAS,IAAI,CAAC,CAAC;CAChB,CAAC,CAAC"}
|
package/dist/literal.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const require__ = require('./_.js');
|
|
2
|
+
|
|
3
|
+
//#region src/literal.ts
|
|
4
|
+
const Literal = (of, base = require__.Empty) => {
|
|
5
|
+
class $Literal extends base {
|
|
6
|
+
value = of;
|
|
7
|
+
static value = of;
|
|
8
|
+
static $shape = "literal";
|
|
9
|
+
serialize() {
|
|
10
|
+
return of;
|
|
11
|
+
}
|
|
12
|
+
static deserialize(value) {
|
|
13
|
+
return new this();
|
|
14
|
+
}
|
|
15
|
+
static $serialize(value) {
|
|
16
|
+
return of;
|
|
17
|
+
}
|
|
18
|
+
static $deserialize(value) {
|
|
19
|
+
return of;
|
|
20
|
+
}
|
|
21
|
+
static $inline;
|
|
22
|
+
}
|
|
23
|
+
return $Literal;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.Literal = Literal;
|
package/dist/literal.mjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Empty } from "./_.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/literal.ts
|
|
4
|
+
const Literal = (of, base = Empty) => {
|
|
5
|
+
class $Literal extends base {
|
|
6
|
+
value = of;
|
|
7
|
+
static value = of;
|
|
8
|
+
static $shape = "literal";
|
|
9
|
+
serialize() {
|
|
10
|
+
return of;
|
|
11
|
+
}
|
|
12
|
+
static deserialize(value) {
|
|
13
|
+
return new this();
|
|
14
|
+
}
|
|
15
|
+
static $serialize(value) {
|
|
16
|
+
return of;
|
|
17
|
+
}
|
|
18
|
+
static $deserialize(value) {
|
|
19
|
+
return of;
|
|
20
|
+
}
|
|
21
|
+
static $inline;
|
|
22
|
+
}
|
|
23
|
+
return $Literal;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { Literal };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal.spec.d.ts","sourceRoot":"","sources":["../src/literal.spec.ts"],"names":[],"mappings":""}
|