arktype 0.1.4 → 0.2.0

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 (66) hide show
  1. package/README.md +1 -1
  2. package/dist/api.cjs +4 -3
  3. package/dist/api.d.ts +3 -2
  4. package/dist/api.mjs +2 -1
  5. package/dist/cjs/nodes/node.js +20 -2
  6. package/dist/cjs/nodes/predicate.js +2 -0
  7. package/dist/cjs/nodes/rules/regex.js +4 -42
  8. package/dist/cjs/nodes/rules/rules.js +15 -11
  9. package/dist/cjs/parse/definition.js +2 -2
  10. package/dist/cjs/parse/tuple/morph.js +7 -6
  11. package/dist/cjs/scopes/cache.js +39 -0
  12. package/dist/cjs/scopes/jsObjects.js +133 -0
  13. package/dist/cjs/{main.js → scopes/scope.js} +31 -364
  14. package/dist/cjs/scopes/standard.js +44 -0
  15. package/dist/cjs/scopes/tsKeywords.js +113 -0
  16. package/dist/cjs/scopes/type.js +70 -0
  17. package/dist/cjs/scopes/validation.js +64 -0
  18. package/dist/cjs/traverse/problems.js +50 -74
  19. package/dist/cjs/traverse/traverse.js +25 -3
  20. package/dist/cjs/utils/numericLiterals.js +1 -0
  21. package/dist/mjs/nodes/node.js +20 -2
  22. package/dist/mjs/nodes/predicate.js +1 -0
  23. package/dist/mjs/nodes/rules/regex.js +3 -41
  24. package/dist/mjs/nodes/rules/rules.js +16 -12
  25. package/dist/mjs/parse/definition.js +1 -1
  26. package/dist/mjs/parse/tuple/morph.js +7 -6
  27. package/dist/mjs/scopes/cache.js +25 -0
  28. package/dist/mjs/scopes/jsObjects.js +119 -0
  29. package/dist/mjs/{main.js → scopes/scope.js} +15 -344
  30. package/dist/mjs/scopes/standard.js +27 -0
  31. package/dist/mjs/scopes/tsKeywords.js +99 -0
  32. package/dist/mjs/scopes/type.js +56 -0
  33. package/dist/mjs/scopes/validation.js +51 -0
  34. package/dist/mjs/traverse/problems.js +49 -73
  35. package/dist/mjs/traverse/traverse.js +26 -4
  36. package/dist/mjs/utils/numericLiterals.js +1 -1
  37. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  38. package/dist/types/nodes/compose.d.ts +1 -1
  39. package/dist/types/nodes/infer.d.ts +4 -4
  40. package/dist/types/nodes/node.d.ts +11 -7
  41. package/dist/types/nodes/predicate.d.ts +43 -54
  42. package/dist/types/nodes/rules/range.d.ts +1 -1
  43. package/dist/types/nodes/rules/regex.d.ts +1 -11
  44. package/dist/types/nodes/rules/rules.d.ts +2 -10
  45. package/dist/types/parse/definition.d.ts +1 -1
  46. package/dist/types/parse/string/ast.d.ts +2 -1
  47. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  48. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  49. package/dist/types/parse/string/reduce/static.d.ts +1 -1
  50. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  51. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  52. package/dist/types/parse/tuple/morph.d.ts +1 -1
  53. package/dist/types/parse/tuple/narrow.d.ts +1 -1
  54. package/dist/types/parse/tuple/tuple.d.ts +1 -1
  55. package/dist/types/scopes/cache.d.ts +14 -0
  56. package/dist/types/scopes/jsObjects.d.ts +36 -0
  57. package/dist/types/scopes/scope.d.ts +76 -0
  58. package/dist/types/scopes/standard.d.ts +280 -0
  59. package/dist/types/scopes/tsKeywords.d.ts +32 -0
  60. package/dist/types/scopes/type.d.ts +53 -0
  61. package/dist/types/scopes/validation.d.ts +21 -0
  62. package/dist/types/traverse/problems.d.ts +29 -32
  63. package/dist/types/traverse/traverse.d.ts +10 -8
  64. package/dist/types/utils/numericLiterals.d.ts +1 -0
  65. package/package.json +2 -2
  66. package/dist/types/main.d.ts +0 -324
