arktype 1.0.16-alpha → 1.0.17-alpha

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 (247) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/src/main.js +38 -0
  3. package/dist/cjs/src/nodes/branch.js +140 -0
  4. package/dist/cjs/src/nodes/compose.js +157 -0
  5. package/dist/{nodes → cjs/src/nodes}/discriminate.js +92 -100
  6. package/dist/cjs/src/nodes/node.js +139 -0
  7. package/dist/cjs/src/nodes/predicate.js +86 -0
  8. package/dist/cjs/src/nodes/rules/class.js +25 -0
  9. package/dist/cjs/src/nodes/rules/collapsibleSet.js +48 -0
  10. package/dist/cjs/src/nodes/rules/divisor.js +29 -0
  11. package/dist/cjs/src/nodes/rules/props.js +160 -0
  12. package/dist/cjs/src/nodes/rules/range.js +105 -0
  13. package/dist/cjs/src/nodes/rules/regex.js +26 -0
  14. package/dist/cjs/src/nodes/rules/rules.js +123 -0
  15. package/dist/cjs/src/parse/ast/ast.js +4 -0
  16. package/dist/cjs/src/parse/ast/bound.js +16 -0
  17. package/dist/cjs/src/parse/ast/config.js +13 -0
  18. package/dist/cjs/src/parse/ast/distributableFunction.js +48 -0
  19. package/dist/cjs/src/parse/ast/divisor.js +9 -0
  20. package/dist/cjs/src/parse/ast/intersection.js +30 -0
  21. package/dist/cjs/src/parse/ast/keyof.js +111 -0
  22. package/dist/cjs/src/parse/ast/morph.js +58 -0
  23. package/dist/cjs/src/parse/ast/narrow.js +22 -0
  24. package/dist/cjs/src/parse/ast/node.js +4 -0
  25. package/dist/cjs/src/parse/ast/tuple.js +94 -0
  26. package/dist/cjs/src/parse/ast/union.js +9 -0
  27. package/dist/cjs/src/parse/ast/utils.js +4 -0
  28. package/dist/cjs/src/parse/definition.js +61 -0
  29. package/dist/cjs/src/parse/record.js +37 -0
  30. package/dist/cjs/src/parse/string/reduce/dynamic.js +167 -0
  31. package/dist/cjs/src/parse/string/reduce/shared.js +23 -0
  32. package/dist/cjs/src/parse/string/reduce/static.js +4 -0
  33. package/dist/cjs/src/parse/string/shift/operand/enclosed.js +54 -0
  34. package/dist/cjs/src/parse/string/shift/operand/operand.js +11 -0
  35. package/dist/cjs/src/parse/string/shift/operand/unenclosed.js +53 -0
  36. package/dist/cjs/src/parse/string/shift/operator/bounds.js +87 -0
  37. package/dist/cjs/src/parse/string/shift/operator/divisor.js +35 -0
  38. package/dist/cjs/src/parse/string/shift/operator/operator.js +26 -0
  39. package/dist/cjs/src/parse/string/shift/scanner.js +114 -0
  40. package/dist/cjs/src/parse/string/string.js +43 -0
  41. package/dist/cjs/src/scopes/ark.js +45 -0
  42. package/dist/cjs/src/scopes/cache.js +52 -0
  43. package/dist/cjs/src/scopes/expressions.js +29 -0
  44. package/dist/cjs/src/scopes/jsObjects.js +93 -0
  45. package/dist/cjs/src/scopes/scope.js +323 -0
  46. package/dist/cjs/src/scopes/tsKeywords.js +113 -0
  47. package/dist/cjs/src/scopes/type.js +50 -0
  48. package/dist/cjs/src/scopes/validation/creditCard.js +47 -0
  49. package/dist/cjs/src/scopes/validation/date.js +62 -0
  50. package/dist/cjs/src/scopes/validation/validation.js +108 -0
  51. package/dist/cjs/src/traverse/problems.js +281 -0
  52. package/dist/cjs/src/traverse/traverse.js +390 -0
  53. package/dist/cjs/src/utils/chainableNoOpProxy.js +23 -0
  54. package/dist/cjs/src/utils/data.js +53 -0
  55. package/dist/cjs/src/utils/domains.js +30 -0
  56. package/dist/cjs/src/utils/errors.js +26 -0
  57. package/dist/cjs/src/utils/freeze.js +15 -0
  58. package/dist/cjs/src/utils/generics.js +63 -0
  59. package/dist/cjs/src/utils/numericLiterals.js +75 -0
  60. package/dist/cjs/src/utils/objectKinds.js +69 -0
  61. package/dist/cjs/src/utils/paths.js +32 -0
  62. package/dist/cjs/src/utils/serialize.js +74 -0
  63. package/dist/cjs/src/utils/unionToTuple.js +4 -0
  64. package/dist/mjs/package.json +3 -0
  65. package/dist/mjs/src/main.js +8 -0
  66. package/dist/mjs/src/nodes/branch.js +123 -0
  67. package/dist/mjs/src/nodes/compose.js +135 -0
  68. package/dist/mjs/src/nodes/discriminate.js +247 -0
  69. package/dist/mjs/src/nodes/node.js +117 -0
  70. package/dist/mjs/src/nodes/predicate.js +69 -0
  71. package/dist/mjs/src/nodes/rules/class.js +11 -0
  72. package/dist/mjs/src/nodes/rules/collapsibleSet.js +34 -0
  73. package/dist/mjs/src/nodes/rules/divisor.js +15 -0
  74. package/dist/mjs/src/nodes/rules/props.js +142 -0
  75. package/dist/mjs/src/nodes/rules/range.js +86 -0
  76. package/dist/mjs/src/nodes/rules/regex.js +11 -0
  77. package/dist/mjs/src/nodes/rules/rules.js +106 -0
  78. package/dist/mjs/src/parse/ast/ast.js +1 -0
  79. package/dist/mjs/src/parse/ast/bound.js +2 -0
  80. package/dist/mjs/src/parse/ast/config.js +9 -0
  81. package/dist/mjs/src/parse/ast/distributableFunction.js +34 -0
  82. package/dist/mjs/src/parse/ast/divisor.js +1 -0
  83. package/dist/mjs/src/parse/ast/intersection.js +16 -0
  84. package/dist/mjs/src/parse/ast/keyof.js +103 -0
  85. package/dist/mjs/src/parse/ast/morph.js +44 -0
  86. package/dist/mjs/src/parse/ast/narrow.js +14 -0
  87. package/dist/mjs/src/parse/ast/node.js +1 -0
  88. package/dist/mjs/src/parse/ast/tuple.js +80 -0
  89. package/dist/mjs/src/parse/ast/union.js +1 -0
  90. package/dist/mjs/src/parse/ast/utils.js +1 -0
  91. package/dist/mjs/src/parse/definition.js +46 -0
  92. package/dist/mjs/src/parse/record.js +29 -0
  93. package/dist/mjs/src/parse/string/reduce/dynamic.js +159 -0
  94. package/dist/mjs/src/parse/string/reduce/shared.js +6 -0
  95. package/dist/mjs/src/parse/string/reduce/static.js +1 -0
  96. package/dist/mjs/src/parse/string/shift/operand/enclosed.js +39 -0
  97. package/dist/mjs/src/parse/string/shift/operand/operand.js +3 -0
  98. package/dist/mjs/src/parse/string/shift/operand/unenclosed.js +36 -0
  99. package/dist/mjs/src/parse/string/shift/operator/bounds.js +70 -0
  100. package/dist/mjs/src/parse/string/shift/operator/divisor.js +21 -0
  101. package/dist/mjs/src/parse/string/shift/operator/operator.js +13 -0
  102. package/dist/mjs/src/parse/string/shift/scanner.js +106 -0
  103. package/dist/mjs/src/parse/string/string.js +28 -0
  104. package/dist/mjs/src/scopes/ark.js +28 -0
  105. package/dist/mjs/src/scopes/cache.js +38 -0
  106. package/dist/{scopes → mjs/src/scopes}/expressions.js +9 -20
  107. package/dist/mjs/src/scopes/jsObjects.js +82 -0
  108. package/dist/mjs/src/scopes/scope.js +303 -0
  109. package/dist/mjs/src/scopes/tsKeywords.js +116 -0
  110. package/dist/mjs/src/scopes/type.js +35 -0
  111. package/dist/{scopes → mjs/src/scopes}/validation/creditCard.js +11 -15
  112. package/dist/{scopes → mjs/src/scopes}/validation/date.js +16 -21
  113. package/dist/mjs/src/scopes/validation/validation.js +112 -0
  114. package/dist/mjs/src/traverse/problems.js +260 -0
  115. package/dist/mjs/src/traverse/traverse.js +373 -0
  116. package/dist/{utils → mjs/src/utils}/chainableNoOpProxy.js +2 -6
  117. package/dist/mjs/src/utils/data.js +38 -0
  118. package/dist/mjs/src/utils/domains.js +15 -0
  119. package/dist/mjs/src/utils/errors.js +10 -0
  120. package/dist/mjs/src/utils/freeze.js +7 -0
  121. package/dist/mjs/src/utils/generics.js +38 -0
  122. package/dist/mjs/src/utils/numericLiterals.js +72 -0
  123. package/dist/mjs/src/utils/objectKinds.js +55 -0
  124. package/dist/{utils → mjs/src/utils}/paths.js +3 -8
  125. package/dist/mjs/src/utils/serialize.js +58 -0
  126. package/dist/mjs/src/utils/unionToTuple.js +1 -0
  127. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  128. package/package.json +13 -5
  129. package/dist/main.js +0 -29
  130. package/dist/nodes/branch.js +0 -135
  131. package/dist/nodes/compose.js +0 -99
  132. package/dist/nodes/node.js +0 -134
  133. package/dist/nodes/predicate.js +0 -88
  134. package/dist/nodes/rules/class.js +0 -23
  135. package/dist/nodes/rules/collapsibleSet.js +0 -34
  136. package/dist/nodes/rules/divisor.js +0 -19
  137. package/dist/nodes/rules/props.js +0 -130
  138. package/dist/nodes/rules/range.js +0 -108
  139. package/dist/nodes/rules/regex.js +0 -17
  140. package/dist/nodes/rules/rules.js +0 -102
  141. package/dist/parse/ast/ast.js +0 -2
  142. package/dist/parse/ast/bound.js +0 -7
  143. package/dist/parse/ast/config.js +0 -14
  144. package/dist/parse/ast/distributableFunction.js +0 -40
  145. package/dist/parse/ast/divisor.js +0 -5
  146. package/dist/parse/ast/intersection.js +0 -21
  147. package/dist/parse/ast/keyof.js +0 -107
  148. package/dist/parse/ast/morph.js +0 -50
  149. package/dist/parse/ast/narrow.js +0 -17
  150. package/dist/parse/ast/node.js +0 -2
  151. package/dist/parse/ast/tuple.js +0 -72
  152. package/dist/parse/ast/union.js +0 -5
  153. package/dist/parse/ast/utils.js +0 -2
  154. package/dist/parse/definition.js +0 -47
  155. package/dist/parse/record.js +0 -31
  156. package/dist/parse/string/reduce/dynamic.js +0 -159
  157. package/dist/parse/string/reduce/shared.js +0 -13
  158. package/dist/parse/string/reduce/static.js +0 -2
  159. package/dist/parse/string/shift/operand/enclosed.js +0 -37
  160. package/dist/parse/string/shift/operand/operand.js +0 -15
  161. package/dist/parse/string/shift/operand/unenclosed.js +0 -45
  162. package/dist/parse/string/shift/operator/bounds.js +0 -83
  163. package/dist/parse/string/shift/operator/divisor.js +0 -23
  164. package/dist/parse/string/shift/operator/operator.js +0 -34
  165. package/dist/parse/string/shift/scanner.js +0 -97
  166. package/dist/parse/string/string.js +0 -35
  167. package/dist/scopes/ark.js +0 -27
  168. package/dist/scopes/cache.js +0 -28
  169. package/dist/scopes/jsObjects.js +0 -20
  170. package/dist/scopes/scope.js +0 -184
  171. package/dist/scopes/tsKeywords.js +0 -49
  172. package/dist/scopes/type.js +0 -40
  173. package/dist/scopes/validation/validation.js +0 -74
  174. package/dist/traverse/problems.js +0 -234
  175. package/dist/traverse/traverse.js +0 -330
  176. package/dist/utils/data.js +0 -39
  177. package/dist/utils/domains.js +0 -27
  178. package/dist/utils/errors.js +0 -17
  179. package/dist/utils/freeze.js +0 -16
  180. package/dist/utils/generics.js +0 -53
  181. package/dist/utils/numericLiterals.js +0 -88
  182. package/dist/utils/objectKinds.js +0 -69
  183. package/dist/utils/serialize.js +0 -71
  184. package/dist/utils/unionToTuple.js +0 -2
  185. /package/dist/{package.json → cjs/package.json} +0 -0
  186. /package/dist/{main.d.ts → types/main.d.ts} +0 -0
  187. /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
  188. /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
  189. /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
  190. /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
  191. /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
  192. /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
  193. /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
  194. /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
  195. /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
  196. /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
  197. /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
  198. /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
  199. /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
  200. /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
  201. /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
  202. /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
  203. /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
  204. /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
  205. /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
  206. /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
  207. /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
  208. /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
  209. /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
  211. /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{parse → types/parse}/definition.d.ts +0 -0
  213. /package/dist/{parse → types/parse}/record.d.ts +0 -0
  214. /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
  225. /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
  226. /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
  227. /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
  228. /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
  230. /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
  232. /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
  236. /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
  237. /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{utils → types/utils}/data.d.ts +0 -0
  239. /package/dist/{utils → types/utils}/domains.d.ts +0 -0
  240. /package/dist/{utils → types/utils}/errors.d.ts +0 -0
  241. /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
  242. /package/dist/{utils → types/utils}/generics.d.ts +0 -0
  243. /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{utils → types/utils}/paths.d.ts +0 -0
  246. /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
  247. /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
