@ddd-ts/shape 0.0.0-compute-timeout-on-process.7 → 0.0.0-compute-timeout-on-process.8

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 (55) hide show
  1. package/dist/_.js +16 -15
  2. package/dist/_.mjs +37 -0
  3. package/dist/_.mjs.map +1 -0
  4. package/dist/addons/microsecond-timestamp.js +2 -2
  5. package/dist/addons/microsecond-timestamp.mjs +62 -0
  6. package/dist/addons/microsecond-timestamp.mjs.map +1 -0
  7. package/dist/choice.js +3 -4
  8. package/dist/choice.mjs +45 -0
  9. package/dist/choice.mjs.map +1 -0
  10. package/dist/class.js +3 -4
  11. package/dist/class.mjs +31 -0
  12. package/dist/class.mjs.map +1 -0
  13. package/dist/dict.js +5 -6
  14. package/dist/dict.mjs +42 -0
  15. package/dist/dict.mjs.map +1 -0
  16. package/dist/discriminated-union.js +6 -5
  17. package/dist/discriminated-union.mjs +65 -0
  18. package/dist/discriminated-union.mjs.map +1 -0
  19. package/dist/either.js +4 -5
  20. package/dist/either.mjs +52 -0
  21. package/dist/either.mjs.map +1 -0
  22. package/dist/index.js +31 -14
  23. package/dist/index.mjs +15 -0
  24. package/dist/literal.js +3 -4
  25. package/dist/literal.mjs +28 -0
  26. package/dist/literal.mjs.map +1 -0
  27. package/dist/mapping.js +7 -8
  28. package/dist/mapping.mjs +46 -0
  29. package/dist/mapping.mjs.map +1 -0
  30. package/dist/multiple.js +4 -5
  31. package/dist/multiple.mjs +120 -0
  32. package/dist/multiple.mjs.map +1 -0
  33. package/dist/nothing.js +3 -4
  34. package/dist/nothing.mjs +20 -0
  35. package/dist/nothing.mjs.map +1 -0
  36. package/dist/optional.js +5 -6
  37. package/dist/optional.mjs +35 -0
  38. package/dist/optional.mjs.map +1 -0
  39. package/dist/primitive.js +3 -4
  40. package/dist/primitive.mjs +31 -0
  41. package/dist/primitive.mjs.map +1 -0
  42. package/package.json +10 -5
  43. package/dist/_.js.map +0 -1
  44. package/dist/addons/microsecond-timestamp.js.map +0 -1
  45. package/dist/choice.js.map +0 -1
  46. package/dist/class.js.map +0 -1
  47. package/dist/dict.js.map +0 -1
  48. package/dist/discriminated-union.js.map +0 -1
  49. package/dist/either.js.map +0 -1
  50. package/dist/literal.js.map +0 -1
  51. package/dist/mapping.js.map +0 -1
  52. package/dist/multiple.js.map +0 -1
  53. package/dist/nothing.js.map +0 -1
  54. package/dist/optional.js.map +0 -1
  55. package/dist/primitive.js.map +0 -1
package/dist/_.js CHANGED
@@ -1,28 +1,28 @@
1
- import { Class } from "./class.js";
2
- import { Dict } from "./dict.js";
3
- import { Primitive } from "./primitive.js";
4
- import { Multiple } from "./multiple.js";
5
- import { Nothing } from "./nothing.js";
6
- import { Literal } from "./literal.js";
1
+ const require_class = require('./class.js');
2
+ const require_dict = require('./dict.js');
3
+ const require_primitive = require('./primitive.js');
4
+ const require_multiple = require('./multiple.js');
5
+ const require_nothing = require('./nothing.js');
6
+ const require_literal = require('./literal.js');
7
7
 
8
8
  //#region src/_.ts
9
9
  var Empty = class {};
10
10
  function Shape(shorthand, base = Empty) {
11
- if (typeof shorthand === "string" || typeof shorthand === "number") return Literal(shorthand, base);
11
+ if (typeof shorthand === "string" || typeof shorthand === "number") return require_literal.Literal(shorthand, base);
12
12
  if (shorthand && "$shape" in shorthand && "name" in shorthand && typeof shorthand.name === "string") {
13
13
  if (shorthand.name.startsWith("$")) return shorthand;
14
- return Class(shorthand, base);
14
+ return require_class.Class(shorthand, base);
15
15
  }
16
- if (shorthand === void 0) return Nothing(void 0, base);
16
+ if (shorthand === void 0) return require_nothing.Nothing(void 0, base);
17
17
  if ([
18
18
  String,
19
19
  Number,
20
20
  Date,
21
21
  Boolean
22
- ].includes(shorthand)) return Primitive(shorthand, base);
23
- if (shorthand && "prototype" in shorthand) return Class(shorthand, base);
24
- if (Array.isArray(shorthand)) return Multiple(shorthand[0], base);
25
- if (typeof shorthand === "object") return Dict(shorthand, base);
22
+ ].includes(shorthand)) return require_primitive.Primitive(shorthand, base);
23
+ if (shorthand && "prototype" in shorthand) return require_class.Class(shorthand, base);
24
+ if (Array.isArray(shorthand)) return require_multiple.Multiple(shorthand[0], base);
25
+ if (typeof shorthand === "object") return require_dict.Dict(shorthand, base);
26
26
  throw new Error(`Could not determine longhand from shorthand ${JSON.stringify(shorthand)}`);
27
27
  }