@@ -0,0 +1,64 @@
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
+ isLuhnValid: ()=>isLuhnValid,
13
+ validationScope: ()=>validationScope,
14
+ validation: ()=>validation
15
+ });
16
+ const _scopeTs = require("./scope.js");
17
+ const isLuhnValid = (creditCardInput)=>{
18
+ const sanitized = creditCardInput.replace(/[- ]+/g, "");
19
+ let sum = 0;
20
+ let digit;
21
+ let tmpNum;
22
+ let shouldDouble;
23
+ for(let i = sanitized.length - 1; i >= 0; i--){
24
+ digit = sanitized.substring(i, i + 1);
25
+ tmpNum = parseInt(digit, 10);
26
+ if (shouldDouble) {
27
+ tmpNum *= 2;
28
+ if (tmpNum >= 10) {
29
+ sum += tmpNum % 10 + 1;
30
+ } else {
31
+ sum += tmpNum;
32
+ }
33
+ } else {
34
+ sum += tmpNum;
35
+ }
36
+ shouldDouble = !shouldDouble;
37
+ }
38
+ return !!(sum % 10 === 0 ? sanitized : false);
39
+ };
40
+ const validationScope = (0, _scopeTs.scope)({
41
+ // Character sets
42
+ alpha: /^[A-Za-z]*$/,
43
+ alphanumeric: /^[A-Za-z\d]*$/,
44
+ lowercase: /^[a-z]*$/,
45
+ uppercase: /^[A-Z]*$/,
46
+ // https://github.com/validatorjs/validator.js
47
+ creditCard: /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
48
+ // https://www.regular-expressions.info
49
+ email: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/,
50
+ // https://github.com/validatorjs/validator.js
51
+ uuid: /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/,
52
+ integer: [
53
+ "node",
54
+ {
55
+ number: {
56
+ divisor: 1
57
+ }
58
+ }
59
+ ]
60
+ }, {
61
+ name: "validation",
62
+ standard: false
63
+ });
64
+ const validation = validationScope.compile();
@@ -15,16 +15,15 @@ _export(exports, {
15
15
  describeDomains: ()=>describeDomains,
16
16
  describeBranches: ()=>describeBranches,
17
17
  DataWrapper: ()=>DataWrapper,
18
- defaultProblemWriters: ()=>defaultProblemWriters,
19
- compileProblemOptions: ()=>compileProblemOptions
18
+ defaultProblemWriters: ()=>defaultProblemWriters
20
19
  });
21
20
  const _scannerTs = require("../parse/string/shift/scanner.js");
22
21
  const _domainsTs = require("../utils/domains.js");
23
- const _genericsTs = require("../utils/generics.js");
22
+ const _numericLiteralsTs = require("../utils/numericLiterals.js");
24
23
  const _objectKindsTs = require("../utils/objectKinds.js");
25
24
  const _pathsTs = require("../utils/paths.js");
26
25
  const _serializeTs = require("../utils/serialize.js");
