arktype 0.1.2 → 0.1.4

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 (72) hide show
  1. package/dist/cjs/main.js +135 -20
  2. package/dist/cjs/nodes/branches.js +7 -7
  3. package/dist/cjs/nodes/compose.js +3 -2
  4. package/dist/cjs/nodes/discriminate.js +5 -4
  5. package/dist/cjs/nodes/functors.js +3 -3
  6. package/dist/cjs/nodes/node.js +1 -1
  7. package/dist/cjs/nodes/predicate.js +19 -14
  8. package/dist/cjs/nodes/rules/collapsibleSet.js +3 -4
  9. package/dist/cjs/nodes/rules/objectKind.js +78 -0
  10. package/dist/cjs/nodes/rules/range.js +2 -2
  11. package/dist/cjs/nodes/rules/rules.js +5 -5
  12. package/dist/cjs/parse/definition.js +26 -6
  13. package/dist/cjs/parse/string/ast.js +1 -1
  14. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  15. package/dist/cjs/parse/tuple/keyof.js +6 -7
  16. package/dist/cjs/parse/tuple/morph.js +2 -2
  17. package/dist/cjs/parse/tuple/tuple.js +1 -1
  18. package/dist/cjs/traverse/problems.js +18 -32
  19. package/dist/cjs/traverse/traverse.js +12 -12
  20. package/dist/cjs/utils/domains.js +10 -47
  21. package/dist/cjs/utils/generics.js +1 -3
  22. package/dist/cjs/utils/objectKinds.js +64 -0
  23. package/dist/cjs/utils/size.js +16 -0
  24. package/dist/mjs/main.js +134 -19
  25. package/dist/mjs/nodes/branches.js +7 -7
  26. package/dist/mjs/nodes/compose.js +3 -2
  27. package/dist/mjs/nodes/discriminate.js +6 -5
  28. package/dist/mjs/nodes/functors.js +3 -3
  29. package/dist/mjs/nodes/node.js +1 -1
  30. package/dist/mjs/nodes/predicate.js +20 -15
  31. package/dist/mjs/nodes/rules/collapsibleSet.js +3 -4
  32. package/dist/mjs/nodes/rules/{subdomain.js → objectKind.js} +10 -10
  33. package/dist/mjs/nodes/rules/range.js +1 -1
  34. package/dist/mjs/nodes/rules/rules.js +5 -5
  35. package/dist/mjs/parse/definition.js +27 -7
  36. package/dist/mjs/parse/string/ast.js +1 -1
  37. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  38. package/dist/mjs/parse/tuple/keyof.js +6 -7
  39. package/dist/mjs/parse/tuple/morph.js +2 -2
  40. package/dist/mjs/parse/tuple/tuple.js +1 -1
  41. package/dist/mjs/traverse/problems.js +18 -31
  42. package/dist/mjs/traverse/traverse.js +12 -12
  43. package/dist/mjs/utils/domains.js +9 -42
  44. package/dist/mjs/utils/generics.js +0 -1
  45. package/dist/mjs/utils/objectKinds.js +48 -0
  46. package/dist/mjs/utils/size.js +2 -0
  47. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  48. package/dist/types/main.d.ts +82 -15
  49. package/dist/types/nodes/branches.d.ts +4 -4
  50. package/dist/types/nodes/compose.d.ts +12 -5
  51. package/dist/types/nodes/discriminate.d.ts +3 -2
  52. package/dist/types/nodes/infer.d.ts +11 -10
  53. package/dist/types/nodes/predicate.d.ts +2 -2
  54. package/dist/types/nodes/rules/objectKind.d.ts +15 -0
  55. package/dist/types/nodes/rules/range.d.ts +1 -1
  56. package/dist/types/nodes/rules/rules.d.ts +4 -4
  57. package/dist/types/parse/definition.d.ts +6 -5
  58. package/dist/types/parse/record.d.ts +2 -2
  59. package/dist/types/parse/string/ast.d.ts +7 -5
  60. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  61. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  62. package/dist/types/parse/tuple/keyof.d.ts +8 -0
  63. package/dist/types/parse/tuple/tuple.d.ts +6 -12
  64. package/dist/types/traverse/problems.d.ts +25 -41
  65. package/dist/types/traverse/traverse.d.ts +24 -4
  66. package/dist/types/utils/domains.d.ts +11 -37
  67. package/dist/types/utils/generics.d.ts +2 -2
  68. package/dist/types/utils/objectKinds.d.ts +83 -0
  69. package/dist/types/utils/size.d.ts +3 -0
  70. package/package.json +4 -4
  71. package/dist/cjs/nodes/rules/subdomain.js +0 -78
  72. package/dist/types/nodes/rules/subdomain.d.ts +0 -15