28
28
  function forward(base, forward) {
@@ -33,5 +33,6 @@ function forward(base, forward) {
33
33
  }
34
34
 
35
35
  //#endregion
36
- export { Empty, Shape, forward };
37
- //# sourceMappingURL=_.js.map
36
+ exports.Empty = Empty;
37
+ exports.Shape = Shape;
38
+ exports.forward = forward;
package/dist/_.mjs ADDED
@@ -0,0 +1,37 @@
1
+ import { Class } from "./class.mjs";
2
+ import { Dict } from "./dict.mjs";
3
+ import { Primitive } from "./primitive.mjs";
4
+ import { Multiple } from "./multiple.mjs";
5
+ import { Nothing } from "./nothing.mjs";
6
+ import { Literal } from "./literal.mjs";
7
+
8
+ //#region src/_.ts
9
+ var Empty = class {};
10
+ function Shape(shorthand, base = Empty) {
11
+ if (typeof shorthand === "string" || typeof shorthand === "number") return Literal(shorthand, base);
12
+ if (shorthand && "$shape" in shorthand && "name" in shorthand && typeof shorthand.name === "string") {
13
+ if (shorthand.name.startsWith("$")) return shorthand;
14
+ return Class(shorthand, base);
15
+ }
16
+ if (shorthand === void 0) return Nothing(void 0, base);
17
+ if ([
18
+ String,
19
+ Number,
20
+ Date,
21
+ Boolean
22
+ ].includes(shorthand)) return Primitive(shorthand, base);
23
+ if (shorthand && "prototype" in shorthand) return Class(shorthand, base);
24
+ if (Array.isArray(shorthand)) return Multiple(shorthand[0], base);
25
+ if (typeof shorthand === "object") return Dict(shorthand, base);
26
+ throw new Error(`Could not determine longhand from shorthand ${JSON.stringify(shorthand)}`);
27
+ }
28
+ function forward(base, forward) {
29
+ for (const key of forward) Object.defineProperty(base.prototype, key, { get() {
30
+ return this.value[key].bind(this.value);
31
+ } });
32
+ return base;
33
+ }
34
+
35
+ //#endregion
36
+ export { Empty, Shape, forward };
37
+ //# sourceMappingURL=_.mjs.map
package/dist/_.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_.mjs","names":[],"sources":["../src/_.ts"],"sourcesContent":["import { Class, IClass, ClassShorthand } from \"./class\";\nimport { Dict, IDict, DictShorthand } from \"./dict\";\nimport { Primitive, PrimitiveShorthand } from \"./primitive\";\nimport { Multiple, IMultiple, MultipleShorthand } from \"./multiple\";\nimport { Nothing, NothingShorthand } from \"./nothing\";\nimport { Literal, ILiteral, LiteralShorthand } from \"./literal\";\n\nexport abstract class Empty {}\n\nexport interface Constructor<T = any, P extends any[] = any[]> {\n new (...args: P): T;\n}\nexport type AbstractConstructor<\n T = any,\n P extends any[] = any[],\n> = abstract new (...args: P) => T;\n\nexport interface Definition {\n $shape: string;\n $inline: any;\n $serialize(value: any): any;\n $deserialize(value: any): any;\n}\n\nexport type MakeAbstract<T> = T extends new (\n ...params: infer P\n) => infer R\n ? Omit<T, \"\"> & (abstract new (...params: P) => R)\n : never;\n\nexport type Concrete<T extends AbstractConstructor<any>> =\n T extends abstract new (\n ...params: infer P\n ) => infer R\n ? Omit<T, \"\"> & {\n new (...params: P): R;\n }\n : never;\n\nexport type Shorthand =\n | Definition\n | DictShorthand\n | PrimitiveShorthand\n | MultipleShorthand\n | NothingShorthand\n | ClassShorthand\n | LiteralShorthand;\n\nexport type DefinitionOf<\n T extends Shorthand | Definition,\n B extends AbstractConstructor<{}> = typeof Empty,\n> = T extends LiteralShorthand\n ? ILiteral<T, B>\n : T extends undefined\n ? ReturnType<typeof Nothing<B>>\n : T extends PrimitiveShorthand\n ? ReturnType<typeof Primitive<T, B>>\n : T extends MultipleShorthand\n ? IMultiple<T[0], B>\n : T extends ClassShorthand\n ? IClass<T, B>\n : T extends Definition\n ? T\n : T extends DictShorthand\n ? IDict<T, B>\n : never;\n\nexport function Shape<\n const S extends Definition | Shorthand,\n B extends AbstractConstructor<{}> = typeof Empty,\n>(shorthand: S, base: B = Empty as any): DefinitionOf<S, B> {\n if (typeof shorthand === \"string\" || typeof shorthand === \"number\") {\n return Literal(shorthand, base) as any;\n }\n\n if (\n shorthand &&\n \"$shape\" in shorthand &&\n \"name\" in shorthand &&\n typeof shorthand.name === \"string\"\n ) {\n if ((shorthand as any).name.startsWith(\"$\")) {\n return shorthand as any;\n }\n return Class(shorthand as any, base) as any;\n }\n if (shorthand === undefined) {\n return Nothing(undefined, base) as any;\n }\n if ([String, Number, Date, Boolean].includes(shorthand as any)) {\n return Primitive(shorthand as any, base) as any;\n }\n if (shorthand && \"prototype\" in shorthand) {\n return Class(shorthand as any, base) as any;\n }\n\n if (Array.isArray(shorthand)) {\n return (Multiple as any)(shorthand[0] as any, base);\n }\n\n if (typeof shorthand === \"object\") {\n return Dict(shorthand as any, base) as any;\n }\n\n throw new Error(\n `Could not determine longhand from shorthand ${JSON.stringify(shorthand)}`,\n );\n}\n\nexport type Expand<T> = T extends { serialize(): any }\n ? T\n : T extends Date\n ? T\n : T extends Record<string, any>\n ? { [key in keyof T]: Expand<T[key]> }\n : T;\n\nexport function forward<\n T extends AbstractConstructor<{ value: any }>,\n const Forward extends T extends AbstractConstructor<{ value: infer U }>\n ? (keyof U)[]\n : never,\n>(\n base: T,\n forward: Forward,\n): MakeAbstract<\n T &\n (new (\n ...args: ConstructorParameters<T>\n ) => InstanceType<T> & {\n [K in Forward[number]]: InstanceType<T>[\"value\"][K];\n })\n> {\n for (const key of forward) {\n Object.defineProperty(base.prototype, key, {\n get() {\n return (this.value as any)[key].bind(this.value);\n },\n });\n }\n return base as any;\n}\n\nexport type MergeClasses<\n B extends AbstractConstructor,\n Current extends AbstractConstructor,\n> = abstract new (\n ...args: ConstructorParameters<Current>\n) => InstanceType<B> & InstanceType<Current>;\n"],"mappings":";;;;;;;;AAOA,IAAsB,QAAtB,MAA4B;AA4D5B,SAAgB,MAGd,WAAc,OAAU,OAAkC;AAC1D,KAAI,OAAO,cAAc,YAAY,OAAO,cAAc,SACxD,QAAO,QAAQ,WAAW,KAAK;AAGjC,KACE,aACA,YAAY,aACZ,UAAU,aACV,OAAO,UAAU,SAAS,UAC1B;AACA,MAAK,UAAkB,KAAK,WAAW,IAAI,CACzC,QAAO;AAET,SAAO,MAAM,WAAkB,KAAK;;AAEtC,KAAI,cAAc,OAChB,QAAO,QAAQ,QAAW,KAAK;AAEjC,KAAI;EAAC;EAAQ;EAAQ;EAAM;EAAQ,CAAC,SAAS,UAAiB,CAC5D,QAAO,UAAU,WAAkB,KAAK;AAE1C,KAAI,aAAa,eAAe,UAC9B,QAAO,MAAM,WAAkB,KAAK;AAGtC,KAAI,MAAM,QAAQ,UAAU,CAC1B,QAAQ,SAAiB,UAAU,IAAW,KAAK;AAGrD,KAAI,OAAO,cAAc,SACvB,QAAO,KAAK,WAAkB,KAAK;AAGrC,OAAM,IAAI,MACR,+CAA+C,KAAK,UAAU,UAAU,GACzE;;AAWH,SAAgB,QAMd,MACA,SAQA;AACA,MAAK,MAAM,OAAO,QAChB,QAAO,eAAe,KAAK,WAAW,KAAK,EACzC,MAAM;AACJ,SAAQ,KAAK,MAAc,KAAK,KAAK,KAAK,MAAM;IAEnD,CAAC;AAEJ,QAAO"}
@@ -1,3 +1,4 @@
1
+
1
2
  //#region src/addons/microsecond-timestamp.ts
