arktype 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +101 -0
  3. package/dist/api.cjs +15 -0
  4. package/dist/api.d.ts +2 -0
  5. package/dist/api.mjs +1 -0
  6. package/dist/cjs/main.js +462 -0
  7. package/dist/cjs/nodes/branches.js +85 -0
  8. package/dist/cjs/nodes/compose.js +140 -0
  9. package/dist/cjs/nodes/discriminate.js +199 -0
  10. package/dist/cjs/nodes/functors.js +35 -0
  11. package/dist/cjs/nodes/infer.js +4 -0
  12. package/dist/cjs/nodes/node.js +108 -0
  13. package/dist/cjs/nodes/predicate.js +87 -0
  14. package/dist/cjs/nodes/rules/class.js +21 -0
  15. package/dist/cjs/nodes/rules/collapsibleSet.js +49 -0
  16. package/dist/cjs/nodes/rules/divisor.js +33 -0
  17. package/dist/cjs/nodes/rules/props.js +72 -0
  18. package/dist/cjs/nodes/rules/range.js +109 -0
  19. package/dist/cjs/nodes/rules/regex.js +68 -0
  20. package/dist/cjs/nodes/rules/rules.js +156 -0
  21. package/dist/cjs/nodes/rules/subdomain.js +78 -0
  22. package/dist/cjs/package.json +3 -0
  23. package/dist/cjs/parse/definition.js +33 -0
  24. package/dist/cjs/parse/record.js +37 -0
  25. package/dist/cjs/parse/string/ast.js +36 -0
  26. package/dist/cjs/parse/string/reduce/dynamic.js +150 -0
  27. package/dist/cjs/parse/string/reduce/shared.js +23 -0
  28. package/dist/cjs/parse/string/reduce/static.js +4 -0
  29. package/dist/cjs/parse/string/shift/operand/enclosed.js +54 -0
  30. package/dist/cjs/parse/string/shift/operand/operand.js +11 -0
  31. package/dist/cjs/parse/string/shift/operand/unenclosed.js +53 -0
  32. package/dist/cjs/parse/string/shift/operator/bounds.js +87 -0
  33. package/dist/cjs/parse/string/shift/operator/divisor.js +35 -0
  34. package/dist/cjs/parse/string/shift/operator/operator.js +26 -0
  35. package/dist/cjs/parse/string/shift/scanner.js +99 -0
  36. package/dist/cjs/parse/string/string.js +43 -0
  37. package/dist/cjs/parse/tuple/keyof.js +46 -0
  38. package/dist/cjs/parse/tuple/morph.js +56 -0
  39. package/dist/cjs/parse/tuple/narrow.js +15 -0
  40. package/dist/cjs/parse/tuple/tuple.js +85 -0
  41. package/dist/cjs/parse/tuple/utils.js +48 -0
  42. package/dist/cjs/traverse/problems.js +249 -0
  43. package/dist/cjs/traverse/traverse.js +268 -0
  44. package/dist/cjs/utils/chainableNoOpProxy.js +23 -0
  45. package/dist/cjs/utils/domains.js +67 -0
  46. package/dist/cjs/utils/errors.js +26 -0
  47. package/dist/cjs/utils/freeze.js +16 -0
  48. package/dist/cjs/utils/generics.js +36 -0
  49. package/dist/cjs/utils/numericLiterals.js +84 -0
  50. package/dist/cjs/utils/paths.js +32 -0
  51. package/dist/cjs/utils/serialize.js +98 -0
  52. package/dist/cjs/utils/unionToTuple.js +4 -0
  53. package/dist/mjs/main.js +442 -0
  54. package/dist/mjs/nodes/branches.js +71 -0
  55. package/dist/mjs/nodes/compose.js +118 -0
  56. package/dist/mjs/nodes/discriminate.js +184 -0
  57. package/dist/mjs/nodes/functors.js +27 -0
  58. package/dist/mjs/nodes/infer.js +1 -0
  59. package/dist/mjs/nodes/node.js +90 -0
  60. package/dist/mjs/nodes/predicate.js +69 -0
  61. package/dist/mjs/nodes/rules/class.js +7 -0
  62. package/dist/mjs/nodes/rules/collapsibleSet.js +35 -0
  63. package/dist/mjs/nodes/rules/divisor.js +19 -0
  64. package/dist/mjs/nodes/rules/props.js +57 -0
  65. package/dist/mjs/nodes/rules/range.js +90 -0
  66. package/dist/mjs/nodes/rules/regex.js +53 -0
  67. package/dist/mjs/nodes/rules/rules.js +138 -0
  68. package/dist/mjs/nodes/rules/subdomain.js +63 -0
  69. package/dist/mjs/package.json +3 -0
  70. package/dist/mjs/parse/definition.js +17 -0
  71. package/dist/mjs/parse/record.js +29 -0
  72. package/dist/mjs/parse/string/ast.js +19 -0
  73. package/dist/mjs/parse/string/reduce/dynamic.js +142 -0
  74. package/dist/mjs/parse/string/reduce/shared.js +6 -0
  75. package/dist/mjs/parse/string/reduce/static.js +1 -0
  76. package/dist/mjs/parse/string/shift/operand/enclosed.js +39 -0
  77. package/dist/mjs/parse/string/shift/operand/operand.js +3 -0
  78. package/dist/mjs/parse/string/shift/operand/unenclosed.js +36 -0
  79. package/dist/mjs/parse/string/shift/operator/bounds.js +70 -0
  80. package/dist/mjs/parse/string/shift/operator/divisor.js +21 -0
  81. package/dist/mjs/parse/string/shift/operator/operator.js +11 -0
  82. package/dist/mjs/parse/string/shift/scanner.js +91 -0
  83. package/dist/mjs/parse/string/string.js +28 -0
  84. package/dist/mjs/parse/tuple/keyof.js +38 -0
  85. package/dist/mjs/parse/tuple/morph.js +42 -0
  86. package/dist/mjs/parse/tuple/narrow.js +7 -0
  87. package/dist/mjs/parse/tuple/tuple.js +71 -0
  88. package/dist/mjs/parse/tuple/utils.js +34 -0
  89. package/dist/mjs/traverse/problems.js +229 -0
  90. package/dist/mjs/traverse/traverse.js +252 -0
  91. package/dist/mjs/utils/chainableNoOpProxy.js +15 -0
  92. package/dist/mjs/utils/domains.js +48 -0
  93. package/dist/mjs/utils/errors.js +10 -0
  94. package/dist/mjs/utils/freeze.js +8 -0
  95. package/dist/mjs/utils/generics.js +15 -0
  96. package/dist/mjs/utils/numericLiterals.js +71 -0
  97. package/dist/mjs/utils/paths.js +18 -0
  98. package/dist/mjs/utils/serialize.js +81 -0
  99. package/dist/mjs/utils/unionToTuple.js +1 -0
  100. package/dist/tsconfig.temp.tsbuildinfo +1 -0
  101. package/dist/types/main.d.ts +257 -0
  102. package/dist/types/nodes/branches.d.ts +14 -0
  103. package/dist/types/nodes/compose.d.ts +92 -0
  104. package/dist/types/nodes/discriminate.d.ts +27 -0
  105. package/dist/types/nodes/functors.d.ts +6 -0
  106. package/dist/types/nodes/infer.d.ts +35 -0
  107. package/dist/types/nodes/node.d.ts +51 -0
  108. package/dist/types/nodes/predicate.d.ts +88 -0
  109. package/dist/types/nodes/rules/class.d.ts +3 -0
  110. package/dist/types/nodes/rules/collapsibleSet.d.ts +4 -0
  111. package/dist/types/nodes/rules/divisor.d.ts +2 -0
  112. package/dist/types/nodes/rules/props.d.ts +20 -0
  113. package/dist/types/nodes/rules/range.d.ts +33 -0
  114. package/dist/types/nodes/rules/regex.d.ts +14 -0
  115. package/dist/types/nodes/rules/rules.d.ts +44 -0
  116. package/dist/types/nodes/rules/subdomain.d.ts +15 -0
  117. package/dist/types/parse/definition.d.ts +29 -0
  118. package/dist/types/parse/record.d.ts +24 -0
  119. package/dist/types/parse/string/ast.d.ts +42 -0
  120. package/dist/types/parse/string/reduce/dynamic.d.ts +34 -0
  121. package/dist/types/parse/string/reduce/shared.d.ts +24 -0
  122. package/dist/types/parse/string/reduce/static.d.ts +115 -0
  123. package/dist/types/parse/string/shift/operand/enclosed.d.ts +28 -0
  124. package/dist/types/parse/string/shift/operand/operand.d.ts +9 -0
  125. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +18 -0
  126. package/dist/types/parse/string/shift/operator/bounds.d.ts +20 -0
  127. package/dist/types/parse/string/shift/operator/divisor.d.ts +9 -0
  128. package/dist/types/parse/string/shift/operator/operator.d.ts +13 -0
  129. package/dist/types/parse/string/shift/scanner.d.ts +89 -0
  130. package/dist/types/parse/string/string.d.ts +27 -0
  131. package/dist/types/parse/tuple/keyof.d.ts +2 -0
  132. package/dist/types/parse/tuple/morph.d.ts +15 -0
  133. package/dist/types/parse/tuple/narrow.d.ts +13 -0
  134. package/dist/types/parse/tuple/tuple.d.ts +37 -0
  135. package/dist/types/parse/tuple/utils.d.ts +19 -0
  136. package/dist/types/traverse/problems.d.ts +123 -0
  137. package/dist/types/traverse/traverse.d.ts +48 -0
  138. package/dist/types/utils/chainableNoOpProxy.d.ts +14 -0
  139. package/dist/types/utils/domains.d.ts +58 -0
  140. package/dist/types/utils/errors.d.ts +6 -0
  141. package/dist/types/utils/freeze.d.ts +2 -0
  142. package/dist/types/utils/generics.d.ts +118 -0
  143. package/dist/types/utils/numericLiterals.d.ts +28 -0
  144. package/dist/types/utils/paths.d.ts +7 -0
  145. package/dist/types/utils/serialize.d.ts +38 -0
  146. package/dist/types/utils/unionToTuple.d.ts +11 -0
  147. package/package.json +102 -2
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ composeIntersection: ()=>composeIntersection,
13
+ throwUndefinedOperandsError: ()=>throwUndefinedOperandsError,
14
+ disjointDescriptionWriters: ()=>disjointDescriptionWriters,
15
+ stringifyRange: ()=>stringifyRange,
16
+ IntersectionState: ()=>IntersectionState,
17
+ anonymousDisjoint: ()=>anonymousDisjoint,
18
+ isDisjoint: ()=>isDisjoint,
19
+ equality: ()=>equality,
20
+ isEquality: ()=>isEquality,
21
+ composeKeyedIntersection: ()=>composeKeyedIntersection
22
+ });
23
+ const _errorsTs = require("../utils/errors.js");
24
+ const _genericsTs = require("../utils/generics.js");
25
+ const _pathsTs = require("../utils/paths.js");
26
+ const _serializeTs = require("../utils/serialize.js");
27
+ function _checkPrivateRedeclaration(obj, privateCollection) {
28
+ if (privateCollection.has(obj)) {
29
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
30
+ }
31
+ }
32
+ function _classApplyDescriptorGet(receiver, descriptor) {
33
+ if (descriptor.get) {
34
+ return descriptor.get.call(receiver);
35
+ }
36
+ return descriptor.value;
37
+ }
38
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
39
+ if (descriptor.set) {
40
+ descriptor.set.call(receiver, value);
41
+ } else {
42
+ if (!descriptor.writable) {
43
+ throw new TypeError("attempted to set read only private field");
44
+ }
45
+ descriptor.value = value;
46
+ }
47
+ }
48
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
49
+ if (!privateMap.has(receiver)) {
50
+ throw new TypeError("attempted to " + action + " private field on non-instance");
51
+ }
52
+ return privateMap.get(receiver);
53
+ }
54
+ function _classPrivateFieldGet(receiver, privateMap) {
55
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
56
+ return _classApplyDescriptorGet(receiver, descriptor);
57
+ }
58
+ function _classPrivateFieldInit(obj, privateMap, value) {
59
+ _checkPrivateRedeclaration(obj, privateMap);
60
+ privateMap.set(obj, value);
61
+ }
62
+ function _classPrivateFieldSet(receiver, privateMap, value) {
63
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
64
+ _classApplyDescriptorSet(receiver, descriptor, value);
65
+ return value;
66
+ }
67
+ const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwUndefinedOperandsError() : r : r === undefined ? l : reducer(l, r, state);
68
+ const throwUndefinedOperandsError = ()=>(0, _errorsTs.throwInternalError)(`Unexpected operation two undefined operands`);
69
+ const disjointDescriptionWriters = {
70
+ domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
71
+ subdomain: ({ l , r })=>`${l} and ${r}`,
72
+ range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
73
+ class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
74
+ tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
75
+ value: ({ l , r })=>`literal values ${(0, _serializeTs.serialize)(l)} and ${(0, _serializeTs.serialize)(r)}`,
76
+ leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(l.value)} and ${(0, _serializeTs.serialize)(r)}`,
77
+ rightAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(r.value)} and ${(0, _serializeTs.serialize)(l)}`
78
+ };
79
+ const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
80
+ var _disjoints = /*#__PURE__*/ new WeakMap();
81
+ class IntersectionState {
82
+ get disjoints() {
83
+ return _classPrivateFieldGet(this, _disjoints);
84
+ }
85
+ addDisjoint(kind, l, r) {
86
+ _classPrivateFieldGet(this, _disjoints)[`${this.path}`] = {
87
+ kind,
88
+ l,
89
+ r
90
+ };
91
+ return empty;
92
+ }
93
+ constructor(type, lastOperator){
94
+ _classPrivateFieldInit(this, _disjoints, {
95
+ writable: true,
96
+ value: void 0
97
+ });
98
+ this.type = type;
99
+ this.lastOperator = lastOperator;
100
+ this.path = new _pathsTs.Path();
101
+ _classPrivateFieldSet(this, _disjoints, {});
102
+ }
103
+ }
104
+ const empty = Symbol("empty");
105
+ const anonymousDisjoint = ()=>empty;
106
+ const isDisjoint = (result)=>result === empty;
107
+ const equal = Symbol("equal");
108
+ const equality = ()=>equal;
109
+ const isEquality = (result)=>result === equal;
110
+ const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
111
+ const result = {};
112
+ const keys = (0, _genericsTs.keysOf)({
113
+ ...l,
114
+ ...r
115
+ });
116
+ let lImpliesR = true;
117
+ let rImpliesL = true;
118
+ for (const k of keys){
119
+ const keyResult = typeof reducer === "function" ? reducer(k, l[k], r[k], state) : reducer[k](l[k], r[k], state);
120
+ if (isEquality(keyResult)) {
121
+ if (l[k] !== undefined) {
122
+ result[k] = l[k];
123
+ }
124
+ } else if (isDisjoint(keyResult)) {
125
+ if (config.onEmpty === "omit") {
126
+ lImpliesR = false;
127
+ rImpliesL = false;
128
+ } else {
129
+ return empty;
130
+ }
131
+ } else {
132
+ if (keyResult !== undefined) {
133
+ result[k] = keyResult;
134
+ }
135
+ lImpliesR && (lImpliesR = keyResult === l[k]);
136
+ rImpliesL && (rImpliesL = keyResult === r[k]);
137
+ }
138
+ }
139
+ return lImpliesR ? rImpliesL ? equality() : l : rImpliesL ? r : result;
140
+ };
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ flattenBranches: ()=>flattenBranches,
13
+ serializeIfAllowed: ()=>serializeIfAllowed,
14
+ serializeCase: ()=>serializeCase
15
+ });
16
+ const _astTs = require("../parse/string/ast.js");
17
+ const _domainsTs = require("../utils/domains.js");
18
+ const _errorsTs = require("../utils/errors.js");
19
+ const _genericsTs = require("../utils/generics.js");
20
+ const _pathsTs = require("../utils/paths.js");
21
+ const _serializeTs = require("../utils/serialize.js");
22
+ const _composeTs = require("./compose.js");
23
+ const _propsTs = require("./rules/props.js");
24
+ const _rulesTs = require("./rules/rules.js");
25
+ const flattenBranches = (branches, ctx)=>{
26
+ const discriminants = calculateDiscriminants(branches, ctx);
27
+ const indices = branches.map((_, i)=>i);
28
+ return discriminate(branches, indices, discriminants, ctx);
29
+ };
30
+ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
31
+ if (remainingIndices.length === 1) {
32
+ return (0, _rulesTs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
33
+ }
34
+ const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
35
+ if (!bestDiscriminant) {
36
+ return [
37
+ [
38
+ "branches",
39
+ remainingIndices.map((i)=>(0, _rulesTs.flattenBranch)(originalBranches[i], ctx))
40
+ ]
41
+ ];
42
+ }
43
+ const cases = {};
44
+ let caseKey;
45
+ for(caseKey in bestDiscriminant.indexCases){
46
+ const nextIndices = bestDiscriminant.indexCases[caseKey];
47
+ cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
48
+ }
49
+ return [
50
+ [
51
+ "switch",
52
+ {
53
+ path: bestDiscriminant.path,
54
+ kind: bestDiscriminant.kind,
55
+ cases
56
+ }
57
+ ]
58
+ ];
59
+ };
60
+ const discriminantKinds = {
61
+ domain: true,
62
+ subdomain: true,
63
+ value: true
64
+ };
65
+ const calculateDiscriminants = (branches, ctx)=>{
66
+ const discriminants = {
67
+ disjointsByPair: {},
68
+ casesByDisjoint: {}
69
+ };
70
+ const morphBranches = branches.map((branch)=>branchIncludesMorph(branch, ctx.type.meta.scope));
71
+ for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
72
+ for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
73
+ const pairKey = `${lIndex}/${rIndex}`;
74
+ const pairDisjoints = [];
75
+ discriminants.disjointsByPair[pairKey] = pairDisjoints;
76
+ const intersectionState = new _composeTs.IntersectionState(ctx.type, "|");
77
+ (0, _rulesTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
78
+ for(const path in intersectionState.disjoints){
79
+ // designed to match subdomain path segments like "${number}"
80
+ if (path.includes("${")) {
81
+ continue;
82
+ }
83
+ const { l , r , kind } = intersectionState.disjoints[path];
84
+ if (!(0, _genericsTs.isKeyOf)(kind, discriminantKinds)) {
85
+ continue;
86
+ }
87
+ const lSerialized = serializeIfAllowed(kind, l);
88
+ const rSerialized = serializeIfAllowed(kind, r);
89
+ if (lSerialized === undefined || rSerialized === undefined) {
90
+ continue;
91
+ }
92
+ const qualifiedDisjoint = path === "/" ? kind : `${path}/${kind}`;
93
+ pairDisjoints.push(qualifiedDisjoint);
94
+ if (!discriminants.casesByDisjoint[qualifiedDisjoint]) {
95
+ discriminants.casesByDisjoint[qualifiedDisjoint] = {
96
+ [lSerialized]: [
97
+ lIndex
98
+ ],
99
+ [rSerialized]: [
100
+ rIndex
101
+ ]
102
+ };
103
+ continue;
104
+ }
105
+ const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
106
+ if (!(0, _genericsTs.hasKey)(cases, lSerialized)) {
107
+ cases[lSerialized] = [
108
+ lIndex
109
+ ];
110
+ } else if (!cases[lSerialized].includes(lIndex)) {
111
+ cases[lSerialized].push(lIndex);
112
+ }
113
+ if (!(0, _genericsTs.hasKey)(cases, rSerialized)) {
114
+ cases[rSerialized] = [
115
+ rIndex
116
+ ];
117
+ } else if (!cases[rSerialized].includes(rIndex)) {
118
+ cases[rSerialized].push(rIndex);
119
+ }
120
+ }
121
+ if ((morphBranches[lIndex] === true || morphBranches[rIndex] === true) && pairDisjoints.length === 0) {
122
+ return (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`));
123
+ }
124
+ }
125
+ }
126
+ return discriminants;
127
+ };
128
+ const parseQualifiedDisjoint = (qualifiedDisjoint)=>{
129
+ const path = _pathsTs.Path.fromString(qualifiedDisjoint);
130
+ return [
131
+ path,
132
+ path.pop()
133
+ ];
134
+ };
135
+ const findBestDiscriminant = (remainingIndices, discriminants)=>{
136
+ let bestDiscriminant;
137
+ for(let i = 0; i < remainingIndices.length - 1; i++){
138
+ const lIndex = remainingIndices[i];
139
+ for(let j = i + 1; j < remainingIndices.length; j++){
140
+ const rIndex = remainingIndices[j];
141
+ const candidates = discriminants.disjointsByPair[`${lIndex}/${rIndex}`];
142
+ for (const qualifiedDisjoint of candidates){
143
+ const indexCases = discriminants.casesByDisjoint[qualifiedDisjoint];
144
+ const filteredCases = {};
145
+ const defaultCases = [
146
+ ...remainingIndices
147
+ ];
148
+ let score = 0;
149
+ let caseKey;
150
+ for(caseKey in indexCases){
151
+ const filteredIndices = indexCases[caseKey].filter((i)=>{
152
+ const remainingIndex = remainingIndices.indexOf(i);
153
+ if (remainingIndex !== -1) {
154
+ delete defaultCases[remainingIndex];
155
+ return true;
156
+ }
157
+ });
158
+ if (filteredIndices.length === 0) {
159
+ continue;
160
+ }
161
+ filteredCases[caseKey] = filteredIndices;
162
+ score++;
163
+ }
164
+ const defaultCaseKeys = (0, _genericsTs.keysOf)(defaultCases);
165
+ if (defaultCaseKeys.length) {
166
+ const defaultIndices = defaultCaseKeys.map((k)=>parseInt(k));
167
+ filteredCases["default"] = defaultIndices;
168
+ }
169
+ if (!bestDiscriminant || score > bestDiscriminant.score) {
170
+ const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
171
+ bestDiscriminant = {
172
+ path,
173
+ kind,
174
+ indexCases: filteredCases,
175
+ score
176
+ };
177
+ if (score === remainingIndices.length) {
178
+ // if we find a candidate that discriminates all branches, return early
179
+ return bestDiscriminant;
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
185
+ return bestDiscriminant;
186
+ };
187
+ const serializeIfAllowed = (kind, data)=>kind === "value" ? serializeIfPrimitive(data) : `${data}`;
188
+ const serializeIfPrimitive = (data)=>{
189
+ const domain = (0, _domainsTs.domainOf)(data);
190
+ return domain === "object" || domain === "symbol" ? undefined : (0, _serializeTs.serializePrimitive)(data);
191
+ };
192
+ const caseSerializers = {
193
+ value: (data)=>serializeIfPrimitive(data) ?? "default",
194
+ subdomain: _domainsTs.subdomainOf,
195
+ domain: _domainsTs.domainOf
196
+ };
197
+ const serializeCase = (kind, data)=>caseSerializers[kind](data);
198
+ const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.isOptional)(prop) ? prop[1] : prop, $)) : false;
199
+ const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _domainsTs.hasSubdomain)(predicate, "Array") ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "functors", {
6
+ enumerable: true,
7
+ get: ()=>functors
8
+ });
9
+ const functors = {
10
+ Array: (node)=>({
11
+ object: {
12
+ subdomain: [
13
+ "Array",
14
+ node
15
+ ]
16
+ }
17
+ }),
18
+ Set: (node)=>({
19
+ object: {
20
+ subdomain: [
21
+ "Set",
22
+ node
23
+ ]
24
+ }
25
+ }),
26
+ Map: (k, v)=>({
27
+ object: {
28
+ subdomain: [
29
+ "Map",
30
+ k,
31
+ v
32
+ ]
33
+ }
34
+ })
35
+ };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ nodeIntersection: ()=>nodeIntersection,
13
+ intersection: ()=>intersection,
14
+ union: ()=>union,
15
+ flattenType: ()=>flattenType,
16
+ flattenNode: ()=>flattenNode,
17
+ isLiteralNode: ()=>isLiteralNode
18
+ });
19
+ const _astTs = require("../parse/string/ast.js");
20
+ const _errorsTs = require("../utils/errors.js");
21
+ const _genericsTs = require("../utils/generics.js");
22
+ const _pathsTs = require("../utils/paths.js");
23
+ const _composeTs = require("./compose.js");
24
+ const _predicateTs = require("./predicate.js");
25
+ const nodeIntersection = (l, r, state)=>{
26
+ state.domain = undefined;
27
+ const lResolution = state.type.meta.scope.resolveNode(l);
28
+ const rResolution = state.type.meta.scope.resolveNode(r);
29
+ const result = resolutionIntersection(lResolution, rResolution, state);
30
+ if (typeof result === "object" && !(0, _genericsTs.hasKeys)(result)) {
31
+ return (0, _genericsTs.hasKeys)(state.disjoints) ? (0, _composeTs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsTs.keysOf)(lResolution), (0, _genericsTs.keysOf)(rResolution));
32
+ }
33
+ return result === lResolution ? l : result === rResolution ? r : result;
34
+ };
35
+ const resolutionIntersection = (0, _composeTs.composeKeyedIntersection)((domain, l, r, context)=>{
36
+ if (l === undefined) {
37
+ return r === undefined ? (0, _composeTs.throwUndefinedOperandsError)() : undefined;
38
+ }
39
+ if (r === undefined) {
40
+ return undefined;
41
+ }
42
+ return (0, _predicateTs.predicateIntersection)(domain, l, r, context);
43
+ }, {
44
+ onEmpty: "omit"
45
+ });
46
+ const intersection = (l, r, type)=>{
47
+ const state = new _composeTs.IntersectionState(type, "&");
48
+ const result = nodeIntersection(l, r, state);
49
+ return (0, _composeTs.isDisjoint)(result) ? (0, _errorsTs.throwParseError)((0, _astTs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeTs.isEquality)(result) ? l : result;
50
+ };
51
+ const union = (l, r, type)=>{
52
+ const lResolution = type.meta.scope.resolveNode(l);
53
+ const rResolution = type.meta.scope.resolveNode(r);
54
+ const result = {};
55
+ const domains = (0, _genericsTs.keysOf)({
56
+ ...lResolution,
57
+ ...rResolution
58
+ });
59
+ for (const domain of domains){
60
+ result[domain] = (0, _genericsTs.hasKey)(lResolution, domain) ? (0, _genericsTs.hasKey)(rResolution, domain) ? (0, _predicateTs.predicateUnion)(domain, lResolution[domain], rResolution[domain], type) : lResolution[domain] : (0, _genericsTs.hasKey)(rResolution, domain) ? rResolution[domain] : (0, _composeTs.throwUndefinedOperandsError)();
61
+ }
62
+ return result;
63
+ };
64
+ const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "subdomain" || flatPredicate[0][0] === "value");
65
+ const flattenType = (type)=>{
66
+ const ctx = {
67
+ type,
68
+ path: new _pathsTs.Path(),
69
+ lastDomain: "undefined"
70
+ };
71
+ return flattenNode(type.node, ctx);
72
+ };
73
+ const flattenNode = (node, ctx)=>{
74
+ if (typeof node === "string") {
75
+ return ctx.type.meta.scope.resolve(node).flat;
76
+ }
77
+ const domains = (0, _genericsTs.keysOf)(node);
78
+ if (domains.length === 1) {
79
+ const domain = domains[0];
80
+ const predicate = node[domain];
81
+ if (predicate === true) {
82
+ return domain;
83
+ }
84
+ ctx.lastDomain = domain;
85
+ const flatPredicate = (0, _predicateTs.flattenPredicate)(predicate, ctx);
86
+ return hasImpliedDomain(flatPredicate) ? flatPredicate : [
87
+ [
88
+ "domain",
89
+ domain
90
+ ],
91
+ ...flatPredicate
92
+ ];
93
+ }
94
+ const result = {};
95
+ for (const domain1 of domains){
96
+ ctx.lastDomain = domain1;
97
+ result[domain1] = (0, _predicateTs.flattenPredicate)(node[domain1], ctx);
98
+ }
99
+ return [
100
+ [
101
+ "domains",
102
+ result
103
+ ]
104
+ ];
105
+ };
106
+ const isLiteralNode = (node, domain)=>{
107
+ return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
108
+ };
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ comparePredicates: ()=>comparePredicates,
13
+ predicateIntersection: ()=>predicateIntersection,
14
+ predicateUnion: ()=>predicateUnion,
15
+ flattenPredicate: ()=>flattenPredicate,
16
+ isLiteralCondition: ()=>isLiteralCondition,
17
+ resolutionExtendsDomain: ()=>resolutionExtendsDomain
18
+ });
19
+ const _domainsTs = require("../utils/domains.js");
20
+ const _genericsTs = require("../utils/generics.js");
21
+ const _branchesTs = require("./branches.js");
22
+ const _composeTs = require("./compose.js");
23
+ const _discriminateTs = require("./discriminate.js");
24
+ const _rulesTs = require("./rules/rules.js");
25
+ const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
26
+ const comparePredicates = (l, r, context)=>{
27
+ if (l === true && r === true) {
28
+ return (0, _composeTs.equality)();
29
+ }
30
+ if (!(0, _domainsTs.hasSubdomain)(l, "Array") && !(0, _domainsTs.hasSubdomain)(r, "Array")) {
31
+ const result = (0, _rulesTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
32
+ return result === l ? l : result === r ? r : result;
33
+ }
34
+ const lBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(l));
35
+ const rBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(r));
36
+ const comparison = (0, _branchesTs.compareBranches)(lBranches, rBranches, context);
37
+ if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
38
+ return (0, _composeTs.equality)();
39
+ }
40
+ if (comparison.lSubconditionsOfR.length + comparison.equalities.length === lBranches.length) {
41
+ return l;
42
+ }
43
+ if (comparison.rSubconditionsOfL.length + comparison.equalities.length === rBranches.length) {
44
+ return r;
45
+ }
46
+ return comparison;
47
+ };
48
+ const predicateIntersection = (domain, l, r, state)=>{
49
+ state.domain = domain;
50
+ const comparison = comparePredicates(l, r, state);
51
+ if (!(0, _branchesTs.isBranchComparison)(comparison)) {
52
+ return comparison;
53
+ }
54
+ return (0, _genericsTs.collapseIfSingleton)([
55
+ ...comparison.distinctIntersections,
56
+ ...comparison.equalities.map((indices)=>comparison.lConditions[indices[0]]),
57
+ ...comparison.lSubconditionsOfR.map((lIndex)=>comparison.lConditions[lIndex]),
58
+ ...comparison.rSubconditionsOfL.map((rIndex)=>comparison.rConditions[rIndex])
59
+ ]);
60
+ };
61
+ const predicateUnion = (domain, l, r, type)=>{
62
+ const state = new _composeTs.IntersectionState(type, "|");
63
+ const comparison = comparePredicates(l, r, state);
64
+ if (!(0, _branchesTs.isBranchComparison)(comparison)) {
65
+ return (0, _composeTs.isEquality)(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
66
+ // and can be simplified to a non-literal boolean.
67
+ domain === "boolean" ? true : [
68
+ emptyRulesIfTrue(l),
69
+ emptyRulesIfTrue(r)
70
+ ];
71
+ }
72
+ return (0, _genericsTs.collapseIfSingleton)([
73
+ ...comparison.lConditions.filter((_, lIndex)=>!comparison.lSubconditionsOfR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
74
+ ...comparison.rConditions.filter((_, rIndex)=>!comparison.rSubconditionsOfL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
75
+ ]);
76
+ };
77
+ const flattenPredicate = (predicate, context)=>{
78
+ if (predicate === true) {
79
+ return [];
80
+ }
81
+ return (0, _domainsTs.hasSubdomain)(predicate, "Array") ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _rulesTs.flattenBranch)(predicate, context);
82
+ };
83
+ const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
84
+ const resolutionExtendsDomain = (resolution, domain)=>{
85
+ const domains = (0, _genericsTs.keysOf)(resolution);
86
+ return domains.length === 1 && domains[0] === domain;
87
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ classIntersection: ()=>classIntersection,
13
+ checkClass: ()=>checkClass
14
+ });
15
+ const _composeTs = require("../compose.js");
16
+ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>l === r ? (0, _composeTs.equality)() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r));
17
+ const checkClass = (data, expectedClass, state)=>{
18
+ if (!(data instanceof expectedClass)) {
19
+ state.problems.add("class", data, expectedClass);
20
+ }
21
+ };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ collapsibleListUnion: ()=>collapsibleListUnion,
13
+ listUnion: ()=>listUnion
14
+ });
15
+ const _domainsTs = require("../../utils/domains.js");
16
+ const _composeTs = require("../compose.js");
17
+ const collapsibleListUnion = (l, r)=>{
18
+ if ((0, _domainsTs.hasSubdomain)(l, "Array")) {
19
+ if ((0, _domainsTs.hasSubdomain)(r, "Array")) {
20
+ const result = listUnion(l, r);
21
+ return result.length === l.length ? result.length === r.length ? (0, _composeTs.equality)() : l : result.length === r.length ? r : result;
22
+ }
23
+ return l.includes(r) ? l : [
24
+ ...l,
25
+ r
26
+ ];
27
+ }
28
+ if ((0, _domainsTs.hasSubdomain)(r, "Array")) {
29
+ return r.includes(l) ? r : [
30
+ ...r,
31
+ l
32
+ ];
33
+ }
34
+ return l === r ? (0, _composeTs.equality)() : [
35
+ l,
36
+ r
37
+ ];
38
+ };
39
+ const listUnion = (l, r)=>{
40
+ const result = [
41
+ ...l
42
+ ];
43
+ for (const expression of r){
44
+ if (!l.includes(expression)) {
45
+ result.push(expression);
46
+ }
47
+ }
48
+ return result;
49
+ };
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ divisorIntersection: ()=>divisorIntersection,
13
+ checkDivisor: ()=>checkDivisor
14
+ });
15
+ const _composeTs = require("../compose.js");
16
+ const divisorIntersection = (0, _composeTs.composeIntersection)((l, r)=>l === r ? (0, _composeTs.equality)() : Math.abs(l * r / greatestCommonDivisor(l, r)));
17
+ // https://en.wikipedia.org/wiki/Euclidean_algorithm
18
+ const greatestCommonDivisor = (l, r)=>{
19
+ let previous;
20
+ let greatestCommonDivisor = l;
21
+ let current = r;
22
+ while(current !== 0){
23
+ previous = current;
24
+ current = greatestCommonDivisor % current;
25
+ greatestCommonDivisor = previous;
26
+ }
27
+ return greatestCommonDivisor;
28
+ };
29
+ const checkDivisor = (data, divisor, state)=>{
30
+ if (data % divisor !== 0) {
31
+ state.problems.add("divisor", data, divisor);
32
+ }
33
+ };