package/dist/cjs/main.js CHANGED
@@ -15,7 +15,7 @@ _export(exports, {
15
15
  scopes: ()=>scopes,
16
16
  spaces: ()=>spaces,
17
17
  type: ()=>type,
18
- writeDuplicateAliasMessage: ()=>writeDuplicateAliasMessage,
18
+ writeDuplicateAliasesMessage: ()=>writeDuplicateAliasesMessage,
19
19
  isType: ()=>isType
20
20
  });
21
21
  const _nodeJs = require("./nodes/node.js");
@@ -108,8 +108,8 @@ let anonymousScopeCount = 0;
108
108
  const scopeRegistry = {};
109
109
  var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _anonymousTypeCount = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _compiled = /*#__PURE__*/ new WeakMap(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
110
110
  class Scope {
111
- createAnonymousTypeName() {
112
- return `anonymousType${++_classPrivateFieldUpdate(this, _anonymousTypeCount).value}`;
111
+ createAnonymousTypeSuffix() {
112
+ return ++_classPrivateFieldUpdate(this, _anonymousTypeCount).value;
113
113
  }
114
114
  get infer() {
115
115
  return _chainableNoOpProxyJs.chainableNoOpProxy;
@@ -168,6 +168,9 @@ class Scope {
168
168
  _classPrivateFieldSet(this, _exports, new Cache());
169
169
  _classPrivateFieldSet(this, _anonymousTypeCount, 0);
170
170
  this.type = (def, opts = {})=>{
171
+ if (opts.name && this.aliases[opts.name]) {
172
+ return (0, _errorsJs.throwParseError)(writeDuplicateAliasesMessage(opts.name));
173
+ }
171
174
  const result = initializeType(def, opts, this);
172
175
  const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, result);
173
176
  result.node = this.resolveNode((0, _definitionJs.parseDefinition)(def, ctx));
@@ -190,7 +193,7 @@ class Scope {
190
193
  }
191
194
  }
192
195
  function register(opts) {
193
- const name = opts.name ? scopeRegistry[opts.name] ? (0, _errorsJs.throwParseError)(`A scope named '${opts.name}' already exists`) : opts.name : `anonymousScope${++anonymousScopeCount}`;
196
+ const name = opts.name ? scopeRegistry[opts.name] ? (0, _errorsJs.throwParseError)(`A scope named '${opts.name}' already exists`) : opts.name : `scope${++anonymousScopeCount}`;
194
197
  scopeRegistry[name] = this;
195
198
  return name;
196
199
  }
@@ -198,7 +201,7 @@ function cacheSpaces(spaces, kind) {
198
201
  for (const space of spaces){
199
202
  for(const name in space){
200
203
  if (_classPrivateFieldGet(this, _resolutions).has(name) || name in this.aliases) {
201
- (0, _errorsJs.throwParseError)(writeDuplicateAliasMessage(name));
204
+ (0, _errorsJs.throwParseError)(writeDuplicateAliasesMessage(name));
202
205
  }
203
206
  _classPrivateFieldGet(this, _resolutions).set(name, space[name]);
204
207
  if (kind === "includes") {
@@ -242,10 +245,10 @@ function resolveRecurse(name, onUnresolvable, seen) {
242
245
  }
243
246
  // TODO: merge scope options
244
247
  const initializeType = (definition, opts, scope)=>{
245
- const name = opts.name ?? scope.createAnonymousTypeName();
248
+ const name = opts.name ?? "type";
246
249
  const meta = {
247
250
  name,
248
- id: `${scope.name}.${name}`,
251
+ id: `${scope.name}.${opts.name ? name : `type${scope.createAnonymousTypeSuffix()}`}`,
249
252
  definition,
250
253
  scope,
251
254
  problems: (0, _problemsTs.compileProblemOptions)(opts.problems),
@@ -322,7 +325,7 @@ const always = {
322
325
  undefined: true
323
326
  };
324
327
  const scope = (aliases, opts = {})=>new Scope(aliases, opts);
325
- const ts = scope({
328
+ const tsKeywords = scope({
326
329
  any: [
327
330
  "node",
328
331
  always
@@ -404,21 +407,130 @@ const ts = scope({
404
407
  {
405
408
  undefined: true
406
409
  }
407
- ],
408
- // TODO: Add remaining JS object types
410
+ ]
411
+ }, {
412
+ name: "ts",
413
+ standard: false
414
+ });
415
+ const tsKeywordsSpace = tsKeywords.compile();
416
+ const jsObjects = scope({
409
417
  Function: [
410
418
  "node",
411
419
  {
412
420
  object: {
413
- subdomain: "Function"
421
+ objectKind: "Function"
422
+ }
423
+ }
424
+ ],
425
+ Array: [
426
+ "node",
427
+ {
428
+ object: {
429
+ objectKind: "Array"
430
+ }
431
+ }
432
+ ],
433
+ Date: [
434
+ "node",
435
+ {
436
+ object: {
437
+ objectKind: "Date"
438
+ }
439
+ }
440
+ ],
441
+ Error: [
442
+ "node",
443
+ {
444
+ object: {
445
+ objectKind: "Error"
446
+ }
447
+ }
448
+ ],
449
+ Map: [
450
+ "node",
451
+ {
452
+ object: {
453
+ objectKind: "Map"
454
+ }
455
+ }
456
+ ],
457
+ RegExp: [
458
+ "node",
459
+ {
460
+ object: {
461
+ objectKind: "RegExp"
462
+ }
463
+ }
464
+ ],
465
+ Set: [
466
+ "node",
467
+ {
468
+ object: {
469
+ objectKind: "Set"
470
+ }
471
+ }
472
+ ],
473
+ Object: [
474
+ "node",
475
+ {
476
+ object: {
477
+ objectKind: "Object"
478
+ }
479
+ }
480
+ ],
481
+ String: [
482
+ "node",
483
+ {
484
+ object: {
485
+ objectKind: "String"
486
+ }
487
+ }
488
+ ],
489
+ Number: [
490
+ "node",
491
+ {
492
+ object: {
493
+ objectKind: "Number"
494
+ }
495
+ }
496
+ ],
497
+ Boolean: [
498
+ "node",
499
+ {
500
+ object: {
501
+ objectKind: "Boolean"
502
+ }
503
+ }
504
+ ],
505
+ WeakMap: [
506
+ "node",
507
+ {
508
+ object: {
509
+ objectKind: "WeakMap"
510
+ }
511
+ }
512
+ ],
513
+ WeakSet: [
514
+ "node",
515
+ {
516
+ object: {
517
+ objectKind: "WeakSet"
518
+ }
519
+ }
520
+ ],
521
+ Promise: [
522
+ "node",
523
+ {
524
+ object: {
525
+ objectKind: "Promise"
414
526
  }
415
527
  }
416
528
  ]
417
529
  }, {
418
- name: "ts",
530
+ name: "jsObjects",
419
531
  standard: false
420
532
  });
421
- const tsTypes = ts.compile();
533
+ const jsObjectsSpace = jsObjects.compile();
422
534
  const validation = scope({
423
535
  email: /^(.+)@(.+)\.(.+)$/,
424
536
  alphanumeric: /^[dA-Za-z]+$/,
@@ -437,26 +549,29 @@ const validation = scope({
437
549
  name: "validation",
438
550
  standard: false
439
551
  });
440
- const validationTypes = validation.compile();
552
+ const validationSpace = validation.compile();
441
553
  const standard = scope({}, {
442
554
  name: "standard",
443
555
  includes: [
444
- tsTypes,
445
- validationTypes
556
+ tsKeywordsSpace,
557
+ jsObjectsSpace,
558
+ validationSpace
446
559
  ],
447
560
  standard: false
448
561
  });
449
562
  const standardTypes = standard.compile();
450
563
  const scopes = {
451
- ts,
564
+ tsKeywords,
565
+ jsObjects,
452
566
  validation,
453
567
  standard
454
568
  };
455
569
  const spaces = {
456
- ts: tsTypes,
457
- validation: validationTypes,
570
+ tsKeywords: tsKeywordsSpace,
571
+ jsObjects: jsObjectsSpace,
572
+ validation: validationSpace,
458
573
  standard: standardTypes
459
574
  };
460
575
  const type = scopes.standard.type;
461
- const writeDuplicateAliasMessage = (name)=>`Alias '${name}' is already defined`;
576
+ const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
462
577
  const isType = (value)=>value?.infer === _chainableNoOpProxyJs.chainableNoOpProxy;
@@ -16,13 +16,13 @@ const _domainsTs = require("../utils/domains.js");
16
16
  const _errorsTs = require("../utils/errors.js");
17
17
  const _composeTs = require("./compose.js");
18
18
  const _rulesTs = require("./rules/rules.js");
19
- const isBranchComparison = (comparison)=>comparison?.lConditions !== undefined;
19
+ const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
20
20
  const compareBranches = (lConditions, rConditions, state)=>{
21
21
  const result = {
22
- lConditions,
23
- rConditions,
24
- lSubconditionsOfR: [],
25
- rSubconditionsOfL: [],
22
+ lBranches: lConditions,
23
+ rBranches: rConditions,
24
+ lExtendsR: [],
25
+ rExtendsL: [],
26
26
  equalities: [],
27
27
  distinctIntersections: []
28
28
  };
@@ -42,14 +42,14 @@ const compareBranches = (lConditions, rConditions, state)=>{
42
42
  // doesn't tell us about any redundancies or add a distinct pair
43
43
  return null;
44
44
  } else if (subresult === l) {
45
- result.lSubconditionsOfR.push(lIndex);
45
+ result.lExtendsR.push(lIndex);
46
46
  // If l is a subtype of the current r branch, intersections
47
47
  // with the remaining branches of r won't lead to distinct
48
48
  // branches, so we set a flag indicating we can skip them.
49
49
  lImpliesR = true;
50
50
  return null;
51
51
  } else if (subresult === r) {
52
- result.rSubconditionsOfL.push(rIndex);
52
+ result.rExtendsL.push(rIndex);
53
53
  // If r is a subtype of the current l branch, it is removed
54
54
  // from pairsByR because future intersections won't lead to
55
55
  // distinct branches.
@@ -68,13 +68,14 @@ const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === un
68
68
  const throwUndefinedOperandsError = ()=>(0, _errorsTs.throwInternalError)(`Unexpected operation two undefined operands`);
69
69
  const disjointDescriptionWriters = {
70
70
  domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
71
- subdomain: ({ l , r })=>`${l} and ${r}`,
71
+ objectKind: ({ l , r })=>`${l} and ${r}`,
72
72
  range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
73
73
  class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
74
74
  tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
75
75
  value: ({ l , r })=>`literal values ${(0, _serializeTs.serialize)(l)} and ${(0, _serializeTs.serialize)(r)}`,
76
76
  leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(l.value)} and ${(0, _serializeTs.serialize)(r)}`,
77
- rightAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(r.value)} and ${(0, _serializeTs.serialize)(l)}`
77
+ rightAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(r.value)} and ${(0, _serializeTs.serialize)(l)}`,
78
+ union: ({ l , r })=>`branches ${(0, _serializeTs.serialize)(l)} and branches ${(0, _serializeTs.serialize)(r)}`
78
79
  };
79
80
  const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
80
81
  var _disjoints = /*#__PURE__*/ new WeakMap();
@@ -17,6 +17,7 @@ const _astTs = require("../parse/string/ast.js");
17
17
  const _domainsTs = require("../utils/domains.js");
18
18
  const _errorsTs = require("../utils/errors.js");
19
19
  const _genericsTs = require("../utils/generics.js");
20
+ const _objectKindsTs = require("../utils/objectKinds.js");
20
21
  const _pathsTs = require("../utils/paths.js");
21
22
  const _serializeTs = require("../utils/serialize.js");
22
23
  const _composeTs = require("./compose.js");
@@ -59,7 +60,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
59
60
  };
60
61
  const discriminantKinds = {
61
62
  domain: true,
62
- subdomain: true,
63
+ objectKind: true,
63
64
  value: true
64
65
  };
65
66
  const calculateDiscriminants = (branches, ctx)=>{
@@ -76,7 +77,7 @@ const calculateDiscriminants = (branches, ctx)=>{
76
77
  const intersectionState = new _composeTs.IntersectionState(ctx.type, "|");
77
78
  (0, _rulesTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
78
79
  for(const path in intersectionState.disjoints){
79
- // designed to match subdomain path segments like "${number}"
80
+ // designed to match objectKind path segments like "${number}"
80
81
  if (path.includes("${")) {
81
82
  continue;
82
83
  }
@@ -191,9 +192,9 @@ const serializeIfPrimitive = (data)=>{
191
192
  };
192
193
  const caseSerializers = {
193
194
  value: (data)=>serializeIfPrimitive(data) ?? "default",
194
- subdomain: _domainsTs.subdomainOf,
195
+ objectKind: (data)=>(0, _objectKindsTs.objectKindOf)(data) ?? "default",
195
196
  domain: _domainsTs.domainOf
196
197
  };
197
198
  const serializeCase = (kind, data)=>caseSerializers[kind](data);
198
199
  const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.isOptional)(prop) ? prop[1] : prop, $)) : false;
199
- const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _domainsTs.hasSubdomain)(predicate, "Array") ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
200
+ const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "functors", {
9
9
  const functors = {
10
10
  Array: (node)=>({
11
11
  object: {
12
- subdomain: [
12
+ objectKind: [
13
13
  "Array",
14
14
  node
15
15
  ]
@@ -17,7 +17,7 @@ const functors = {
17
17
  }),
18
18
  Set: (node)=>({
19
19
  object: {
20
- subdomain: [
20
+ objectKind: [
21
21
  "Set",
22
22
  node
23
23
  ]
@@ -25,7 +25,7 @@ const functors = {
25
25
  }),
26
26
  Map: (k, v)=>({
27
27
  object: {
28
- subdomain: [
28
+ objectKind: [
29
29
  "Map",
30
30
  k,
31
31
  v
@@ -61,7 +61,7 @@ const union = (l, r, type)=>{
61
61
  }
62
62
  return result;
63
63
  };
64
- const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "subdomain" || flatPredicate[0][0] === "value");
64
+ const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "objectKind" || flatPredicate[0][0] === "value");
65
65
  const flattenType = (type)=>{
66
66
  const ctx = {
67
67
  type,
@@ -16,8 +16,8 @@ _export(exports, {
16
16
  isLiteralCondition: ()=>isLiteralCondition,
17
17
  resolutionExtendsDomain: ()=>resolutionExtendsDomain
18
18
  });
19
- const _domainsTs = require("../utils/domains.js");
20
19
  const _genericsTs = require("../utils/generics.js");
20
+ const _objectKindsTs = require("../utils/objectKinds.js");
21
21
  const _branchesTs = require("./branches.js");
22
22
  const _composeTs = require("./compose.js");
23
23
  const _discriminateTs = require("./discriminate.js");
@@ -27,7 +27,7 @@ const comparePredicates = (l, r, context)=>{
27
27
  if (l === true && r === true) {
28
28
  return (0, _composeTs.equality)();
29
29
  }
30
- if (!(0, _domainsTs.hasSubdomain)(l, "Array") && !(0, _domainsTs.hasSubdomain)(r, "Array")) {
30
+ if (!(0, _objectKindsTs.isArray)(l) && !(0, _objectKindsTs.isArray)(r)) {
31
31
  const result = (0, _rulesTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
32
32
  return result === l ? l : result === r ? r : result;
33
33
  }
@@ -37,10 +37,10 @@ const comparePredicates = (l, r, context)=>{
37
37
  if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
38
38
  return (0, _composeTs.equality)();
39
39
  }
40
- if (comparison.lSubconditionsOfR.length + comparison.equalities.length === lBranches.length) {
40
+ if (comparison.lExtendsR.length + comparison.equalities.length === lBranches.length) {
41
41
  return l;
42
42
  }
43
- if (comparison.rSubconditionsOfL.length + comparison.equalities.length === rBranches.length) {
43
+ if (comparison.rExtendsL.length + comparison.equalities.length === rBranches.length) {
44
44
  return r;
45
45
  }
46
46
  return comparison;
@@ -51,12 +51,16 @@ const predicateIntersection = (domain, l, r, state)=>{
51
51
  if (!(0, _branchesTs.isBranchComparison)(comparison)) {
52
52
  return comparison;
53
53
  }
54
- return (0, _genericsTs.collapseIfSingleton)([
54
+ const resultBranches = [
55
55
  ...comparison.distinctIntersections,
56
- ...comparison.equalities.map((indices)=>comparison.lConditions[indices[0]]),
57
- ...comparison.lSubconditionsOfR.map((lIndex)=>comparison.lConditions[lIndex]),
58
- ...comparison.rSubconditionsOfL.map((rIndex)=>comparison.rConditions[rIndex])
59
- ]);
56
+ ...comparison.equalities.map((indices)=>comparison.lBranches[indices[0]]),
57
+ ...comparison.lExtendsR.map((lIndex)=>comparison.lBranches[lIndex]),
58
+ ...comparison.rExtendsL.map((rIndex)=>comparison.rBranches[rIndex])
59
+ ];
60
+ if (resultBranches.length === 0) {
61
+ state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
62
+ }
63
+ return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
60
64
  };
61
65
  const predicateUnion = (domain, l, r, type)=>{
62
66
  const state = new _composeTs.IntersectionState(type, "|");
@@ -69,16 +73,17 @@ const predicateUnion = (domain, l, r, type)=>{
69
73
  emptyRulesIfTrue(r)
70
74
  ];
71
75
  }
72
- return (0, _genericsTs.collapseIfSingleton)([
73
- ...comparison.lConditions.filter((_, lIndex)=>!comparison.lSubconditionsOfR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
74
- ...comparison.rConditions.filter((_, rIndex)=>!comparison.rSubconditionsOfL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
75
- ]);
76
+ const resultBranches = [
77
+ ...comparison.lBranches.filter((_, lIndex)=>!comparison.lExtendsR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
78
+ ...comparison.rBranches.filter((_, rIndex)=>!comparison.rExtendsL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
79
+ ];
80
+ return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
76
81
  };
77
82
  const flattenPredicate = (predicate, context)=>{
78
83
  if (predicate === true) {
79
84
  return [];
80
85
  }
81
- return (0, _domainsTs.hasSubdomain)(predicate, "Array") ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _rulesTs.flattenBranch)(predicate, context);
86
+ return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _rulesTs.flattenBranch)(predicate, context);
82
87
  };
83
88
  const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
84
89
  const resolutionExtendsDomain = (resolution, domain)=>{
@@ -12,11 +12,10 @@ _export(exports, {
12
12
  collapsibleListUnion: ()=>collapsibleListUnion,
13
13
  listUnion: ()=>listUnion
14
14
  });
15
- const _domainsTs = require("../../utils/domains.js");
16
15
  const _composeTs = require("../compose.js");
17
16
  const collapsibleListUnion = (l, r)=>{
18
- if ((0, _domainsTs.hasSubdomain)(l, "Array")) {
19
- if ((0, _domainsTs.hasSubdomain)(r, "Array")) {
17
+ if (Array.isArray(l)) {
18
+ if (Array.isArray(r)) {
20
19
  const result = listUnion(l, r);
21
20
  return result.length === l.length ? result.length === r.length ? (0, _composeTs.equality)() : l : result.length === r.length ? r : result;
22
21
  }
@@ -25,7 +24,7 @@ const collapsibleListUnion = (l, r)=>{
25
24
  r
26
25
  ];
27
26
  }
28
- if ((0, _domainsTs.hasSubdomain)(r, "Array")) {
27
+ if (Array.isArray(r)) {
29
28
  return r.includes(l) ? r : [
30
29
  ...r,
31
30
  l
@@ -0,0 +1,78 @@
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
+ objectKindIntersection: ()=>objectKindIntersection,
13
+ objectKindPathSegments: ()=>objectKindPathSegments,
14
+ flattenObjectKind: ()=>flattenObjectKind
15
+ });
16
+ const _errorsJs = require("../../utils/errors.js");
17
+ const _composeJs = require("../compose.js");
18
+ const _nodeJs = require("../node.js");
19
+ const objectKindIntersection = (0, _composeJs.composeIntersection)((l, r, state)=>{
20
+ if (typeof l === "string") {
21
+ if (typeof r === "string") {
22
+ return l === r ? (0, _composeJs.equality)() : state.addDisjoint("objectKind", l, r);
23
+ }
24
+ return l === r[0] ? r : state.addDisjoint("objectKind", l, r[0]);
25
+ }
26
+ if (typeof r === "string") {
27
+ return l[0] === r ? l : state.addDisjoint("objectKind", l[0], r);
28
+ }
29
+ if (l[0] !== r[0]) {
30
+ return state.addDisjoint("objectKind", l[0], r[0]);
31
+ }
32
+ const result = [
33
+ l[0]
34
+ ];
35
+ let lImpliesR = true;
36
+ let rImpliesL = true;
37
+ for(let i = 1; i < l.length; i++){
38
+ const lNode = l[i];
39
+ const rNode = r[i];
40
+ state.path.push(objectKindParameterToPathSegment(l[0], i));
41
+ const parameterResult = (0, _nodeJs.nodeIntersection)(lNode, rNode, state);
42
+ state.path.pop();
43
+ if ((0, _composeJs.isEquality)(parameterResult)) {
44
+ result[i] = lNode;
45
+ } else if (parameterResult === l) {
46
+ result[i] = lNode;
47
+ rImpliesL = false;
48
+ } else if (parameterResult === r) {
49
+ result[i] = rNode;
50
+ lImpliesR = false;
51
+ } else if ((0, _composeJs.isDisjoint)(parameterResult)) {
52
+ return parameterResult;
53
+ } else {
54
+ result[i] = parameterResult;
55
+ lImpliesR = false;
56
+ rImpliesL = false;
57
+ }
58
+ }
59
+ return lImpliesR ? rImpliesL ? (0, _composeJs.equality)() : l : rImpliesL ? r : result;
60
+ });
61
+ const objectKindParameterToPathSegment = (objectKind, i)=>objectKind === "Array" ? "${number}" : objectKind === "Set" ? "${item}" : objectKind === "Map" ? i === 1 ? "${key}" : "${value}" : (0, _errorsJs.throwInternalError)(`Unexpected parameterized objectKind '${objectKind}'`);
62
+ const objectKindPathSegments = {
63
+ "${number}": true,
64
+ "${item}": true,
65
+ "${key}": true,
66
+ "${value}": true
67
+ };
68
+ const flattenObjectKind = (entries, rule, ctx)=>entries.push([
69
+ "objectKind",
70
+ typeof rule === "string" ? rule : rule[0] === "Map" ? [
71
+ rule[0],
72
+ (0, _nodeJs.flattenNode)(rule[1], ctx),
73
+ (0, _nodeJs.flattenNode)(rule[2], ctx)
74
+ ] : [
75
+ rule[0],
76
+ (0, _nodeJs.flattenNode)(rule[1], ctx)
77
+ ]
78
+ ]);
@@ -17,7 +17,7 @@ _export(exports, {
17
17
  checkBound: ()=>checkBound,
18
18
  compareStrictness: ()=>compareStrictness
19
19
  });
20
- const _domainsTs = require("../../utils/domains.js");
20
+ const _sizeTs = require("../../utils/size.js");
21
21
  const _composeTs = require("../compose.js");
22
22
  const minComparators = {
23
23
  ">": true,
@@ -94,7 +94,7 @@ const flattenRange = (entries, range, ctx)=>{
94
94
  }
95
95
  };
96
96
  const checkBound = (data, bound, state)=>{
97
- if (!comparatorCheckers[bound.comparator]((0, _domainsTs.sizeOf)(data), bound.limit)) {
97
+ if (!comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(data), bound.limit)) {
98
98
  state.problems.add("bound", data, bound);
99
99
  }
100
100
  };
@@ -23,10 +23,10 @@ const _composeTs = require("../compose.js");
23
23
  const _classTs = require("./class.js");
24
24
  const _collapsibleSetTs = require("./collapsibleSet.js");
25
25
  const _divisorTs = require("./divisor.js");
26
+ const _objectKindTs = require("./objectKind.js");
26
27
  const _propsTs = require("./props.js");
27
28
  const _rangeTs = require("./range.js");
28
29
  const _regexTs = require("./regex.js");
29
- const _subdomainTs = require("./subdomain.js");
30
30
  const rulesOf = (branch)=>branch.input ?? branch;
31
31
  const branchIntersection = (l, r, state)=>{
32
32
  const lRules = rulesOf(l);
@@ -40,7 +40,7 @@ const branchIntersection = (l, r, state)=>{
40
40
  morph: l.morph
41
41
  };
42
42
  }
43
- return state.lastOperator === "&" ? (0, _errorsTs.throwParseError)((0, _astTs.writeImplicitNeverMessage)(state.path, "of morphs")) : {};
43
+ return state.lastOperator === "&" ? (0, _errorsTs.throwParseError)((0, _astTs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
44
44
  }
45
45
  return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
46
46
  input: (0, _composeTs.isEquality)(rulesResult) ? l.input : rulesResult,
@@ -58,7 +58,7 @@ const branchIntersection = (l, r, state)=>{
58
58
  const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? (0, _composeTs.equality)() : state.addDisjoint("value", l.value, r.value) : literalSatisfiesRules(l.value, r, state) ? l : state.addDisjoint("leftAssignability", l, r) : "value" in r ? literalSatisfiesRules(r.value, l, state) ? r : state.addDisjoint("rightAssignability", l, r) : narrowableRulesIntersection(l, r, state);
59
59
  const narrowIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
60
60
  const narrowableRulesIntersection = (0, _composeTs.composeKeyedIntersection)({
61
- subdomain: _subdomainTs.subdomainIntersection,
61
+ objectKind: _objectKindTs.objectKindIntersection,
62
62
  divisor: _divisorTs.divisorIntersection,
63
63
  regex: _regexTs.regexIntersection,
64
64
  props: _propsTs.propsIntersection,
@@ -69,7 +69,7 @@ const narrowableRulesIntersection = (0, _composeTs.composeKeyedIntersection)({
69
69
  onEmpty: "bubble"
70
70
  });
71
71
  const ruleFlatteners = {
72
- subdomain: _subdomainTs.flattenSubdomain,
72
+ objectKind: _objectKindTs.flattenObjectKind,
73
73
  regex: (entries, rule)=>{
74
74
  for (const source of (0, _genericsTs.listFrom)(rule)){
75
75
  entries.push([
@@ -113,7 +113,7 @@ const precedenceMap = {
113
113
  value: 0,
114
114
  domains: 0,
115
115
  branches: 0,
116
- subdomain: 0,
116
+ objectKind: 0,
117
117
  switch: 0,
118
118
  alias: 0,
119
119
  // Shallow: All shallow checks will be performed even if one or more fail
@@ -17,16 +17,36 @@ _export(exports, {
17
17
  const _mainTs = require("../main.js");
18
18
  const _domainsTs = require("../utils/domains.js");
19
19
  const _errorsTs = require("../utils/errors.js");
20
+ const _objectKindsTs = require("../utils/objectKinds.js");
21
+ const _serializeTs = require("../utils/serialize.js");
20
22
  const _recordTs = require("./record.js");
21
23
  const _stringTs = require("./string/string.js");
22
24
  const _tupleTs = require("./tuple/tuple.js");
23
25
  const parseDefinition = (def, ctx)=>{
24
- const subdomain = (0, _domainsTs.subdomainOf)(def);
25
- return subdomain === "string" ? (0, _stringTs.parseString)(def, ctx) : subdomain === "object" ? (0, _recordTs.parseRecord)(def, ctx) : subdomain === "Array" ? (0, _tupleTs.parseTuple)(def, ctx) : subdomain === "RegExp" ? {
26
- string: {
27
- regex: def.source
28
- }
29
- } : (0, _mainTs.isType)(def) ? def.node : (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage(subdomain));
26
+ const domain = (0, _domainsTs.domainOf)(def);
27
+ if (domain === "string") {
28
+ return (0, _stringTs.parseString)(def, ctx);
29
+ }
30
+ if (domain !== "object") {
31
+ return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage(domain));
32
+ }
33
+ const objectKind = (0, _objectKindsTs.objectKindOf)(def);
34
+ switch(objectKind){
35
+ case "Object":
36
+ return (0, _recordTs.parseRecord)(def, ctx);
37
+ case "Array":
38
+ return (0, _tupleTs.parseTuple)(def, ctx);
39
+ case "RegExp":
40
+ return {
41
+ string: {
42
+ regex: def.source
43
+ }
44
+ };
45
+ case "Function":
46
+ return (0, _mainTs.isType)(def) ? def.node : (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage("Function"));
47
+ default:
48
+ return (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage(objectKind ?? (0, _serializeTs.stringify)(def)));
49
+ }
30
50
  };
31
51
  const as = Symbol("as");
32
52
  const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";