@ddd-ts/shape 0.0.36 → 0.0.38

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.
Files changed (94) hide show
  1. package/LICENSE +21 -0
  2. package/dist/_.d.ts +6 -6
  3. package/dist/_.d.ts.map +1 -1
  4. package/dist/_.js +34 -51
  5. package/dist/_.mjs +36 -0
  6. package/dist/addons/microsecond-timestamp.js +62 -75
  7. package/dist/addons/microsecond-timestamp.mjs +61 -0
  8. package/dist/choice.d.ts +1 -1
  9. package/dist/choice.d.ts.map +1 -1
  10. package/dist/choice.js +43 -48
  11. package/dist/choice.mjs +44 -0
  12. package/dist/class.d.ts +1 -1
  13. package/dist/class.d.ts.map +1 -1
  14. package/dist/class.js +29 -30
  15. package/dist/class.mjs +30 -0
  16. package/dist/dict.d.ts +1 -1
  17. package/dist/dict.d.ts.map +1 -1
  18. package/dist/dict.js +40 -46
  19. package/dist/dict.mjs +41 -0
  20. package/dist/discriminated-union.d.ts +2 -2
  21. package/dist/discriminated-union.d.ts.map +1 -1
  22. package/dist/discriminated-union.js +59 -90
  23. package/dist/discriminated-union.mjs +64 -0
  24. package/dist/either.d.ts +2 -2
  25. package/dist/either.d.ts.map +1 -1
  26. package/dist/either.js +50 -61
  27. package/dist/either.mjs +51 -0
  28. package/dist/index.js +32 -30
  29. package/dist/index.mjs +15 -0
  30. package/dist/literal.d.ts +1 -1
  31. package/dist/literal.d.ts.map +1 -1
  32. package/dist/literal.js +26 -26
  33. package/dist/literal.mjs +27 -0
  34. package/dist/mapping.d.ts +1 -1
  35. package/dist/mapping.d.ts.map +1 -1
  36. package/dist/mapping.js +44 -49
  37. package/dist/mapping.mjs +45 -0
  38. package/dist/multiple.d.ts +1 -1
  39. package/dist/multiple.d.ts.map +1 -1
  40. package/dist/multiple.js +118 -119
  41. package/dist/multiple.mjs +119 -0
  42. package/dist/nothing.d.ts +1 -1
  43. package/dist/nothing.d.ts.map +1 -1
  44. package/dist/nothing.js +18 -18
  45. package/dist/nothing.mjs +19 -0
  46. package/dist/optional.d.ts +1 -1
  47. package/dist/optional.d.ts.map +1 -1
  48. package/dist/optional.js +33 -40
  49. package/dist/optional.mjs +34 -0
  50. package/dist/primitive.d.ts +1 -1
  51. package/dist/primitive.d.ts.map +1 -1
  52. package/dist/primitive.js +29 -32
  53. package/dist/primitive.mjs +30 -0
  54. package/dist/test.d.ts +1 -1
  55. package/dist/test.d.ts.map +1 -1
  56. package/package.json +21 -10
  57. package/dist/_.js.map +0 -1
  58. package/dist/addons/microsecond-timestamp.js.map +0 -1
  59. package/dist/choice.js.map +0 -1
  60. package/dist/choice.spec.js +0 -147
  61. package/dist/choice.spec.js.map +0 -1
  62. package/dist/class.js.map +0 -1
  63. package/dist/class.spec.js +0 -39
  64. package/dist/class.spec.js.map +0 -1
  65. package/dist/dict.js.map +0 -1
  66. package/dist/dict.spec.js +0 -180
  67. package/dist/dict.spec.js.map +0 -1
  68. package/dist/discriminated-union.js.map +0 -1
  69. package/dist/discriminated-union.spec.js +0 -262
  70. package/dist/discriminated-union.spec.js.map +0 -1
  71. package/dist/either.js.map +0 -1
  72. package/dist/either.spec.js +0 -232
  73. package/dist/either.spec.js.map +0 -1
  74. package/dist/index.js.map +0 -1
  75. package/dist/literal.js.map +0 -1
  76. package/dist/literal.spec.js +0 -162
  77. package/dist/literal.spec.js.map +0 -1
  78. package/dist/mapping.js.map +0 -1
  79. package/dist/mapping.spec.js +0 -207
  80. package/dist/mapping.spec.js.map +0 -1
  81. package/dist/multiple.js.map +0 -1
  82. package/dist/multiple.spec.js +0 -207
  83. package/dist/multiple.spec.js.map +0 -1
  84. package/dist/nothing.js.map +0 -1
  85. package/dist/nothing.spec.js +0 -172
  86. package/dist/nothing.spec.js.map +0 -1
  87. package/dist/optional.js.map +0 -1
  88. package/dist/optional.spec.js +0 -172
  89. package/dist/optional.spec.js.map +0 -1
  90. package/dist/primitive.js.map +0 -1
  91. package/dist/primitive.spec.js +0 -162
  92. package/dist/primitive.spec.js.map +0 -1
  93. package/dist/test.js +0 -45
  94. package/dist/test.js.map +0 -1
