arktype 1.0.14-alpha → 1.0.15-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 +68 -11
  2. package/dist/main.js +29 -0
  3. package/dist/nodes/branch.js +135 -0
  4. package/dist/nodes/compose.js +99 -0
  5. package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
  6. package/dist/nodes/node.js +134 -0
  7. package/dist/nodes/predicate.js +88 -0
  8. package/dist/nodes/rules/class.js +23 -0
  9. package/dist/nodes/rules/collapsibleSet.js +34 -0
  10. package/dist/nodes/rules/divisor.js +19 -0
  11. package/dist/nodes/rules/props.js +130 -0
  12. package/dist/nodes/rules/range.js +108 -0
  13. package/dist/nodes/rules/regex.js +17 -0
  14. package/dist/nodes/rules/rules.js +102 -0
  15. package/dist/parse/ast/ast.js +2 -0
  16. package/dist/parse/ast/bound.js +7 -0
  17. package/dist/parse/ast/config.js +14 -0
  18. package/dist/parse/ast/distributableFunction.js +40 -0
  19. package/dist/parse/ast/divisor.js +5 -0
  20. package/dist/parse/ast/intersection.js +21 -0
  21. package/dist/parse/ast/keyof.js +107 -0
  22. package/dist/parse/ast/morph.js +50 -0
  23. package/dist/parse/ast/narrow.js +17 -0
  24. package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
  25. package/dist/parse/ast/node.js +2 -0
  26. package/dist/parse/ast/tuple.js +72 -0
  27. package/dist/parse/ast/union.js +5 -0
  28. package/dist/parse/ast/utils.js +2 -0
  29. package/dist/parse/definition.js +47 -0
  30. package/dist/parse/record.js +31 -0
  31. package/dist/parse/string/reduce/dynamic.js +159 -0
  32. package/dist/parse/string/reduce/shared.js +13 -0
  33. package/dist/parse/string/reduce/static.js +2 -0
  34. package/dist/parse/string/shift/operand/enclosed.js +37 -0
  35. package/dist/parse/string/shift/operand/operand.js +15 -0
  36. package/dist/parse/string/shift/operand/unenclosed.js +45 -0
  37. package/dist/parse/string/shift/operator/bounds.js +83 -0
  38. package/dist/parse/string/shift/operator/divisor.js +23 -0
  39. package/dist/parse/string/shift/operator/operator.js +34 -0
  40. package/dist/parse/string/shift/scanner.js +97 -0
  41. package/dist/parse/string/string.js +35 -0
  42. package/dist/scopes/ark.js +27 -0
  43. package/dist/scopes/cache.js +28 -0
  44. package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
  45. package/dist/scopes/jsObjects.js +20 -0
  46. package/dist/scopes/scope.js +184 -0
  47. package/dist/scopes/tsKeywords.js +49 -0
  48. package/dist/scopes/type.js +40 -0
  49. package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
  50. package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
  51. package/dist/scopes/validation/validation.js +74 -0
  52. package/dist/traverse/problems.js +234 -0
  53. package/dist/traverse/traverse.js +330 -0
  54. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  55. package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
  56. package/dist/utils/data.js +39 -0
  57. package/dist/utils/domains.js +27 -0
  58. package/dist/utils/errors.js +17 -0
  59. package/dist/utils/freeze.js +16 -0
  60. package/dist/utils/generics.js +53 -0
  61. package/dist/utils/numericLiterals.js +88 -0
  62. package/dist/utils/objectKinds.js +69 -0
  63. package/dist/{mjs/utils → utils}/paths.js +8 -3
  64. package/dist/utils/serialize.js +71 -0
  65. package/dist/utils/unionToTuple.js +2 -0
  66. package/package.json +25 -39
  67. package/dist/cjs/main.js +0 -38
  68. package/dist/cjs/nodes/branch.js +0 -140
  69. package/dist/cjs/nodes/compose.js +0 -157
  70. package/dist/cjs/nodes/node.js +0 -139
  71. package/dist/cjs/nodes/predicate.js +0 -86
  72. package/dist/cjs/nodes/rules/class.js +0 -25
  73. package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
  74. package/dist/cjs/nodes/rules/divisor.js +0 -29
  75. package/dist/cjs/nodes/rules/props.js +0 -160
  76. package/dist/cjs/nodes/rules/range.js +0 -105
  77. package/dist/cjs/nodes/rules/regex.js +0 -26
  78. package/dist/cjs/nodes/rules/rules.js +0 -123
  79. package/dist/cjs/package.json +0 -3
  80. package/dist/cjs/parse/ast/ast.js +0 -4
  81. package/dist/cjs/parse/ast/bound.js +0 -16
  82. package/dist/cjs/parse/ast/config.js +0 -13
  83. package/dist/cjs/parse/ast/distributableFunction.js +0 -48
  84. package/dist/cjs/parse/ast/divisor.js +0 -9
  85. package/dist/cjs/parse/ast/intersection.js +0 -30
  86. package/dist/cjs/parse/ast/keyof.js +0 -111
  87. package/dist/cjs/parse/ast/morph.js +0 -58
  88. package/dist/cjs/parse/ast/narrow.js +0 -22
  89. package/dist/cjs/parse/ast/node.js +0 -4
  90. package/dist/cjs/parse/ast/tuple.js +0 -94
  91. package/dist/cjs/parse/ast/union.js +0 -9
  92. package/dist/cjs/parse/ast/utils.js +0 -4
  93. package/dist/cjs/parse/definition.js +0 -61
  94. package/dist/cjs/parse/record.js +0 -37
  95. package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
  96. package/dist/cjs/parse/string/reduce/shared.js +0 -23
  97. package/dist/cjs/parse/string/reduce/static.js +0 -4
  98. package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
  99. package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
  100. package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
  101. package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
  102. package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
  103. package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
  104. package/dist/cjs/parse/string/shift/scanner.js +0 -114
  105. package/dist/cjs/parse/string/string.js +0 -43
  106. package/dist/cjs/scopes/ark.js +0 -45
  107. package/dist/cjs/scopes/cache.js +0 -52
  108. package/dist/cjs/scopes/expressions.js +0 -29
  109. package/dist/cjs/scopes/jsObjects.js +0 -93
  110. package/dist/cjs/scopes/scope.js +0 -323
  111. package/dist/cjs/scopes/tsKeywords.js +0 -113
  112. package/dist/cjs/scopes/type.js +0 -50
  113. package/dist/cjs/scopes/validation/creditCard.js +0 -47
  114. package/dist/cjs/scopes/validation/date.js +0 -62
  115. package/dist/cjs/scopes/validation/validation.js +0 -104
  116. package/dist/cjs/traverse/problems.js +0 -281
  117. package/dist/cjs/traverse/traverse.js +0 -390
  118. package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
  119. package/dist/cjs/utils/data.js +0 -53
  120. package/dist/cjs/utils/domains.js +0 -30
  121. package/dist/cjs/utils/errors.js +0 -26
  122. package/dist/cjs/utils/freeze.js +0 -15
  123. package/dist/cjs/utils/generics.js +0 -63
  124. package/dist/cjs/utils/numericLiterals.js +0 -75
  125. package/dist/cjs/utils/objectKinds.js +0 -69
  126. package/dist/cjs/utils/paths.js +0 -32
  127. package/dist/cjs/utils/serialize.js +0 -74
  128. package/dist/cjs/utils/unionToTuple.js +0 -4
  129. package/dist/mjs/main.js +0 -8
  130. package/dist/mjs/nodes/branch.js +0 -123
  131. package/dist/mjs/nodes/compose.js +0 -135
  132. package/dist/mjs/nodes/discriminate.js +0 -247
  133. package/dist/mjs/nodes/node.js +0 -117
  134. package/dist/mjs/nodes/predicate.js +0 -69
  135. package/dist/mjs/nodes/rules/class.js +0 -11
  136. package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
  137. package/dist/mjs/nodes/rules/divisor.js +0 -15
  138. package/dist/mjs/nodes/rules/props.js +0 -142
  139. package/dist/mjs/nodes/rules/range.js +0 -86
  140. package/dist/mjs/nodes/rules/regex.js +0 -11
  141. package/dist/mjs/nodes/rules/rules.js +0 -106
  142. package/dist/mjs/package.json +0 -3
  143. package/dist/mjs/parse/ast/ast.js +0 -1
  144. package/dist/mjs/parse/ast/bound.js +0 -2
  145. package/dist/mjs/parse/ast/config.js +0 -9
  146. package/dist/mjs/parse/ast/distributableFunction.js +0 -34
  147. package/dist/mjs/parse/ast/divisor.js +0 -1
  148. package/dist/mjs/parse/ast/intersection.js +0 -16
  149. package/dist/mjs/parse/ast/keyof.js +0 -103
  150. package/dist/mjs/parse/ast/morph.js +0 -44
  151. package/dist/mjs/parse/ast/narrow.js +0 -14
  152. package/dist/mjs/parse/ast/node.js +0 -1
  153. package/dist/mjs/parse/ast/tuple.js +0 -80
  154. package/dist/mjs/parse/ast/union.js +0 -1
  155. package/dist/mjs/parse/ast/utils.js +0 -1
  156. package/dist/mjs/parse/definition.js +0 -46
  157. package/dist/mjs/parse/record.js +0 -29
  158. package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
  159. package/dist/mjs/parse/string/reduce/shared.js +0 -6
  160. package/dist/mjs/parse/string/reduce/static.js +0 -1
  161. package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
  162. package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
  163. package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
  164. package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
  165. package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
  166. package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
  167. package/dist/mjs/parse/string/shift/scanner.js +0 -106
  168. package/dist/mjs/parse/string/string.js +0 -28
  169. package/dist/mjs/scopes/ark.js +0 -28
  170. package/dist/mjs/scopes/cache.js +0 -38
  171. package/dist/mjs/scopes/jsObjects.js +0 -82
  172. package/dist/mjs/scopes/scope.js +0 -303
  173. package/dist/mjs/scopes/tsKeywords.js +0 -116
  174. package/dist/mjs/scopes/type.js +0 -35
  175. package/dist/mjs/scopes/validation/validation.js +0 -108
  176. package/dist/mjs/traverse/problems.js +0 -260
  177. package/dist/mjs/traverse/traverse.js +0 -373
  178. package/dist/mjs/utils/data.js +0 -38
  179. package/dist/mjs/utils/domains.js +0 -15
  180. package/dist/mjs/utils/errors.js +0 -10
  181. package/dist/mjs/utils/freeze.js +0 -7
  182. package/dist/mjs/utils/generics.js +0 -38
  183. package/dist/mjs/utils/numericLiterals.js +0 -72
  184. package/dist/mjs/utils/objectKinds.js +0 -55
  185. package/dist/mjs/utils/serialize.js +0 -58
  186. package/dist/mjs/utils/unionToTuple.js +0 -1
  187. /package/dist/{types/main.d.ts → main.d.ts} +0 -0
  188. /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
  189. /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
  190. /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
  191. /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
  192. /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
  193. /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
  194. /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
  195. /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
  196. /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
  197. /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
  198. /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
  199. /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
  200. /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
  201. /package/dist/{types/parse → parse}/ast/bound.d.ts +0 -0
  202. /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
  203. /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
  204. /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
  205. /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
  206. /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
  207. /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
  208. /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
  209. /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
  211. /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{types/parse → parse}/definition.d.ts +0 -0
  213. /package/dist/{types/parse → parse}/record.d.ts +0 -0
  214. /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
  225. /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
  226. /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
  227. /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
  228. /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
  230. /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
  232. /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
  236. /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
  237. /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{types/utils → utils}/data.d.ts +0 -0
  239. /package/dist/{types/utils → utils}/domains.d.ts +0 -0
  240. /package/dist/{types/utils → utils}/errors.d.ts +0 -0
  241. /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
  242. /package/dist/{types/utils → utils}/generics.d.ts +0 -0
  243. /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{types/utils → utils}/paths.d.ts +0 -0
  246. /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
  247. /package/dist/{types/utils → utils}/unionToTuple.d.ts +0 -0
