arktype 0.2.0 → 0.4.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 (138) hide show
  1. package/dist/api.cjs +9 -0
  2. package/dist/api.d.ts +10 -0
  3. package/dist/api.mjs +1 -0
  4. package/dist/cjs/nodes/{branches.js → branch.js} +65 -10
  5. package/dist/cjs/nodes/compose.js +2 -3
  6. package/dist/cjs/nodes/discriminate.js +9 -15
  7. package/dist/cjs/nodes/node.js +21 -11
  8. package/dist/cjs/nodes/predicate.js +8 -10
  9. package/dist/cjs/nodes/rules/class.js +10 -4
  10. package/dist/cjs/nodes/rules/divisor.js +1 -5
  11. package/dist/cjs/nodes/rules/props.js +75 -25
  12. package/dist/cjs/nodes/rules/range.js +2 -6
  13. package/dist/cjs/nodes/rules/regex.js +1 -5
  14. package/dist/cjs/nodes/rules/rules.js +23 -63
  15. package/dist/cjs/parse/ast/ast.js +4 -0
  16. package/dist/cjs/parse/ast/bound.js +9 -0
  17. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  18. package/dist/cjs/parse/ast/divisor.js +9 -0
  19. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  20. package/dist/cjs/parse/ast/keyof.js +109 -0
  21. package/dist/cjs/parse/{tuple → ast}/morph.js +2 -2
  22. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  23. package/dist/cjs/parse/{tuple → ast}/tuple.js +23 -16
  24. package/dist/cjs/parse/ast/union.js +9 -0
  25. package/dist/cjs/parse/ast/utils.js +4 -0
  26. package/dist/cjs/parse/definition.js +12 -2
  27. package/dist/cjs/parse/string/reduce/dynamic.js +7 -8
  28. package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
  29. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  30. package/dist/cjs/parse/string/string.js +2 -2
  31. package/dist/cjs/scopes/expressions.js +29 -0
  32. package/dist/cjs/scopes/jsObjects.js +14 -14
  33. package/dist/cjs/scopes/scope.js +56 -4
  34. package/dist/cjs/scopes/type.js +4 -7
  35. package/dist/cjs/traverse/problems.js +67 -53
  36. package/dist/cjs/traverse/traverse.js +103 -135
  37. package/dist/cjs/utils/data.js +39 -0
  38. package/dist/cjs/utils/generics.js +20 -2
  39. package/dist/cjs/utils/numericLiterals.js +2 -0
  40. package/dist/cjs/utils/objectKinds.js +1 -2
  41. package/dist/mjs/nodes/{branches.js → branch.js} +54 -2
  42. package/dist/mjs/nodes/compose.js +3 -4
  43. package/dist/mjs/nodes/discriminate.js +9 -15
  44. package/dist/mjs/nodes/node.js +17 -8
  45. package/dist/mjs/nodes/predicate.js +4 -5
  46. package/dist/mjs/nodes/rules/class.js +10 -4
  47. package/dist/mjs/nodes/rules/divisor.js +1 -5
  48. package/dist/mjs/nodes/rules/props.js +74 -27
  49. package/dist/mjs/nodes/rules/range.js +2 -6
  50. package/dist/mjs/nodes/rules/regex.js +1 -5
  51. package/dist/mjs/nodes/rules/rules.js +23 -62
  52. package/dist/mjs/parse/ast/ast.js +1 -0
  53. package/dist/mjs/parse/ast/bound.js +1 -0
  54. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  55. package/dist/mjs/parse/ast/divisor.js +1 -0
  56. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  57. package/dist/mjs/parse/ast/keyof.js +101 -0
  58. package/dist/mjs/parse/{tuple → ast}/morph.js +2 -2
  59. package/dist/mjs/parse/ast/narrow.js +7 -0
  60. package/dist/mjs/parse/{tuple → ast}/tuple.js +24 -17
  61. package/dist/mjs/parse/ast/union.js +1 -0
  62. package/dist/mjs/parse/ast/utils.js +1 -0
  63. package/dist/mjs/parse/definition.js +12 -2
  64. package/dist/mjs/parse/string/reduce/dynamic.js +8 -9
  65. package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
  66. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  67. package/dist/mjs/parse/string/string.js +2 -2
  68. package/dist/mjs/scopes/expressions.js +9 -0
  69. package/dist/mjs/scopes/jsObjects.js +14 -14
  70. package/dist/mjs/scopes/scope.js +55 -3
  71. package/dist/mjs/scopes/type.js +4 -7
  72. package/dist/mjs/traverse/problems.js +68 -53
  73. package/dist/mjs/traverse/traverse.js +107 -139
  74. package/dist/mjs/utils/data.js +24 -0
  75. package/dist/mjs/utils/generics.js +18 -1
  76. package/dist/mjs/utils/numericLiterals.js +1 -0
  77. package/dist/mjs/utils/objectKinds.js +1 -1
  78. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  79. package/dist/types/nodes/branch.d.ts +27 -0
  80. package/dist/types/nodes/compose.d.ts +5 -10
  81. package/dist/types/nodes/discriminate.d.ts +3 -3
  82. package/dist/types/nodes/infer.d.ts +21 -20
  83. package/dist/types/nodes/node.d.ts +12 -16
  84. package/dist/types/nodes/predicate.d.ts +4 -59
  85. package/dist/types/nodes/rules/class.d.ts +3 -2
  86. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  87. package/dist/types/nodes/rules/props.d.ts +19 -11
  88. package/dist/types/nodes/rules/range.d.ts +3 -2
  89. package/dist/types/nodes/rules/regex.d.ts +2 -1
  90. package/dist/types/nodes/rules/rules.d.ts +12 -14
  91. package/dist/types/parse/ast/ast.d.ts +24 -0
  92. package/dist/types/parse/ast/bound.d.ts +10 -0
  93. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  94. package/dist/types/parse/ast/divisor.d.ts +8 -0
  95. package/dist/types/parse/ast/intersection.d.ts +17 -0
  96. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  97. package/dist/types/parse/{tuple → ast}/morph.d.ts +3 -3
  98. package/dist/types/parse/ast/narrow.d.ts +21 -0
  99. package/dist/types/parse/ast/tuple.d.ts +40 -0
  100. package/dist/types/parse/ast/union.d.ts +12 -0
  101. package/dist/types/parse/ast/utils.d.ts +7 -0
  102. package/dist/types/parse/definition.d.ts +9 -7
  103. package/dist/types/parse/record.d.ts +2 -2
  104. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  105. package/dist/types/parse/string/reduce/shared.d.ts +6 -5
  106. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  107. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +2 -2
  108. package/dist/types/parse/string/shift/operator/bounds.d.ts +4 -3
  109. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  110. package/dist/types/parse/string/string.d.ts +2 -3
  111. package/dist/types/scopes/expressions.d.ts +48 -0
  112. package/dist/types/scopes/scope.d.ts +17 -7
  113. package/dist/types/scopes/type.d.ts +13 -15
  114. package/dist/types/traverse/problems.d.ts +17 -41
  115. package/dist/types/traverse/traverse.d.ts +85 -52
  116. package/dist/types/utils/data.d.ts +13 -0
  117. package/dist/types/utils/domains.d.ts +1 -1
  118. package/dist/types/utils/generics.d.ts +13 -10
  119. package/dist/types/utils/numericLiterals.d.ts +1 -0
  120. package/dist/types/utils/objectKinds.d.ts +43 -43
  121. package/dist/types/utils/paths.d.ts +2 -0
  122. package/package.json +1 -2
  123. package/dist/cjs/nodes/functors.js +0 -35
  124. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  125. package/dist/cjs/parse/tuple/keyof.js +0 -45
  126. package/dist/cjs/utils/size.js +0 -16
  127. package/dist/mjs/nodes/functors.js +0 -27
  128. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  129. package/dist/mjs/parse/tuple/keyof.js +0 -37
  130. package/dist/mjs/parse/tuple/narrow.js +0 -7
  131. package/dist/mjs/utils/size.js +0 -2
  132. package/dist/types/nodes/branches.d.ts +0 -14
  133. package/dist/types/nodes/functors.d.ts +0 -6
  134. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
  135. package/dist/types/parse/string/ast.d.ts +0 -45
  136. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  137. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  138. package/dist/types/utils/size.d.ts +0 -3