@@ -1,147 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const choice_1 = require("./choice");
4
- const dict_1 = require("./dict");
5
- const test_1 = require("./test");
6
- describe("Choice", () => {
7
- it("class definition", () => {
8
- class Test extends (0, choice_1.Choice)(["a", "b", "c"]) {
9
- test = true;
10
- }
11
- // Constructor parameters
12
- (0, test_1.ex)(Test).toHaveFirstParam().ok;
13
- // Deserialization
14
- (0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
15
- const a = Test.deserialize("a");
16
- (0, test_1.ex)(a).toBeInstanceOf(Test).ok;
17
- // Additional prototype
18
- (0, test_1.ex)(a.test).toBe(true).ok;
19
- // Inherited prototype on deserialization
20
- (0, test_1.ex)(a.value).toBe("a").ok;
21
- (0, test_1.ex)(a.match({
22
- a: () => 1,
23
- b: () => 2,
24
- _: () => 3,
25
- })).toBe(1).ok;
26
- (0, test_1.ex)(a.is("a")).toBe(true).ok;
27
- // Serialization
28
- (0, test_1.ex)(a.serialize()).toBe("a").ok;
29
- // Instantiation
30
- const b = new Test("a");
31
- (0, test_1.ex)(b).toBeInstanceOf(Test).ok;
32
- // Inherited prototype on instantiation
33
- (0, test_1.ex)(b.value).toBe("a").ok;
34
- (0, test_1.ex)(a.match({
35
- a: () => 1,
36
- b: () => 2,
37
- _: () => 3,
38
- })).toBe(1).ok;
39
- (0, test_1.ex)(b.is("a")).toBe(true).ok;
40
- // Inherited statics
41
- (0, test_1.ex)(Test.a()).toBeInstanceOf(Test).ok;
42
- (0, test_1.ex)(Test.b()).toBeInstanceOf(Test).ok;
43
- (0, test_1.ex)(Test.c()).toBeInstanceOf(Test).ok;
44
- });
45
- it("inlined definition", () => {
46
- class Test extends (0, dict_1.Dict)({ nested: (0, choice_1.Choice)(["a", "b", "c"]) }) {
47
- }
48
- // Constructor parameters
49
- (0, test_1.ex)(Test).toHaveFirstParam().ok;
50
- // Deserialization
51
- (0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
52
- const a = Test.deserialize({ nested: "b" });
53
- (0, test_1.ex)(a).toBeInstanceOf(Test).ok;
54
- // Inherited prototype on deserialization
55
- (0, test_1.ex)(a.nested).toBe("b").ok;
56
- // Serialization
57
- (0, test_1.ex)(a.serialize()).toStrictEqual({
58
- nested: "b",
59
- }).ok;
60
- // Instantiation
61
- const e = new Test({ nested: "b" });
62
- (0, test_1.ex)(e).toBeInstanceOf(Test).ok;
63
- });
64
- it("referenced definition", () => {
65
- class Reference extends (0, choice_1.Choice)(["a", "b", "c"]) {
66
- }
67
- class Test extends (0, dict_1.Dict)({ nested: Reference }) {
68
- }
69
- // Constructor parameters
70
- (0, test_1.ex)(Test).toHaveFirstParam().ok;
71
- // Deserialization
72
- (0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
73
- const a = Test.deserialize({ nested: "a" });
74
- (0, test_1.ex)(a).toBeInstanceOf(Test);
75
- // Preserve reference
76
- (0, test_1.ex)(a.nested).toBeInstanceOf(Reference);
77
- (0, test_1.ex)(a.nested.is("a")).toBe(true);
78
- // Serialization
79
- (0, test_1.ex)(a.serialize()).toStrictEqual({ nested: "a" });
80
- // Instantiation
81
- const b = new Test({ nested: new Reference("c") });
82
- expect(b).toBeInstanceOf(Test);
83
- });
84
- it("mixin extension are not supported", () => {
85
- const Mixin = (choices) => {
86
- // @ts-expect-error https://github.com/microsoft/TypeScript/issues/57674
87
- class Mix extends (0, choice_1.Choice)(choices) {
88
- static d;
89
- }
90
- return Mix;
91
- };
92
- expect(true).toBe(true);
93
- });
94
- it("mixin supersede", () => {
95
- const Testable = (choices) => {
96
- class I {
97
- deep = true;
98
- static deep = true;
99
- }
100
- return (0, choice_1.Choice)(choices, I);
101
- };
102
- class Test extends Testable(["a", "b", "c"]) {
103
- test = true;
104
- // @ts-expect-error is not assignable to parameter of type 'true'
105
- abstract = false;
106
- }
107
- // Constructor parameters
108
- (0, test_1.ex)(Test).toHaveFirstParam().ok;
109
- // Deserialization
110
- (0, test_1.ex)(Test.deserialize).toHaveFirstParam().ok;
111
- const a = Test.deserialize("a");
112
- (0, test_1.ex)(a).toBeInstanceOf(Test).ok;
113
- // Additional prototype
114
- (0, test_1.ex)(a.test).toBe(true).ok;
115
- // Inherited prototype on deserialization
116
- (0, test_1.ex)(a.value).toBe("a").ok;
117
- (0, test_1.ex)(a.match({
118
- a: () => 1,
119
- b: () => 2,
120
- _: () => 3,
121
- })).toBe(1).ok;
122
- (0, test_1.ex)(a.is("a")).toBe(true).ok;
123
- // Serialization
124
- (0, test_1.ex)(a.serialize()).toBe("a").ok;
125
- // Instantiation
126
- const b = new Test("a");
127
- (0, test_1.ex)(b).toBeInstanceOf(Test).ok;
128
- // Inherited prototype on instantiation
129
- (0, test_1.ex)(b.value).toBe("a").ok;
130
- (0, test_1.ex)(a.match({
131
- a: () => 1,
132
- b: () => 2,
133
- _: () => 3,
134
- })).toBe(1).ok;
135
- (0, test_1.ex)(b.is("a")).toBe(true).ok;
136
- // Inherited statics
137
- (0, test_1.ex)(Test.a()).toBeInstanceOf(Test).ok;
138
- (0, test_1.ex)(Test.b()).toBeInstanceOf(Test).ok;
139
- (0, test_1.ex)(Test.c()).toBeInstanceOf(Test).ok;
140
- // Additional inherited static prototype
141
- (0, test_1.ex)(Test.deep).toBe(true).ok;
142
- // Additional inherited prototype
143
- (0, test_1.ex)(b.abstract).toBe(false).ok;
144
- (0, test_1.ex)(b.deep).toBe(true).ok;
145
- });
146
- });
147
- //# sourceMappingURL=choice.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"choice.spec.js","sourceRoot":"","sources":["../src/choice.spec.ts"],"names":[],"mappings":";;AAAA,qCAAkC;AAClC,iCAA8B;AAC9B,iCAA4B;AAE5B,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,EAAE,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAC1B,MAAM,IAAK,SAAQ,I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C","sourcesContent":["import { Choice } from \"./choice\";\nimport { Dict } from \"./dict\";\nimport { ex } from \"./test\";\n\ndescribe(\"Choice\", () => {\n it(\"class definition\", () => {\n class Test extends Choice([\"a\", \"b\", \"c\"]) {\n test = true as const;\n }\n\n type Serialized = \"a\" | \"b\" | \"c\";\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\");\n ex(a).toBeInstanceOf(Test).ok;\n\n // Additional prototype\n ex(a.test).toBe(true as const).ok;\n\n // Inherited prototype on deserialization\n ex(a.value).toBe<\"a\" | \"b\" | \"c\">(\"a\").ok;\n ex(\n a.match({\n a: () => 1 as const,\n b: () => 2 as const,\n _: () => 3 as const,\n }),\n ).toBe<1 | 2 | 3>(1).ok;\n ex(a.is(\"a\")).toBe(true).ok;\n\n // Serialization\n ex(a.serialize()).toBe<\"a\" | \"b\" | \"c\">(\"a\").ok;\n\n // Instantiation\n const b = new Test(\"a\");\n ex(b).toBeInstanceOf(Test).ok;\n\n // Inherited prototype on instantiation\n ex(b.value).toBe<\"a\" | \"b\" | \"c\">(\"a\").ok;\n ex(\n a.match({\n a: () => 1 as const,\n b: () => 2 as const,\n _: () => 3 as const,\n }),\n ).toBe<1 | 2 | 3>(1).ok;\n ex(b.is(\"a\")).toBe(true).ok;\n\n // Inherited statics\n ex(Test.a()).toBeInstanceOf(Test).ok;\n ex(Test.b()).toBeInstanceOf(Test).ok;\n ex(Test.c()).toBeInstanceOf(Test).ok;\n });\n\n it(\"inlined definition\", () => {\n class Test extends Dict({ nested: Choice([\"a\", \"b\", \"c\"]) }) {}\n\n type Serialized = { nested: \"a\" | \"b\" | \"c\" };\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({ nested: \"b\" });\n ex(a).toBeInstanceOf(Test).ok;\n\n // Inherited prototype on deserialization\n ex(a.nested).toBe<\"a\" | \"b\" | \"c\">(\"b\").ok;\n\n // Serialization\n ex(a.serialize()).toStrictEqual<{ nested: \"a\" | \"b\" | \"c\" }>({\n nested: \"b\",\n }).ok;\n\n // Instantiation\n const e = new Test({ nested: \"b\" });\n ex(e).toBeInstanceOf(Test).ok;\n });\n\n it(\"referenced definition\", () => {\n class Reference extends Choice([\"a\", \"b\", \"c\"]) {}\n class Test extends Dict({ nested: Reference }) {}\n\n type Serialized = { nested: \"a\" | \"b\" | \"c\" };\n\n // Constructor parameters\n ex(Test).toHaveFirstParam<{ nested: Reference }>().ok;\n\n // Deserialization\n ex(Test.deserialize).toHaveFirstParam<Serialized>().ok;\n const a = Test.deserialize({ nested: \"a\" });\n ex(a).toBeInstanceOf(Test);\n\n // Preserve reference\n ex(a.nested).toBeInstanceOf(Reference);\n ex(a.nested.is(\"a\")).toBe(true);\n\n // Serialization\n ex(a.serialize()).toStrictEqual<Serialized>({ nested: \"a\" });\n\n // Instantiation\n const b = new Test({ nested: new Reference(\"c\") });\n expect(b).toBeInstanceOf(Test);\n });\n\n it(\"mixin extension are not supported\", () => {\n const Mixin = <const S extends string[]>(choices: S) => {\n // @ts-expect-error https://github.com/microsoft/TypeScript/issues/57674\n abstract class Mix extends Choice(choices) {\n static d: \"d\";\n }\n\n return Mix as typeof Mix & { [K in S[number]]: K };\n };\n\n expect(true).toBe(true);\n });\n\n it(\"mixin supersede\", () => {\n const Testable = <const S extends string[]>(choices: 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 Choice(choices, I);\n };\n\n class Test extends Testable([\"a\", \"b\", \"c\"]) {\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 type Serialized = \"a\" | \"b\" | \"c\";\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\");\n ex(a).toBeInstanceOf(Test).ok;\n\n // Additional prototype\n ex(a.test).toBe(true as const).ok;\n\n // Inherited prototype on deserialization\n ex(a.value).toBe<Serialized>(\"a\").ok;\n ex(\n a.match({\n a: () => 1 as const,\n b: () => 2 as const,\n _: () => 3 as const,\n }),\n ).toBe<1 | 2 | 3>(1).ok;\n ex(a.is(\"a\")).toBe(true).ok;\n\n // Serialization\n ex(a.serialize()).toBe<Serialized>(\"a\").ok;\n\n // Instantiation\n const b = new Test(\"a\");\n ex(b).toBeInstanceOf(Test).ok;\n\n // Inherited prototype on instantiation\n ex(b.value).toBe<Serialized>(\"a\").ok;\n ex(\n a.match({\n a: () => 1 as const,\n b: () => 2 as const,\n _: () => 3 as const,\n }),\n ).toBe<1 | 2 | 3>(1).ok;\n ex(b.is(\"a\")).toBe(true).ok;\n\n // Inherited statics\n ex(Test.a()).toBeInstanceOf(Test).ok;\n ex(Test.b()).toBeInstanceOf(Test).ok;\n ex(Test.c()).toBeInstanceOf(Test).ok;\n\n // Additional inherited static prototype\n ex(Test.deep).toBe(true as const).ok;\n\n // Additional inherited prototype\n ex(b.abstract).toBe(false as const).ok;\n ex(b.deep).toBe(true as const).ok;\n });\n});\n"]}
package/dist/class.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"class.js","sourceRoot":"","sources":["../src/class.ts"],"names":[],"mappings":";;;AAAA,0BAAsE;AAM/D,MAAM,KAAK,GAAG,CAMnB,EAAK,EACL,IAAI,GAAG,QAAK,EACE,EAAE;IAIhB,MAAe,MAAO,SAAQ,IAAI;QAIb;QAHnB,MAAM,CAAC,MAAM,GAAG,OAAgB,CAAC;QACjC,MAAM,CAAC,GAAG,GAAG,EAAE,CAAC;QAEhB,YAAmB,KAAsB;YACvC,KAAK,EAAE,CAAC;YADS,UAAK,GAAL,KAAK,CAAiB;QAEzC,CAAC;QAED,MAAM,CAAC,WAAW,CAEhB,KAA8C;YAE9C,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,CAAoB,CAAC;QAC5D,CAAC;QAED,SAAS;YACP,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;QAChC,CAAC;QAED,MAAM,CAAC,YAAY,CAAC,KAAsC;YACxD,OAAO,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC;QAED,MAAM,CAAC,UAAU,CAEf,KAAa;YAEb,OAAO,KAAK,CAAC,SAAS,EAAE,CAAC;QAC3B,CAAC;QAED,MAAM,CAAC,OAAO,CAAS;;IAGzB,OAAO,MAAa,CAAC;AACvB,CAAC,CAAC;AA9CW,QAAA,KAAK,SA8ChB","sourcesContent":["import { AbstractConstructor, Empty, Constructor, Expand } from \"./_\";\n\nexport type ClassShorthand = Constructor<{ serialize(): any }> & {\n deserialize(value: any): any;\n};\n\nexport const Class = <\n S extends Constructor<{ serialize(): any }> & {\n deserialize(value: any): any;\n },\n B extends AbstractConstructor<any> = typeof Empty,\n>(\n of: S,\n base = Empty,\n): IClass<S, B> => {\n type Serialized = ReturnType<InstanceType<S>[\"serialize\"]>;\n type Inline = InstanceType<S>;\n\n abstract class $Class extends base {\n static $shape = \"class\" as const;\n static $of = of;\n\n constructor(public value: InstanceType<B>) {\n super();\n }\n\n static deserialize<T extends Constructor<any>>(\n this: T,\n value: Expand<Parameters<S[\"deserialize\"]>[0]>,\n ): InstanceType<T> {\n return new this(of.deserialize(value)) as InstanceType<T>;\n }\n\n serialize<T extends $Class>(this: T): Expand<Serialized> {\n return this.value.serialize();\n }\n\n static $deserialize(value: Parameters<S[\"deserialize\"]>[0]): Inline {\n return of.deserialize(value);\n }\n\n static $serialize<T extends typeof $Class>(\n this: T,\n value: Inline,\n ): Serialized {\n return value.serialize();\n }\n\n static $inline: Inline;\n }\n\n return $Class as any;\n};\n\nexport type IClass<\n S extends ClassShorthand,\n B extends AbstractConstructor<any> = typeof Empty,\n> = (abstract new (\n value: InstanceType<B>,\n) => {\n value: InstanceType<B>;\n serialize<T>(this: T): Expand<ReturnType<InstanceType<S>[\"serialize\"]>>;\n}) & {\n $shape: \"class\";\n $of: S;\n deserialize<T extends Constructor<any>>(\n this: T,\n value: Expand<Parameters<S[\"deserialize\"]>[0]>,\n ): InstanceType<T>;\n $deserialize(value: Parameters<S[\"deserialize\"]>[0]): InstanceType<S>;\n $serialize<T>(\n this: T,\n value: InstanceType<S>,\n ): ReturnType<InstanceType<S>[\"serialize\"]>;\n $inline: InstanceType<S>;\n};\n"]}
@@ -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
@@ -1 +0,0 @@
1
- {"version":3,"file":"class.spec.js","sourceRoot":"","sources":["../src/class.spec.ts"],"names":[],"mappings":";;AAAA,mCAAgC;AAChC,iCAA8B;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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.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,CAA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AG,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.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
@@ -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,IAAI,CAAC,GAA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} 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 +0,0 @@
1
- {"version":3,"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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"]}