@ddd-ts/shape 0.0.36 → 0.0.37
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/package.json +15 -6
- package/dist/_.d.ts +0 -39
- package/dist/_.d.ts.map +0 -1
- package/dist/_.js +0 -55
- package/dist/_.js.map +0 -1
- package/dist/addons/microsecond-timestamp.d.ts +0 -24
- package/dist/addons/microsecond-timestamp.d.ts.map +0 -1
- package/dist/addons/microsecond-timestamp.js +0 -75
- package/dist/addons/microsecond-timestamp.js.map +0 -1
- package/dist/choice.d.ts +0 -30
- package/dist/choice.d.ts.map +0 -1
- package/dist/choice.js +0 -49
- package/dist/choice.js.map +0 -1
- package/dist/choice.spec.d.ts +0 -2
- package/dist/choice.spec.d.ts.map +0 -1
- package/dist/choice.spec.js +0 -147
- package/dist/choice.spec.js.map +0 -1
- package/dist/class.d.ts +0 -23
- package/dist/class.d.ts.map +0 -1
- package/dist/class.js +0 -31
- package/dist/class.js.map +0 -1
- package/dist/class.spec.d.ts +0 -2
- package/dist/class.spec.d.ts.map +0 -1
- package/dist/class.spec.js +0 -39
- package/dist/class.spec.js.map +0 -1
- package/dist/dict.d.ts +0 -43
- package/dist/dict.d.ts.map +0 -1
- package/dist/dict.js +0 -47
- package/dist/dict.js.map +0 -1
- package/dist/dict.spec.d.ts +0 -2
- package/dist/dict.spec.d.ts.map +0 -1
- package/dist/dict.spec.js +0 -180
- package/dist/dict.spec.js.map +0 -1
- package/dist/discriminated-union.d.ts +0 -74
- package/dist/discriminated-union.d.ts.map +0 -1
- package/dist/discriminated-union.js +0 -97
- package/dist/discriminated-union.js.map +0 -1
- package/dist/discriminated-union.spec.d.ts +0 -2
- package/dist/discriminated-union.spec.d.ts.map +0 -1
- package/dist/discriminated-union.spec.js +0 -262
- package/dist/discriminated-union.spec.js.map +0 -1
- package/dist/either.d.ts +0 -84
- package/dist/either.d.ts.map +0 -1
- package/dist/either.js +0 -62
- package/dist/either.js.map +0 -1
- package/dist/either.spec.d.ts +0 -2
- package/dist/either.spec.d.ts.map +0 -1
- package/dist/either.spec.js +0 -232
- package/dist/either.spec.js.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -30
- package/dist/index.js.map +0 -1
- package/dist/literal.d.ts +0 -15
- package/dist/literal.d.ts.map +0 -1
- package/dist/literal.js +0 -27
- package/dist/literal.js.map +0 -1
- package/dist/literal.spec.d.ts +0 -2
- package/dist/literal.spec.d.ts.map +0 -1
- package/dist/literal.spec.js +0 -162
- package/dist/literal.spec.js.map +0 -1
- package/dist/mapping.d.ts +0 -43
- package/dist/mapping.d.ts.map +0 -1
- package/dist/mapping.js +0 -50
- package/dist/mapping.js.map +0 -1
- package/dist/mapping.spec.d.ts +0 -2
- package/dist/mapping.spec.d.ts.map +0 -1
- package/dist/mapping.spec.js +0 -207
- package/dist/mapping.spec.js.map +0 -1
- package/dist/multiple.d.ts +0 -20
- package/dist/multiple.d.ts.map +0 -1
- package/dist/multiple.js +0 -120
- package/dist/multiple.js.map +0 -1
- package/dist/multiple.spec.d.ts +0 -2
- package/dist/multiple.spec.d.ts.map +0 -1
- package/dist/multiple.spec.js +0 -207
- package/dist/multiple.spec.js.map +0 -1
- package/dist/nothing.d.ts +0 -14
- package/dist/nothing.d.ts.map +0 -1
- package/dist/nothing.js +0 -19
- package/dist/nothing.js.map +0 -1
- package/dist/nothing.spec.d.ts +0 -2
- package/dist/nothing.spec.d.ts.map +0 -1
- package/dist/nothing.spec.js +0 -172
- package/dist/nothing.spec.js.map +0 -1
- package/dist/optional.d.ts +0 -26
- package/dist/optional.d.ts.map +0 -1
- package/dist/optional.js +0 -41
- package/dist/optional.js.map +0 -1
- package/dist/optional.spec.d.ts +0 -2
- package/dist/optional.spec.d.ts.map +0 -1
- package/dist/optional.spec.js +0 -172
- package/dist/optional.spec.js.map +0 -1
- package/dist/primitive.d.ts +0 -38
- package/dist/primitive.d.ts.map +0 -1
- package/dist/primitive.js +0 -33
- package/dist/primitive.js.map +0 -1
- package/dist/primitive.spec.d.ts +0 -2
- package/dist/primitive.spec.d.ts.map +0 -1
- package/dist/primitive.spec.js +0 -162
- package/dist/primitive.spec.js.map +0 -1
- package/dist/test.d.ts +0 -36
- package/dist/test.d.ts.map +0 -1
- package/dist/test.js +0 -45
- package/dist/test.js.map +0 -1
package/dist/class.spec.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const class_1 = require("./class");
|
|
4
|
-
const dict_1 = require("./dict");
|
|
5
|
-
const test_1 = require("./test");
|
|
6
|
-
describe("Class", () => {
|
|
7
|
-
it("inline definition", () => {
|
|
8
|
-
class A {
|
|
9
|
-
serialize() {
|
|
10
|
-
return "a";
|
|
11
|
-
}
|
|
12
|
-
static deserialize(value) {
|
|
13
|
-
return new A();
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
class Test extends (0, dict_1.Dict)({
|
|
17
|
-
nested: (0, class_1.Class)(A),
|
|
18
|
-
}) {
|
|
19
|
-
test = true;
|
|
20
|
-
}
|
|
21
|
-
// Constructor parameters
|
|
22
|
-
(0, test_1.ex)(Test).toHaveFirstParam().ok;
|
|
23
|
-
// Deserializes to self
|
|
24
|
-
(0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
|
|
25
|
-
const a = Test.deserialize({ nested: "a" });
|
|
26
|
-
(0, test_1.ex)(a).toBeInstanceOf(Test).ok;
|
|
27
|
-
// Property is still there
|
|
28
|
-
const b = a.test;
|
|
29
|
-
(0, test_1.ex)(b).toBe(true).ok;
|
|
30
|
-
// Serializes to a precise type
|
|
31
|
-
(0, test_1.ex)(a.serialize()).toStrictEqual({ nested: "a" }).ok;
|
|
32
|
-
// Shape is still there
|
|
33
|
-
(0, test_1.ex)(a.nested.serialize()).toBe("a").ok;
|
|
34
|
-
// Instantiates with a precise type
|
|
35
|
-
const e = new Test({ nested: new A() });
|
|
36
|
-
(0, test_1.ex)(e).toBeInstanceOf(Test).ok;
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
//# sourceMappingURL=class.spec.js.map
|
package/dist/class.spec.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"class.spec.js","sourceRoot":"","sources":["../src/class.spec.ts"],"names":[],"mappings":";;AAAA,mCAAgC;AAChC,iCAA8B;AAC9B,iCAA4B;AAE5B,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACrB,EAAE,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAC3B,MAAM,CAAC;YACL,SAAS;gBACP,OAAO,GAAG,CAAC;YACb,CAAC;YAED,MAAM,CAAC,WAAW,CAAC,KAAa;gBAC9B,OAAO,IAAI,CAAC,EAAE,CAAC;YACjB,CAAC;SACF;QAED,MAAM,IAAK,SAAQ,IAAA,WAAI,EAAC;YACtB,MAAM,EAAE,IAAA,aAAK,EAAC,CAAC,CAAC;SACjB,CAAC;YACA,IAAI,GAAG,IAAa,CAAC;SACtB;QAID,yBAAyB;QACzB,IAAA,SAAE,EAAC,IAAI,CAAC,CAAC,gBAAgB,EAAiB,CAAC,EAAE,CAAC;QAE9C,uBAAuB;QACvB,IAAA,SAAE,EAAC,IAAI,CAAC,WAAW,CAAC,CAAC,gBAAgB,EAAc,CAAC,EAAE,CAAC;QACvD,MAAM,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QAC5C,IAAA,SAAE,EAAC,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QAE9B,0BAA0B;QAC1B,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;QACjB,IAAA,SAAE,EAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAa,CAAC,CAAC,EAAE,CAAC;QAE7B,+BAA+B;QAC/B,IAAA,SAAE,EAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;QAEpD,uBAAuB;QACvB,IAAA,SAAE,EAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QAEtC,mCAAmC;QACnC,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;QACxC,IAAA,SAAE,EAAC,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IAChC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { Class } from \"./class\";\nimport { Dict } from \"./dict\";\nimport { ex } from \"./test\";\n\ndescribe(\"Class\", () => {\n it(\"inline definition\", () => {\n class A {\n serialize() {\n return \"a\";\n }\n\n static deserialize(value: string) {\n return new A();\n }\n }\n\n class Test extends Dict({\n nested: Class(A),\n }) {\n test = true as const;\n }\n\n type Serialized = { nested: string };\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<{ nested: A }>().ok;\n\n // Deserializes to self\n ex(Test.deserialize).toHaveFirstParam<Serialized>().ok;\n const a = Test.deserialize({ nested: \"a\" });\n ex(a).toBeInstanceOf(Test).ok;\n\n // Property is still there\n const b = a.test;\n ex(b).toBe(true as const).ok;\n\n // Serializes to a precise type\n ex(a.serialize()).toStrictEqual({ nested: \"a\" }).ok;\n\n // Shape is still there\n ex(a.nested.serialize()).toBe(\"a\").ok;\n\n // Instantiates with a precise type\n const e = new Test({ nested: new A() });\n ex(e).toBeInstanceOf(Test).ok;\n });\n});\n"]}
|
package/dist/dict.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { DefinitionOf, Shorthand, Expand, AbstractConstructor, Constructor, Empty } from "./_";
|
|
2
|
-
export type DictShorthand = {
|
|
3
|
-
[key: string]: Shorthand;
|
|
4
|
-
};
|
|
5
|
-
type Internal<S extends DictShorthand, B extends AbstractConstructor<{}>> = {
|
|
6
|
-
Definition: {
|
|
7
|
-
-readonly [K in keyof S]: DefinitionOf<S[K]>;
|
|
8
|
-
};
|
|
9
|
-
Serialized: (B extends {
|
|
10
|
-
$name: infer U;
|
|
11
|
-
} ? {
|
|
12
|
-
$name: U;
|
|
13
|
-
} : {}) & {
|
|
14
|
-
-readonly [K in keyof S]: ReturnType<DefinitionOf<S[K]>["$serialize"]>;
|
|
15
|
-
};
|
|
16
|
-
Deserializing: (B extends {
|
|
17
|
-
$name: infer U;
|
|
18
|
-
} ? {
|
|
19
|
-
$name: U;
|
|
20
|
-
} : {}) & {
|
|
21
|
-
-readonly [K in keyof S]: Parameters<DefinitionOf<S[K]>["$deserialize"]>[0];
|
|
22
|
-
};
|
|
23
|
-
Inline: {
|
|
24
|
-
-readonly [K in keyof S]: DefinitionOf<S[K]>["$inline"];
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
export declare const Dict: <const S extends {
|
|
28
|
-
[key: string]: any;
|
|
29
|
-
}, B extends AbstractConstructor<{}> = typeof Empty>(of: S, base?: B) => IDict<S, B>;
|
|
30
|
-
export type IDict<S extends {
|
|
31
|
-
[key: string]: any;
|
|
32
|
-
}, B extends AbstractConstructor<{}> = typeof Empty> = Omit<B, ""> & {
|
|
33
|
-
$shape: "dict";
|
|
34
|
-
$of: S;
|
|
35
|
-
deserialize<T extends Constructor>(this: T, value: Expand<Internal<S, B>["Serialized"]>): InstanceType<T>;
|
|
36
|
-
$deserialize<T>(this: T, value: Internal<S, B>["Deserializing"]): Internal<S, B>["Inline"];
|
|
37
|
-
$serialize<T extends Constructor>(this: T, value: InstanceType<T>): Internal<S, B>["Serialized"];
|
|
38
|
-
$inline: Internal<S, B>["Inline"];
|
|
39
|
-
} & (abstract new (value: Expand<Internal<S, B>["Inline"]>) => InstanceType<B> & {
|
|
40
|
-
serialize(): Expand<Internal<S, B>["Serialized"]>;
|
|
41
|
-
} & Internal<S, B>["Inline"]);
|
|
42
|
-
export {};
|
|
43
|
-
//# sourceMappingURL=dict.d.ts.map
|
package/dist/dict.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dict.d.ts","sourceRoot":"","sources":["../src/dict.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,SAAS,EAET,MAAM,EACN,mBAAmB,EACnB,WAAW,EACX,KAAK,EACN,MAAM,KAAK,CAAC;AAEb,MAAM,MAAM,aAAa,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEzD,KAAK,QAAQ,CAAC,CAAC,SAAS,aAAa,EAAE,CAAC,SAAS,mBAAmB,CAAC,EAAE,CAAC,IAAI;IAC1E,UAAU,EAAE;QAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAAE,CAAC;IAC7D,UAAU,EAAE,CAAC,CAAC,SAAS;QAAE,KAAK,EAAE,MAAM,CAAC,CAAA;KAAE,GAAG;QAAE,KAAK,EAAE,CAAC,CAAA;KAAE,GAAG,EAAE,CAAC,GAAG;QAC/D,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;KACvE,CAAC;IACF,aAAa,EAAE,CAAC,CAAC,SAAS;QAAE,KAAK,EAAE,MAAM,CAAC,CAAA;KAAE,GAAG;QAAE,KAAK,EAAE,CAAC,CAAA;KAAE,GAAG,EAAE,CAAC,GAAG;QAClE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;KAC5E,CAA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AC,QAAQ,CAAC,CAAC;CACnC,GAAG,CAAC,QAAQ,MACT,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,KACpC,YAAY,CAAC,CAAC,CAAC,GAAG;IACrB,SAAS,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC;CACnD,GAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC"}
|
package/dist/dict.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Dict = void 0;
|
|
4
|
-
const _1 = require("./_");
|
|
5
|
-
const Dict = (of, base = _1.Empty) => {
|
|
6
|
-
class $Dict extends base {
|
|
7
|
-
static $shape = "dict";
|
|
8
|
-
static $of = of;
|
|
9
|
-
constructor(...args) {
|
|
10
|
-
super();
|
|
11
|
-
Object.assign(this, args[0]);
|
|
12
|
-
}
|
|
13
|
-
serialize() {
|
|
14
|
-
return $Dict.$serialize(this);
|
|
15
|
-
}
|
|
16
|
-
static deserialize(value) {
|
|
17
|
-
const runtime = this.$deserialize(value);
|
|
18
|
-
return new this(runtime);
|
|
19
|
-
}
|
|
20
|
-
static $deserialize(value) {
|
|
21
|
-
const split = Object.entries(of);
|
|
22
|
-
const transform = split.map(([key, child]) => {
|
|
23
|
-
const longhand = (0, _1.Shape)(child);
|
|
24
|
-
const deserialized = longhand.$deserialize(value[key]);
|
|
25
|
-
return [key, deserialized];
|
|
26
|
-
});
|
|
27
|
-
const merge = Object.fromEntries(transform);
|
|
28
|
-
return merge;
|
|
29
|
-
}
|
|
30
|
-
static $serialize(value) {
|
|
31
|
-
const split = Object.entries(of);
|
|
32
|
-
const transform = split.map(([key, child]) => {
|
|
33
|
-
const longhand = (0, _1.Shape)(child);
|
|
34
|
-
const serialized = longhand.$serialize(value[key]);
|
|
35
|
-
return [key, serialized];
|
|
36
|
-
});
|
|
37
|
-
const merge = Object.fromEntries(transform);
|
|
38
|
-
if ("$name" in base) {
|
|
39
|
-
return { ...merge, $name: base.$name };
|
|
40
|
-
}
|
|
41
|
-
return merge;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return $Dict;
|
|
45
|
-
};
|
|
46
|
-
exports.Dict = Dict;
|
|
47
|
-
//# sourceMappingURL=dict.js.map
|
package/dist/dict.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dict.js","sourceRoot":"","sources":["../src/dict.ts"],"names":[],"mappings":";;;AAAA,0BAQa;AAiBN,MAAM,IAAI,GAAG,CAIlB,EAAK,EACL,OAAU,QAAY,EACT,EAAE;IACf,MAAe,KAAM,SAAS,IAAuC;QACnE,MAAM,CAAC,MAAM,GAAG,MAAe,CAAC;QAChC,MAAM,CAAC,GAAG,GAAG,EAAE,CAAC;QAEhB,YAAY,GAAG,IAAW;YACxB,KAAK,EAAE,CAAC;YACR,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/B,CAAC;QAED,SAAS;YACP,OAAO,KAAK,CAAC,UAAU,CAAC,IAAW,CAAQ,CAAC;QAC9C,CAAC;QAED,MAAM,CAAC,WAAW,CAEhB,KAAU;YAEV,MAAM,OAAO,GAAI,IAAY,CAAC,YAAY,CAAC,KAAY,CAAC,CAAC;YACzD,OAAO,IAAI,IAAI,CAAC,OAAc,CAAQ,CAAC;QACzC,CAAC;QAED,MAAM,CAAC,YAAY,CAAkC,KAAU;YAC7D,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;YACjC,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;gBAC3C,MAAM,QAAQ,GAAG,IAAA,QAAK,EAAC,KAAK,CAAQ,CAAC;gBACrC,MAAM,YAAY,GAAG,QAAQ,CAAC,YAAY,CAAE,KAAa,CAAC,GAAG,CAAC,CAAC,CAAC;gBAChE,OAAO,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;YAC7B,CAAC,CAAC,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;YAC5C,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,CAAC,UAAU,CAEf,KAAsB;YAEtB,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;YACjC,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;gBAC3C,MAAM,QAAQ,GAAG,IAAA,QAAK,EAAC,KAAY,CAAQ,CAAC;gBAC5C,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAE,KAAa,CAAC,GAAG,CAAC,CAAC,CAAC;gBAC5D,OAAO,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;YAC3B,CAAC,CAAC,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;YAE5C,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;gBACpB,OAAO,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAS,CAAC;YAChD,CAAC;YAED,OAAO,KAAY,CAAC;QACtB,CAAC;;IAGH,OAAO,KAAY,CAAC;AACtB,CAAC,CAAC;AA5DW,QAAA,IAAI,QA4Df","sourcesContent":["import {\n DefinitionOf,\n Shorthand,\n Shape,\n Expand,\n AbstractConstructor,\n Constructor,\n Empty,\n} from \"./_\";\n\nexport type DictShorthand = { [key: string]: Shorthand };\n\ntype Internal<S extends DictShorthand, B extends AbstractConstructor<{}>> = {\n Definition: { -readonly [K in keyof S]: DefinitionOf<S[K]> };\n Serialized: (B extends { $name: infer U } ? { $name: U } : {}) & {\n -readonly [K in keyof S]: ReturnType<DefinitionOf<S[K]>[\"$serialize\"]>;\n };\n Deserializing: (B extends { $name: infer U } ? { $name: U } : {}) & {\n -readonly [K in keyof S]: Parameters<DefinitionOf<S[K]>[\"$deserialize\"]>[0];\n };\n Inline: {\n -readonly [K in keyof S]: DefinitionOf<S[K]>[\"$inline\"];\n };\n};\n\nexport const Dict = <\n const S extends { [key: string]: any },\n B extends AbstractConstructor<{}> = typeof Empty,\n>(\n of: S,\n base: B = Empty as any,\n): IDict<S, B> => {\n abstract class $Dict extends (base as any as AbstractConstructor<{}>) {\n static $shape = \"dict\" as const;\n static $of = of;\n\n constructor(...args: any[]) {\n super();\n Object.assign(this, args[0]);\n }\n\n serialize() {\n return $Dict.$serialize(this as any) as any;\n }\n\n static deserialize<T extends Constructor>(\n this: T,\n value: any,\n ): InstanceType<T> {\n const runtime = (this as any).$deserialize(value as any);\n return new this(runtime as any) as any;\n }\n\n static $deserialize<T extends typeof $Dict>(this: T, value: any): any {\n const split = Object.entries(of);\n const transform = split.map(([key, child]) => {\n const longhand = Shape(child) as any;\n const deserialized = longhand.$deserialize((value as any)[key]);\n return [key, deserialized];\n });\n const merge = Object.fromEntries(transform);\n return merge;\n }\n\n static $serialize<T extends typeof $Dict>(\n this: T,\n value: InstanceType<T>,\n ): any {\n const split = Object.entries(of);\n const transform = split.map(([key, child]) => {\n const longhand = Shape(child as any) as any;\n const serialized = longhand.$serialize((value as any)[key]);\n return [key, serialized];\n });\n const merge = Object.fromEntries(transform);\n\n if (\"$name\" in base) {\n return { ...merge, $name: base.$name } as any;\n }\n\n return merge as any;\n }\n }\n\n return $Dict as any;\n};\n\nexport type IDict<\n S extends {\n [key: string]: any;\n },\n B extends AbstractConstructor<{}> = typeof Empty,\n> = Omit<B, \"\"> & {\n $shape: \"dict\";\n $of: S;\n deserialize<T extends Constructor>(\n this: T,\n value: Expand<Internal<S, B>[\"Serialized\"]>,\n ): InstanceType<T>;\n $deserialize<T>(\n this: T,\n value: Internal<S, B>[\"Deserializing\"],\n ): Internal<S, B>[\"Inline\"];\n $serialize<T extends Constructor>(\n this: T,\n value: InstanceType<T>,\n ): Internal<S, B>[\"Serialized\"];\n $inline: Internal<S, B>[\"Inline\"];\n} & (abstract new (\n value: Expand<Internal<S, B>[\"Inline\"]>,\n ) => InstanceType<B> & {\n serialize(): Expand<Internal<S, B>[\"Serialized\"]>;\n } & Internal<S, B>[\"Inline\"]);\n"]}
|
package/dist/dict.spec.d.ts
DELETED
package/dist/dict.spec.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dict.spec.d.ts","sourceRoot":"","sources":["../src/dict.spec.ts"],"names":[],"mappings":""}
|
package/dist/dict.spec.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const dict_1 = require("./dict");
|
|
4
|
-
const primitive_1 = require("./primitive");
|
|
5
|
-
const test_1 = require("./test");
|
|
6
|
-
describe("Dict", () => {
|
|
7
|
-
it("class definition", () => {
|
|
8
|
-
class Test extends (0, dict_1.Dict)({
|
|
9
|
-
a: (0, primitive_1.Primitive)(String),
|
|
10
|
-
b: Number,
|
|
11
|
-
}) {
|
|
12
|
-
test = true;
|
|
13
|
-
}
|
|
14
|
-
// Constructor parameters
|
|
15
|
-
(0, test_1.ex)(Test).toHaveFirstParam().ok;
|
|
16
|
-
// Deserialization
|
|
17
|
-
(0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
|
|
18
|
-
const a = Test.deserialize({ a: "a", b: 1 });
|
|
19
|
-
expect(a).toBeInstanceOf(Test);
|
|
20
|
-
// Additional prototype
|
|
21
|
-
(0, test_1.ex)(a.test).toBe(true).ok;
|
|
22
|
-
// Inherited prototype from deserialization
|
|
23
|
-
(0, test_1.ex)(a.a).toBe("a").ok;
|
|
24
|
-
(0, test_1.ex)(a.b).toBe(1).ok;
|
|
25
|
-
// Serialization
|
|
26
|
-
(0, test_1.ex)(a.serialize()).toStrictEqual({ a: "a", b: 1 }).ok;
|
|
27
|
-
// Instantiation
|
|
28
|
-
const b = new Test({ a: "a", b: 1 });
|
|
29
|
-
(0, test_1.ex)(b).toBeInstanceOf(Test).ok;
|
|
30
|
-
// Inherited prototype from instantiation
|
|
31
|
-
(0, test_1.ex)(b.a).toBe("a").ok;
|
|
32
|
-
(0, test_1.ex)(b.b).toBe(1).ok;
|
|
33
|
-
});
|
|
34
|
-
it("inline definition", () => {
|
|
35
|
-
class Test extends (0, dict_1.Dict)({
|
|
36
|
-
shorthand: { short: String },
|
|
37
|
-
longhand: (0, dict_1.Dict)({ long: Number }),
|
|
38
|
-
}) {
|
|
39
|
-
test = true;
|
|
40
|
-
}
|
|
41
|
-
// Constructor parameters
|
|
42
|
-
(0, test_1.ex)(Test).toHaveFirstParam().ok;
|
|
43
|
-
// Deserialization
|
|
44
|
-
(0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
|
|
45
|
-
const a = Test.deserialize({
|
|
46
|
-
shorthand: { short: "test" },
|
|
47
|
-
longhand: { long: 1 },
|
|
48
|
-
});
|
|
49
|
-
(0, test_1.ex)(a).toBeInstanceOf(Test).ok;
|
|
50
|
-
// Additional prototype
|
|
51
|
-
(0, test_1.ex)(a.test).toBe(true);
|
|
52
|
-
// Inherited prototype from deserialization
|
|
53
|
-
(0, test_1.ex)(a.shorthand).toStrictEqual({ short: "test" }).ok;
|
|
54
|
-
(0, test_1.ex)(a.longhand).toStrictEqual({ long: 1 }).ok;
|
|
55
|
-
// Serialization
|
|
56
|
-
(0, test_1.ex)(a.serialize()).toStrictEqual({
|
|
57
|
-
shorthand: { short: "test" },
|
|
58
|
-
longhand: { long: 1 },
|
|
59
|
-
}).ok;
|
|
60
|
-
// Instantiation
|
|
61
|
-
const b = new Test({ shorthand: { short: "test" }, longhand: { long: 1 } });
|
|
62
|
-
(0, test_1.ex)(b).toBeInstanceOf(Test).ok;
|
|
63
|
-
// Inherited prototype
|
|
64
|
-
(0, test_1.ex)(b.shorthand).toStrictEqual({ short: "test" }).ok;
|
|
65
|
-
(0, test_1.ex)(b.longhand).toStrictEqual({ long: 1 }).ok;
|
|
66
|
-
});
|
|
67
|
-
it("referenced definition", () => {
|
|
68
|
-
class A extends (0, dict_1.Dict)({ a: (0, primitive_1.Primitive)(Number) }) {
|
|
69
|
-
}
|
|
70
|
-
class Test extends (0, dict_1.Dict)({ nested: A }) {
|
|
71
|
-
test = true;
|
|
72
|
-
}
|
|
73
|
-
// Constructor parameters
|
|
74
|
-
(0, test_1.ex)(Test).toHaveFirstParam().ok;
|
|
75
|
-
// Deserialization
|
|
76
|
-
(0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
|
|
77
|
-
const a = Test.deserialize({ nested: { a: 1 } });
|
|
78
|
-
(0, test_1.ex)(a).toBeInstanceOf(Test).ok;
|
|
79
|
-
// Additional prototype
|
|
80
|
-
(0, test_1.ex)(a.test).toBe(true).ok;
|
|
81
|
-
// Serialization
|
|
82
|
-
(0, test_1.ex)(a.serialize()).toStrictEqual({ nested: { a: 1 } }).ok;
|
|
83
|
-
// Instantiation
|
|
84
|
-
const b = new Test({ nested: new A({ a: 2 }) });
|
|
85
|
-
(0, test_1.ex)(b).toBeInstanceOf(Test);
|
|
86
|
-
// Inherited prototype
|
|
87
|
-
(0, test_1.ex)(b.nested).toBeInstanceOf(A);
|
|
88
|
-
(0, test_1.ex)(b.nested.a).toBe(2);
|
|
89
|
-
});
|
|
90
|
-
it("mixin extension", () => {
|
|
91
|
-
const Testable = (shape) => {
|
|
92
|
-
// Dict does not support not specifying the specific keys
|
|
93
|
-
// of the shape at the extends level.
|
|
94
|
-
// This is due to the fact that the shape is not known at the
|
|
95
|
-
// time of the class definition, thus the returned instance
|
|
96
|
-
// would have unknown members at compile time.
|
|
97
|
-
// Use mixin supersede instead.
|
|
98
|
-
class I extends (0, dict_1.Dict)({ nested: shape }) {
|
|
99
|
-
deep = true;
|
|
100
|
-
static deep = true;
|
|
101
|
-
}
|
|
102
|
-
return I;
|
|
103
|
-
};
|
|
104
|
-
class Test extends Testable({ a: (0, primitive_1.Primitive)(Number) }) {
|
|
105
|
-
test = true;
|
|
106
|
-
// @ts-expect-error is not assignable to parameter of type 'true'
|
|
107
|
-
abstract = false;
|
|
108
|
-
}
|
|
109
|
-
// Constructor parameters
|
|
110
|
-
(0, test_1.ex)(Test).toHaveFirstParam().ok;
|
|
111
|
-
// Additional static prototype
|
|
112
|
-
(0, test_1.ex)(Test.deep).toBe(true).ok;
|
|
113
|
-
// Deserialization
|
|
114
|
-
(0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
|
|
115
|
-
const a = Test.deserialize({ nested: { a: 1 } });
|
|
116
|
-
(0, test_1.ex)(a).toBeInstanceOf(Test).ok;
|
|
117
|
-
// Additional prototype on deserialization
|
|
118
|
-
(0, test_1.ex)(a.test).toBe(true).ok;
|
|
119
|
-
(0, test_1.ex)(a.abstract).toBe(false).ok;
|
|
120
|
-
// Extended prototype on deserialization
|
|
121
|
-
(0, test_1.ex)(a.deep).toBe(true).ok;
|
|
122
|
-
// Inherited prototype on deserialization
|
|
123
|
-
(0, test_1.ex)(a.nested).toStrictEqual({ a: 1 }).ok;
|
|
124
|
-
// Serialization
|
|
125
|
-
(0, test_1.ex)(a.serialize()).toStrictEqual({ nested: { a: 1 } }).ok;
|
|
126
|
-
// Instantiation
|
|
127
|
-
const b = new Test({ nested: { a: 2 } });
|
|
128
|
-
(0, test_1.ex)(b).toBeInstanceOf(Test).ok;
|
|
129
|
-
// Additional prototype on instantiation
|
|
130
|
-
(0, test_1.ex)(b.test).toBe(true).ok;
|
|
131
|
-
(0, test_1.ex)(b.abstract).toBe(false).ok;
|
|
132
|
-
// Extended prototype on instantiation
|
|
133
|
-
(0, test_1.ex)(b.deep).toBe(true).ok;
|
|
134
|
-
// Inherited prototype on instantiation
|
|
135
|
-
(0, test_1.ex)(b.nested).toStrictEqual({ a: 2 }).ok;
|
|
136
|
-
});
|
|
137
|
-
it("mixin supersede", () => {
|
|
138
|
-
const Testable = (shape) => {
|
|
139
|
-
class I {
|
|
140
|
-
deep = true;
|
|
141
|
-
static deep = true;
|
|
142
|
-
}
|
|
143
|
-
return (0, dict_1.Dict)(shape, I);
|
|
144
|
-
};
|
|
145
|
-
class Test extends Testable({ a: (0, primitive_1.Primitive)(Number) }) {
|
|
146
|
-
test = true;
|
|
147
|
-
// @ts-expect-error is not assignable to parameter of type 'true'
|
|
148
|
-
abstract = false;
|
|
149
|
-
}
|
|
150
|
-
// Constructor parameters
|
|
151
|
-
(0, test_1.ex)(Test).toHaveFirstParam().ok;
|
|
152
|
-
// Additional static prototype
|
|
153
|
-
(0, test_1.ex)(Test.deep).toBe(true).ok;
|
|
154
|
-
// Deserialization
|
|
155
|
-
(0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
|
|
156
|
-
const a = Test.deserialize({ a: 1 });
|
|
157
|
-
(0, test_1.ex)(a).toBeInstanceOf(Test).ok;
|
|
158
|
-
// Additional prototype on deserialization
|
|
159
|
-
(0, test_1.ex)(a.test).toBe(true).ok;
|
|
160
|
-
(0, test_1.ex)(a.abstract).toBe(false).ok;
|
|
161
|
-
// Superseded prototype on deserialization
|
|
162
|
-
(0, test_1.ex)(a.deep).toBe(true).ok;
|
|
163
|
-
// Inherited prototype on deserialization
|
|
164
|
-
(0, test_1.ex)(a.a).toBe(1).ok;
|
|
165
|
-
// Serialization
|
|
166
|
-
(0, test_1.ex)(a.serialize()).toStrictEqual({ a: 1 }).ok;
|
|
167
|
-
// Instantiation
|
|
168
|
-
const b = new Test({ a: 2 });
|
|
169
|
-
(0, test_1.ex)(b).toBeInstanceOf(Test).ok;
|
|
170
|
-
// Additional prototype on instantiation
|
|
171
|
-
(0, test_1.ex)(b.test).toBe(true).ok;
|
|
172
|
-
(0, test_1.ex)(b.abstract).toBe(false).ok;
|
|
173
|
-
// Superseded prototype on instantiation
|
|
174
|
-
(0, test_1.ex)(b.deep).toBe(true).ok;
|
|
175
|
-
(0, test_1.ex)(b.abstract).toBe(false).ok;
|
|
176
|
-
// Inherited prototype on instantiation
|
|
177
|
-
(0, test_1.ex)(b.a).toBe(2).ok;
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
//# sourceMappingURL=dict.spec.js.map
|
package/dist/dict.spec.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dict.spec.js","sourceRoot":"","sources":["../src/dict.spec.ts"],"names":[],"mappings":";;AAAA,iCAA6C;AAC7C,2CAAwC;AACxC,iCAA4B;AAE5B,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAC1B,MAAM,IAAK,SAAQ,IAAA,WAAI,EAAC;YACtB,CAAC,EAAE,IAAA,qBAAS,EAAC,MAAM,CAAC;YACpB,CAAC,EAAE,MAAM;SACV,CAAC;YACA,IAAI,GAAG,IAAa,CAAC;SACtB;QAID,yBAAyB;QACzB,IAAA,SAAE,EAAC,IAAI,CAAC,CAAC,gBAAgB,EAAc,CAAC,EAAE,CAAC;QAE3C,kBAAkB;QAClB,IAAA,SAAE,EAAC,IAAI,CAAC,WAAW,CAAC,CAAC,gBAAgB,EAAc,CAAC,EAAE,CAAC;QACvD,MAAM,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAC7C,MAAM,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;QAE/B,uBAAuB;QACvB,IAAA,SAAE,EAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAa,CAAC,CAAC,EAAE,CAAC;QAElC,2CAA2C;QAC3C,IAAA,SAAE,EAAC,CAAC,CAAC,CAAC,CAAC,CAAC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./dict\";\nimport { Primitive } from \"./primitive\";\nimport { ex } from \"./test\";\n\ndescribe(\"Dict\", () => {\n it(\"class definition\", () => {\n class Test extends Dict({\n a: Primitive(String),\n b: Number,\n }) {\n test = true as const;\n }\n\n type Serialized = { a: string; b: number };\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<Serialized>().ok;\n\n // Deserialization\n ex(Test.deserialize).toHaveFirstParam<Serialized>().ok;\n const a = Test.deserialize({ a: \"a\", b: 1 });\n expect(a).toBeInstanceOf(Test);\n\n // Additional prototype\n ex(a.test).toBe(true as const).ok;\n\n // Inherited prototype from deserialization\n ex(a.a).toBe(\"a\").ok;\n ex(a.b).toBe(1).ok;\n\n // Serialization\n ex(a.serialize()).toStrictEqual({ a: \"a\", b: 1 }).ok;\n\n // Instantiation\n const b = new Test({ a: \"a\", b: 1 });\n ex(b).toBeInstanceOf(Test).ok;\n\n // Inherited prototype from instantiation\n ex(b.a).toBe(\"a\").ok;\n ex(b.b).toBe(1).ok;\n });\n\n it(\"inline definition\", () => {\n class Test extends Dict({\n shorthand: { short: String },\n longhand: Dict({ long: Number }),\n }) {\n test = true as const;\n }\n\n type Serialized = {\n shorthand: { short: string };\n longhand: { long: number };\n };\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<Serialized>().ok;\n\n // Deserialization\n ex(Test.deserialize).toHaveFirstParam<Serialized>().ok;\n const a = Test.deserialize({\n shorthand: { short: \"test\" },\n longhand: { long: 1 },\n });\n ex(a).toBeInstanceOf(Test).ok;\n\n // Additional prototype\n ex(a.test).toBe(true);\n\n // Inherited prototype from deserialization\n ex(a.shorthand).toStrictEqual({ short: \"test\" }).ok;\n ex(a.longhand).toStrictEqual({ long: 1 }).ok;\n\n // Serialization\n ex(a.serialize()).toStrictEqual({\n shorthand: { short: \"test\" },\n longhand: { long: 1 },\n }).ok;\n\n // Instantiation\n const b = new Test({ shorthand: { short: \"test\" }, longhand: { long: 1 } });\n ex(b).toBeInstanceOf(Test).ok;\n\n // Inherited prototype\n ex(b.shorthand).toStrictEqual({ short: \"test\" }).ok;\n ex(b.longhand).toStrictEqual({ long: 1 }).ok;\n });\n\n it(\"referenced definition\", () => {\n class A extends Dict({ a: Primitive(Number) }) {}\n\n class Test extends Dict({ nested: A }) {\n test = true as const;\n }\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<{ nested: A }>().ok;\n\n // Deserialization\n ex(Test.deserialize).toHaveFirstParam<{ nested: { a: number } }>().ok;\n const a = Test.deserialize({ nested: { a: 1 } });\n ex(a).toBeInstanceOf(Test).ok;\n\n // Additional prototype\n ex(a.test).toBe(true as const).ok;\n\n // Serialization\n ex(a.serialize()).toStrictEqual({ nested: { a: 1 } }).ok;\n\n // Instantiation\n const b = new Test({ nested: new A({ a: 2 }) });\n ex(b).toBeInstanceOf(Test);\n\n // Inherited prototype\n ex(b.nested).toBeInstanceOf(A);\n ex(b.nested.a).toBe(2);\n });\n\n it(\"mixin extension\", () => {\n const Testable = <S extends DictShorthand>(shape: S) => {\n // Dict does not support not specifying the specific keys\n // of the shape at the extends level.\n // This is due to the fact that the shape is not known at the\n // time of the class definition, thus the returned instance\n // would have unknown members at compile time.\n // Use mixin supersede instead.\n abstract class I extends Dict({ nested: shape }) {\n deep = true as const;\n static deep = true as const;\n\n abstract abstract: true;\n }\n\n return I;\n };\n\n class Test extends Testable({ a: Primitive(Number) }) {\n test = true as const;\n\n // @ts-expect-error is not assignable to parameter of type 'true'\n abstract = false as const;\n }\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<{ nested: { a: number } }>().ok;\n\n // Additional static prototype\n ex(Test.deep).toBe(true as const).ok;\n\n // Deserialization\n ex(Test.deserialize).toHaveFirstParam<{ nested: { a: number } }>().ok;\n const a = Test.deserialize({ nested: { a: 1 } });\n ex(a).toBeInstanceOf(Test).ok;\n\n // Additional prototype on deserialization\n ex(a.test).toBe(true as const).ok;\n ex(a.abstract).toBe(false as const).ok;\n\n // Extended prototype on deserialization\n ex(a.deep).toBe(true as const).ok;\n\n // Inherited prototype on deserialization\n ex(a.nested).toStrictEqual({ a: 1 }).ok;\n\n // Serialization\n ex(a.serialize()).toStrictEqual({ nested: { a: 1 } }).ok;\n\n // Instantiation\n const b = new Test({ nested: { a: 2 } });\n ex(b).toBeInstanceOf(Test).ok;\n\n // Additional prototype on instantiation\n ex(b.test).toBe(true as const).ok;\n ex(b.abstract).toBe(false as const).ok;\n\n // Extended prototype on instantiation\n ex(b.deep).toBe(true as const).ok;\n\n // Inherited prototype on instantiation\n ex(b.nested).toStrictEqual({ a: 2 }).ok;\n });\n\n it(\"mixin supersede\", () => {\n const Testable = <S extends DictShorthand>(shape: S) => {\n abstract class I {\n deep = true as const;\n static deep = true as const;\n\n abstract abstract: true;\n }\n\n return Dict(shape, I);\n };\n\n class Test extends Testable({ a: Primitive(Number) }) {\n test = true as const;\n\n // @ts-expect-error is not assignable to parameter of type 'true'\n abstract = false as const;\n }\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<{ a: number }>().ok;\n\n // Additional static prototype\n ex(Test.deep).toBe(true as const).ok;\n\n // Deserialization\n ex(Test.deserialize).toHaveFirstParam<{ a: number }>().ok;\n const a = Test.deserialize({ a: 1 });\n ex(a).toBeInstanceOf(Test).ok;\n\n // Additional prototype on deserialization\n ex(a.test).toBe(true as const).ok;\n ex(a.abstract).toBe(false as const).ok;\n\n // Superseded prototype on deserialization\n ex(a.deep).toBe(true as const).ok;\n\n // Inherited prototype on deserialization\n ex(a.a).toBe(1).ok;\n\n // Serialization\n ex(a.serialize()).toStrictEqual({ a: 1 }).ok;\n\n // Instantiation\n const b = new Test({ a: 2 });\n ex(b).toBeInstanceOf(Test).ok;\n\n // Additional prototype on instantiation\n ex(b.test).toBe(true as const).ok;\n ex(b.abstract).toBe(false as const).ok;\n\n // Superseded prototype on instantiation\n ex(b.deep).toBe(true as const).ok;\n ex(b.abstract).toBe(false as const).ok;\n\n // Inherited prototype on instantiation\n ex(b.a).toBe(2).ok;\n });\n});\n"]}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { Expand, DefinitionOf, AbstractConstructor, Empty, type Shorthand, type Definition } from "./_";
|
|
2
|
-
import { 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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"discriminated-union.d.ts","sourceRoot":"","sources":["../src/discriminated-union.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EACN,YAAY,EACZ,mBAAmB,EACnB,KAAK,EACL,KAAK,SAAS,EAEd,KAAK,UAAU,EAChB,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5C,KAAK,mBAAmB,CAAC,CAAC,IAAI,CAC5B,CAAC,SAAS,OAAO,GACb,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,GACd,KAAK,CACV,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,GAC1B,CAAC,GACD,KAAK,CAAC;AACV,KAAK,QAAQ,CAAC,CAAC,IAAI,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC;AAE5E,KAAK,YAAY,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,GAAG,EAAE,IAAI,OAAO,CAAC,CAAC,CAAC,SAAS,IAAI,GACpE,YAAY,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,GAC1D,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;AACd,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC;AAEtE,KAAK,UAAU,CAAC,CAAC,IAAI,YAAY,CAAC,CAAC,CAAC,SAAS,MAAM,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C,CAAC,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,GACvC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GACtB,KAAK,CAAC,GACV,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;CACvE,CAAC,CAAC"}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DiscriminatedUnion = void 0;
|
|
4
|
-
exports.findBestKey = findBestKey;
|
|
5
|
-
exports.prepareShapeMap = prepareShapeMap;
|
|
6
|
-
const _1 = require("./_");
|
|
7
|
-
function findBestKey(config) {
|
|
8
|
-
const hash = {};
|
|
9
|
-
for (const c of config) {
|
|
10
|
-
const shape = "$of" in c ? c.$of : c;
|
|
11
|
-
for (const key in shape) {
|
|
12
|
-
const k = key;
|
|
13
|
-
if (hash[key]) {
|
|
14
|
-
hash[key].add(shape[k]);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
hash[key] = new Set([shape[k]]);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
const key = Object.entries(hash)
|
|
22
|
-
.map(([key, value]) => [key, value.size])
|
|
23
|
-
.filter(([_, value]) => value === config.length)
|
|
24
|
-
.sort((a, b) => b[1] - a[1])?.[0]?.[0];
|
|
25
|
-
if (!key) {
|
|
26
|
-
throw new Error("Could not find key for DiscriminatedUnion");
|
|
27
|
-
}
|
|
28
|
-
return key;
|
|
29
|
-
}
|
|
30
|
-
function prepareShapeMap(config, key) {
|
|
31
|
-
return config.reduce((acc, c) => {
|
|
32
|
-
const shape = (0, _1.Shape)(c);
|
|
33
|
-
// Here, we are handling the two cases of the configuration:
|
|
34
|
-
// - the first one is when the configuration is a Definition directly.
|
|
35
|
-
// we can access the discriminator directly from the shape $of property
|
|
36
|
-
// - the second one is when the configuration is a DictShorthand or a plain Class.
|
|
37
|
-
// we first have to turn it into a Definition before accessing the discriminator
|
|
38
|
-
// Dict $of property references the configuration provided, which allows us to capture the discriminator
|
|
39
|
-
// Class $of property references the constructor of the class, which allows us to capture the discriminator
|
|
40
|
-
// which is mandatorily defined on the static side of the class
|
|
41
|
-
const discriminator = key in c
|
|
42
|
-
? c[key]
|
|
43
|
-
: "$of" in c
|
|
44
|
-
? c.$of[key]
|
|
45
|
-
: shape.$of[key];
|
|
46
|
-
acc[discriminator] = shape;
|
|
47
|
-
return acc;
|
|
48
|
-
}, {});
|
|
49
|
-
}
|
|
50
|
-
const DiscriminatedUnion = (of, ...args) => {
|
|
51
|
-
const base = args[0] || _1.Empty;
|
|
52
|
-
const key = findBestKey(of);
|
|
53
|
-
const map = prepareShapeMap(of, key);
|
|
54
|
-
class $DiscriminatedUnion extends base {
|
|
55
|
-
value;
|
|
56
|
-
constructor(value) {
|
|
57
|
-
super();
|
|
58
|
-
this.value = value;
|
|
59
|
-
}
|
|
60
|
-
static $of = of;
|
|
61
|
-
static $shape = "discriminated-union";
|
|
62
|
-
serialize() {
|
|
63
|
-
return $DiscriminatedUnion.$serialize(this.value);
|
|
64
|
-
}
|
|
65
|
-
match(matcher, fallback) {
|
|
66
|
-
const element = this.value;
|
|
67
|
-
const discriminant = element[key];
|
|
68
|
-
const handler = matcher[discriminant];
|
|
69
|
-
if (handler) {
|
|
70
|
-
return handler(element);
|
|
71
|
-
}
|
|
72
|
-
if (fallback) {
|
|
73
|
-
return fallback(element);
|
|
74
|
-
}
|
|
75
|
-
if (matcher._) {
|
|
76
|
-
return matcher._(element);
|
|
77
|
-
}
|
|
78
|
-
throw new Error("Non-exhaustive match");
|
|
79
|
-
}
|
|
80
|
-
static deserialize(value) {
|
|
81
|
-
return new this(this.$deserialize(value));
|
|
82
|
-
}
|
|
83
|
-
static $deserialize(value) {
|
|
84
|
-
const definition = map[value[key]];
|
|
85
|
-
if (!definition) {
|
|
86
|
-
throw new Error("Cannot deserialize DiscriminatedUnion");
|
|
87
|
-
}
|
|
88
|
-
return definition.$deserialize(value);
|
|
89
|
-
}
|
|
90
|
-
static $serialize(value) {
|
|
91
|
-
return map[value[key]].$serialize(value);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return $DiscriminatedUnion;
|
|
95
|
-
};
|
|
96
|
-
exports.DiscriminatedUnion = DiscriminatedUnion;
|
|
97
|
-
//# sourceMappingURL=discriminated-union.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"discriminated-union.js","sourceRoot":"","sources":["../src/discriminated-union.ts"],"names":[],"mappings":";;;AA6HA,kCAyBC;AAED,0CAyBC;AAjLD,0BASa;AAoHb,SAAgB,WAAW,CAAC,MAAuC;IACjE,MAAM,IAAI,GAAgC,EAAE,CAAC;IAE7C,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE,CAAC;QACvB,MAAM,KAAK,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACrC,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;YACxB,MAAM,CAAC,GAAG,GAAyB,CAAC;YACpC,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBACd,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YAC1B,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;SAC7B,GAAG,CAAC,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AAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;YACnC,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;YAC3D,CAAC;YACD,OAAQ,UAAkB,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QACjD,CAAC;QAED,MAAM,CAAC,UAAU,CAAC,KAAU;YAC1B,OAAO,GAAG,CAAE,KAAa,CAAC,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QACpD,CAAC;;IAGH,OAAO,mBAA0B,CAAC;AACpC,CAAC,CAAC;AA5DW,QAAA,kBAAkB,sBA4D7B","sourcesContent":["import {\n Constructor,\n Expand,\n DefinitionOf,\n AbstractConstructor,\n Empty,\n type Shorthand,\n Shape,\n type Definition,\n} from \"./_\";\nimport { ClassShorthand } from \"./class\";\nimport { type DictShorthand } from \"./dict\";\n\ntype UnionToIntersection<U> = (\n U extends unknown\n ? (k: U) => void\n : never\n) extends (k: infer I) => void\n ? I\n : never;\ntype PopUnion<U> = UnionToOvlds<U> extends (a: infer A) => void ? A : never;\n\ntype UnionToArray<T, A extends unknown[] = []> = IsUnion<T> extends true\n ? UnionToArray<Exclude<T, PopUnion<T>>, [PopUnion<T>, ...A]>\n : [T, ...A];\ntype IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;\n\ntype CountUnion<T> = UnionToArray<T> extends infer U extends any[]\n ? U[\"length\"]\n : never;\n\ntype UnshiftUnion<U> = UnionToArray<U> extends [infer F, ...any[]] ? F : never;\n\ntype UnionToOvlds<U> = UnionToIntersection<\n U extends any ? (f: U) => void : never\n>;\n\ntype IsStringLiteral<T> = T extends string\n ? string extends T\n ? false\n : true\n : false;\n\ntype FindBestKeyForMatching<FromUnion> = UnshiftUnion<\n keyof FromUnion extends infer K\n ? K extends keyof FromUnion\n ? CountUnion<Pick<FromUnion, K>[K]> extends CountUnion<FromUnion>\n ? [IsStringLiteral<FromUnion[K]>] extends [true]\n ? K\n : never\n : never\n : never\n : never\n>;\n\ntype MatcherConfig = { [key: string]: any };\n\ntype ExhaustiveMatcher<C> = C extends MatcherConfig\n ? {\n [key in keyof C]: (value: Expand<DefinitionOf<C[key]>[\"$inline\"]>) => any;\n }\n : never;\n\ntype UnsafeFallthroughMatcher<C> = C extends MatcherConfig\n ? {\n [key in keyof C]?: (\n value: Expand<DefinitionOf<C[key]>[\"$inline\"]>,\n ) => any;\n } & {\n _: (value: Expand<DefinitionOf<C[keyof C]>[\"$inline\"]>) => any;\n }\n : never;\n\ntype PartialMatcher<C> = C extends MatcherConfig\n ? {\n [key in keyof C]?: (\n value: Expand<DefinitionOf<C[key]>[\"$inline\"]>,\n ) => any;\n }\n : never;\n\ntype Matcher<C> = C extends MatcherConfig\n ? ExhaustiveMatcher<C> | UnsafeFallthroughMatcher<C> | PartialMatcher<C>\n : never;\n\ntype Config = DictShorthandInput | ClassInput | ClassDictInput | DictInput;\n\ntype ClassInput = ClassShorthand;\n\ntype ClassDictInput = ClassShorthand & { $of: {} };\n\ntype DictInput = { $of: {} };\n\ntype DictShorthandInput = DictShorthand;\n\ntype Access<T, K> = K extends keyof T ? T[K] : never;\n\ntype Entries<T extends readonly any[], K> = UnionToIntersection<\n {\n [i in keyof T]: Access<GetShape<T[i]>, K> extends infer U extends string\n ? IsStringLiteral<U> extends true\n ? {\n [key in U]: T[i] extends DictShorthandInput\n ? DefinitionOf<T[i]>\n : T[i];\n }\n : never\n : never;\n }[number]\n>;\n\ntype GetShape<S extends Config> = S extends DictInput\n ? S[\"$of\"]\n : S extends ClassInput\n ? Access<S, \"prototype\">\n : S;\n\nexport type BestKey<S extends readonly Config[]> = {\n [key in keyof S]: GetShape<S[key]>;\n}[number] extends infer U\n ? FindBestKeyForMatching<U>\n : never;\n\nexport type DiscriminatedUnionConfiguration = readonly Config[];\n\nexport function findBestKey(config: DiscriminatedUnionConfiguration) {\n const hash: Record<string, Set<string>> = {};\n\n for (const c of config) {\n const shape = \"$of\" in c ? c.$of : c;\n for (const key in shape) {\n const k = key as keyof typeof shape;\n if (hash[key]) {\n hash[key].add(shape[k]);\n } else {\n hash[key] = new Set([shape[k]]);\n }\n }\n }\n\n const key = Object.entries(hash)\n .map(([key, value]) => [key, value.size] as const)\n .filter(([_, value]) => value === config.length)\n .sort((a, b) => b[1] - a[1])?.[0]?.[0];\n\n if (!key) {\n throw new Error(\"Could not find key for DiscriminatedUnion\");\n }\n\n return key;\n}\n\nexport function prepareShapeMap(\n config: DiscriminatedUnionConfiguration,\n key: string,\n) {\n return config.reduce<{ [key: string]: Definition }>((acc, c) => {\n const shape = Shape(c);\n\n // Here, we are handling the two cases of the configuration:\n // - the first one is when the configuration is a Definition directly.\n // we can access the discriminator directly from the shape $of property\n // - the second one is when the configuration is a DictShorthand or a plain Class.\n // we first have to turn it into a Definition before accessing the discriminator\n // Dict $of property references the configuration provided, which allows us to capture the discriminator\n // Class $of property references the constructor of the class, which allows us to capture the discriminator\n // which is mandatorily defined on the static side of the class\n const discriminator =\n key in c\n ? (c as any)[key]\n : \"$of\" in c\n ? (c as any).$of[key]\n : (shape as any).$of[key];\n\n acc[discriminator] = shape;\n return acc;\n }, {});\n}\n\ntype Internal<\n S extends DiscriminatedUnionConfiguration,\n K extends BestKey<S>,\n> = {\n Map: Entries<S, K>;\n Serialized: ReturnType<DefinitionOf<S[number]>[\"$serialize\"]>;\n Inline: DefinitionOf<S[number]>[\"$inline\"];\n};\n\nexport const DiscriminatedUnion = <\n S extends DiscriminatedUnionConfiguration,\n K extends BestKey<S>,\n const B extends AbstractConstructor = typeof Empty,\n>(\n of: S,\n ...args: [base?: B]\n): IDiscriminatedUnion<S, K, B> => {\n const base = args[0] || (Empty as any);\n\n const key = findBestKey(of);\n const map = prepareShapeMap(of, key);\n\n abstract class $DiscriminatedUnion extends (base as any as Constructor<{}>) {\n constructor(public value: any) {\n super();\n }\n\n static $of = of;\n static $shape = \"discriminated-union\" as const;\n\n serialize() {\n return $DiscriminatedUnion.$serialize(this.value);\n }\n\n match(matcher: any, fallback: any) {\n const element: any = this.value;\n const discriminant = element[key];\n\n const handler = matcher[discriminant];\n if (handler) {\n return handler(element);\n }\n if (fallback) {\n return fallback(element);\n }\n if (matcher._) {\n return matcher._(element);\n }\n throw new Error(\"Non-exhaustive match\");\n }\n\n static deserialize(value: any) {\n return new (this as any)(this.$deserialize(value as any)) as any;\n }\n\n static $deserialize(value: any) {\n const definition = map[value[key]];\n if (!definition) {\n throw new Error(\"Cannot deserialize DiscriminatedUnion\");\n }\n return (definition as any).$deserialize(value);\n }\n\n static $serialize(value: any): any {\n return map[(value as any)[key]].$serialize(value);\n }\n }\n\n return $DiscriminatedUnion as any;\n};\n\nexport type IDiscriminatedUnion<\n S extends DiscriminatedUnionConfiguration,\n K extends BestKey<S>,\n B extends AbstractConstructor = typeof Empty,\n> = Omit<B, \"prototype\"> & {\n $shape: \"discriminated-union\";\n $of: S;\n serialized: Internal<S, K>[\"Serialized\"];\n deserialize<T>(\n this: T,\n value: Expand<Internal<S, K>[\"Serialized\"]>,\n ): T extends AbstractConstructor ? InstanceType<T> : any;\n $deserialize<T>(\n this: T,\n value: Internal<S, K>[\"Serialized\"],\n ): Internal<S, K>[\"Inline\"];\n $serialize<T extends AbstractConstructor>(\n this: T,\n value: InstanceType<T>,\n ): Internal<S, K>[\"Serialized\"];\n $inline: Internal<S, K>[\"Inline\"];\n} & (abstract new (\n value: Internal<S, K>[\"Inline\"],\n ) => InstanceType<B> & {\n value: Internal<S, K>[\"Inline\"];\n serialize(): Internal<S, K>[\"Serialized\"];\n match<\n M extends Matcher<Internal<S, K>[\"Map\"]>,\n F extends M extends ExhaustiveMatcher<Internal<S, K>[\"Map\"]>\n ? []\n : M extends UnsafeFallthroughMatcher<Internal<S, K>[\"Map\"]>\n ? []\n : M extends PartialMatcher<Internal<S, K>[\"Map\"]>\n ? [\n fallback: (\n value: Omit<Internal<S, K>[\"Map\"], keyof M>[keyof Omit<\n Internal<S, K>[\"Map\"],\n keyof M\n >] extends infer U extends Shorthand\n ? Expand<DefinitionOf<U>[\"$inline\"]>\n : never,\n ) => any,\n ]\n : [],\n >(\n matcher: M,\n ...fallback_n: F\n ):\n | (M[keyof M] extends (...args: any[]) => any\n ? ReturnType<M[keyof M]>\n : never)\n | (F[0] extends (...args: any[]) => any ? ReturnType<F[0]> : never);\n });\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"discriminated-union.spec.d.ts","sourceRoot":"","sources":["../src/discriminated-union.spec.ts"],"names":[],"mappings":""}
|