27
- var _definitions_code, _definitions_code1, _definitions_code2, _clausesByPath, _pathKey;
26
+ var _definitions_code, _definitions_code1, _clausesByPath, _pathKey;
28
27
  class ArkTypeError extends TypeError {
29
28
  constructor(problems){
30
29
  super(problems.summary);
@@ -36,46 +35,50 @@ class Problem {
36
35
  return this.message;
37
36
  }
38
37
  get message() {
39
- return this.writers.message(this.reason, this.path);
38
+ return this.writers.addContext(this.reason, this.path);
40
39
  }
41
40
  get reason() {
42
- return this.writers.reason(this.mustBe, this.was);
41
+ return this.writers.writeReason(this.mustBe, new DataWrapper(this.data));
43
42
  }
44
43
  get mustBe() {
45
- return this.writers.mustBe(this.rule);
44
+ return this.writers.mustBe(this.source);
46
45
  }
47
- get was() {
48
- return this.writers.was(new DataWrapper(this.data));
49
- }
50
- constructor(code, path, data, rule, writers){
46
+ constructor(code, path, data, source, writers){
51
47
  this.code = code;
52
48
  this.path = path;
53
49
  this.data = data;
54
- this.rule = rule;
50
+ this.source = source;
55
51
  this.writers = writers;
52
+ if (this.code === "multi") {
53
+ this.parts = this.source;
54
+ }
56
55
  }
57
56
  }
58
57
  class ProblemArray extends Array {
59
- add(code, data, input) {
58
+ add(code, data, source) {
60
59
  const problem = new Problem(code, // copy the path to avoid future mutations affecting it
61
- _pathsTs.Path.from(this.state.path), data, input, this.state.type.meta.problems[code]);
60
+ _pathsTs.Path.from(this.state.path), data, source, this.state.getConfigForProblemCode(code));
62
61
  const pathKey = `${this.state.path}`;
63
- // TODO: Fix multi
64
- // const existing = this.byPath[pathKey]
65
- // if (existing) {
66
- // if (existing.code === "multi") {
67
- // existing.message += `\n• ${problem.message}`
68
- // } else {
69
- // this.byPath[pathKey] = new Problem(
70
- // "multi",
71
- // existing.path,
72
- // `• ${existing.message}\n• ${problem.message}`
73
- // )
74
- // }
75
- // } else {
76
- // }
77
- this.byPath[pathKey] = problem;
78
- this.push(problem);
62
+ const existing = this.byPath[pathKey];
63
+ if (existing) {
64
+ if (existing.parts) {
65
+ existing.parts.push(problem);
66
+ } else {
67
+ const problemIntersection = new Problem("multi", existing.path, data, [
68
+ existing,
69
+ problem
70
+ ], this.state.getConfigForProblemCode("multi"));
71
+ const existingIndex = this.indexOf(existing);
72
+ // If existing is found (which it always should be unless this was externally mutated),
73
+ // replace it with the new problem intersection. In case it isn't for whatever reason,
74
+ // just append the intersection.
75
+ this[existingIndex === -1 ? this.length : existingIndex] = problemIntersection;
76
+ this.byPath[pathKey] = problemIntersection;
77
+ }
78
+ } else {
79
+ this.byPath[pathKey] = problem;
80
+ this.push(problem);
81
+ }
79
82
  }
80
83
  get summary() {
81
84
  if (this.length === 1) {
@@ -140,48 +143,46 @@ class DataWrapper {
140
143
  this.value = value;
141
144
  }
142
145
  }
143
- const writeDefaultWasDescription = (data)=>`${data}`;
144
- const writeDefaultProblemReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
145
- const writeDefaultProblemMessage = (reason, path)=>path.length ? `${path} ${reason}` : capitalize(reason);
146
+ const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
147
+ const writeDefaultProblemMessage = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
146
148
  const compileDefaultProblemWriters = (definitions)=>{
147
149
  let code;
148
150
  for(code in definitions){
149
- (_definitions_code = definitions[code]).was ?? (_definitions_code.was = writeDefaultWasDescription);
150
- (_definitions_code1 = definitions[code]).reason ?? (_definitions_code1.reason = writeDefaultProblemReason);
151
- (_definitions_code2 = definitions[code]).message ?? (_definitions_code2.message = writeDefaultProblemMessage);
151
+ (_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
152
+ (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = writeDefaultProblemMessage);
152
153
  }
153
154
  return definitions;
154
155
  };
155
156
  const defaultProblemWriters = compileDefaultProblemWriters({
156
157
  divisor: {
157
- mustBe: (divisor)=>divisor === 1 ? `an integer` : `divisible by ${divisor}`
158
+ mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
158
159
  },
159
160
  class: {
160
161
  mustBe: (constructor)=>`an instance of ${constructor.name}`,
161
- was: (data)=>data.className
162
+ writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
162
163
  },
163
164
  domain: {
164
165
  mustBe: (domain)=>_domainsTs.domainDescriptions[domain],
165
- was: (data)=>data.domain
166
+ writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
166
167
  },
167
168
  domainBranches: {
168
169
  mustBe: (domains)=>describeDomains(domains),
169
- was: (data)=>data.domain
170
+ writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
170
171
  },
171
172
  objectKind: {
172
173
  mustBe: (kind)=>_objectKindsTs.objectKindDescriptions[kind],
173
- was: (data)=>data.objectKind
174
+ writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.objectKind)
174
175
  },
175
176
  missing: {
176
177
  mustBe: ()=>"defined",
177
- was: ()=>""
178
+ writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
178
179
  },
179
180
  bound: {
180
181
  mustBe: (bound)=>`${_scannerTs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
181
- was: (data)=>`${data.size}`
182
+ writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
182
183
  },
183
184
  regex: {
184
- mustBe: (regex)=>`a string matching /${regex.source}/`
185
+ mustBe: (expression)=>expression[0] === "/" ? `a string matching ${expression}` : `a valid ${expression}`
185
186
  },
186
187
  value: {
187
188
  mustBe: _serializeTs.stringify
@@ -199,37 +200,12 @@ const defaultProblemWriters = compileDefaultProblemWriters({
199
200
  }
200
201
  return describeBranches(Object.entries(clausesByPath).map(([path, clauses])=>`${path} must be ${clauses.length === 1 ? clauses[0] : describeBranches(clauses)}`));
201
202
  },
202
- reason: (mustBe, data)=>`${mustBe} (was ${data})`,
203
- message: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
203
+ writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
204
+ addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
204
205
  },
205
206
  multi: {
206
- mustBe: (problems)=>"• " + problems.join("\n• "),
207
- reason: (mustBe)=>mustBe
207
+ mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
208
+ writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
209
+ addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
208
210
  }
209
211
  });
210
- const codes = (0, _genericsTs.keysOf)(defaultProblemWriters);
211
- const compileProblemOptions = (opts, base = defaultProblemWriters)=>{
212
- if (!opts) {
213
- return base;
214
- }
215
- const { all , ...byCode } = opts;
216
- const result = {};
217
- let code;
218
- if (all) {
219
- for (code of codes){
220
- result[code] = {
221
- ...base[code],
222
- ...all,
223
- ...byCode[code]
224
- };
225
- }
226
- } else {
227
- for (code of codes){
228
- result[code] = byCode[code] ? {
229
- ...base[code],
230
- ...byCode[code]
231
- } : base[code];
232
- }
233
- }
234
- return result;
235
- };
@@ -69,6 +69,21 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
69
69
  }
70
70
  var _seen = /*#__PURE__*/ new WeakMap();
71
71
  class TraversalState {
72
+ getConfigForProblemCode(code) {
73
+ if (!this.configs.length) {
74
+ return _problemsJs.defaultProblemWriters[code];
75
+ }
76
+ for(let i = this.configs.length - 1; i >= 0; i--){
77
+ if (this.configs[i][code] || this.configs[i]["defaults"]) {
78
+ return {
79
+ ..._problemsJs.defaultProblemWriters[code],
80
+ ...this.configs[i]["defaults"],
81
+ ...this.configs[i][code]
82
+ };
83
+ }
84
+ }
85
+ return _problemsJs.defaultProblemWriters[code];
86
+ }
72
87
  traverseResolution(data, name) {
73
88
  const resolution = this.type.meta.scope.resolve(name);
74
89
  const id = resolution.meta.id;
@@ -88,11 +103,10 @@ class TraversalState {
88
103
  ];
89
104
  }
90
105
  }
91
- // TODO: type not an accurate reflection of context because not always included
92
- const lastType = this.type;
106
+ const lastResolution = this.type;
93
107
  this.type = resolution;
94
108
  traverse(data, resolution.flat, this);
95
- this.type = lastType;
109
+ this.type = lastResolution;
96
110
  if (isObject) {
97
111
  _classPrivateFieldGet(this, _seen)[id].pop();
98
112
  }
@@ -129,6 +143,9 @@ class TraversalState {
129
143
  this.problems = new _problemsJs.Problems(this);
130
144
  this.failFast = false;
131
145
  _classPrivateFieldSet(this, _seen, {});
146
+ this.configs = type.meta.scope.config ? [
147
+ type.meta.scope.config
148
+ ] : [];
132
149
  }
133
150
  }
134
151
  const traverse = (data, node, state)=>{
@@ -260,6 +277,11 @@ const checkers = {
260
277
  alias: (data, name, state)=>state.traverseResolution(data, name),
261
278
  class: _classJs.checkClass,
262
279
  narrow: (data, narrow, state)=>narrow(data, state.problems),
280
+ config: (data, { config , node }, state)=>{
281
+ state.configs.push(config);
282
+ traverse(data, node, state);
283
+ state.configs.pop();
284
+ },
263
285
  value: (data, value, state)=>{
264
286
  if (data !== value) {
265
287
  state.problems.add("value", data, value);
@@ -10,6 +10,7 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  wellFormedIntegerMatcher: ()=>wellFormedIntegerMatcher,
13
+ isWellFormedInteger: ()=>isWellFormedInteger,
13
14
  writeMalformedNumericLiteralMessage: ()=>writeMalformedNumericLiteralMessage,
14
15
  tryParseWellFormedNumber: ()=>tryParseWellFormedNumber,
15
16
  tryParseWellFormedInteger: ()=>tryParseWellFormedInteger,
@@ -50,11 +50,29 @@ export const flattenType = (type)=>{
50
50
  path: new Path(),
51
51
  lastDomain: "undefined"
52
52
  };
53
- return flattenNode(type.node, ctx);
53
+ return type.meta.config ? [
54
+ [
55
+ "config",
56
+ {
57
+ config: type.meta.config,
58
+ node: flattenNode(type.node, ctx)
59
+ }
60
+ ]
61
+ ] : flattenNode(type.node, ctx);
54
62
  };
55
63
  export const flattenNode = (node, ctx)=>{
56
64
  if (typeof node === "string") {
57
- return ctx.type.meta.scope.resolve(node).flat;
65
+ const resolution = ctx.type.meta.scope.resolve(node);
66
+ const updatedScopeConfig = resolution.meta.scope !== ctx.type.meta.scope && resolution.meta.scope.config;
67
+ return updatedScopeConfig ? [
68
+ [
69
+ "config",
70
+ {
71
+ config: updatedScopeConfig,
72
+ node: resolution.flat
73
+ }
74
+ ]
75
+ ] : resolution.flat;
58
76
  }
59
77
  const domains = keysOf(node);
60
78
  if (domains.length === 1) {
@@ -4,6 +4,7 @@ import { compareBranches, isBranchComparison } from "./branches.js";
4
4
  import { equality, IntersectionState, isEquality } from "./compose.js";
5
5
  import { flattenBranches } from "./discriminate.js";
6
6
  import { branchIntersection, flattenBranch } from "./rules/rules.js";
7
+ export const branchIsTransformation = (branch)=>"rules" in branch;
7
8
  const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
8
9
  export const comparePredicates = (l, r, context)=>{
9
10
  if (l === true && r === true) {
@@ -1,53 +1,15 @@
1
1
  import { composeIntersection } from "../compose.js";
2
2
  import { collapsibleListUnion } from "./collapsibleSet.js";
3
3
  const regexCache = {};
4
- // Non-trivial expressions should have an explanation or atttribution
5
- export const sources = {
6
- // Character sets
7
- alpha: /^[A-Z]*$/i,
8
- alphanumeric: /^[A-Z\d]*$/i,
9
- lowercase: /^[a-z]*$/,
10
- uppercase: /^[A-Z]*$/,
11
- // https://github.com/validatorjs/validator.js
12
- creditCard: /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
13
- // https://www.regular-expressions.info
14
- email: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
15
- // https://github.com/validatorjs/validator.js
16
- uuid: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
17
- };
18
4
  export const getRegex = (source)=>{
19
5
  if (!regexCache[source]) {
20
6
  regexCache[source] = new RegExp(source);
21
7
  }
22
8
  return regexCache[source];
23
9
  };
24
- export const checkRegex = (data, regex, state)=>{
25
- if (!regex.test(data)) {
26
- state.problems.add("regex", data, regex);
10
+ export const checkRegex = (data, source, state)=>{
11
+ if (!getRegex(source).test(data)) {
12
+ state.problems.add("regex", data, state.path.length === 0 && state.type.name !== "type" ? state.type.name : `/${source}/`);
27
13
  }
28
14
  };
29
15
  export const regexIntersection = composeIntersection(collapsibleListUnion);
30
- // https://github.com/validatorjs/validator.js
31
- export const isLuhnValid = (creditCardInput)=>{
32
- const sanitized = creditCardInput.replace(/[- ]+/g, "");
33
- let sum = 0;
34
- let digit;
35
- let tmpNum;
36
- let shouldDouble;
37
- for(let i = sanitized.length - 1; i >= 0; i--){
38
- digit = sanitized.substring(i, i + 1);
39
- tmpNum = parseInt(digit, 10);
40
- if (shouldDouble) {
41
- tmpNum *= 2;
42
- if (tmpNum >= 10) {
43
- sum += tmpNum % 10 + 1;
44
- } else {
45
- sum += tmpNum;
46
- }
47
- } else {
48
- sum += tmpNum;
49
- }
50
- shouldDouble = !shouldDouble;
51
- }
52
- return !!(sum % 10 === 0 ? sanitized : false);
53
- };
@@ -2,14 +2,15 @@ import { writeImplicitNeverMessage } from "../../parse/string/ast.js";
2
2
  import { throwParseError } from "../../utils/errors.js";
3
3
  import { listFrom } from "../../utils/generics.js";
4
4
  import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
5
+ import { branchIsTransformation } from "../predicate.js";
5
6
  import { classIntersection } from "./class.js";
6
7
  import { collapsibleListUnion } from "./collapsibleSet.js";
7
8
  import { divisorIntersection } from "./divisor.js";
8
9
  import { flattenObjectKind, objectKindIntersection } from "./objectKind.js";
9
10
  import { flattenProps, propsIntersection } from "./props.js";
10
11
  import { flattenRange, rangeIntersection } from "./range.js";
11
- import { getRegex, regexIntersection } from "./regex.js";
12
- const rulesOf = (branch)=>branch.input ?? branch;
12
+ import { regexIntersection } from "./regex.js";
13
+ const rulesOf = (branch)=>branch.rules ?? branch;
13
14
  export const branchIntersection = (l, r, state)=>{
14
15
  const lRules = rulesOf(l);
15
16
  const rRules = rulesOf(r);
@@ -18,20 +19,20 @@ export const branchIntersection = (l, r, state)=>{
18
19
  if ("morph" in r) {
19
20
  if (l.morph === r.morph) {
20
21
  return isEquality(rulesResult) || isDisjoint(rulesResult) ? rulesResult : {
21
- input: rulesResult,
22
+ rules: rulesResult,
22
23
  morph: l.morph
23
24
  };
24
25
  }
25
26
  return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
26
27
  }
27
28
  return isDisjoint(rulesResult) ? rulesResult : {
28
- input: isEquality(rulesResult) ? l.input : rulesResult,
29
+ rules: isEquality(rulesResult) ? l.rules : rulesResult,
29
30
  morph: l.morph
30
31
  };
31
32
  }
32
33
  if ("morph" in r) {
33
34
  return isDisjoint(rulesResult) ? rulesResult : {
34
- input: isEquality(rulesResult) ? r.input : rulesResult,
35
+ rules: isEquality(rulesResult) ? r.rules : rulesResult,
35
36
  morph: r.morph
36
37
  };
37
38
  }
@@ -56,7 +57,7 @@ const ruleFlatteners = {
56
57
  for (const source of listFrom(rule)){
57
58
  entries.push([
58
59
  "regex",
59
- getRegex(source)
60
+ source
60
61
  ]);
61
62
  }
62
63
  },
@@ -91,6 +92,7 @@ const ruleFlatteners = {
91
92
  };
92
93
  export const precedenceMap = {
93
94
  // Critical: No other checks are performed if these fail
95
+ config: 0,
94
96
  domain: 0,
95
97
  value: 0,
96
98
  domains: 0,
@@ -112,12 +114,14 @@ export const precedenceMap = {
112
114
  morph: 4
113
115
  };
114
116
  export const flattenBranch = (branch, ctx)=>{
115
- if ("morph" in branch) {
116
- const result = flattenRules(branch.input, ctx);
117
- result.push([
118
- "morph",
119
- branch.morph
120
- ]);
117
+ if (branchIsTransformation(branch)) {
118
+ const result = flattenRules(branch.rules, ctx);
119
+ if (branch.morph) {
120
+ result.push([
121
+ "morph",
122
+ branch.morph
123
+ ]);
124
+ }
121
125
  return result;
122
126
  }
123
127
  return flattenRules(branch, ctx);
@@ -1,4 +1,4 @@
1
- import { isType } from "../main.js";
1
+ import { isType } from "../scopes/type.js";
2
2
  import { domainOf } from "../utils/domains.js";
3
3
  import { throwParseError } from "../utils/errors.js";
4
4
  import { objectKindOf } from "../utils/objectKinds.js";
@@ -1,3 +1,4 @@
1
+ import { branchIsTransformation } from "../../nodes/predicate.js";
1
2
  import { throwInternalError, throwParseError } from "../../utils/errors.js";
2
3
  import { isArray } from "../../utils/objectKinds.js";
3
4
  import { stringify } from "../../utils/serialize.js";
@@ -15,7 +16,7 @@ export const parseMorphTuple = (def, ctx)=>{
15
16
  const predicate = resolution[domain];
16
17
  if (predicate === true) {
17
18
  result[domain] = {
18
- input: {},
19
+ rules: {},
19
20
  morph
20
21
  };
21
22
  } else if (typeof predicate === "object") {
@@ -26,17 +27,17 @@ export const parseMorphTuple = (def, ctx)=>{
26
27
  }
27
28
  return result;
28
29
  };
29
- const applyMorph = (branch, morph)=>branch.morph ? {
30
- input: branch.input,
31
- morph: Array.isArray(branch.morph) ? [
30
+ const applyMorph = (branch, morph)=>branchIsTransformation(branch) ? {
31
+ ...branch,
32
+ morph: branch.morph ? Array.isArray(branch.morph) ? [
32
33
  ...branch.morph,
33
34
  morph
34
35
  ] : [
35
36
  branch.morph,
36
37
  morph
37
- ]
38
+ ] : morph
38
39
  } : {
39
- input: branch,
40
+ rules: branch,
40
41
  morph
41
42
  };
42
43
  export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '=>' (was ${typeof value})`;
@@ -0,0 +1,25 @@
1
+ import { deepFreeze } from "../utils/freeze.js";
2
+ export class Cache {
3
+ get root() {
4
+ return this.cache;
5
+ }
6
+ has(name) {
7
+ return name in this.cache;
8
+ }
9
+ get(name) {
10
+ return this.cache[name];
11
+ }
12
+ set(name, item) {
13
+ this.cache[name] = item;
14
+ return item;
15
+ }
16
+ constructor(){
17
+ this.cache = {};
18
+ }
19
+ }
20
+ export class FreezingCache extends Cache {
21
+ set(name, item) {
22
+ this.cache[name] = deepFreeze(item);
23
+ return item;
24
+ }
25
+ }