@@ -12,7 +12,7 @@ _export(exports, {
12
12
  TraversalState: ()=>TraversalState,
13
13
  traverse: ()=>traverse,
14
14
  checkEntries: ()=>checkEntries,
15
- checkObjectKind: ()=>checkObjectKind
15
+ checkRequiredProp: ()=>checkRequiredProp
16
16
  });
17
17
  const _discriminateJs = require("../nodes/discriminate.js");
18
18
  const _classJs = require("../nodes/rules/class.js");
@@ -21,9 +21,7 @@ const _rangeJs = require("../nodes/rules/range.js");
21
21
  const _regexJs = require("../nodes/rules/regex.js");
22
22
  const _rulesJs = require("../nodes/rules/rules.js");
23
23
  const _domainsJs = require("../utils/domains.js");
24
- const _errorsJs = require("../utils/errors.js");
25
24
  const _genericsJs = require("../utils/generics.js");
26
- const _objectKindsJs = require("../utils/objectKinds.js");
27
25
  const _pathsJs = require("../utils/paths.js");
28
26
  const _serializeJs = require("../utils/serialize.js");
29
27
  const _problemsJs = require("./problems.js");
@@ -84,9 +82,21 @@ class TraversalState {
84
82
  }
85
83
  return _problemsJs.defaultProblemWriters[code];