2
3
  var MicrosecondTimestamp = class MicrosecondTimestamp {
3
4
  static MILLISECOND = new MicrosecondTimestamp(BigInt(1e3));
@@ -58,5 +59,4 @@ var MicrosecondTimestamp = class MicrosecondTimestamp {
58
59
  };
59
60
 
60
61
  //#endregion
61
- export { MicrosecondTimestamp };
62
- //# sourceMappingURL=microsecond-timestamp.js.map
62
+ exports.MicrosecondTimestamp = MicrosecondTimestamp;
@@ -0,0 +1,62 @@
1
+ //#region src/addons/microsecond-timestamp.ts
2
+ var MicrosecondTimestamp = class MicrosecondTimestamp {
3
+ static MILLISECOND = new MicrosecondTimestamp(BigInt(1e3));
4
+ static SECOND = this.MILLISECOND.mult(1e3);
5
+ static MINUTE = this.SECOND.mult(60);
6
+ static HOUR = this.MINUTE.mult(60);
7
+ static DAY = this.HOUR.mult(24);
8
+ static WEEK = this.DAY.mult(7);
9
+ static MONTH = this.DAY.mult(30);
10
+ constructor(micros) {
11
+ this.micros = micros;
12
+ }
13
+ isAfter(other) {
14
+ return this.micros > other.micros;
15
+ }
16
+ equals(other) {
17
+ return this.micros === other.micros;
18
+ }
19
+ isBefore(other) {
20
+ return this.micros < other.micros;
21
+ }
22
+ add(micros) {
23
+ const value = micros instanceof MicrosecondTimestamp ? micros.micros : micros;
24
+ return new MicrosecondTimestamp(this.micros + value);
25
+ }
26
+ sub(micros) {
27
+ const value = micros instanceof MicrosecondTimestamp ? micros.micros : micros;
28
+ return new MicrosecondTimestamp(this.micros - value);
29
+ }
30
+ mult(factor) {
31
+ return new MicrosecondTimestamp(this.micros * BigInt(factor));
32
+ }
33
+ static now() {
34
+ return new MicrosecondTimestamp(BigInt(Date.now()) * BigInt(1e3));
35
+ }
36
+ static fromNanoseconds(nanoseconds) {
37
+ return new MicrosecondTimestamp(nanoseconds / BigInt(1e3));
38
+ }
39
+ static fromMicroseconds(microseconds) {
40
+ return new MicrosecondTimestamp(microseconds);
41
+ }
42
+ static deserialize(serialized) {
43
+ if (serialized instanceof MicrosecondTimestamp) return serialized;
44
+ if (typeof serialized === "bigint") return new MicrosecondTimestamp(serialized);
45
+ if ("microseconds" in serialized && typeof serialized.microseconds === "bigint") return new MicrosecondTimestamp(serialized.microseconds);
46
+ return new MicrosecondTimestamp(BigInt(serialized.getTime()) * BigInt(1e3));
47
+ }
48
+ serialize() {
49
+ const date = new Date(Number(this.micros / BigInt(1e3)));
50
+ date.microseconds = this.micros;
51
+ return date;
52
+ }
53
+ static sort(left, right) {
54
+ if (left.isAfter(right)) return 1;
55
+ if (left.isBefore(right)) return -1;
56
+ return 0;
57
+ }
58
+ };
59
+
60
+ //#endregion
61
+ export { MicrosecondTimestamp };
62
+ //# sourceMappingURL=microsecond-timestamp.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"microsecond-timestamp.mjs","names":[],"sources":["../../src/addons/microsecond-timestamp.ts"],"sourcesContent":["export class MicrosecondTimestamp {\n static MILLISECOND = new MicrosecondTimestamp(BigInt(1_000));\n static SECOND = this.MILLISECOND.mult(1000);\n static MINUTE = this.SECOND.mult(60);\n static HOUR = this.MINUTE.mult(60);\n static DAY = this.HOUR.mult(24);\n static WEEK = this.DAY.mult(7);\n static MONTH = this.DAY.mult(30);\n\n constructor(readonly micros: bigint) {}\n\n isAfter(other: MicrosecondTimestamp): boolean {\n return this.micros > other.micros;\n }\n\n equals(other: MicrosecondTimestamp): boolean {\n return this.micros === other.micros;\n }\n\n isBefore(other: MicrosecondTimestamp): boolean {\n return this.micros < other.micros;\n }\n\n add(micros: bigint | MicrosecondTimestamp): MicrosecondTimestamp {\n const value =\n micros instanceof MicrosecondTimestamp ? micros.micros : micros;\n return new MicrosecondTimestamp(this.micros + value);\n }\n\n sub(micros: bigint | MicrosecondTimestamp): MicrosecondTimestamp {\n const value =\n micros instanceof MicrosecondTimestamp ? micros.micros : micros;\n return new MicrosecondTimestamp(this.micros - value);\n }\n\n mult(factor: number): MicrosecondTimestamp {\n return new MicrosecondTimestamp(this.micros * BigInt(factor));\n }\n\n static now(): MicrosecondTimestamp {\n return new MicrosecondTimestamp(BigInt(Date.now()) * BigInt(1000));\n }\n\n static fromNanoseconds(nanoseconds: bigint): MicrosecondTimestamp {\n return new MicrosecondTimestamp(nanoseconds / BigInt(1000));\n }\n\n static fromMicroseconds(microseconds: bigint): MicrosecondTimestamp {\n return new MicrosecondTimestamp(microseconds);\n }\n\n static deserialize(\n serialized: Date | MicrosecondTimestamp | bigint,\n ): MicrosecondTimestamp {\n if (serialized instanceof MicrosecondTimestamp) {\n return serialized;\n }\n if (typeof serialized === \"bigint\") {\n return new MicrosecondTimestamp(serialized);\n }\n\n if (\n \"microseconds\" in serialized &&\n typeof serialized.microseconds === \"bigint\"\n ) {\n return new MicrosecondTimestamp(serialized.microseconds);\n }\n\n const micros = BigInt(serialized.getTime()) * BigInt(1000);\n return new MicrosecondTimestamp(micros);\n }\n\n serialize() {\n const date = new Date(Number(this.micros / BigInt(1000)));\n (date as any).microseconds = this.micros;\n return date;\n }\n\n static sort(left: MicrosecondTimestamp, right: MicrosecondTimestamp): number {\n if (left.isAfter(right)) {\n return 1;\n }\n if (left.isBefore(right)) {\n return -1;\n }\n return 0;\n }\n}\n"],"mappings":";AAAA,IAAa,uBAAb,MAAa,qBAAqB;CAChC,OAAO,cAAc,IAAI,qBAAqB,OAAO,IAAM,CAAC;CAC5D,OAAO,SAAS,KAAK,YAAY,KAAK,IAAK;CAC3C,OAAO,SAAS,KAAK,OAAO,KAAK,GAAG;CACpC,OAAO,OAAO,KAAK,OAAO,KAAK,GAAG;CAClC,OAAO,MAAM,KAAK,KAAK,KAAK,GAAG;CAC/B,OAAO,OAAO,KAAK,IAAI,KAAK,EAAE;CAC9B,OAAO,QAAQ,KAAK,IAAI,KAAK,GAAG;CAEhC,YAAY,AAAS,QAAgB;EAAhB;;CAErB,QAAQ,OAAsC;AAC5C,SAAO,KAAK,SAAS,MAAM;;CAG7B,OAAO,OAAsC;AAC3C,SAAO,KAAK,WAAW,MAAM;;CAG/B,SAAS,OAAsC;AAC7C,SAAO,KAAK,SAAS,MAAM;;CAG7B,IAAI,QAA6D;EAC/D,MAAM,QACJ,kBAAkB,uBAAuB,OAAO,SAAS;AAC3D,SAAO,IAAI,qBAAqB,KAAK,SAAS,MAAM;;CAGtD,IAAI,QAA6D;EAC/D,MAAM,QACJ,kBAAkB,uBAAuB,OAAO,SAAS;AAC3D,SAAO,IAAI,qBAAqB,KAAK,SAAS,MAAM;;CAGtD,KAAK,QAAsC;AACzC,SAAO,IAAI,qBAAqB,KAAK,SAAS,OAAO,OAAO,CAAC;;CAG/D,OAAO,MAA4B;AACjC,SAAO,IAAI,qBAAqB,OAAO,KAAK,KAAK,CAAC,GAAG,OAAO,IAAK,CAAC;;CAGpE,OAAO,gBAAgB,aAA2C;AAChE,SAAO,IAAI,qBAAqB,cAAc,OAAO,IAAK,CAAC;;CAG7D,OAAO,iBAAiB,cAA4C;AAClE,SAAO,IAAI,qBAAqB,aAAa;;CAG/C,OAAO,YACL,YACsB;AACtB,MAAI,sBAAsB,qBACxB,QAAO;AAET,MAAI,OAAO,eAAe,SACxB,QAAO,IAAI,qBAAqB,WAAW;AAG7C,MACE,kBAAkB,cAClB,OAAO,WAAW,iBAAiB,SAEnC,QAAO,IAAI,qBAAqB,WAAW,aAAa;AAI1D,SAAO,IAAI,qBADI,OAAO,WAAW,SAAS,CAAC,GAAG,OAAO,IAAK,CACnB;;CAGzC,YAAY;EACV,MAAM,OAAO,IAAI,KAAK,OAAO,KAAK,SAAS,OAAO,IAAK,CAAC,CAAC;AACzD,EAAC,KAAa,eAAe,KAAK;AAClC,SAAO;;CAGT,OAAO,KAAK,MAA4B,OAAqC;AAC3E,MAAI,KAAK,QAAQ,MAAM,CACrB,QAAO;AAET,MAAI,KAAK,SAAS,MAAM,CACtB,QAAO;AAET,SAAO"}
package/dist/choice.js CHANGED
@@ -1,7 +1,7 @@
1
- import { Empty } from "./_.js";
1
+ const require__ = require('./_.js');
2
2
 
3
3
  //#region src/choice.ts
4
- const Choice = (config, base = Empty) => {
4
+ const Choice = (config, base = require__.Empty) => {
5
5
  class $Choice extends base {
6
6
  static $shape = "choice";
7
7
  static $of = config;
@@ -41,5 +41,4 @@ const Choice = (config, base = Empty) => {
41
41
  };
42
42
 
43
43
  //#endregion
44
- export { Choice };
45
- //# sourceMappingURL=choice.js.map
44
+ exports.Choice = Choice;
@@ -0,0 +1,45 @@
1
+ import { Empty } from "./_.mjs";
2
+
3
+ //#region src/choice.ts
4
+ const Choice = (config, base = Empty) => {
5
+ class $Choice extends base {
6
+ static $shape = "choice";
7
+ static $of = config;
8
+ static values = config;
9
+ constructor(value) {
10
+ super();
11
+ this.value = value;
12
+ }
13
+ is(value) {
14
+ return this.value === value;
15
+ }
16
+ match(matcher) {
17
+ const handler = matcher[this.value];
18
+ if (handler) return handler();
19
+ return matcher._();
20
+ }
21
+ serialize() {
22
+ return this.value;
23
+ }
24
+ static deserialize(value) {
25
+ return new this(value);
26
+ }
27
+ static $deserialize(value) {
28
+ return value;
29
+ }
30
+ static $serialize(value) {
31
+ return value;
32
+ }
33
+ static $inline;
34
+ static {
35
+ for (const choice of config) this[choice] = function() {
36
+ return new this(choice);
37
+ };
38
+ }
39
+ }
40
+ return $Choice;
41
+ };
42
+
43
+ //#endregion
44
+ export { Choice };
45
+ //# sourceMappingURL=choice.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"choice.mjs","names":[],"sources":["../src/choice.ts"],"sourcesContent":["import { AbstractConstructor, Empty, Expand, Constructor } from \"./_\";\n\nexport type ChoiceMatcher<S extends string[]> =\n | {\n [key in S[number]]: () => any;\n }\n | ({\n _: () => any;\n } & {\n [key in S[number]]?: () => any;\n });\n\nexport type ChoiceMatcherResult<M extends ChoiceMatcher<any>> =\n M[keyof M] extends () => infer R ? R : never;\n\nexport const Choice = <\n const S extends string[],\n B extends AbstractConstructor<{}> = typeof Empty,\n>(\n config: S,\n base: B = Empty as any,\n): IChoice<S, B> => {\n type Inline = S[number];\n\n abstract class $Choice extends (base as any as Constructor<{}>) {\n static $shape = \"choice\" as const;\n\n static $of = config;\n static values = config;\n\n constructor(public value: Expand<Inline>) {\n super();\n }\n\n is<T extends Inline>(\n value: T,\n ): this is Omit<this, \"value\" | \"serialize\"> & {\n value: T;\n serialize(): T;\n } {\n return this.value === value;\n }\n\n match<M extends ChoiceMatcher<S>>(matcher: M): ChoiceMatcherResult<M> {\n const handler = matcher[this.value];\n if (handler) return handler();\n return (matcher as { _: () => any })._();\n }\n\n serialize(): Inline {\n return this.value;\n }\n\n static deserialize<T extends typeof $Choice>(\n this: T,\n value: Inline,\n ): InstanceType<T> {\n return new (this as any)(value as any) as InstanceType<T>;\n }\n static $deserialize<T extends typeof $Choice>(\n this: T,\n value: Inline,\n ): Inline {\n return value;\n }\n static $serialize<T extends typeof $Choice>(\n this: T,\n value: Inline,\n ): Inline {\n return value;\n }\n\n static $inline: Inline;\n\n static {\n for (const choice of config) {\n (this as any)[choice] = function <T extends Constructor>(this: T) {\n return new this(choice as any);\n };\n }\n }\n }\n\n return $Choice as any;\n};\n\nexport type IChoice<\n S extends string[],\n B extends AbstractConstructor<{}> = typeof Empty,\n> = Omit<B, \"\"> & {\n $shape: \"choice\";\n $of: S;\n values: S;\n deserialize<T extends Constructor>(\n this: T,\n value: S[number],\n ): InstanceType<T>;\n $deserialize<T>(this: T, value: S[number]): S[number];\n $serialize<T>(this: T, value: S[number]): S[number];\n $inline: S[number];\n} & (abstract new (\n value: S[number],\n ) => InstanceType<B> & {\n value: S[number];\n is<TH, T extends S[number]>(\n this: TH,\n value: T,\n ): this is Omit<TH, \"serialize\" | \"value\"> & {\n value: T;\n serialize(): T;\n };\n match<M extends ChoiceMatcher<S>>(matcher: M): ChoiceMatcherResult<M>;\n serialize(): S[number];\n }) & {\n [K in S[number]]: <T extends Constructor>(this: T) => InstanceType<T>;\n };\n//# sourceMappingURL=choice.d.ts.map\n"],"mappings":";;;AAeA,MAAa,UAIX,QACA,OAAU,UACQ;CAGlB,MAAe,gBAAiB,KAAgC;EAC9D,OAAO,SAAS;EAEhB,OAAO,MAAM;EACb,OAAO,SAAS;EAEhB,YAAY,AAAO,OAAuB;AACxC,UAAO;GADU;;EAInB,GACE,OAIA;AACA,UAAO,KAAK,UAAU;;EAGxB,MAAkC,SAAoC;GACpE,MAAM,UAAU,QAAQ,KAAK;AAC7B,OAAI,QAAS,QAAO,SAAS;AAC7B,UAAQ,QAA6B,GAAG;;EAG1C,YAAoB;AAClB,UAAO,KAAK;;EAGd,OAAO,YAEL,OACiB;AACjB,UAAO,IAAK,KAAa,MAAa;;EAExC,OAAO,aAEL,OACQ;AACR,UAAO;;EAET,OAAO,WAEL,OACQ;AACR,UAAO;;EAGT,OAAO;EAEP;AACE,QAAK,MAAM,UAAU,OACnB,CAAC,KAAa,UAAU,WAA0C;AAChE,WAAO,IAAI,KAAK,OAAc;;;;AAMtC,QAAO"}
package/dist/class.js CHANGED
@@ -1,7 +1,7 @@
1
- import { Empty } from "./_.js";
1
+ const require__ = require('./_.js');
2
2
 
3
3
  //#region src/class.ts
4
- const Class = (of, base = Empty) => {
4
+ const Class = (of, base = require__.Empty) => {
5
5
  class $Class extends base {
6
6
  static $shape = "class";
7
7
  static $of = of;
@@ -27,5 +27,4 @@ const Class = (of, base = Empty) => {
27
27
  };
28
28
 
29
29
  //#endregion
30
- export { Class };
31
- //# sourceMappingURL=class.js.map
30
+ exports.Class = Class;
package/dist/class.mjs ADDED
@@ -0,0 +1,31 @@
1
+ import { Empty } from "./_.mjs";
2
+
3
+ //#region src/class.ts
4
+ const Class = (of, base = Empty) => {
5
+ class $Class extends base {
6
+ static $shape = "class";
7
+ static $of = of;
8
+ constructor(value) {
9
+ super();
10
+ this.value = value;
11
+ }
12
+ static deserialize(value) {
13
+ return new this(of.deserialize(value));
14
+ }
15
+ serialize() {
16
+ return this.value.serialize();
17
+ }
18
+ static $deserialize(value) {
19
+ return of.deserialize(value);
20
+ }
21
+ static $serialize(value) {
22
+ return value.serialize();
23
+ }
24
+ static $inline;
25
+ }
26
+ return $Class;
27
+ };
28
+
29
+ //#endregion
30
+ export { Class };
31
+ //# sourceMappingURL=class.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class.mjs","names":[],"sources":["../src/class.ts"],"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"],"mappings":";;;AAMA,MAAa,SAMX,IACA,OAAO,UACU;CAIjB,MAAe,eAAe,KAAK;EACjC,OAAO,SAAS;EAChB,OAAO,MAAM;EAEb,YAAY,AAAO,OAAwB;AACzC,UAAO;GADU;;EAInB,OAAO,YAEL,OACiB;AACjB,UAAO,IAAI,KAAK,GAAG,YAAY,MAAM,CAAC;;EAGxC,YAAyD;AACvD,UAAO,KAAK,MAAM,WAAW;;EAG/B,OAAO,aAAa,OAAgD;AAClE,UAAO,GAAG,YAAY,MAAM;;EAG9B,OAAO,WAEL,OACY;AACZ,UAAO,MAAM,WAAW;;EAG1B,OAAO;;AAGT,QAAO"}
package/dist/dict.js CHANGED
@@ -1,7 +1,7 @@
1
- import { Empty, Shape } from "./_.js";
1
+ const require__ = require('./_.js');
2
2
 
3
3
  //#region src/dict.ts
4
- const Dict = (of, base = Empty) => {
4
+ const Dict = (of, base = require__.Empty) => {
5
5
  class $Dict extends base {
6
6
  static $shape = "dict";
7
7
  static $of = of;
@@ -18,13 +18,13 @@ const Dict = (of, base = Empty) => {
18
18
  }
19
19
  static $deserialize(value) {
20
20
  const transform = Object.entries(of).map(([key, child]) => {
21
- return [key, Shape(child).$deserialize(value[key])];
21
+ return [key, require__.Shape(child).$deserialize(value[key])];
22
22
  });
23
23
  return Object.fromEntries(transform);
24
24
  }
25
25
  static $serialize(value) {
26
26
  const transform = Object.entries(of).map(([key, child]) => {
27
- return [key, Shape(child).$serialize(value[key])];
27
+ return [key, require__.Shape(child).$serialize(value[key])];
28
28
  });
29
29
  const merge = Object.fromEntries(transform);
30
30
  if ("$name" in base) return {
@@ -38,5 +38,4 @@ const Dict = (of, base = Empty) => {
38
38
  };
39
39
 
40
40
  //#endregion
41
- export { Dict };
42
- //# sourceMappingURL=dict.js.map
41
+ exports.Dict = Dict;
package/dist/dict.mjs ADDED
@@ -0,0 +1,42 @@
1
+ import { Empty, Shape } from "./_.mjs";
2
+
3
+ //#region src/dict.ts
4
+ const Dict = (of, base = Empty) => {
5
+ class $Dict extends base {
6
+ static $shape = "dict";
7
+ static $of = of;
8
+ constructor(...args) {
9
+ super();
10
+ Object.assign(this, args[0]);
11
+ }
12
+ serialize() {
13
+ return $Dict.$serialize(this);
14
+ }
15
+ static deserialize(value) {
16
+ const runtime = this.$deserialize(value);
17
+ return new this(runtime);
18
+ }
19
+ static $deserialize(value) {
20
+ const transform = Object.entries(of).map(([key, child]) => {
21
+ return [key, Shape(child).$deserialize(value[key])];
22
+ });
23
+ return Object.fromEntries(transform);
24
+ }
25
+ static $serialize(value) {
26
+ const transform = Object.entries(of).map(([key, child]) => {
27
+ return [key, Shape(child).$serialize(value[key])];
28
+ });
29
+ const merge = Object.fromEntries(transform);
30
+ if ("$name" in base) return {
31
+ ...merge,
32
+ $name: base.$name
33
+ };
34
+ return merge;
35
+ }
36
+ }
37
+ return $Dict;
38
+ };
39
+
40
+ //#endregion
41
+ export { Dict };
42
+ //# sourceMappingURL=dict.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dict.mjs","names":[],"sources":["../src/dict.ts"],"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"],"mappings":";;;AAyBA,MAAa,QAIX,IACA,OAAU,UACM;CAChB,MAAe,cAAe,KAAwC;EACpE,OAAO,SAAS;EAChB,OAAO,MAAM;EAEb,YAAY,GAAG,MAAa;AAC1B,UAAO;AACP,UAAO,OAAO,MAAM,KAAK,GAAG;;EAG9B,YAAY;AACV,UAAO,MAAM,WAAW,KAAY;;EAGtC,OAAO,YAEL,OACiB;GACjB,MAAM,UAAW,KAAa,aAAa,MAAa;AACxD,UAAO,IAAI,KAAK,QAAe;;EAGjC,OAAO,aAA8C,OAAiB;GAEpE,MAAM,YADQ,OAAO,QAAQ,GAAG,CACR,KAAK,CAAC,KAAK,WAAW;AAG5C,WAAO,CAAC,KAFS,MAAM,MAAM,CACC,aAAc,MAAc,KAAK,CACrC;KAC1B;AAEF,UADc,OAAO,YAAY,UAAU;;EAI7C,OAAO,WAEL,OACK;GAEL,MAAM,YADQ,OAAO,QAAQ,GAAG,CACR,KAAK,CAAC,KAAK,WAAW;AAG5C,WAAO,CAAC,KAFS,MAAM,MAAa,CACR,WAAY,MAAc,KAAK,CACnC;KACxB;GACF,MAAM,QAAQ,OAAO,YAAY,UAAU;AAE3C,OAAI,WAAW,KACb,QAAO;IAAE,GAAG;IAAO,OAAO,KAAK;IAAO;AAGxC,UAAO;;;AAIX,QAAO"}
@@ -1,4 +1,4 @@
1
- import { Empty, Shape } from "./_.js";
1
+ const require__ = require('./_.js');
2
2
 
3
3
  //#region src/discriminated-union.ts
4
4
  function findBestKey(config) {
@@ -17,14 +17,14 @@ function findBestKey(config) {
17
17
  }
18
18
  function prepareShapeMap(config, key) {
19
19
  return config.reduce((acc, c) => {
20
- const shape = Shape(c);
20
+ const shape = require__.Shape(c);
21
21
  const discriminator = key in c ? c[key] : "$of" in c ? c.$of[key] : shape.$of[key];
22
22
  acc[discriminator] = shape;
23
23
  return acc;
24
24
  }, {});
25
25
  }
26
26
  const DiscriminatedUnion = (of, ...args) => {
27
- const base = args[0] || Empty;
27
+ const base = args[0] || require__.Empty;
28
28
  const key = findBestKey(of);
29
29
  const map = prepareShapeMap(of, key);
30
30
  class $DiscriminatedUnion extends base {
@@ -61,5 +61,6 @@ const DiscriminatedUnion = (of, ...args) => {
61
61
  };
62
62
 
63
63
  //#endregion
64
- export { DiscriminatedUnion, findBestKey, prepareShapeMap };
65
- //# sourceMappingURL=discriminated-union.js.map
64
+ exports.DiscriminatedUnion = DiscriminatedUnion;
65
+ exports.findBestKey = findBestKey;
66
+ exports.prepareShapeMap = prepareShapeMap;
@@ -0,0 +1,65 @@
1
+ import { Empty, Shape } from "./_.mjs";
2
+
3
+ //#region src/discriminated-union.ts
4
+ function findBestKey(config) {
5
+ const hash = {};
6
+ for (const c of config) {
7
+ const shape = "$of" in c ? c.$of : c;
8
+ for (const key in shape) {
9
+ const k = key;
10
+ if (hash[key]) hash[key].add(shape[k]);
11
+ else hash[key] = new Set([shape[k]]);
12
+ }
13
+ }
14
+ const key = Object.entries(hash).map(([key, value]) => [key, value.size]).filter(([_, value]) => value === config.length).sort((a, b) => b[1] - a[1])?.[0]?.[0];
15
+ if (!key) throw new Error("Could not find key for DiscriminatedUnion");
16
+ return key;
17
+ }
18
+ function prepareShapeMap(config, key) {
19
+ return config.reduce((acc, c) => {
20
+ const shape = Shape(c);
21
+ const discriminator = key in c ? c[key] : "$of" in c ? c.$of[key] : shape.$of[key];
22
+ acc[discriminator] = shape;
23
+ return acc;
24
+ }, {});
25
+ }
26
+ const DiscriminatedUnion = (of, ...args) => {
27
+ const base = args[0] || Empty;
28
+ const key = findBestKey(of);
29
+ const map = prepareShapeMap(of, key);
30
+ class $DiscriminatedUnion extends base {
31
+ constructor(value) {
32
+ super();
33
+ this.value = value;
34
+ }
35
+ static $of = of;
36
+ static $shape = "discriminated-union";
37
+ serialize() {
38
+ return $DiscriminatedUnion.$serialize(this.value);
39
+ }
40
+ match(matcher, fallback) {
41
+ const element = this.value;
42
+ const handler = matcher[element[key]];
43
+ if (handler) return handler(element);
44
+ if (fallback) return fallback(element);
45
+ if (matcher._) return matcher._(element);
46
+ throw new Error("Non-exhaustive match");
47
+ }
48
+ static deserialize(value) {
49
+ return new this(this.$deserialize(value));
50
+ }
51
+ static $deserialize(value) {
52
+ const definition = map[value[key]];
53
+ if (!definition) throw new Error("Cannot deserialize DiscriminatedUnion");
54
+ return definition.$deserialize(value);
55
+ }
56
+ static $serialize(value) {
57
+ return map[value[key]].$serialize(value);
58
+ }
59
+ }
60
+ return $DiscriminatedUnion;
61
+ };
62
+
63
+ //#endregion
64
+ export { DiscriminatedUnion, findBestKey, prepareShapeMap };
65
+ //# sourceMappingURL=discriminated-union.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discriminated-union.mjs","names":[],"sources":["../src/discriminated-union.ts"],"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"],"mappings":";;;AA6HA,SAAgB,YAAY,QAAyC;CACnE,MAAM,OAAoC,EAAE;AAE5C,MAAK,MAAM,KAAK,QAAQ;EACtB,MAAM,QAAQ,SAAS,IAAI,EAAE,MAAM;AACnC,OAAK,MAAM,OAAO,OAAO;GACvB,MAAM,IAAI;AACV,OAAI,KAAK,KACP,MAAK,KAAK,IAAI,MAAM,GAAG;OAEvB,MAAK,OAAO,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;;;CAKrC,MAAM,MAAM,OAAO,QAAQ,KAAK,CAC7B,KAAK,CAAC,KAAK,WAAW,CAAC,KAAK,MAAM,KAAK,CAAU,CACjD,QAAQ,CAAC,GAAG,WAAW,UAAU,OAAO,OAAO,CAC/C,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,KAAK;AAEtC,KAAI,CAAC,IACH,OAAM,IAAI,MAAM,4CAA4C;AAG9D,QAAO;;AAGT,SAAgB,gBACd,QACA,KACA;AACA,QAAO,OAAO,QAAuC,KAAK,MAAM;EAC9D,MAAM,QAAQ,MAAM,EAAE;EAUtB,MAAM,gBACJ,OAAO,IACF,EAAU,OACX,SAAS,IACN,EAAU,IAAI,OACd,MAAc,IAAI;AAE3B,MAAI,iBAAiB;AACrB,SAAO;IACN,EAAE,CAAC;;AAYR,MAAa,sBAKX,IACA,GAAG,SAC8B;CACjC,MAAM,OAAO,KAAK,MAAO;CAEzB,MAAM,MAAM,YAAY,GAAG;CAC3B,MAAM,MAAM,gBAAgB,IAAI,IAAI;CAEpC,MAAe,4BAA6B,KAAgC;EAC1E,YAAY,AAAO,OAAY;AAC7B,UAAO;GADU;;EAInB,OAAO,MAAM;EACb,OAAO,SAAS;EAEhB,YAAY;AACV,UAAO,oBAAoB,WAAW,KAAK,MAAM;;EAGnD,MAAM,SAAc,UAAe;GACjC,MAAM,UAAe,KAAK;GAG1B,MAAM,UAAU,QAFK,QAAQ;AAG7B,OAAI,QACF,QAAO,QAAQ,QAAQ;AAEzB,OAAI,SACF,QAAO,SAAS,QAAQ;AAE1B,OAAI,QAAQ,EACV,QAAO,QAAQ,EAAE,QAAQ;AAE3B,SAAM,IAAI,MAAM,uBAAuB;;EAGzC,OAAO,YAAY,OAAY;AAC7B,UAAO,IAAK,KAAa,KAAK,aAAa,MAAa,CAAC;;EAG3D,OAAO,aAAa,OAAY;GAC9B,MAAM,aAAa,IAAI,MAAM;AAC7B,OAAI,CAAC,WACH,OAAM,IAAI,MAAM,wCAAwC;AAE1D,UAAQ,WAAmB,aAAa,MAAM;;EAGhD,OAAO,WAAW,OAAiB;AACjC,UAAO,IAAK,MAAc,MAAM,WAAW,MAAM;;;AAIrD,QAAO"}
package/dist/either.js CHANGED
@@ -1,9 +1,9 @@
1
- import { Empty, Shape } from "./_.js";
1
+ const require__ = require('./_.js');
2
2
 
3
3
  //#region src/either.ts
4
- const Either = (of, base = Empty) => {
4
+ const Either = (of, base = require__.Empty) => {
5
5
  const definitions = Object.fromEntries(Object.entries(of).map(([key, value]) => {
6
- return [key, Shape(value)];
6
+ return [key, require__.Shape(value)];
7
7
  }));
8
8
  class $Either extends base {
9
9
  constructor(value) {
@@ -48,5 +48,4 @@ const Either = (of, base = Empty) => {
48
48
  };
49
49
 
50
50
  //#endregion
51
- export { Either };
52
- //# sourceMappingURL=either.js.map
51
+ exports.Either = Either;
@@ -0,0 +1,52 @@
1
+ import { Empty, Shape } from "./_.mjs";
2
+
3
+ //#region src/either.ts
4
+ const Either = (of, base = Empty) => {
5
+ const definitions = Object.fromEntries(Object.entries(of).map(([key, value]) => {
6
+ return [key, Shape(value)];
7
+ }));
8
+ class $Either extends base {
9
+ constructor(value) {
10
+ super();
11
+ this.value = value;
12
+ }
13
+ static serialized;
14
+ static of = of;
15
+ static $shape = "either";
16
+ serialize() {
17
+ return $Either.$serialize(this.value);
18
+ }
19
+ match(...[matcher, fallback]) {
20
+ const handler = matcher[Object.entries(of).find(([_, v]) => v === this.value.constructor)?.[0]];
21
+ if (handler) return handler(this.value);
22
+ if (fallback) return fallback(this.value);
23
+ if (matcher._) return matcher._(this.value);
24
+ throw new Error("Non-exhaustive match");
25
+ }
26
+ static deserialize(value) {
27
+ return new this(this.$deserialize(value));
28
+ }
29
+ static $deserialize(value) {
30
+ const { _key: key, ...serialized } = value;
31
+ const definition = definitions[key];
32
+ if (!definition) throw new Error("Cannot deserialize Either");
33
+ return definition.$deserialize(serialized);
34
+ }
35
+ static $serialize(value) {
36
+ const key = Object.entries(of).find(([_, v]) => v === value.constructor)?.[0];
37
+ if (!key) throw new Error("Cannot serialize Either, no matching key");
38
+ const definition = definitions[key];
39
+ if (!definition) throw new Error("Cannot serialize Either");
40
+ return {
41
+ ...definition.$serialize(value),
42
+ _key: key
43
+ };
44
+ }
45
+ static $inline;
46
+ }
47
+ return $Either;
48
+ };
49
+
50
+ //#endregion
51
+ export { Either };
52
+ //# sourceMappingURL=either.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"either.mjs","names":[],"sources":["../src/either.ts"],"sourcesContent":["import {\n Constructor,\n Expand,\n DefinitionOf,\n Shape,\n AbstractConstructor,\n Empty,\n} from \"./_\";\nimport { ClassShorthand } from \"./class\";\nimport { PrimitiveShorthand } from \"./primitive\";\n\ntype Config = { [key: string]: any };\n\ntype ExhaustiveMatcher<C extends Config> = {\n [key in keyof C]: (value: InstanceType<C[key]>) => any;\n};\n\ntype UnsafeFallthroughMatcher<C extends Config> = {\n [key in keyof C]?: (value: InstanceType<C[key]>) => any;\n} & {\n _: (value: InstanceType<C[keyof C]>) => any;\n};\n\ntype PartialMatcher<C extends Config> = {\n [key in keyof C]?: (value: InstanceType<C[key]>) => any;\n};\n\ntype Matcher<C extends Config> =\n | ExhaustiveMatcher<C>\n | UnsafeFallthroughMatcher<C>\n | PartialMatcher<C>;\n\nexport type EitherConfiguration = {\n [key: string]: ClassShorthand;\n};\n\ntype Internal<\n S extends EitherConfiguration,\n B extends AbstractConstructor<{}>,\n> = {\n Definition: DefinitionOf<S[keyof S]>;\n Serialized: (B extends { $name: infer U } ? { $name: U } : {}) & {\n _key: keyof S;\n } & ReturnType<DefinitionOf<S[keyof S]>[\"$serialize\"]>;\n Deserializing: (B extends { $name: infer U } ? { $name: U } : {}) & {\n _key: keyof S;\n } & Parameters<DefinitionOf<S[keyof S]>[\"$deserialize\"]>[0];\n Inline: DefinitionOf<S[keyof S]>[\"$inline\"];\n};\n\nexport const Either = <\n const S extends EitherConfiguration,\n const B extends AbstractConstructor<{}> = typeof Empty,\n>(\n of: S,\n base: B = Empty as any,\n) => {\n type Serialized = {\n [K in keyof S]: { _key: K } & ReturnType<DefinitionOf<S[K]>[\"$serialize\"]>;\n }[keyof S];\n\n type Inline = DefinitionOf<S[keyof S]>[\"$inline\"];\n\n const definitions = Object.fromEntries(\n Object.entries(of).map(([key, value]) => {\n return [key, Shape(value)] as const;\n }),\n );\n\n abstract class $Either extends (base as any as Constructor<{}>) {\n constructor(public value: Inline) {\n super();\n }\n\n static serialized: Serialized;\n\n static of = of;\n\n static $shape = \"either\" as const;\n\n serialize(): Expand<Serialized> {\n return ($Either as any).$serialize(this.value) as any;\n }\n\n match<\n M extends Matcher<S>,\n F extends M extends ExhaustiveMatcher<S>\n ? []\n : M extends UnsafeFallthroughMatcher<S>\n ? []\n : M extends PartialMatcher<S>\n ? [\n fallback: (\n value: InstanceType<Omit<S, keyof M>[keyof Omit<S, keyof M>]>,\n ) => any,\n ]\n : [],\n >(\n ...[matcher, fallback]: [matcher: M, ...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 const key: any = Object.entries(of).find(\n ([_, v]) => v === ((this.value as any).constructor as any),\n )?.[0] as any;\n\n const handler = matcher[key];\n if (handler) {\n return handler(this.value as any);\n }\n if (fallback) {\n return fallback(this.value as any);\n }\n if (matcher._) {\n return matcher._(this.value as any);\n }\n throw new Error(\"Non-exhaustive match\");\n }\n\n static deserialize<T extends typeof $Either>(\n this: T,\n value: Expand<Serialized>,\n ): InstanceType<T> {\n return new (this as any)(this.$deserialize(value as any)) as any;\n }\n\n static $deserialize<T extends typeof $Either>(\n this: T,\n value: Serialized,\n ): Inline {\n const { _key: key, ...serialized } = value as any;\n const definition = definitions[key];\n if (!definition) {\n throw new Error(\"Cannot deserialize Either\");\n }\n return (definition as any).$deserialize(serialized);\n }\n\n static $serialize<T extends typeof $Either>(\n this: T,\n value: Inline,\n ): Serialized {\n const key = Object.entries(of).find(\n ([_, v]) => v === ((value as any).constructor as any),\n )?.[0];\n if (!key) {\n throw new Error(\"Cannot serialize Either, no matching key\");\n }\n\n const definition = definitions[key];\n if (!definition) {\n throw new Error(\"Cannot serialize Either\");\n }\n return { ...(definition as any).$serialize(value), _key: key } as any;\n }\n\n static $inline: Inline;\n }\n\n type EitherConstructor = abstract new (\n value: Expand<Inline>,\n ) => InstanceType<B> & $Either;\n type Either = Omit<B, \"prototype\"> &\n Omit<typeof $Either, \"prototype\"> &\n EitherConstructor;\n\n return $Either as Either;\n};\n\nexport type Either<\n S extends EitherConfiguration,\n B extends AbstractConstructor<{}> = typeof Empty,\n> = Omit<B, \"prototype\"> &\n (abstract new (\n value: Internal<S, B>[\"Inline\"],\n ) => {\n value: Internal<S, B>[\"Inline\"];\n serialize(): Internal<S, B>[\"Serialized\"];\n match<\n M extends Matcher<S>,\n F extends M extends ExhaustiveMatcher<S>\n ? []\n : M extends UnsafeFallthroughMatcher<S>\n ? []\n : M extends PartialMatcher<S>\n ? [\n fallback: (\n value: InstanceType<Omit<S, keyof M>[keyof Omit<S, keyof M>]>,\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 serialized: Internal<S, B>[\"Serialized\"];\n of: S;\n $shape: \"either\";\n deserialize<T extends Constructor>(\n this: T,\n value: Internal<S, B>[\"Serialized\"],\n ): InstanceType<T>;\n $deserialize<T>(\n this: T,\n value: Internal<S, B>[\"Serialized\"],\n ): Internal<S, B>[\"Inline\"];\n $serialize<T>(\n this: T,\n value: Internal<S, B>[\"Inline\"],\n ): Internal<S, B>[\"Serialized\"];\n $inline: Internal<S, B>[\"Inline\"];\n };\n"],"mappings":";;;AAkDA,MAAa,UAIX,IACA,OAAU,UACP;CAOH,MAAM,cAAc,OAAO,YACzB,OAAO,QAAQ,GAAG,CAAC,KAAK,CAAC,KAAK,WAAW;AACvC,SAAO,CAAC,KAAK,MAAM,MAAM,CAAC;GAC1B,CACH;CAED,MAAe,gBAAiB,KAAgC;EAC9D,YAAY,AAAO,OAAe;AAChC,UAAO;GADU;;EAInB,OAAO;EAEP,OAAO,KAAK;EAEZ,OAAO,SAAS;EAEhB,YAAgC;AAC9B,UAAQ,QAAgB,WAAW,KAAK,MAAM;;EAGhD,MAcE,GAAG,CAAC,SAAS,WAKuD;GAKpE,MAAM,UAAU,QAJC,OAAO,QAAQ,GAAG,CAAC,MACjC,CAAC,GAAG,OAAO,MAAQ,KAAK,MAAc,YACxC,GAAG;AAGJ,OAAI,QACF,QAAO,QAAQ,KAAK,MAAa;AAEnC,OAAI,SACF,QAAO,SAAS,KAAK,MAAa;AAEpC,OAAI,QAAQ,EACV,QAAO,QAAQ,EAAE,KAAK,MAAa;AAErC,SAAM,IAAI,MAAM,uBAAuB;;EAGzC,OAAO,YAEL,OACiB;AACjB,UAAO,IAAK,KAAa,KAAK,aAAa,MAAa,CAAC;;EAG3D,OAAO,aAEL,OACQ;GACR,MAAM,EAAE,MAAM,KAAK,GAAG,eAAe;GACrC,MAAM,aAAa,YAAY;AAC/B,OAAI,CAAC,WACH,OAAM,IAAI,MAAM,4BAA4B;AAE9C,UAAQ,WAAmB,aAAa,WAAW;;EAGrD,OAAO,WAEL,OACY;GACZ,MAAM,MAAM,OAAO,QAAQ,GAAG,CAAC,MAC5B,CAAC,GAAG,OAAO,MAAQ,MAAc,YACnC,GAAG;AACJ,OAAI,CAAC,IACH,OAAM,IAAI,MAAM,2CAA2C;GAG7D,MAAM,aAAa,YAAY;AAC/B,OAAI,CAAC,WACH,OAAM,IAAI,MAAM,0BAA0B;AAE5C,UAAO;IAAE,GAAI,WAAmB,WAAW,MAAM;IAAE,MAAM;IAAK;;EAGhE,OAAO;;AAUT,QAAO"}