@@ -1,48 +1,39 @@
1
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
- serializeDefinitionIfAllowed: ()=>serializeDefinitionIfAllowed,
14
- serializeCase: ()=>serializeCase
15
- });
16
- const _unionJs = require("../parse/ast/union.js");
17
- const _domainsJs = require("../utils/domains.js");
18
- const _errorsJs = require("../utils/errors.js");
19
- const _genericsJs = require("../utils/generics.js");
20
- const _objectKindsJs = require("../utils/objectKinds.js");
21
- const _pathsJs = require("../utils/paths.js");
22
- const _serializeJs = require("../utils/serialize.js");
23
- const _branchJs = require("./branch.js");
24
- const _composeJs = require("./compose.js");
25
- const _propsJs = require("./rules/props.js");
26
- const flattenBranches = (branches, ctx)=>{
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeCase = exports.serializeDefinitionIfAllowed = exports.flattenBranches = void 0;
4
+ const union_js_1 = require("../parse/ast/union.js");
5
+ const domains_js_1 = require("../utils/domains.js");
6
+ const errors_js_1 = require("../utils/errors.js");
7
+ const generics_js_1 = require("../utils/generics.js");
8
+ const objectKinds_js_1 = require("../utils/objectKinds.js");
9
+ const paths_js_1 = require("../utils/paths.js");
10
+ const serialize_js_1 = require("../utils/serialize.js");
11
+ const branch_js_1 = require("./branch.js");
12
+ const compose_js_1 = require("./compose.js");
13
+ const props_js_1 = require("./rules/props.js");
14
+ const flattenBranches = (branches, ctx) => {
27
15
  const discriminants = calculateDiscriminants(branches, ctx);
28
- const indices = branches.map((_, i)=>i);
16
+ const indices = branches.map((_, i) => i);
29
17
  return discriminate(branches, indices, discriminants, ctx);
30
18
  };
31
- const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
19
+ exports.flattenBranches = flattenBranches;
20
+ const discriminate = (originalBranches, remainingIndices, discriminants, ctx) => {
32
21
  if (remainingIndices.length === 1) {
33
- return (0, _branchJs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
22
+ return (0, branch_js_1.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
34
23
  }
35
24
  const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
36
25
  if (!bestDiscriminant) {
37
26
  return [
38
27
  [
39
28
  "branches",
40
- remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.scope) ? (0, _errorsJs.throwParseError)((0, _unionJs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchJs.flattenBranch)(originalBranches[i], ctx))
29
+ remainingIndices.map((i) => branchIncludesMorph(originalBranches[i], ctx.type.scope)
30
+ ? (0, errors_js_1.throwParseError)((0, union_js_1.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`))
31
+ : (0, branch_js_1.flattenBranch)(originalBranches[i], ctx))
41
32
  ]
42
33
  ];
43
34
  }
44
35
  const cases = {};
45
- for(const caseKey in bestDiscriminant.indexCases){
36
+ for (const caseKey in bestDiscriminant.indexCases) {
46
37
  const nextIndices = bestDiscriminant.indexCases[caseKey];
47
38
  cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
48
39
  if (caseKey !== "default") {
@@ -60,24 +51,30 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
60
51
  ]
61
52
  ];
62
53
  };
63
- const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
64
- for(let i = 0; i < entries.length; i++){
54
+ const pruneDiscriminant = (entries, segments, discriminant, ctx) => {
55
+ for (let i = 0; i < entries.length; i++) {
65
56
  const [k, v] = entries[i];
66
57
  if (!segments.length) {
67
58
  if (discriminant.kind === "domain") {
68
59
  if (k === "domain" || k === "domains") {
69
60
  entries.splice(i, 1);
70
61
  return;
71
- } else if (k === "class" || k === "value") {
62
+ }
63
+ else if (k === "class" || k === "value") {
72
64
  // these keys imply a domain, but also add additional
73
65
  // information, so can't be pruned
74
66
  return;
75
67
  }
76
- } else if (discriminant.kind === k) {
68
+ }
69
+ else if (discriminant.kind === k) {
77
70
  entries.splice(i, 1);
78
71
  return;
79
72
  }
80
- } else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
73
+ }
74
+ else if ((k === "requiredProp" ||
75
+ k === "prerequisiteProp" ||
76
+ k === "optionalProp") &&
77
+ v[0] === segments[0]) {
81
78
  if (typeof v[1] === "string") {
82
79
  if (discriminant.kind !== "domain") {
83
80
  return throwInternalPruneFailure(discriminant);
@@ -94,18 +91,21 @@ const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
94
91
  // check for branch keys, which must be traversed even if there are no
95
92
  // segments left
96
93
  if (k === "domains") {
97
- /* c8 ignore next */ if ((0, _genericsJs.keyCount)(v) !== 1 || !v.object) {
94
+ /* c8 ignore next */
95
+ if ((0, generics_js_1.keyCount)(v) !== 1 || !v.object) {
98
96
  return throwInternalPruneFailure(discriminant);
99
97
  }
100
98
  pruneDiscriminant(v.object, segments, discriminant, ctx);
101
99
  return;
102
- } else if (k === "switch") {
103
- for(const caseKey in v.cases){
100
+ }
101
+ else if (k === "switch") {
102
+ for (const caseKey in v.cases) {
104
103
  pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
105
104
  }
106
105
  return;
107
- } else if (k === "branches") {
108
- for (const branch of v){
106
+ }
107
+ else if (k === "branches") {
108
+ for (const branch of v) {
109
109
  pruneDiscriminant(branch, segments, discriminant, ctx);
110
110
  }
111
111
  return;
@@ -113,34 +113,37 @@ const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
113
113
  }
114
114
  return throwInternalPruneFailure(discriminant);
115
115
  };
116
- const throwInternalPruneFailure = (discriminant)=>(0, _errorsJs.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
116
+ const throwInternalPruneFailure = (discriminant) => (0, errors_js_1.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
117
117
  const discriminantKinds = {
118
118
  domain: true,
119
119
  class: true,
120
120
  value: true
121
121
  };
122
- const calculateDiscriminants = (branches, ctx)=>{
122
+ const calculateDiscriminants = (branches, ctx) => {
123
123
  const discriminants = {
124
124
  disjointsByPair: {},
125
125
  casesByDisjoint: {}
126
126
  };
127
- for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
128
- for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
127
+ for (let lIndex = 0; lIndex < branches.length - 1; lIndex++) {
128
+ for (let rIndex = lIndex + 1; rIndex < branches.length; rIndex++) {
129
129
  const pairKey = `${lIndex}/${rIndex}`;
130
130
  const pairDisjoints = [];
131
131
  discriminants.disjointsByPair[pairKey] = pairDisjoints;
132
- const intersectionState = new _composeJs.IntersectionState(ctx.type, "|");
133
- (0, _branchJs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
134
- for(const path in intersectionState.disjoints){
135
- if (path.includes(_propsJs.mappedKeys.index)) {
132
+ const intersectionState = new compose_js_1.IntersectionState(ctx.type, "|");
133
+ (0, branch_js_1.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
134
+ for (const path in intersectionState.disjoints) {
135
+ if (path.includes(props_js_1.mappedKeys.index)) {
136
+ // containers could be empty and therefore their elements cannot be used to discriminate
137
+ // allowing this via a special case where both are length >0 tracked here:
138
+ // https://github.com/arktypeio/arktype/issues/593
136
139
  continue;
137
140
  }
138
- const { l , r , kind } = intersectionState.disjoints[path];
139
- if (!(0, _genericsJs.isKeyOf)(kind, discriminantKinds)) {
141
+ const { l, r, kind } = intersectionState.disjoints[path];
142
+ if (!(0, generics_js_1.isKeyOf)(kind, discriminantKinds)) {
140
143
  continue;
141
144
  }
142
- const lSerialized = serializeDefinitionIfAllowed(kind, l);
143
- const rSerialized = serializeDefinitionIfAllowed(kind, r);
145
+ const lSerialized = (0, exports.serializeDefinitionIfAllowed)(kind, l);
146
+ const rSerialized = (0, exports.serializeDefinitionIfAllowed)(kind, r);
144
147
  if (lSerialized === undefined || rSerialized === undefined) {
145
148
  continue;
146
149
  }
@@ -148,30 +151,24 @@ const calculateDiscriminants = (branches, ctx)=>{
148
151
  pairDisjoints.push(qualifiedDisjoint);
149
152
  if (!discriminants.casesByDisjoint[qualifiedDisjoint]) {
150
153
  discriminants.casesByDisjoint[qualifiedDisjoint] = {
151
- [lSerialized]: [
152
- lIndex
153
- ],
154
- [rSerialized]: [
155
- rIndex
156
- ]
154
+ [lSerialized]: [lIndex],
155
+ [rSerialized]: [rIndex]
157
156
  };
158
157
  continue;
159
158
  }
160
159
  const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
161
160
  const existingLBranch = cases[lSerialized];
162
161
  if (!existingLBranch) {
163
- cases[lSerialized] = [
164
- lIndex
165
- ];
166
- } else if (!existingLBranch.includes(lIndex)) {
162
+ cases[lSerialized] = [lIndex];
163
+ }
164
+ else if (!existingLBranch.includes(lIndex)) {
167
165
  existingLBranch.push(lIndex);
168
166
  }
169
167
  const existingRBranch = cases[rSerialized];
170
168
  if (!existingRBranch) {
171
- cases[rSerialized] = [
172
- rIndex
173
- ];
174
- } else if (!existingRBranch.includes(rIndex)) {
169
+ cases[rSerialized] = [rIndex];
170
+ }
171
+ else if (!existingRBranch.includes(rIndex)) {
175
172
  existingRBranch.push(rIndex);
176
173
  }
177
174
  }
@@ -179,29 +176,26 @@ const calculateDiscriminants = (branches, ctx)=>{
179
176
  }
180
177
  return discriminants;
181
178
  };
182
- const parseQualifiedDisjoint = (qualifiedDisjoint)=>{
183
- const path = _pathsJs.Path.fromString(qualifiedDisjoint);
184
- return [
185
- path,
186
- path.pop()
187
- ];
179
+ const parseQualifiedDisjoint = (qualifiedDisjoint) => {
180
+ const path = paths_js_1.Path.fromString(qualifiedDisjoint);
181
+ return [path, path.pop()];
188
182
  };
189
- const findBestDiscriminant = (remainingIndices, discriminants)=>{
183
+ const findBestDiscriminant = (remainingIndices, discriminants) => {
190
184
  let bestDiscriminant;
191
- for(let i = 0; i < remainingIndices.length - 1; i++){
185
+ for (let i = 0; i < remainingIndices.length - 1; i++) {
192
186
  const lIndex = remainingIndices[i];
193
- for(let j = i + 1; j < remainingIndices.length; j++){
187
+ for (let j = i + 1; j < remainingIndices.length; j++) {
194
188
  const rIndex = remainingIndices[j];
195
189
  const candidates = discriminants.disjointsByPair[`${lIndex}/${rIndex}`];
196
- for (const qualifiedDisjoint of candidates){
190
+ for (const qualifiedDisjoint of candidates) {
197
191
  const indexCases = discriminants.casesByDisjoint[qualifiedDisjoint];
198
192
  const filteredCases = {};
199
193
  const defaultCases = [
200
194
  ...remainingIndices
201
195
  ];
202
196
  let score = 0;
203
- for(const caseKey in indexCases){
204
- const filteredIndices = indexCases[caseKey].filter((i)=>{
197
+ for (const caseKey in indexCases) {
198
+ const filteredIndices = indexCases[caseKey].filter((i) => {
205
199
  const remainingIndex = remainingIndices.indexOf(i);
206
200
  if (remainingIndex !== -1) {
207
201
  delete defaultCases[remainingIndex];
@@ -214,9 +208,9 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
214
208
  filteredCases[caseKey] = filteredIndices;
215
209
  score++;
216
210
  }
217
- const defaultCaseKeys = (0, _genericsJs.objectKeysOf)(defaultCases);
211
+ const defaultCaseKeys = (0, generics_js_1.objectKeysOf)(defaultCases);
218
212
  if (defaultCaseKeys.length) {
219
- filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
213
+ filteredCases["default"] = defaultCaseKeys.map((k) => parseInt(k));
220
214
  }
221
215
  if (!bestDiscriminant || score > bestDiscriminant.score) {
222
216
  const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
@@ -236,27 +230,41 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
236
230
  }
237
231
  return bestDiscriminant;
238
232
  };
239
- const serializeDefinitionIfAllowed = (kind, definition)=>{
240
- switch(kind){
233
+ const serializeDefinitionIfAllowed = (kind, definition) => {
234
+ switch (kind) {
241
235
  case "value":
242
236
  return serializeIfPrimitive(definition);
243
237
  case "domain":
244
238
  return definition;
245
239
  case "class":
246
- return (0, _objectKindsJs.getExactConstructorObjectKind)(definition);
240
+ return (0, objectKinds_js_1.getExactConstructorObjectKind)(definition);
247
241
  default:
248
242
  return;
249
243
  }
250
244
  };
251
- const serializeIfPrimitive = (data)=>{
252
- const domain = (0, _domainsJs.domainOf)(data);
253
- return domain === "object" || domain === "symbol" ? undefined : (0, _serializeJs.serializePrimitive)(data);
245
+ exports.serializeDefinitionIfAllowed = serializeDefinitionIfAllowed;
246
+ const serializeIfPrimitive = (data) => {
247
+ const domain = (0, domains_js_1.domainOf)(data);
248
+ return domain === "object" || domain === "symbol"
249
+ ? undefined
250
+ : (0, serialize_js_1.serializePrimitive)(data);
254
251
  };
255
252
  const serializeData = {
256
- value: (data)=>serializeIfPrimitive(data) ?? "default",
257
- class: (data)=>(0, _objectKindsJs.objectKindOf)(data) ?? "default",
258
- domain: _domainsJs.domainOf
253
+ value: (data) => serializeIfPrimitive(data) ?? "default",
254
+ class: (data) => (0, objectKinds_js_1.objectKindOf)(data) ?? "default",
255
+ domain: domains_js_1.domainOf
259
256
  };
260
- const serializeCase = (kind, data)=>serializeData[kind](data);
261
- const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsJs.propToNode)(prop), $)) : false;
262
- const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : (0, _objectKindsJs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
257
+ const serializeCase = (kind, data) => serializeData[kind](data);
258
+ exports.serializeCase = serializeCase;
259
+ const branchIncludesMorph = (branch, $) => "morph" in branch
260
+ ? true
261
+ : "props" in branch
262
+ ? Object.values(branch.props).some((prop) => nodeIncludesMorph((0, props_js_1.propToNode)(prop), $))
263
+ : false;
264
+ const nodeIncludesMorph = (node, $) => typeof node === "string"
265
+ ? $.resolve(node).includesMorph
266
+ : Object.values($.resolveTypeNode(node)).some((predicate) => predicate === true
267
+ ? false
268
+ : (0, objectKinds_js_1.isArray)(predicate)
269
+ ? predicate.some((branch) => branchIncludesMorph(branch, $))
270
+ : branchIncludesMorph(predicate, $));
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toArrayNode = exports.resolutionExtendsDomain = exports.isLiteralNode = exports.flattenTypeNode = exports.flattenNode = exports.flattenType = exports.rootUnion = exports.rootIntersection = exports.nodeIntersection = exports.isConfigNode = void 0;
4
+ const intersection_js_1 = require("../parse/ast/intersection.js");
5
+ const errors_js_1 = require("../utils/errors.js");
6
+ const generics_js_1 = require("../utils/generics.js");
7
+ const paths_js_1 = require("../utils/paths.js");
8
+ const compose_js_1 = require("./compose.js");
9
+ const predicate_js_1 = require("./predicate.js");
10
+ const props_js_1 = require("./rules/props.js");
11
+ const isConfigNode = (node) => "config" in node;
12
+ exports.isConfigNode = isConfigNode;
13
+ const nodeIntersection = (l, r, state) => {
14
+ state.domain = undefined;
15
+ const lDomains = state.type.scope.resolveTypeNode(l);
16
+ const rDomains = state.type.scope.resolveTypeNode(r);
17
+ const result = typeNodeIntersection(lDomains, rDomains, state);
18
+ if (typeof result === "object" && !(0, generics_js_1.hasKeys)(result)) {
19
+ return (0, generics_js_1.hasKeys)(state.disjoints)
20
+ ? (0, compose_js_1.anonymousDisjoint)()
21
+ : state.addDisjoint("domain", (0, generics_js_1.objectKeysOf)(lDomains), (0, generics_js_1.objectKeysOf)(rDomains));
22
+ }
23
+ return result === lDomains ? l : result === rDomains ? r : result;
24
+ };
25
+ exports.nodeIntersection = nodeIntersection;
26
+ const typeNodeIntersection = (0, compose_js_1.composeKeyedIntersection)((domain, l, r, context) => {
27
+ if (l === undefined) {
28
+ return r === undefined
29
+ ? (0, errors_js_1.throwInternalError)(compose_js_1.undefinedOperandsMessage)
30
+ : undefined;
31
+ }
32
+ if (r === undefined) {
33
+ return undefined;
34
+ }
35
+ return (0, predicate_js_1.predicateIntersection)(domain, l, r, context);
36
+ }, { onEmpty: "omit" });
37
+ const rootIntersection = (l, r, type) => {
38
+ const state = new compose_js_1.IntersectionState(type, "&");
39
+ const result = (0, exports.nodeIntersection)(l, r, state);
40
+ return (0, compose_js_1.isDisjoint)(result)
41
+ ? (0, errors_js_1.throwParseError)((0, intersection_js_1.compileDisjointReasonsMessage)(state.disjoints))
42
+ : (0, compose_js_1.isEquality)(result)
43
+ ? l
44
+ : result;
45
+ };
46
+ exports.rootIntersection = rootIntersection;
47
+ const rootUnion = (l, r, type) => {
48
+ const lDomains = type.scope.resolveTypeNode(l);
49
+ const rDomains = type.scope.resolveTypeNode(r);
50
+ const result = {};
51
+ const domains = (0, generics_js_1.objectKeysOf)({ ...lDomains, ...rDomains });
52
+ for (const domain of domains) {
53
+ result[domain] = (0, generics_js_1.hasKey)(lDomains, domain)
54
+ ? (0, generics_js_1.hasKey)(rDomains, domain)
55
+ ? (0, predicate_js_1.predicateUnion)(domain, lDomains[domain], rDomains[domain], type)
56
+ : lDomains[domain]
57
+ : (0, generics_js_1.hasKey)(rDomains, domain)
58
+ ? rDomains[domain]
59
+ : (0, errors_js_1.throwInternalError)(compose_js_1.undefinedOperandsMessage);
60
+ }
61
+ return result;
62
+ };
63
+ exports.rootUnion = rootUnion;
64
+ const hasImpliedDomain = (flatPredicate) => flatPredicate[0] &&
65
+ (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
66
+ const flattenType = (type) => {
67
+ const ctx = {
68
+ type,
69
+ path: new paths_js_1.Path(),
70
+ lastDomain: "undefined"
71
+ };
72
+ return (0, exports.flattenNode)(type.node, ctx);
73
+ };
74
+ exports.flattenType = flattenType;
75
+ const flattenNode = (node, ctx) => {
76
+ if (typeof node === "string") {
77
+ return ctx.type.scope.resolve(node).flat;
78
+ }
79
+ const hasConfig = (0, exports.isConfigNode)(node);
80
+ const flattenedTypeNode = (0, exports.flattenTypeNode)(hasConfig ? node.node : node, ctx);
81
+ return hasConfig
82
+ ? [
83
+ [
84
+ "config",
85
+ {
86
+ config: (0, generics_js_1.entriesOf)(node.config),
87
+ node: flattenedTypeNode
88
+ }
89
+ ]
90
+ ]
91
+ : flattenedTypeNode;
92
+ };
93
+ exports.flattenNode = flattenNode;
94
+ const flattenTypeNode = (node, ctx) => {
95
+ const domains = (0, generics_js_1.objectKeysOf)(node);
96
+ if (domains.length === 1) {
97
+ const domain = domains[0];
98
+ const predicate = node[domain];
99
+ if (predicate === true) {
100
+ return domain;
101
+ }
102
+ ctx.lastDomain = domain;
103
+ const flatPredicate = (0, predicate_js_1.flattenPredicate)(predicate, ctx);
104
+ return hasImpliedDomain(flatPredicate)
105
+ ? flatPredicate
106
+ : [["domain", domain], ...flatPredicate];
107
+ }
108
+ const result = {};
109
+ for (const domain of domains) {
110
+ ctx.lastDomain = domain;
111
+ result[domain] = (0, predicate_js_1.flattenPredicate)(node[domain], ctx);
112
+ }
113
+ return [["domains", result]];
114
+ };
115
+ exports.flattenTypeNode = flattenTypeNode;
116
+ const isLiteralNode = (node, domain) => {
117
+ return ((0, exports.resolutionExtendsDomain)(node, domain) &&
118
+ (0, predicate_js_1.isLiteralCondition)(node[domain]));
119
+ };
120
+ exports.isLiteralNode = isLiteralNode;
121
+ const resolutionExtendsDomain = (resolution, domain) => {
122
+ const domains = (0, generics_js_1.objectKeysOf)(resolution);
123
+ return domains.length === 1 && domains[0] === domain;
124
+ };
125
+ exports.resolutionExtendsDomain = resolutionExtendsDomain;
126
+ const toArrayNode = (node) => ({
127
+ object: {
128
+ class: Array,
129
+ props: {
130
+ [props_js_1.mappedKeys.index]: node
131
+ }
132
+ }
133
+ });
134
+ exports.toArrayNode = toArrayNode;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLiteralCondition = exports.flattenPredicate = exports.predicateUnion = exports.predicateIntersection = exports.comparePredicates = void 0;
4
+ const generics_js_1 = require("../utils/generics.js");
5
+ const objectKinds_js_1 = require("../utils/objectKinds.js");
6
+ const branch_js_1 = require("./branch.js");
7
+ const compose_js_1 = require("./compose.js");
8
+ const discriminate_js_1 = require("./discriminate.js");
9
+ const emptyRulesIfTrue = (predicate) => (predicate === true ? {} : predicate);
10
+ const comparePredicates = (l, r, context) => {
11
+ if (l === true && r === true) {
12
+ return (0, compose_js_1.equality)();
13
+ }
14
+ if (!(0, objectKinds_js_1.isArray)(l) && !(0, objectKinds_js_1.isArray)(r)) {
15
+ const result = (0, branch_js_1.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
16
+ return result === l ? l : result === r ? r : result;
17
+ }
18
+ const lBranches = (0, generics_js_1.listFrom)(emptyRulesIfTrue(l));
19
+ const rBranches = (0, generics_js_1.listFrom)(emptyRulesIfTrue(r));
20
+ const comparison = (0, branch_js_1.compareBranches)(lBranches, rBranches, context);
21
+ if (comparison.equalities.length === lBranches.length &&
22
+ comparison.equalities.length === rBranches.length) {
23
+ return (0, compose_js_1.equality)();
24
+ }
25
+ if (comparison.lExtendsR.length + comparison.equalities.length ===
26
+ lBranches.length) {
27
+ return l;
28
+ }
29
+ if (comparison.rExtendsL.length + comparison.equalities.length ===
30
+ rBranches.length) {
31
+ return r;
32
+ }
33
+ return comparison;
34
+ };
35
+ exports.comparePredicates = comparePredicates;
36
+ const predicateIntersection = (domain, l, r, state) => {
37
+ state.domain = domain;
38
+ const comparison = (0, exports.comparePredicates)(l, r, state);
39
+ if (!(0, branch_js_1.isBranchComparison)(comparison)) {
40
+ return comparison;
41
+ }
42
+ const resultBranches = [
43
+ ...comparison.distinctIntersections,
44
+ ...comparison.equalities.map((indices) => comparison.lBranches[indices[0]]),
45
+ ...comparison.lExtendsR.map((lIndex) => comparison.lBranches[lIndex]),
46
+ ...comparison.rExtendsL.map((rIndex) => comparison.rBranches[rIndex])
47
+ ];
48
+ if (resultBranches.length === 0) {
49
+ state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
50
+ }
51
+ return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
52
+ };
53
+ exports.predicateIntersection = predicateIntersection;
54
+ const predicateUnion = (domain, l, r, type) => {
55
+ const state = new compose_js_1.IntersectionState(type, "|");
56
+ const comparison = (0, exports.comparePredicates)(l, r, state);
57
+ if (!(0, branch_js_1.isBranchComparison)(comparison)) {
58
+ return (0, compose_js_1.isEquality)(comparison) || comparison === l
59
+ ? r
60
+ : comparison === r
61
+ ? l
62
+ : // if a boolean has multiple branches, neither of which is a
63
+ // subtype of the other, it consists of two opposite literals
64
+ // and can be simplified to a non-literal boolean.
65
+ domain === "boolean"
66
+ ? true
67
+ : [emptyRulesIfTrue(l), emptyRulesIfTrue(r)];
68
+ }
69
+ const resultBranches = [
70
+ ...comparison.lBranches.filter((_, lIndex) => !comparison.lExtendsR.includes(lIndex) &&
71
+ !comparison.equalities.some((indexPair) => indexPair[0] === lIndex)),
72
+ ...comparison.rBranches.filter((_, rIndex) => !comparison.rExtendsL.includes(rIndex) &&
73
+ !comparison.equalities.some((indexPair) => indexPair[1] === rIndex))
74
+ ];
75
+ return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
76
+ };
77
+ exports.predicateUnion = predicateUnion;
78
+ const flattenPredicate = (predicate, context) => {
79
+ if (predicate === true) {
80
+ return [];
81
+ }
82
+ return (0, objectKinds_js_1.isArray)(predicate)
83
+ ? (0, discriminate_js_1.flattenBranches)(predicate, context)
84
+ : (0, branch_js_1.flattenBranch)(predicate, context);
85
+ };
86
+ exports.flattenPredicate = flattenPredicate;
87
+ const isLiteralCondition = (predicate) => typeof predicate === "object" && "value" in predicate;
88
+ exports.isLiteralCondition = isLiteralCondition;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkClass = exports.classIntersection = void 0;
4
+ const objectKinds_js_1 = require("../../utils/objectKinds.js");
5
+ const compose_js_1 = require("../compose.js");
6
+ exports.classIntersection = (0, compose_js_1.composeIntersection)((l, r, state) => {
7
+ return l === r
8
+ ? (0, compose_js_1.equality)()
9
+ : l instanceof r
10
+ ? l
11
+ : r instanceof l
12
+ ? r
13
+ : state.addDisjoint("class", l, r);
14
+ });
15
+ const checkClass = (expectedClass, state) => {
16
+ if (typeof expectedClass === "string") {
17
+ return ((0, objectKinds_js_1.objectKindOf)(state.data) === expectedClass ||
18
+ !state.problems.add("class", expectedClass));
19
+ }
20
+ return (state.data instanceof expectedClass ||
21
+ !state.problems.add("class", expectedClass));
22
+ };
23
+ exports.checkClass = checkClass;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listUnion = exports.collapsibleListUnion = void 0;
4
+ const compose_js_1 = require("../compose.js");
5
+ const collapsibleListUnion = (l, r) => {
6
+ if (Array.isArray(l)) {
7
+ if (Array.isArray(r)) {
8
+ const result = (0, exports.listUnion)(l, r);
9
+ return result.length === l.length
10
+ ? result.length === r.length
11
+ ? (0, compose_js_1.equality)()
12
+ : l
13
+ : result.length === r.length
14
+ ? r
15
+ : result;
16
+ }
17
+ return l.includes(r) ? l : [...l, r];
18
+ }
19
+ if (Array.isArray(r)) {
20
+ return r.includes(l) ? r : [...r, l];
21
+ }
22
+ return l === r ? (0, compose_js_1.equality)() : [l, r];
23
+ };
24
+ exports.collapsibleListUnion = collapsibleListUnion;
25
+ const listUnion = (l, r) => {
26
+ const result = [...l];
27
+ for (const expression of r) {
28
+ if (!l.includes(expression)) {
29
+ result.push(expression);
30
+ }
31
+ }
32
+ return result;
33
+ };
34
+ exports.listUnion = listUnion;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkDivisor = exports.divisorIntersection = void 0;
4
+ const compose_js_1 = require("../compose.js");
5
+ exports.divisorIntersection = (0, compose_js_1.composeIntersection)((l, r) => l === r ? (0, compose_js_1.equality)() : Math.abs((l * r) / greatestCommonDivisor(l, r)));
6
+ // https://en.wikipedia.org/wiki/Euclidean_algorithm
7
+ const greatestCommonDivisor = (l, r) => {
8
+ let previous;
9
+ let greatestCommonDivisor = l;
10
+ let current = r;
11
+ while (current !== 0) {
12
+ previous = current;
13
+ current = greatestCommonDivisor % current;
14
+ greatestCommonDivisor = previous;
15
+ }
16
+ return greatestCommonDivisor;
17
+ };
18
+ const checkDivisor = (divisor, state) => state.data % divisor === 0 || !state.problems.add("divisor", divisor);
19
+ exports.checkDivisor = checkDivisor;