86
84
  }
87
- traverseResolution(data, name) {
85
+ traverseKey(key, node) {
86
+ const lastData = this.data;
87
+ this.data = this.data[key];
88
+ this.path.push(key);
89
+ const isValid = traverse(node, this);
90
+ this.path.pop();
91
+ lastData[key] = this.data;
92
+ this.data = lastData;
93
+ return isValid;
94
+ }
95
+ traverseResolution(name) {
88
96
  const resolution = this.type.meta.scope.resolve(name);
89
97
  const id = resolution.meta.id;
98
+ // this assignment helps with narrowing
99
+ const data = this.data;
90
100
  const isObject = (0, _domainsJs.hasDomain)(data, "object");
91
101
  if (isObject) {
92
102
  if ((0, _genericsJs.hasKey)(_classPrivateFieldGet(this, _seen), id)) {
@@ -94,7 +104,7 @@ class TraversalState {
94
104
  // if data has already been checked by this alias as part of
95
105
  // a resolution higher up on the call stack, it must be valid
96
106
  // or we wouldn't be here
97
- return;
107
+ return true;
98
108
  }
99
109
  _classPrivateFieldGet(this, _seen)[id].push(data);
100
110
  } else {
@@ -105,39 +115,39 @@ class TraversalState {
105
115
  }
106
116
  const lastResolution = this.type;
107
117
  this.type = resolution;
108
- traverse(data, resolution.flat, this);
118
+ const isValid = traverse(resolution.flat, this);
109
119
  this.type = lastResolution;
110
120
  if (isObject) {
111
121
  _classPrivateFieldGet(this, _seen)[id].pop();
112
122
  }
123
+ return isValid;
113
124
  }
114
- traverseBranches(data, branches) {
115
- const lastProblems = this.problems;
116
- const lastPath = this.path;
125
+ traverseBranches(branches) {
117
126
  const lastFailFast = this.failFast;
118
127
  this.failFast = true;
119
- const branchProblems = [];
128
+ const lastProblems = this.problems;
129
+ const branchProblems = new _problemsJs.Problems(this);
130
+ this.problems = branchProblems;
131
+ const lastPath = this.path;
132
+ let hasValidBranch = false;
120
133
  for (const branch of branches){
121
- this.problems = new _problemsJs.Problems(this);
122
134
  this.path = new _pathsJs.Path();
123
- checkEntries(data, branch, this);
124
- if (!this.problems.length) {
135
+ if (checkEntries(branch, this)) {
136
+ hasValidBranch = true;
125
137
  break;
126
138
  }
127
- branchProblems.push(this.problems[0]);
128
139
  }
129
- this.failFast = lastFailFast;
130
140
  this.path = lastPath;
131
141
  this.problems = lastProblems;
132
- if (branchProblems.length === branches.length) {
133
- this.problems.add("branches", data, branchProblems);
134
- }
142
+ this.failFast = lastFailFast;
143
+ return hasValidBranch || this.problems.create("branches", branchProblems);
135
144
  }
136
- constructor(type){
145
+ constructor(data, type){
137
146
  _classPrivateFieldInit(this, _seen, {
138
147
  writable: true,
139
148
  value: void 0
140
149
  });
150
+ this.data = data;
141
151
  this.type = type;
142
152
  this.path = new _pathsJs.Path();
143
153
  this.problems = new _problemsJs.Problems(this);
@@ -148,143 +158,101 @@ class TraversalState {
148
158
  ] : [];
149
159
  }
150
160
  }
151
- const traverse = (data, node, state)=>{
152
- if (typeof node === "string") {
153
- if ((0, _domainsJs.domainOf)(data) !== node) {
154
- return state.problems.add("domain", data, node);
155
- }
156
- return;
157
- }
158
- return checkEntries(data, node, state);
159
- };
160
- const checkEntries = (data, entries, state)=>{
161
- let problemsPrecedence = Number.POSITIVE_INFINITY;
162
- const initialProblemsCount = state.problems.length;
163
- for (const [k, v] of entries){
164
- if (_rulesJs.precedenceMap[k] > problemsPrecedence) {
165
- return;
166
- }
167
- if (k === "morph") {
168
- if (typeof v === "function") {
169
- return v(data);
170
- }
171
- let out = data;
172
- for (const morph of v){
173
- out = morph(out);
161
+ const traverse = (node, state)=>typeof node === "string" ? (0, _domainsJs.domainOf)(state.data) === node || state.problems.create("domain", node) : checkEntries(node, state);
162
+ const checkEntries = (entries, state)=>{
163
+ let isValid = true;
164
+ for(let i = 0; i < entries.length; i++){
165
+ const [k, v] = entries[i];
166
+ const entryAllowsData = entryCheckers[k](v, state);
167
+ isValid && (isValid = entryAllowsData);
168
+ if (!isValid) {
169
+ if (state.failFast) {
170
+ return false;
174
171
  }
175
- return out;
176
- } else {
177
- checkers[k](data, v, state);
178
- if (state.problems.length > initialProblemsCount) {
179
- if (state.failFast) {
180
- return;
181
- }
182
- problemsPrecedence = _rulesJs.precedenceMap[k];
172
+ if (i < entries.length - 1 && _rulesJs.precedenceMap[k] < _rulesJs.precedenceMap[entries[i + 1][0]]) {
173
+ // if we've encountered a problem, there is at least one entry
174
+ // remaining, and the next entry is of a higher precedence level
175
+ // than the current entry, return immediately
176
+ return false;
183
177
  }
184
178
  }
185
179
  }
186
- return data;
180
+ return isValid;
187
181
  };
188
- const createPropChecker = (propKind)=>(data, props, state)=>{
189
- const rootPath = state.path;
190
- for (const [propKey, propNode] of props){
191
- state.path.push(propKey);
192
- if (!(0, _genericsJs.hasKey)(data, propKey)) {
193
- if (propKind !== "optionalProps") {
194
- state.problems.add("missing", undefined, undefined);
195
- }
196
- } else {
197
- traverse(data[propKey], propNode, state);
198
- }
199
- state.path.pop();
200
- }
201
- state.path = rootPath;
202
- };
203
- const checkRequiredProps = createPropChecker("requiredProps");
204
- const checkOptionalProps = createPropChecker("optionalProps");
205
- const checkObjectKind = (data, rule, state)=>{
206
- const dataObjectKind = (0, _objectKindsJs.objectKindOf)(data);
207
- if (typeof rule === "string") {
208
- if (dataObjectKind !== rule) {
209
- return state.problems.add("objectKind", data, rule);
210
- }
211
- return;
212
- }
213
- if (dataObjectKind !== rule[0]) {
214
- return state.problems.add("objectKind", data, rule[0]);
215
- }
216
- if (dataObjectKind === "Array" && typeof rule[2] === "number") {
217
- const actual = data.length;
218
- const expected = rule[2];
219
- if (expected !== actual) {
220
- return state.problems.add("bound", data, {
221
- comparator: "==",
222
- limit: expected,
223
- units: "items"
224
- });
225
- }
226
- }
227
- if (dataObjectKind === "Array" || dataObjectKind === "Set") {
228
- let i = 0;
229
- for (const item of data){
230
- state.path.push(`${i}`);
231
- traverse(item, rule[1], state);
232
- state.path.pop();
233
- i++;
234
- }
235
- } else {
236
- return (0, _errorsJs.throwInternalError)(`Unexpected objectKind entry ${(0, _serializeJs.stringify)(rule)}`);
182
+ const checkRequiredProp = (prop, state)=>{
183
+ if (prop[0] in state.data) {
184
+ return state.traverseKey(prop[0], prop[1]);
237
185
  }
238
- return true;
186
+ return state.problems.create("missing", undefined, {
187
+ path: state.path.concat(prop[0]),
188
+ data: undefined
189
+ });
239
190
  };
240
- const checkers = {
191
+ const entryCheckers = {
241
192
  regex: _regexJs.checkRegex,
242
193
  divisor: _divisorJs.checkDivisor,
243
- domains: (data, domains, state)=>{
244
- const entries = domains[(0, _domainsJs.domainOf)(data)];
245
- if (entries) {
246
- checkEntries(data, entries, state);
247
- } else {
248
- state.problems.add("domainBranches", data, (0, _genericsJs.keysOf)(domains));
194
+ domains: (domains, state)=>{
195
+ const entries = domains[(0, _domainsJs.domainOf)(state.data)];
196
+ return entries ? checkEntries(entries, state) : state.problems.create("domainBranches", (0, _genericsJs.objectKeysOf)(domains));
197
+ },
198
+ domain: (domain, state)=>(0, _domainsJs.domainOf)(state.data) === domain || state.problems.create("domain", domain),
199
+ bound: _rangeJs.checkBound,
200
+ optionalProp: (prop, state)=>{
201
+ if (prop[0] in state.data) {
202
+ return state.traverseKey(prop[0], prop[1]);
249
203
  }
204
+ return true;
250
205
  },
251
- domain: (data, domain, state)=>{
252
- if ((0, _domainsJs.domainOf)(data) !== domain) {
253
- state.problems.add("domain", data, domain);
206
+ // these checks work the same way, the keys are only distinct so that
207
+ // prerequisite props can have a higher precedence
208
+ requiredProp: checkRequiredProp,
209
+ prerequisiteProp: checkRequiredProp,
210
+ indexProp: (node, state)=>{
211
+ if (!Array.isArray(state.data)) {
212
+ return state.problems.create("class", "Array");
213
+ }
214
+ let isValid = true;
215
+ for(let i = 0; i < state.data.length; i++){
216
+ isValid && (isValid = state.traverseKey(`${i}`, node));
217
+ if (!isValid && state.failFast) {
218
+ return false;
219
+ }
254
220
  }
221
+ return isValid;
255
222
  },
256
- objectKind: checkObjectKind,
257
- bound: _rangeJs.checkBound,
258
- requiredProps: checkRequiredProps,
259
- optionalProps: checkOptionalProps,
260
- branches: (data, branches, state)=>state.traverseBranches(data, branches),
261
- switch: (data, rule, state)=>{
262
- const dataAtPath = (0, _pathsJs.getPath)(data, rule.path);
223
+ branches: (branches, state)=>state.traverseBranches(branches),
224
+ switch: (rule, state)=>{
225
+ const dataAtPath = (0, _pathsJs.getPath)(state.data, rule.path);
263
226
  const caseKey = (0, _discriminateJs.serializeCase)(rule.kind, dataAtPath);
264
227
  if ((0, _genericsJs.hasKey)(rule.cases, caseKey)) {
265
- return checkEntries(data, rule.cases[caseKey], state);
228
+ return checkEntries(rule.cases[caseKey], state);
266
229
  }
267
- const caseKeys = (0, _genericsJs.keysOf)(rule.cases);
268
- const lastPath = state.path;
269
- state.path = state.path.concat(rule.path);
270
- if (rule.kind === "value") {
271
- state.problems.add("valueBranches", dataAtPath, caseKeys.map((k)=>(0, _serializeJs.deserializePrimitive)(k)));
272
- } else {
273
- state.problems.add("domainBranches", dataAtPath, caseKeys);
274
- }
275
- state.path = lastPath;
230
+ const caseKeys = (0, _genericsJs.objectKeysOf)(rule.cases);
231
+ const missingCasePath = state.path.concat(rule.path);
232
+ return rule.kind === "value" ? state.problems.create("valueBranches", caseKeys.map((k)=>(0, _serializeJs.deserializePrimitive)(k)), {
233
+ path: missingCasePath,
234
+ data: dataAtPath
235
+ }) : state.problems.create("domainBranches", caseKeys, {
236
+ path: missingCasePath,
237
+ data: dataAtPath
238
+ });
276
239
  },
277
- alias: (data, name, state)=>state.traverseResolution(data, name),
240
+ alias: (name, state)=>state.traverseResolution(name),
278
241
  class: _classJs.checkClass,
279
- narrow: (data, narrow, state)=>narrow(data, state.problems),
280
- config: (data, { config , node }, state)=>{
242
+ narrow: (narrow, state)=>narrow(state.data, state.problems),
243
+ config: ({ config , node }, state)=>{
281
244
  state.configs.push(config);
282
- traverse(data, node, state);
245
+ const result = traverse(node, state);
283
246
  state.configs.pop();
247
+ return result;
284
248
  },
285
- value: (data, value, state)=>{
286
- if (data !== value) {
287
- state.problems.add("value", data, value);
249
+ value: (value, state)=>state.data === value || state.problems.create("value", value),
250
+ morph: (morph, state)=>{
251
+ const out = morph(state.data);
252
+ if (out instanceof _problemsJs.Problem) {
253
+ return state.problems.add(out);
288
254
  }
255
+ state.data = out;
256
+ return true;
289
257
  }
290
258
  };
@@ -0,0 +1,39 @@
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
+ sizeOf: ()=>sizeOf,
13
+ unitsOf: ()=>unitsOf,
14
+ DataWrapper: ()=>DataWrapper
15
+ });
16
+ const _domainsTs = require("./domains.js");
17
+ const _serializeTs = require("./serialize.js");
18
+ const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
19
+ const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items" : "";
20
+ class DataWrapper {
21
+ toString() {
22
+ return (0, _serializeTs.stringify)(this.value);
23
+ }
24
+ get domain() {
25
+ return (0, _domainsTs.domainOf)(this.value);
26
+ }
27
+ get size() {
28
+ return sizeOf(this.value);
29
+ }
30
+ get units() {
31
+ return unitsOf(this.value);
32
+ }
33
+ get className() {
34
+ return Object(this.value).constructor.name;
35
+ }
36
+ constructor(value){
37
+ this.value = value;
38
+ }
39
+ }
@@ -12,7 +12,8 @@ _export(exports, {
12
12
  asConst: ()=>asConst,
13
13
  isKeyOf: ()=>isKeyOf,
14
14
  entriesOf: ()=>entriesOf,
15
- keysOf: ()=>keysOf,
15
+ objectKeysOf: ()=>objectKeysOf,
16
+ prototypeKeysOf: ()=>prototypeKeysOf,
16
17
  hasKey: ()=>hasKey,
17
18
  keyCount: ()=>keyCount,
18
19
  hasKeys: ()=>hasKeys,
@@ -22,7 +23,24 @@ const _domainsTs = require("./domains.js");
22
23
  const asConst = (t)=>t;
23
24
  const isKeyOf = (k, obj)=>k in obj;
24
25
  const entriesOf = (o)=>Object.entries(o);
25
- const keysOf = (o)=>Object.keys(o);
26
+ const objectKeysOf = (o)=>Object.keys(o);
27
+ const prototypeKeysOf = (value)=>{
28
+ const result = [];
29
+ while(value !== Object.prototype && value !== null && value !== undefined){
30
+ for (const k of Object.getOwnPropertyNames(value)){
31
+ if (!result.includes(k)) {
32
+ result.push(k);
33
+ }
34
+ }
35
+ for (const symbol of Object.getOwnPropertySymbols(value)){
36
+ if (!result.includes(symbol)) {
37
+ result.push(symbol);
38
+ }
39
+ }
40
+ value = Object.getPrototypeOf(value);
41
+ }
42
+ return result;
43
+ };
26
44
  const hasKey = (o, k)=>{
27
45
  const valueAtKey = o?.[k];
28
46
  return valueAtKey !== undefined && valueAtKey !== null;
@@ -11,6 +11,7 @@ function _export(target, all) {
11
11
  _export(exports, {
12
12
  wellFormedIntegerMatcher: ()=>wellFormedIntegerMatcher,
13
13
  isWellFormedInteger: ()=>isWellFormedInteger,
14
+ wellFormedNonNegativeIntegerMatcher: ()=>wellFormedNonNegativeIntegerMatcher,
14
15
  writeMalformedNumericLiteralMessage: ()=>writeMalformedNumericLiteralMessage,
15
16
  tryParseWellFormedNumber: ()=>tryParseWellFormedNumber,
16
17
  tryParseWellFormedInteger: ()=>tryParseWellFormedInteger,
@@ -36,6 +37,7 @@ const numberLikeMatcher = /^-?\d*\.?\d*$/;
36
37
  const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
37
38
  const wellFormedIntegerMatcher = /^(?:0|(?:-?[1-9]\d*))$/;
38
39
  const isWellFormedInteger = (s)=>wellFormedIntegerMatcher.test(s);
40
+ const wellFormedNonNegativeIntegerMatcher = /^(?:0|(?:[1-9]\d*))$/;
39
41
  const integerLikeMatcher = /^-?\d+$/;
40
42
  const isIntegerLike = (s)=>integerLikeMatcher.test(s);
41
43
  const numericLiteralDescriptions = {
@@ -9,14 +9,13 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
+ defaultObjectKinds: ()=>defaultObjectKinds,
12
13
  objectKindOf: ()=>objectKindOf,
13
14
  hasObjectKind: ()=>hasObjectKind,
14
15
  isArray: ()=>isArray,
15
16
  objectKindDescriptions: ()=>objectKindDescriptions
16
17
  });
17
18
  const _domainsTs = require("./domains.js");
18
- // Built-in object constructors based on a subset of:
19
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
20
19
  const defaultObjectKinds = {
21
20
  Array,
22
21
  Date,
@@ -1,7 +1,8 @@
1
+ import { writeImplicitNeverMessage } from "../parse/ast/intersection.js";
1
2
  import { domainOf, hasDomain } from "../utils/domains.js";
2
- import { throwInternalError } from "../utils/errors.js";
3
+ import { throwInternalError, throwParseError } from "../utils/errors.js";
3
4
  import { isDisjoint, isEquality } from "./compose.js";
4
- import { branchIntersection } from "./rules/rules.js";
5
+ import { flattenRules, rulesIntersection } from "./rules/rules.js";
5
6
  export const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
6
7
  export const compareBranches = (lConditions, rConditions, state)=>{
7
8
  const result = {
@@ -69,3 +70,54 @@ export const compareBranches = (lConditions, rConditions, state)=>{
69
70
  result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
70
71
  return result;
71
72
  };
73
+ export const isMorphBranch = (branch)=>"morph" in branch;
74
+ export const flattenBranch = (branch, ctx)=>{
75
+ if (isMorphBranch(branch)) {
76
+ const result = flattenRules(branch.rules, ctx);
77
+ if (branch.morph) {
78
+ if (typeof branch.morph === "function") {
79
+ result.push([
80
+ "morph",
81
+ branch.morph
82
+ ]);
83
+ } else {
84
+ for (const morph of branch.morph){
85
+ result.push([
86
+ "morph",
87
+ morph
88
+ ]);
89
+ }
90
+ }
91
+ }
92
+ return result;
93
+ }
94
+ return flattenRules(branch, ctx);
95
+ };
96
+ const rulesOf = (branch)=>branch.rules ?? branch;
97
+ export const branchIntersection = (l, r, state)=>{
98
+ const lRules = rulesOf(l);
99
+ const rRules = rulesOf(r);
100
+ const rulesResult = rulesIntersection(lRules, rRules, state);
101
+ if ("morph" in l) {
102
+ if ("morph" in r) {
103
+ if (l.morph === r.morph) {
104
+ return isEquality(rulesResult) || isDisjoint(rulesResult) ? rulesResult : {
105
+ rules: rulesResult,
106
+ morph: l.morph
107
+ };
108
+ }
109
+ return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
110
+ }
111
+ return isDisjoint(rulesResult) ? rulesResult : {
112
+ rules: isEquality(rulesResult) ? l.rules : rulesResult,
113
+ morph: l.morph
114
+ };
115
+ }
116
+ if ("morph" in r) {
117
+ return isDisjoint(rulesResult) ? rulesResult : {
118
+ rules: isEquality(rulesResult) ? r.rules : rulesResult,
119
+ morph: r.morph
120
+ };
121
+ }
122
+ return rulesResult;
123
+ };
@@ -39,16 +39,15 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
39
39
  return value;
40
40
  }
41
41
  import { throwInternalError } from "../utils/errors.js";
42
- import { keysOf } from "../utils/generics.js";
42
+ import { objectKeysOf } from "../utils/generics.js";
43
43
  import { Path } from "../utils/paths.js";
44
44
  import { serialize } from "../utils/serialize.js";
45
45
  export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwUndefinedOperandsError() : r : r === undefined ? l : reducer(l, r, state);
46
46
  export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
47
47
  export const disjointDescriptionWriters = {
48
48
  domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
49
- objectKind: ({ l , r })=>`${l} and ${r}`,
50
49
  range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
51
- class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
50
+ class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
52
51
  tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
53
52
  value: ({ l , r })=>`literal values ${serialize(l)} and ${serialize(r)}`,
54
53
  leftAssignability: ({ l , r })=>`literal value ${serialize(l.value)} and ${serialize(r)}`,
@@ -88,7 +87,7 @@ export const equality = ()=>equal;
88
87
  export const isEquality = (result)=>result === equal;
89
88
  export const composeKeyedIntersection = (reducer, config)=>(l, r, state)=>{
90
89
  const result = {};
91
- const keys = keysOf({
90
+ const keys = objectKeysOf({
92
91
  ...l,
93
92
  ...r
94
93
  });
@@ -1,13 +1,13 @@
1
- import { writeUndiscriminatableMorphUnionMessage } from "../parse/string/ast.js";
1
+ import { writeUndiscriminatableMorphUnionMessage } from "../parse/ast/union.js";
2
2
  import { domainOf } from "../utils/domains.js";
3
3
  import { throwParseError } from "../utils/errors.js";
4
- import { hasKey, isKeyOf, keysOf } from "../utils/generics.js";
4
+ import { hasKey, isKeyOf, objectKeysOf } from "../utils/generics.js";
5
5
  import { isArray, objectKindOf } from "../utils/objectKinds.js";
6
6
  import { Path } from "../utils/paths.js";
7
7
  import { serializePrimitive } from "../utils/serialize.js";
8
+ import { branchIntersection, flattenBranch } from "./branch.js";
8
9
  import { IntersectionState } from "./compose.js";
9
- import { isOptional } from "./rules/props.js";
10
- import { branchIntersection, flattenBranch } from "./rules/rules.js";
10
+ import { mappedKeys, propToNode } from "./rules/props.js";
11
11
  export const flattenBranches = (branches, ctx)=>{
12
12
  const discriminants = calculateDiscriminants(branches, ctx);
13
13
  const indices = branches.map((_, i)=>i);
@@ -22,7 +22,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
22
22
  return [
23
23
  [
24
24
  "branches",
25
- remainingIndices.map((i)=>flattenBranch(originalBranches[i], ctx))
25
+ remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`)) : flattenBranch(originalBranches[i], ctx))
26
26
  ]
27
27
  ];
28
28
  }
@@ -53,7 +53,6 @@ const calculateDiscriminants = (branches, ctx)=>{
53
53
  disjointsByPair: {},
54
54
  casesByDisjoint: {}
55
55
  };
56
- const morphBranches = branches.map((branch)=>branchIncludesMorph(branch, ctx.type.meta.scope));
57
56
  for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
58
57
  for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
59
58
  const pairKey = `${lIndex}/${rIndex}`;
@@ -62,8 +61,7 @@ const calculateDiscriminants = (branches, ctx)=>{
62
61
  const intersectionState = new IntersectionState(ctx.type, "|");
63
62
  branchIntersection(branches[lIndex], branches[rIndex], intersectionState);
64
63
  for(const path in intersectionState.disjoints){
65
- // designed to match objectKind path segments like "${number}"
66
- if (path.includes("${")) {
64
+ if (path.includes(mappedKeys.index)) {
67
65
  continue;
68
66
  }
69
67
  const { l , r , kind } = intersectionState.disjoints[path];
@@ -104,9 +102,6 @@ const calculateDiscriminants = (branches, ctx)=>{
104
102
  cases[rSerialized].push(rIndex);
105
103
  }
106
104
  }
107
- if ((morphBranches[lIndex] === true || morphBranches[rIndex] === true) && pairDisjoints.length === 0) {
108
- return throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`));
109
- }
110
105
  }
111
106
  }
112
107
  return discriminants;
@@ -147,10 +142,9 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
147
142
  filteredCases[caseKey] = filteredIndices;
148
143
  score++;
149
144
  }
150
- const defaultCaseKeys = keysOf(defaultCases);
145
+ const defaultCaseKeys = objectKeysOf(defaultCases);
151
146
  if (defaultCaseKeys.length) {
152
- const defaultIndices = defaultCaseKeys.map((k)=>parseInt(k));
153
- filteredCases["default"] = defaultIndices;
147
+ filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
154
148
  }
155
149
  if (!bestDiscriminant || score > bestDiscriminant.score) {
156
150
  const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
@@ -181,5 +175,5 @@ const caseSerializers = {
181
175
  domain: domainOf
182
176
  };
183
177
  export const serializeCase = (kind, data)=>caseSerializers[kind](data);
184
- const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(isOptional(prop) ? prop[1] : prop, $)) : false;
178
+ const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
185
179
  const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));