@@ -0,0 +1,135 @@
1
+ function _checkPrivateRedeclaration(obj, privateCollection) {
2
+ if (privateCollection.has(obj)) {
3
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
4
+ }
5
+ }
6
+ function _classApplyDescriptorGet(receiver, descriptor) {
7
+ if (descriptor.get) {
8
+ return descriptor.get.call(receiver);
9
+ }
10
+ return descriptor.value;
11
+ }
12
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
13
+ if (descriptor.set) {
14
+ descriptor.set.call(receiver, value);
15
+ } else {
16
+ if (!descriptor.writable) {
17
+ throw new TypeError("attempted to set read only private field");
18
+ }
19
+ descriptor.value = value;
20
+ }
21
+ }
22
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
23
+ if (!privateMap.has(receiver)) {
24
+ throw new TypeError("attempted to " + action + " private field on non-instance");
25
+ }
26
+ return privateMap.get(receiver);
27
+ }
28
+ function _classPrivateFieldGet(receiver, privateMap) {
29
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
30
+ return _classApplyDescriptorGet(receiver, descriptor);
31
+ }
32
+ function _classPrivateFieldInit(obj, privateMap, value) {
33
+ _checkPrivateRedeclaration(obj, privateMap);
34
+ privateMap.set(obj, value);
35
+ }
36
+ function _classPrivateFieldSet(receiver, privateMap, value) {
37
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
38
+ _classApplyDescriptorSet(receiver, descriptor, value);
39
+ return value;
40
+ }
41
+ function _defineProperty(obj, key, value) {
42
+ if (key in obj) {
43
+ Object.defineProperty(obj, key, {
44
+ value: value,
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true
48
+ });
49
+ } else {
50
+ obj[key] = value;
51
+ }
52
+ return obj;
53
+ }
54
+ import { throwInternalError } from "../utils/errors.js";
55
+ import { objectKeysOf } from "../utils/generics.js";
56
+ import { Path } from "../utils/paths.js";
57
+ import { stringify } from "../utils/serialize.js";
58
+ export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwInternalError(undefinedOperandsMessage) : r : r === undefined ? l : reducer(l, r, state);
59
+ export const undefinedOperandsMessage = `Unexpected operation two undefined operands`;
60
+ export const disjointDescriptionWriters = {
61
+ domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
62
+ range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
63
+ class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
64
+ tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
65
+ value: ({ l , r })=>`literal values ${stringify(l)} and ${stringify(r)}`,
66
+ leftAssignability: ({ l , r })=>`literal value ${stringify(l.value)} and ${stringify(r)}`,
67
+ rightAssignability: ({ l , r })=>`literal value ${stringify(r.value)} and ${stringify(l)}`,
68
+ union: ({ l , r })=>`branches ${stringify(l)} and branches ${stringify(r)}`
69
+ };
70
+ export 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";
71
+ var _disjoints = /*#__PURE__*/ new WeakMap();
72
+ export class IntersectionState {
73
+ get disjoints() {
74
+ return _classPrivateFieldGet(this, _disjoints);
75
+ }
76
+ addDisjoint(kind, l, r) {
77
+ _classPrivateFieldGet(this, _disjoints)[`${this.path}`] = {
78
+ kind,
79
+ l,
80
+ r
81
+ };
82
+ return empty;
83
+ }
84
+ constructor(type, lastOperator){
85
+ _defineProperty(this, "type", void 0);
86
+ _defineProperty(this, "lastOperator", void 0);
87
+ _defineProperty(this, "path", void 0);
88
+ _defineProperty(this, "domain", void 0);
89
+ _classPrivateFieldInit(this, _disjoints, {
90
+ writable: true,
91
+ value: void 0
92
+ });
93
+ this.type = type;
94
+ this.lastOperator = lastOperator;
95
+ this.path = new Path();
96
+ _classPrivateFieldSet(this, _disjoints, {});
97
+ }
98
+ }
99
+ const empty = Symbol("empty");
100
+ export const anonymousDisjoint = ()=>empty;
101
+ export const isDisjoint = (result)=>result === empty;
102
+ const equal = Symbol("equal");
103
+ export const equality = ()=>equal;
104
+ export const isEquality = (result)=>result === equal;
105
+ export const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
106
+ const result = {};
107
+ const keys = objectKeysOf({
108
+ ...l,
109
+ ...r
110
+ });
111
+ let lImpliesR = true;
112
+ let rImpliesL = true;
113
+ for (const k of keys){
114
+ const keyResult = typeof reducer === "function" ? reducer(k, l[k], r[k], state) : reducer[k](l[k], r[k], state);
115
+ if (isEquality(keyResult)) {
116
+ if (l[k] !== undefined) {
117
+ result[k] = l[k];
118
+ }
119
+ } else if (isDisjoint(keyResult)) {
120
+ if (config.onEmpty === "omit") {
121
+ lImpliesR = false;
122
+ rImpliesL = false;
123
+ } else {
124
+ return empty;
125
+ }
126
+ } else {
127
+ if (keyResult !== undefined) {
128
+ result[k] = keyResult;
129
+ }
130
+ lImpliesR && (lImpliesR = keyResult === l[k]);
131
+ rImpliesL && (rImpliesL = keyResult === r[k]);
132
+ }
133
+ }
134
+ return lImpliesR ? rImpliesL ? equality() : l : rImpliesL ? r : result;
135
+ };
@@ -0,0 +1,247 @@
1
+ import { writeUndiscriminatableMorphUnionMessage } from "../parse/ast/union.js";
2
+ import { domainOf } from "../utils/domains.js";
3
+ import { throwInternalError, throwParseError } from "../utils/errors.js";
4
+ import { isKeyOf, keyCount, objectKeysOf } from "../utils/generics.js";
5
+ import { getExactConstructorObjectKind, isArray, objectKindOf } from "../utils/objectKinds.js";
6
+ import { Path } from "../utils/paths.js";
7
+ import { serializePrimitive } from "../utils/serialize.js";
8
+ import { branchIntersection, flattenBranch } from "./branch.js";
9
+ import { IntersectionState } from "./compose.js";
10
+ import { mappedKeys, propToNode } from "./rules/props.js";
11
+ export const flattenBranches = (branches, ctx)=>{
12
+ const discriminants = calculateDiscriminants(branches, ctx);
13
+ const indices = branches.map((_, i)=>i);
14
+ return discriminate(branches, indices, discriminants, ctx);
15
+ };
16
+ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
17
+ if (remainingIndices.length === 1) {
18
+ return flattenBranch(originalBranches[remainingIndices[0]], ctx);
19
+ }
20
+ const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
21
+ if (!bestDiscriminant) {
22
+ return [
23
+ [
24
+ "branches",
25
+ remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.scope) ? throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`)) : flattenBranch(originalBranches[i], ctx))
26
+ ]
27
+ ];
28
+ }
29
+ const cases = {};
30
+ for(const caseKey in bestDiscriminant.indexCases){
31
+ const nextIndices = bestDiscriminant.indexCases[caseKey];
32
+ cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
33
+ if (caseKey !== "default") {
34
+ pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
35
+ }
36
+ }
37
+ return [
38
+ [
39
+ "switch",
40
+ {
41
+ path: bestDiscriminant.path,
42
+ kind: bestDiscriminant.kind,
43
+ cases
44
+ }
45
+ ]
46
+ ];
47
+ };
48
+ const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
49
+ for(let i = 0; i < entries.length; i++){
50
+ const [k, v] = entries[i];
51
+ if (!segments.length) {
52
+ if (discriminant.kind === "domain") {
53
+ if (k === "domain" || k === "domains") {
54
+ entries.splice(i, 1);
55
+ return;
56
+ } else if (k === "class" || k === "value") {
57
+ // these keys imply a domain, but also add additional
58
+ // information, so can't be pruned
59
+ return;
60
+ }
61
+ } else if (discriminant.kind === k) {
62
+ entries.splice(i, 1);
63
+ return;
64
+ }
65
+ } else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
66
+ if (typeof v[1] === "string") {
67
+ if (discriminant.kind !== "domain") {
68
+ return throwInternalPruneFailure(discriminant);
69
+ }
70
+ entries.splice(i, 1);
71
+ return;
72
+ }
73
+ pruneDiscriminant(v[1], segments.slice(1), discriminant, ctx);
74
+ if (v[1].length === 0) {
75
+ entries.splice(i, 1);
76
+ }
77
+ return;
78
+ }
79
+ // check for branch keys, which must be traversed even if there are no
80
+ // segments left
81
+ if (k === "domains") {
82
+ /* c8 ignore next */ if (keyCount(v) !== 1 || !v.object) {
83
+ return throwInternalPruneFailure(discriminant);
84
+ }
85
+ pruneDiscriminant(v.object, segments, discriminant, ctx);
86
+ return;
87
+ } else if (k === "switch") {
88
+ for(const caseKey in v.cases){
89
+ pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
90
+ }
91
+ return;
92
+ } else if (k === "branches") {
93
+ for (const branch of v){
94
+ pruneDiscriminant(branch, segments, discriminant, ctx);
95
+ }
96
+ return;
97
+ }
98
+ }
99
+ return throwInternalPruneFailure(discriminant);
100
+ };
101
+ const throwInternalPruneFailure = (discriminant)=>throwInternalError(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
102
+ const discriminantKinds = {
103
+ domain: true,
104
+ class: true,
105
+ value: true
106
+ };
107
+ const calculateDiscriminants = (branches, ctx)=>{
108
+ const discriminants = {
109
+ disjointsByPair: {},
110
+ casesByDisjoint: {}
111
+ };
112
+ for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
113
+ for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
114
+ const pairKey = `${lIndex}/${rIndex}`;
115
+ const pairDisjoints = [];
116
+ discriminants.disjointsByPair[pairKey] = pairDisjoints;
117
+ const intersectionState = new IntersectionState(ctx.type, "|");
118
+ branchIntersection(branches[lIndex], branches[rIndex], intersectionState);
119
+ for(const path in intersectionState.disjoints){
120
+ if (path.includes(mappedKeys.index)) {
121
+ continue;
122
+ }
123
+ const { l , r , kind } = intersectionState.disjoints[path];
124
+ if (!isKeyOf(kind, discriminantKinds)) {
125
+ continue;
126
+ }
127
+ const lSerialized = serializeDefinitionIfAllowed(kind, l);
128
+ const rSerialized = serializeDefinitionIfAllowed(kind, r);
129
+ if (lSerialized === undefined || rSerialized === undefined) {
130
+ continue;
131
+ }
132
+ const qualifiedDisjoint = path === "/" ? kind : `${path}/${kind}`;
133
+ pairDisjoints.push(qualifiedDisjoint);
134
+ if (!discriminants.casesByDisjoint[qualifiedDisjoint]) {
135
+ discriminants.casesByDisjoint[qualifiedDisjoint] = {
136
+ [lSerialized]: [
137
+ lIndex
138
+ ],
139
+ [rSerialized]: [
140
+ rIndex
141
+ ]
142
+ };
143
+ continue;
144
+ }
145
+ const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
146
+ const existingLBranch = cases[lSerialized];
147
+ if (!existingLBranch) {
148
+ cases[lSerialized] = [
149
+ lIndex
150
+ ];
151
+ } else if (!existingLBranch.includes(lIndex)) {
152
+ existingLBranch.push(lIndex);
153
+ }
154
+ const existingRBranch = cases[rSerialized];
155
+ if (!existingRBranch) {
156
+ cases[rSerialized] = [
157
+ rIndex
158
+ ];
159
+ } else if (!existingRBranch.includes(rIndex)) {
160
+ existingRBranch.push(rIndex);
161
+ }
162
+ }
163
+ }
164
+ }
165
+ return discriminants;
166
+ };
167
+ const parseQualifiedDisjoint = (qualifiedDisjoint)=>{
168
+ const path = Path.fromString(qualifiedDisjoint);
169
+ return [
170
+ path,
171
+ path.pop()
172
+ ];
173
+ };
174
+ const findBestDiscriminant = (remainingIndices, discriminants)=>{
175
+ let bestDiscriminant;
176
+ for(let i = 0; i < remainingIndices.length - 1; i++){
177
+ const lIndex = remainingIndices[i];
178
+ for(let j = i + 1; j < remainingIndices.length; j++){
179
+ const rIndex = remainingIndices[j];
180
+ const candidates = discriminants.disjointsByPair[`${lIndex}/${rIndex}`];
181
+ for (const qualifiedDisjoint of candidates){
182
+ const indexCases = discriminants.casesByDisjoint[qualifiedDisjoint];
183
+ const filteredCases = {};
184
+ const defaultCases = [
185
+ ...remainingIndices
186
+ ];
187
+ let score = 0;
188
+ for(const caseKey in indexCases){
189
+ const filteredIndices = indexCases[caseKey].filter((i)=>{
190
+ const remainingIndex = remainingIndices.indexOf(i);
191
+ if (remainingIndex !== -1) {
192
+ delete defaultCases[remainingIndex];
193
+ return true;
194
+ }
195
+ });
196
+ if (filteredIndices.length === 0) {
197
+ continue;
198
+ }
199
+ filteredCases[caseKey] = filteredIndices;
200
+ score++;
201
+ }
202
+ const defaultCaseKeys = objectKeysOf(defaultCases);
203
+ if (defaultCaseKeys.length) {
204
+ filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
205
+ }
206
+ if (!bestDiscriminant || score > bestDiscriminant.score) {
207
+ const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
208
+ bestDiscriminant = {
209
+ path,
210
+ kind,
211
+ indexCases: filteredCases,
212
+ score
213
+ };
214
+ if (score === remainingIndices.length) {
215
+ // if we find a candidate that discriminates all branches, return early
216
+ return bestDiscriminant;
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+ return bestDiscriminant;
223
+ };
224
+ export const serializeDefinitionIfAllowed = (kind, definition)=>{
225
+ switch(kind){
226
+ case "value":
227
+ return serializeIfPrimitive(definition);
228
+ case "domain":
229
+ return definition;
230
+ case "class":
231
+ return getExactConstructorObjectKind(definition);
232
+ default:
233
+ return;
234
+ }
235
+ };
236
+ const serializeIfPrimitive = (data)=>{
237
+ const domain = domainOf(data);
238
+ return domain === "object" || domain === "symbol" ? undefined : serializePrimitive(data);
239
+ };
240
+ const serializeData = {
241
+ value: (data)=>serializeIfPrimitive(data) ?? "default",
242
+ class: (data)=>objectKindOf(data) ?? "default",
243
+ domain: domainOf
244
+ };
245
+ export const serializeCase = (kind, data)=>serializeData[kind](data);
246
+ const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
247
+ const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
@@ -0,0 +1,117 @@
1
+ import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
2
+ import { throwInternalError, throwParseError } from "../utils/errors.js";
3
+ import { entriesOf, hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
4
+ import { Path } from "../utils/paths.js";
5
+ import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality, undefinedOperandsMessage } from "./compose.js";
6
+ import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion } from "./predicate.js";
7
+ import { mappedKeys } from "./rules/props.js";
8
+ export const isConfigNode = (node)=>"config" in node;
9
+ export const nodeIntersection = (l, r, state)=>{
10
+ state.domain = undefined;
11
+ const lDomains = state.type.scope.resolveTypeNode(l);
12
+ const rDomains = state.type.scope.resolveTypeNode(r);
13
+ const result = typeNodeIntersection(lDomains, rDomains, state);
14
+ if (typeof result === "object" && !hasKeys(result)) {
15
+ return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(lDomains), objectKeysOf(rDomains));
16
+ }
17
+ return result === lDomains ? l : result === rDomains ? r : result;
18
+ };
19
+ const typeNodeIntersection = composeKeyedIntersection((domain, l, r, context)=>{
20
+ if (l === undefined) {
21
+ return r === undefined ? throwInternalError(undefinedOperandsMessage) : undefined;
22
+ }
23
+ if (r === undefined) {
24
+ return undefined;
25
+ }
26
+ return predicateIntersection(domain, l, r, context);
27
+ }, {
28
+ onEmpty: "omit"
29
+ });
30
+ export const rootIntersection = (l, r, type)=>{
31
+ const state = new IntersectionState(type, "&");
32
+ const result = nodeIntersection(l, r, state);
33
+ return isDisjoint(result) ? throwParseError(compileDisjointReasonsMessage(state.disjoints)) : isEquality(result) ? l : result;
34
+ };
35
+ export const rootUnion = (l, r, type)=>{
36
+ const lDomains = type.scope.resolveTypeNode(l);
37
+ const rDomains = type.scope.resolveTypeNode(r);
38
+ const result = {};
39
+ const domains = objectKeysOf({
40
+ ...lDomains,
41
+ ...rDomains
42
+ });
43
+ for (const domain of domains){
44
+ result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] : throwInternalError(undefinedOperandsMessage);
45
+ }
46
+ return result;
47
+ };
48
+ const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
49
+ export const flattenType = (type)=>{
50
+ const ctx = {
51
+ type,
52
+ path: new Path(),
53
+ lastDomain: "undefined"
54
+ };
55
+ return flattenNode(type.node, ctx);
56
+ };
57
+ export const flattenNode = (node, ctx)=>{
58
+ if (typeof node === "string") {
59
+ return ctx.type.scope.resolve(node).flat;
60
+ }
61
+ const hasConfig = isConfigNode(node);
62
+ const flattenedTypeNode = flattenTypeNode(hasConfig ? node.node : node, ctx);
63
+ return hasConfig ? [
64
+ [
65
+ "config",
66
+ {
67
+ config: entriesOf(node.config),
68
+ node: flattenedTypeNode
69
+ }
70
+ ]
71
+ ] : flattenedTypeNode;
72
+ };
73
+ export const flattenTypeNode = (node, ctx)=>{
74
+ const domains = objectKeysOf(node);
75
+ if (domains.length === 1) {
76
+ const domain = domains[0];
77
+ const predicate = node[domain];
78
+ if (predicate === true) {
79
+ return domain;
80
+ }
81
+ ctx.lastDomain = domain;
82
+ const flatPredicate = flattenPredicate(predicate, ctx);
83
+ return hasImpliedDomain(flatPredicate) ? flatPredicate : [
84
+ [
85
+ "domain",
86
+ domain
87
+ ],
88
+ ...flatPredicate
89
+ ];
90
+ }
91
+ const result = {};
92
+ for (const domain of domains){
93
+ ctx.lastDomain = domain;
94
+ result[domain] = flattenPredicate(node[domain], ctx);
95
+ }
96
+ return [
97
+ [
98
+ "domains",
99
+ result
100
+ ]
101
+ ];
102
+ };
103
+ export const isLiteralNode = (node, domain)=>{
104
+ return resolutionExtendsDomain(node, domain) && isLiteralCondition(node[domain]);
105
+ };
106
+ export const resolutionExtendsDomain = (resolution, domain)=>{
107
+ const domains = objectKeysOf(resolution);
108
+ return domains.length === 1 && domains[0] === domain;
109
+ };
110
+ export const toArrayNode = (node)=>({
111
+ object: {
112
+ class: Array,
113
+ props: {
114
+ [mappedKeys.index]: node
115
+ }
116
+ }
117
+ });
@@ -0,0 +1,69 @@
1
+ import { listFrom } from "../utils/generics.js";
2
+ import { isArray } from "../utils/objectKinds.js";
3
+ import { branchIntersection, compareBranches, flattenBranch, isBranchComparison } from "./branch.js";
4
+ import { equality, IntersectionState, isEquality } from "./compose.js";
5
+ import { flattenBranches } from "./discriminate.js";
6
+ const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
7
+ export const comparePredicates = (l, r, context)=>{
8
+ if (l === true && r === true) {
9
+ return equality();
10
+ }
11
+ if (!isArray(l) && !isArray(r)) {
12
+ const result = branchIntersection(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
13
+ return result === l ? l : result === r ? r : result;
14
+ }
15
+ const lBranches = listFrom(emptyRulesIfTrue(l));
16
+ const rBranches = listFrom(emptyRulesIfTrue(r));
17
+ const comparison = compareBranches(lBranches, rBranches, context);
18
+ if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
19
+ return equality();
20
+ }
21
+ if (comparison.lExtendsR.length + comparison.equalities.length === lBranches.length) {
22
+ return l;
23
+ }
24
+ if (comparison.rExtendsL.length + comparison.equalities.length === rBranches.length) {
25
+ return r;
26
+ }
27
+ return comparison;
28
+ };
29
+ export const predicateIntersection = (domain, l, r, state)=>{
30
+ state.domain = domain;
31
+ const comparison = comparePredicates(l, r, state);
32
+ if (!isBranchComparison(comparison)) {
33
+ return comparison;
34
+ }
35
+ const resultBranches = [
36
+ ...comparison.distinctIntersections,
37
+ ...comparison.equalities.map((indices)=>comparison.lBranches[indices[0]]),
38
+ ...comparison.lExtendsR.map((lIndex)=>comparison.lBranches[lIndex]),
39
+ ...comparison.rExtendsL.map((rIndex)=>comparison.rBranches[rIndex])
40
+ ];
41
+ if (resultBranches.length === 0) {
42
+ state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
43
+ }
44
+ return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
45
+ };
46
+ export const predicateUnion = (domain, l, r, type)=>{
47
+ const state = new IntersectionState(type, "|");
48
+ const comparison = comparePredicates(l, r, state);
49
+ if (!isBranchComparison(comparison)) {
50
+ return isEquality(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
51
+ // and can be simplified to a non-literal boolean.
52
+ domain === "boolean" ? true : [
53
+ emptyRulesIfTrue(l),
54
+ emptyRulesIfTrue(r)
55
+ ];
56
+ }
57
+ const resultBranches = [
58
+ ...comparison.lBranches.filter((_, lIndex)=>!comparison.lExtendsR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
59
+ ...comparison.rBranches.filter((_, rIndex)=>!comparison.rExtendsL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
60
+ ];
61
+ return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
62
+ };
63
+ export const flattenPredicate = (predicate, context)=>{
64
+ if (predicate === true) {
65
+ return [];
66
+ }
67
+ return isArray(predicate) ? flattenBranches(predicate, context) : flattenBranch(predicate, context);
68
+ };
69
+ export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
@@ -0,0 +1,11 @@
1
+ import { objectKindOf } from "../../utils/objectKinds.js";
2
+ import { composeIntersection, equality } from "../compose.js";
3
+ export const classIntersection = composeIntersection((l, r, state)=>{
4
+ return l === r ? equality() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r);
5
+ });
6
+ export const checkClass = (expectedClass, state)=>{
7
+ if (typeof expectedClass === "string") {
8
+ return objectKindOf(state.data) === expectedClass || !state.problems.add("class", expectedClass);
9
+ }
10
+ return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
11
+ };
@@ -0,0 +1,34 @@
1
+ import { equality } from "../compose.js";
2
+ export const collapsibleListUnion = (l, r)=>{
3
+ if (Array.isArray(l)) {
4
+ if (Array.isArray(r)) {
5
+ const result = listUnion(l, r);
6
+ return result.length === l.length ? result.length === r.length ? equality() : l : result.length === r.length ? r : result;
7
+ }
8
+ return l.includes(r) ? l : [
9
+ ...l,
10
+ r
11
+ ];
12
+ }
13
+ if (Array.isArray(r)) {
14
+ return r.includes(l) ? r : [
15
+ ...r,
16
+ l
17
+ ];
18
+ }
19
+ return l === r ? equality() : [
20
+ l,
21
+ r
22
+ ];
23
+ };
24
+ export const listUnion = (l, r)=>{
25
+ const result = [
26
+ ...l
27
+ ];
28
+ for (const expression of r){
29
+ if (!l.includes(expression)) {
30
+ result.push(expression);
31
+ }
32
+ }
33
+ return result;
34
+ };
@@ -0,0 +1,15 @@
1
+ import { composeIntersection, equality } from "../compose.js";
2
+ export const divisorIntersection = composeIntersection((l, r)=>l === r ? equality() : Math.abs(l * r / greatestCommonDivisor(l, r)));
3
+ // https://en.wikipedia.org/wiki/Euclidean_algorithm
4
+ const greatestCommonDivisor = (l, r)=>{
5
+ let previous;
6
+ let greatestCommonDivisor = l;
7
+ let current = r;
8
+ while(current !== 0){
9
+ previous = current;
10
+ current = greatestCommonDivisor % current;
11
+ greatestCommonDivisor = previous;
12
+ }
13
+ return greatestCommonDivisor;
14
+ };
15
+ export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || !state.problems.add("divisor", divisor);