arktype 0.1.4 → 0.3.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 (106) 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/{branches.js → branch.js} +56 -10
  6. package/dist/cjs/nodes/compose.js +1 -2
  7. package/dist/cjs/nodes/discriminate.js +7 -13
  8. package/dist/cjs/nodes/node.js +32 -4
  9. package/dist/cjs/nodes/predicate.js +7 -7
  10. package/dist/cjs/nodes/rules/class.js +10 -4
  11. package/dist/cjs/nodes/rules/divisor.js +1 -5
  12. package/dist/cjs/nodes/rules/props.js +75 -25
  13. package/dist/cjs/nodes/rules/range.js +1 -5
  14. package/dist/cjs/nodes/rules/regex.js +2 -44
  15. package/dist/cjs/nodes/rules/rules.js +21 -61
  16. package/dist/cjs/parse/definition.js +2 -2
  17. package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
  18. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  19. package/dist/cjs/parse/string/string.js +2 -2
  20. package/dist/cjs/parse/tuple/keyof.js +37 -18
  21. package/dist/cjs/parse/tuple/morph.js +7 -6
  22. package/dist/cjs/parse/tuple/tuple.js +16 -9
  23. package/dist/cjs/scopes/cache.js +39 -0
  24. package/dist/cjs/scopes/jsObjects.js +133 -0
  25. package/dist/cjs/{main.js → scopes/scope.js} +31 -364
  26. package/dist/cjs/scopes/standard.js +44 -0
  27. package/dist/cjs/scopes/tsKeywords.js +113 -0
  28. package/dist/cjs/scopes/type.js +67 -0
  29. package/dist/cjs/scopes/validation.js +64 -0
  30. package/dist/cjs/traverse/problems.js +55 -95
  31. package/dist/cjs/traverse/traverse.js +127 -134
  32. package/dist/cjs/utils/numericLiterals.js +3 -0
  33. package/dist/cjs/utils/objectKinds.js +1 -2
  34. package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
  35. package/dist/mjs/nodes/compose.js +1 -2
  36. package/dist/mjs/nodes/discriminate.js +6 -12
  37. package/dist/mjs/nodes/node.js +30 -3
  38. package/dist/mjs/nodes/predicate.js +2 -2
  39. package/dist/mjs/nodes/rules/class.js +10 -4
  40. package/dist/mjs/nodes/rules/divisor.js +1 -5
  41. package/dist/mjs/nodes/rules/props.js +74 -27
  42. package/dist/mjs/nodes/rules/range.js +1 -5
  43. package/dist/mjs/nodes/rules/regex.js +1 -43
  44. package/dist/mjs/nodes/rules/rules.js +22 -61
  45. package/dist/mjs/parse/definition.js +1 -1
  46. package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
  47. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  48. package/dist/mjs/parse/string/string.js +2 -2
  49. package/dist/mjs/parse/tuple/keyof.js +38 -19
  50. package/dist/mjs/parse/tuple/morph.js +7 -6
  51. package/dist/mjs/parse/tuple/tuple.js +17 -10
  52. package/dist/mjs/scopes/cache.js +25 -0
  53. package/dist/mjs/scopes/jsObjects.js +119 -0
  54. package/dist/mjs/{main.js → scopes/scope.js} +15 -344
  55. package/dist/mjs/scopes/standard.js +27 -0
  56. package/dist/mjs/scopes/tsKeywords.js +99 -0
  57. package/dist/mjs/scopes/type.js +53 -0
  58. package/dist/mjs/scopes/validation.js +51 -0
  59. package/dist/mjs/traverse/problems.js +55 -95
  60. package/dist/mjs/traverse/traverse.js +129 -135
  61. package/dist/mjs/utils/numericLiterals.js +2 -1
  62. package/dist/mjs/utils/objectKinds.js +1 -1
  63. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  64. package/dist/types/nodes/branch.d.ts +36 -0
  65. package/dist/types/nodes/compose.d.ts +6 -11
  66. package/dist/types/nodes/discriminate.d.ts +1 -1
  67. package/dist/types/nodes/infer.d.ts +16 -15
  68. package/dist/types/nodes/node.d.ts +18 -9
  69. package/dist/types/nodes/predicate.d.ts +18 -57
  70. package/dist/types/nodes/rules/class.d.ts +3 -2
  71. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  72. package/dist/types/nodes/rules/props.d.ts +19 -11
  73. package/dist/types/nodes/rules/range.d.ts +2 -1
  74. package/dist/types/nodes/rules/regex.d.ts +2 -11
  75. package/dist/types/nodes/rules/rules.d.ts +11 -21
  76. package/dist/types/parse/definition.d.ts +1 -1
  77. package/dist/types/parse/string/ast.d.ts +4 -2
  78. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  79. package/dist/types/parse/string/reduce/static.d.ts +1 -1
  80. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  81. package/dist/types/parse/string/string.d.ts +1 -1
  82. package/dist/types/parse/tuple/keyof.d.ts +2 -2
  83. package/dist/types/parse/tuple/morph.d.ts +1 -1
  84. package/dist/types/parse/tuple/narrow.d.ts +1 -1
  85. package/dist/types/parse/tuple/tuple.d.ts +1 -1
  86. package/dist/types/scopes/cache.d.ts +14 -0
  87. package/dist/types/scopes/jsObjects.d.ts +36 -0
  88. package/dist/types/scopes/scope.d.ts +76 -0
  89. package/dist/types/scopes/standard.d.ts +280 -0
  90. package/dist/types/scopes/tsKeywords.d.ts +32 -0
  91. package/dist/types/scopes/type.d.ts +47 -0
  92. package/dist/types/scopes/validation.d.ts +21 -0
  93. package/dist/types/traverse/problems.d.ts +35 -53
  94. package/dist/types/traverse/traverse.d.ts +30 -24
  95. package/dist/types/utils/generics.d.ts +0 -1
  96. package/dist/types/utils/numericLiterals.d.ts +2 -0
  97. package/dist/types/utils/objectKinds.d.ts +43 -43
  98. package/package.json +2 -3
  99. package/dist/cjs/nodes/functors.js +0 -35
  100. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  101. package/dist/mjs/nodes/functors.js +0 -27
  102. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  103. package/dist/types/main.d.ts +0 -324
  104. package/dist/types/nodes/branches.d.ts +0 -14
  105. package/dist/types/nodes/functors.d.ts +0 -6
  106. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  </div>
5
5
  <div align="center">
6
6
 
7
- Isomorphic type syntax for TS/JS
7
+ Isomorphic types for TS/JS
8
8
 
9
9
  </div>
10
10
 
package/dist/api.cjs CHANGED
@@ -9,10 +9,11 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- scope: ()=>_mainTs.scope,
13
- type: ()=>_mainTs.type,
12
+ scope: ()=>_scopeTs.scope,
13
+ type: ()=>_standardTs.type,
14
14
  Problems: ()=>_problemsTs.Problems,
15
15
  Problem: ()=>_problemsTs.Problem
16
16
  });
17
- const _mainTs = require("./cjs/main.js");
17
+ const _scopeTs = require("./cjs/scopes/scope.js");
18
+ const _standardTs = require("./cjs/scopes/standard.js");
18
19
  const _problemsTs = require("./cjs/traverse/problems.js");
package/dist/api.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export { scope, type } from "./types/main.js"
2
- export type { Type } from "./types/main.js"
1
+ export { scope } from "./types/scopes/scope.js"
2
+ export { type } from "./types/scopes/standard.js"
3
+ export type { Type } from "./types/scopes/type.js"
3
4
  export { Problems, Problem } from "./types/traverse/problems.js"
package/dist/api.mjs CHANGED
@@ -1,2 +1,3 @@
1
- export { scope, type } from "./mjs/main.js";
1
+ export { scope } from "./mjs/scopes/scope.js";
2
+ export { type } from "./mjs/scopes/standard.js";
2
3
  export { Problems, Problem } from "./mjs/traverse/problems.js";
@@ -10,12 +10,16 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  isBranchComparison: ()=>isBranchComparison,
13
- compareBranches: ()=>compareBranches
13
+ compareBranches: ()=>compareBranches,
14
+ isMorphBranch: ()=>isMorphBranch,
15
+ flattenBranch: ()=>flattenBranch,
16
+ branchIntersection: ()=>branchIntersection
14
17
  });
15
- const _domainsTs = require("../utils/domains.js");
16
- const _errorsTs = require("../utils/errors.js");
17
- const _composeTs = require("./compose.js");
18
- const _rulesTs = require("./rules/rules.js");
18
+ const _astJs = require("../parse/string/ast.js");
19
+ const _domainsJs = require("../utils/domains.js");
20
+ const _errorsJs = require("../utils/errors.js");
21
+ const _composeJs = require("./compose.js");
22
+ const _rulesJs = require("./rules/rules.js");
19
23
  const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
20
24
  const compareBranches = (lConditions, rConditions, state)=>{
21
25
  const result = {
@@ -37,8 +41,8 @@ const compareBranches = (lConditions, rConditions, state)=>{
37
41
  return null;
38
42
  }
39
43
  const r = rPairs.condition;
40
- const subresult = (0, _rulesTs.branchIntersection)(l, r, state);
41
- if ((0, _composeTs.isDisjoint)(subresult)) {
44
+ const subresult = branchIntersection(l, r, state);
45
+ if ((0, _composeJs.isDisjoint)(subresult)) {
42
46
  // doesn't tell us about any redundancies or add a distinct pair
43
47
  return null;
44
48
  } else if (subresult === l) {
@@ -55,7 +59,7 @@ const compareBranches = (lConditions, rConditions, state)=>{
55
59
  // distinct branches.
56
60
  rPairs.distinct = null;
57
61
  return null;
58
- } else if ((0, _composeTs.isEquality)(subresult)) {
62
+ } else if ((0, _composeJs.isEquality)(subresult)) {
59
63
  // Combination of l and r subtype cases.
60
64
  result.equalities.push([
61
65
  lIndex,
@@ -64,13 +68,13 @@ const compareBranches = (lConditions, rConditions, state)=>{
64
68
  lImpliesR = true;
65
69
  rPairs.distinct = null;
66
70
  return null;
67
- } else if ((0, _domainsTs.hasDomain)(subresult, "object")) {
71
+ } else if ((0, _domainsJs.hasDomain)(subresult, "object")) {
68
72
  // Neither branch is a subtype of the other, return
69
73
  // the result of the intersection as a candidate
70
74
  // branch for the final union
71
75
  return subresult;
72
76
  }
73
- return (0, _errorsTs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsTs.domainOf)(subresult)}'`);
77
+ return (0, _errorsJs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsJs.domainOf)(subresult)}'`);
74
78
  });
75
79
  if (!lImpliesR) {
76
80
  for(let i = 0; i < pairs.length; i++){
@@ -83,3 +87,45 @@ const compareBranches = (lConditions, rConditions, state)=>{
83
87
  result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
84
88
  return result;
85
89
  };
90
+ const isMorphBranch = (branch)=>"morph" in branch;
91
+ const flattenBranch = (branch, ctx)=>{
92
+ if (isMorphBranch(branch)) {
93
+ const result = (0, _rulesJs.flattenRules)(branch.rules, ctx);
94
+ if (branch.morph) {
95
+ result.push([
96
+ "morph",
97
+ branch.morph
98
+ ]);
99
+ }
100
+ return result;
101
+ }
102
+ return (0, _rulesJs.flattenRules)(branch, ctx);
103
+ };
104
+ const rulesOf = (branch)=>branch.rules ?? branch;
105
+ const branchIntersection = (l, r, state)=>{
106
+ const lRules = rulesOf(l);
107
+ const rRules = rulesOf(r);
108
+ const rulesResult = (0, _rulesJs.rulesIntersection)(lRules, rRules, state);
109
+ if ("morph" in l) {
110
+ if ("morph" in r) {
111
+ if (l.morph === r.morph) {
112
+ return (0, _composeJs.isEquality)(rulesResult) || (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
113
+ rules: rulesResult,
114
+ morph: l.morph
115
+ };
116
+ }
117
+ return state.lastOperator === "&" ? (0, _errorsJs.throwParseError)((0, _astJs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
118
+ }
119
+ return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
120
+ rules: (0, _composeJs.isEquality)(rulesResult) ? l.rules : rulesResult,
121
+ morph: l.morph
122
+ };
123
+ }
124
+ if ("morph" in r) {
125
+ return (0, _composeJs.isDisjoint)(rulesResult) ? rulesResult : {
126
+ rules: (0, _composeJs.isEquality)(rulesResult) ? r.rules : rulesResult,
127
+ morph: r.morph
128
+ };
129
+ }
130
+ return rulesResult;
131
+ };
@@ -68,9 +68,8 @@ 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
- objectKind: ({ l , r })=>`${l} and ${r}`,
72
71
  range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
73
- class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
72
+ class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
74
73
  tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
75
74
  value: ({ l , r })=>`literal values ${(0, _serializeTs.serialize)(l)} and ${(0, _serializeTs.serialize)(r)}`,
76
75
  leftAssignability: ({ l , r })=>`literal value ${(0, _serializeTs.serialize)(l.value)} and ${(0, _serializeTs.serialize)(r)}`,
@@ -20,9 +20,9 @@ const _genericsTs = require("../utils/generics.js");
20
20
  const _objectKindsTs = require("../utils/objectKinds.js");
21
21
  const _pathsTs = require("../utils/paths.js");
22
22
  const _serializeTs = require("../utils/serialize.js");
23
+ const _branchTs = require("./branch.js");
23
24
  const _composeTs = require("./compose.js");
24
25
  const _propsTs = require("./rules/props.js");
25
- const _rulesTs = require("./rules/rules.js");
26
26
  const flattenBranches = (branches, ctx)=>{
27
27
  const discriminants = calculateDiscriminants(branches, ctx);
28
28
  const indices = branches.map((_, i)=>i);
@@ -30,14 +30,14 @@ const flattenBranches = (branches, ctx)=>{
30
30
  };
31
31
  const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
32
32
  if (remainingIndices.length === 1) {
33
- return (0, _rulesTs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
33
+ return (0, _branchTs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
34
34
  }
35
35
  const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
36
36
  if (!bestDiscriminant) {
37
37
  return [
38
38
  [
39
39
  "branches",
40
- remainingIndices.map((i)=>(0, _rulesTs.flattenBranch)(originalBranches[i], ctx))
40
+ remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.meta.scope) ? (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchTs.flattenBranch)(originalBranches[i], ctx))
41
41
  ]
42
42
  ];
43
43
  }
@@ -68,17 +68,15 @@ const calculateDiscriminants = (branches, ctx)=>{
68
68
  disjointsByPair: {},
69
69
  casesByDisjoint: {}
70
70
  };
71
- const morphBranches = branches.map((branch)=>branchIncludesMorph(branch, ctx.type.meta.scope));
72
71
  for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
73
72
  for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
74
73
  const pairKey = `${lIndex}/${rIndex}`;
75
74
  const pairDisjoints = [];
76
75
  discriminants.disjointsByPair[pairKey] = pairDisjoints;
77
76
  const intersectionState = new _composeTs.IntersectionState(ctx.type, "|");
78
- (0, _rulesTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
77
+ (0, _branchTs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
79
78
  for(const path in intersectionState.disjoints){
80
- // designed to match objectKind path segments like "${number}"
81
- if (path.includes("${")) {
79
+ if (path.includes(_propsTs.mappedKeys.index)) {
82
80
  continue;
83
81
  }
84
82
  const { l , r , kind } = intersectionState.disjoints[path];
@@ -119,9 +117,6 @@ const calculateDiscriminants = (branches, ctx)=>{
119
117
  cases[rSerialized].push(rIndex);
120
118
  }
121
119
  }
122
- if ((morphBranches[lIndex] === true || morphBranches[rIndex] === true) && pairDisjoints.length === 0) {
123
- return (0, _errorsTs.throwParseError)((0, _astTs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`));
124
- }
125
120
  }
126
121
  }
127
122
  return discriminants;
@@ -164,8 +159,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
164
159
  }
165
160
  const defaultCaseKeys = (0, _genericsTs.keysOf)(defaultCases);
166
161
  if (defaultCaseKeys.length) {
167
- const defaultIndices = defaultCaseKeys.map((k)=>parseInt(k));
168
- filteredCases["default"] = defaultIndices;
162
+ filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
169
163
  }
170
164
  if (!bestDiscriminant || score > bestDiscriminant.score) {
171
165
  const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
@@ -196,5 +190,5 @@ const caseSerializers = {
196
190
  domain: _domainsTs.domainOf
197
191
  };
198
192
  const serializeCase = (kind, data)=>caseSerializers[kind](data);
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;
193
+ const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
200
194
  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, $));
@@ -14,7 +14,8 @@ _export(exports, {
14
14
  union: ()=>union,
15
15
  flattenType: ()=>flattenType,
16
16
  flattenNode: ()=>flattenNode,
17
- isLiteralNode: ()=>isLiteralNode
17
+ isLiteralNode: ()=>isLiteralNode,
18
+ arrayOf: ()=>arrayOf
18
19
  });
19
20
  const _astTs = require("../parse/string/ast.js");
20
21
  const _errorsTs = require("../utils/errors.js");
@@ -22,6 +23,7 @@ const _genericsTs = require("../utils/generics.js");
22
23
  const _pathsTs = require("../utils/paths.js");
23
24
  const _composeTs = require("./compose.js");
24
25
  const _predicateTs = require("./predicate.js");
26
+ const _propsTs = require("./rules/props.js");
25
27
  const nodeIntersection = (l, r, state)=>{
26
28
  state.domain = undefined;
27
29
  const lResolution = state.type.meta.scope.resolveNode(l);
@@ -61,18 +63,36 @@ const union = (l, r, type)=>{
61
63
  }
62
64
  return result;
63
65
  };
64
- const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "objectKind" || flatPredicate[0][0] === "value");
66
+ const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
65
67
  const flattenType = (type)=>{
66
68
  const ctx = {
67
69
  type,
68
70
  path: new _pathsTs.Path(),
69
71
  lastDomain: "undefined"
70
72
  };
71
- return flattenNode(type.node, ctx);
73
+ return type.meta.config ? [
74
+ [
75
+ "config",
76
+ {
77
+ config: type.meta.config,
78
+ node: flattenNode(type.node, ctx)
79
+ }
80
+ ]
81
+ ] : flattenNode(type.node, ctx);
72
82
  };
73
83
  const flattenNode = (node, ctx)=>{
74
84
  if (typeof node === "string") {
75
- return ctx.type.meta.scope.resolve(node).flat;
85
+ const resolution = ctx.type.meta.scope.resolve(node);
86
+ const updatedScopeConfig = resolution.meta.scope !== ctx.type.meta.scope && resolution.meta.scope.config;
87
+ return updatedScopeConfig ? [
88
+ [
89
+ "config",
90
+ {
91
+ config: updatedScopeConfig,
92
+ node: resolution.flat
93
+ }
94
+ ]
95
+ ] : resolution.flat;
76
96
  }
77
97
  const domains = (0, _genericsTs.keysOf)(node);
78
98
  if (domains.length === 1) {
@@ -106,3 +126,11 @@ const flattenNode = (node, ctx)=>{
106
126
  const isLiteralNode = (node, domain)=>{
107
127
  return (0, _predicateTs.resolutionExtendsDomain)(node, domain) && (0, _predicateTs.isLiteralCondition)(node[domain]);
108
128
  };
129
+ const arrayOf = (node)=>({
130
+ object: {
131
+ class: "Array",
132
+ props: {
133
+ [_propsTs.mappedKeys.index]: node
134
+ }
135
+ }
136
+ });
@@ -18,22 +18,22 @@ _export(exports, {
18
18
  });
19
19
  const _genericsTs = require("../utils/generics.js");
20
20
  const _objectKindsTs = require("../utils/objectKinds.js");
21
- const _branchesTs = require("./branches.js");
21
+ const _branchTs = require("./branch.js");
22
22
  const _composeTs = require("./compose.js");
23
23
  const _discriminateTs = require("./discriminate.js");
24
- const _rulesTs = require("./rules/rules.js");
24
+ require("./rules/rules.js");
25
25
  const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
26
26
  const comparePredicates = (l, r, context)=>{
27
27
  if (l === true && r === true) {
28
28
  return (0, _composeTs.equality)();
29
29
  }
30
30
  if (!(0, _objectKindsTs.isArray)(l) && !(0, _objectKindsTs.isArray)(r)) {
31
- const result = (0, _rulesTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
31
+ const result = (0, _branchTs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
32
32
  return result === l ? l : result === r ? r : result;
33
33
  }
34
34
  const lBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(l));
35
35
  const rBranches = (0, _genericsTs.listFrom)(emptyRulesIfTrue(r));
36
- const comparison = (0, _branchesTs.compareBranches)(lBranches, rBranches, context);
36
+ const comparison = (0, _branchTs.compareBranches)(lBranches, rBranches, context);
37
37
  if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
38
38
  return (0, _composeTs.equality)();
39
39
  }
@@ -48,7 +48,7 @@ const comparePredicates = (l, r, context)=>{
48
48
  const predicateIntersection = (domain, l, r, state)=>{
49
49
  state.domain = domain;
50
50
  const comparison = comparePredicates(l, r, state);
51
- if (!(0, _branchesTs.isBranchComparison)(comparison)) {
51
+ if (!(0, _branchTs.isBranchComparison)(comparison)) {
52
52
  return comparison;
53
53
  }
54
54
  const resultBranches = [
@@ -65,7 +65,7 @@ const predicateIntersection = (domain, l, r, state)=>{
65
65
  const predicateUnion = (domain, l, r, type)=>{
66
66
  const state = new _composeTs.IntersectionState(type, "|");
67
67
  const comparison = comparePredicates(l, r, state);
68
- if (!(0, _branchesTs.isBranchComparison)(comparison)) {
68
+ if (!(0, _branchTs.isBranchComparison)(comparison)) {
69
69
  return (0, _composeTs.isEquality)(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
70
70
  // and can be simplified to a non-literal boolean.
71
71
  domain === "boolean" ? true : [
@@ -83,7 +83,7 @@ const flattenPredicate = (predicate, context)=>{
83
83
  if (predicate === true) {
84
84
  return [];
85
85
  }
86
- return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _rulesTs.flattenBranch)(predicate, context);
86
+ return (0, _objectKindsTs.isArray)(predicate) ? (0, _discriminateTs.flattenBranches)(predicate, context) : (0, _branchTs.flattenBranch)(predicate, context);
87
87
  };
88
88
  const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
89
89
  const resolutionExtendsDomain = (resolution, domain)=>{
@@ -12,10 +12,16 @@ _export(exports, {
12
12
  classIntersection: ()=>classIntersection,
13
13
  checkClass: ()=>checkClass
14
14
  });
15
+ const _objectKindsTs = require("../../utils/objectKinds.js");
15
16
  const _composeTs = require("../compose.js");
16
- const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>l === r ? (0, _composeTs.equality)() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r));
17
- const checkClass = (data, expectedClass, state)=>{
18
- if (!(data instanceof expectedClass)) {
19
- state.problems.add("class", data, expectedClass);
17
+ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
18
+ const lClass = typeof l === "string" ? _objectKindsTs.defaultObjectKinds[l] : l;
19
+ const rClass = typeof r === "string" ? _objectKindsTs.defaultObjectKinds[r] : r;
20
+ return lClass === rClass ? (0, _composeTs.equality)() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
21
+ });
22
+ const checkClass = (expectedClass, state)=>{
23
+ if (typeof expectedClass === "string") {
24
+ return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.add("class", state.data, expectedClass);
20
25
  }
26
+ return state.data instanceof expectedClass || state.problems.add("class", state.data, expectedClass);
21
27
  };
@@ -26,8 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
26
26
  }
27
27
  return greatestCommonDivisor;
28
28
  };
29
- const checkDivisor = (data, divisor, state)=>{
30
- if (data % divisor !== 0) {
31
- state.problems.add("divisor", data, divisor);
32
- }
33
- };
29
+ const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.add("divisor", state.data, divisor);
@@ -10,14 +10,59 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  isOptional: ()=>isOptional,
13
+ isPrerequisite: ()=>isPrerequisite,
14
+ mappedKeys: ()=>mappedKeys,
15
+ propToNode: ()=>propToNode,
13
16
  propsIntersection: ()=>propsIntersection,
14
17
  flattenProps: ()=>flattenProps
15
18
  });
19
+ const _genericsTs = require("../../utils/generics.js");
16
20
  const _composeTs = require("../compose.js");
17
21
  const _nodeTs = require("../node.js");
18
22
  const isOptional = (prop)=>prop[0] === "?";
19
- const nodeFrom = (prop)=>isOptional(prop) ? prop[1] : prop;
20
- const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.composeKeyedIntersection)((propKey, l, r, context)=>{
23
+ const isPrerequisite = (prop)=>prop[0] === "!";
24
+ const mappedKeys = {
25
+ index: "[index]"
26
+ };
27
+ const propToNode = (prop)=>isOptional(prop) || isPrerequisite(prop) ? prop[1] : prop;
28
+ const getTupleLengthIfPresent = (result)=>{
29
+ if (typeof result.length === "object" && isPrerequisite(result.length) && typeof result.length[1] !== "string" && (0, _nodeTs.isLiteralNode)(result.length[1], "number")) {
30
+ return result.length[1].number.value;
31
+ }
32
+ };
33
+ const propsIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
34
+ const result = propKeysIntersection(l, r, state);
35
+ if (typeof result === "symbol") {
36
+ return result;
37
+ }
38
+ const lengthValue = getTupleLengthIfPresent(result);
39
+ if (lengthValue === undefined || !(0, _genericsTs.hasKey)(result, mappedKeys.index)) {
40
+ return result;
41
+ }
42
+ // if we are at this point, we have an array with an exact length (i.e.
43
+ // a tuple) and an index signature. Intersection each tuple item with
44
+ // the index signature node and remove the index signature via a new
45
+ // updated result, copied from result to avoid mutating existing references.
46
+ const { [mappedKeys.index]: indexProp , ...updatedResult } = result;
47
+ const indexNode = propToNode(indexProp);
48
+ for(let i = 0; i < lengthValue; i++){
49
+ if (!updatedResult[i]) {
50
+ updatedResult[i] = indexNode;
51
+ continue;
52
+ }
53
+ const existingNodeAtIndex = propToNode(updatedResult[i]);
54
+ state.path.push(`${i}`);
55
+ const updatedResultAtIndex = (0, _nodeTs.nodeIntersection)(existingNodeAtIndex, indexNode, state);
56
+ state.path.pop();
57
+ if ((0, _composeTs.isDisjoint)(updatedResultAtIndex)) {
58
+ return updatedResultAtIndex;
59
+ } else if (!(0, _composeTs.isEquality)(updatedResultAtIndex) && updatedResultAtIndex !== existingNodeAtIndex) {
60
+ updatedResult[i] = updatedResultAtIndex;
61
+ }
62
+ }
63
+ return updatedResult;
64
+ });
65
+ const propKeysIntersection = (0, _composeTs.composeKeyedIntersection)((propKey, l, r, context)=>{
21
66
  if (l === undefined) {
22
67
  return r === undefined ? (0, _composeTs.equality)() : r;
23
68
  }
@@ -25,7 +70,7 @@ const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.com
25
70
  return l;
26
71
  }
27
72
  context.path.push(propKey);
28
- const result = (0, _nodeTs.nodeIntersection)(nodeFrom(l), nodeFrom(r), context);
73
+ const result = (0, _nodeTs.nodeIntersection)(propToNode(l), propToNode(r), context);
29
74
  context.path.pop();
30
75
  const resultIsOptional = isOptional(l) && isOptional(r);
31
76
  if ((0, _composeTs.isDisjoint)(result) && resultIsOptional) {
@@ -37,36 +82,41 @@ const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.com
37
82
  return result;
38
83
  }, {
39
84
  onEmpty: "bubble"
40
- }));
85
+ });
41
86
  const flattenProps = (entries, props, ctx)=>{
42
- const requiredProps = [];
43
- const optionalProps = [];
44
87
  for(const k in props){
45
88
  const prop = props[k];
46
89
  ctx.path.push(k);
47
- if (isOptional(prop)) {
48
- optionalProps.push([
49
- k,
50
- (0, _nodeTs.flattenNode)(prop[1], ctx)
90
+ if (k === mappedKeys.index) {
91
+ entries.push([
92
+ "indexProp",
93
+ (0, _nodeTs.flattenNode)(propToNode(prop), ctx)
94
+ ]);
95
+ } else if (isOptional(prop)) {
96
+ entries.push([
97
+ "optionalProp",
98
+ [
99
+ k,
100
+ (0, _nodeTs.flattenNode)(prop[1], ctx)
101
+ ]
102
+ ]);
103
+ } else if (isPrerequisite(prop)) {
104
+ entries.push([
105
+ "prerequisiteProp",
106
+ [
107
+ k,
108
+ (0, _nodeTs.flattenNode)(prop[1], ctx)
109
+ ]
51
110
  ]);
52
111
  } else {
53
- requiredProps.push([
54
- k,
55
- (0, _nodeTs.flattenNode)(prop, ctx)
112
+ entries.push([
113
+ "requiredProp",
114
+ [
115
+ k,
116
+ (0, _nodeTs.flattenNode)(prop, ctx)
117
+ ]
56
118
  ]);
57
119
  }
58
120
  ctx.path.pop();
59
121
  }
60
- if (requiredProps.length) {
61
- entries.push([
62
- "requiredProps",
63
- requiredProps
64
- ]);
65
- }
66
- if (optionalProps.length) {
67
- entries.push([
68
- "optionalProps",
69
- optionalProps
70
- ]);
71
- }
72
122
  };
@@ -93,11 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
93
93
  ]);
94
94
  }
95
95
  };
96
- const checkBound = (data, bound, state)=>{
97
- if (!comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(data), bound.limit)) {
98
- state.problems.add("bound", data, bound);
99
- }
100
- };
96
+ const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _sizeTs.sizeOf)(state.data), bound.limit) || state.problems.add("bound", state.data, bound);
101
97
  const comparatorCheckers = {
102
98
  "<": (size, limit)=>size < limit,
103
99
  ">": (size, limit)=>size > limit,
@@ -9,60 +9,18 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- sources: ()=>sources,
13
12
  getRegex: ()=>getRegex,
14
13
  checkRegex: ()=>checkRegex,
15
- regexIntersection: ()=>regexIntersection,
16
- isLuhnValid: ()=>isLuhnValid
14
+ regexIntersection: ()=>regexIntersection
17
15
  });
18
16
  const _composeTs = require("../compose.js");
19
17
  const _collapsibleSetTs = require("./collapsibleSet.js");
20
18
  const regexCache = {};
21
- const sources = {
22
- // Character sets
23
- alpha: /^[A-Z]*$/i,
24
- alphanumeric: /^[A-Z\d]*$/i,
25
- lowercase: /^[a-z]*$/,
26
- uppercase: /^[A-Z]*$/,
27
- // https://github.com/validatorjs/validator.js
28
- 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}))$/,
29
- // https://www.regular-expressions.info
30
- email: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
31
- // https://github.com/validatorjs/validator.js
32
- uuid: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
33
- };
34
19
  const getRegex = (source)=>{
35
20
  if (!regexCache[source]) {
36
21
  regexCache[source] = new RegExp(source);
37
22
  }
38
23
  return regexCache[source];
39
24
  };
40
- const checkRegex = (data, regex, state)=>{
41
- if (!regex.test(data)) {
42
- state.problems.add("regex", data, regex);
43
- }
44
- };
25
+ const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.add("regex", state.data, `/${source}/`);
45
26
  const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
46
- const isLuhnValid = (creditCardInput)=>{
47
- const sanitized = creditCardInput.replace(/[- ]+/g, "");
48
- let sum = 0;
49
- let digit;
50
- let tmpNum;
51
- let shouldDouble;
52
- for(let i = sanitized.length - 1; i >= 0; i--){
53
- digit = sanitized.substring(i, i + 1);
54
- tmpNum = parseInt(digit, 10);
55
- if (shouldDouble) {
56
- tmpNum *= 2;
57
- if (tmpNum >= 10) {
58
- sum += tmpNum % 10 + 1;
59
- } else {
60
- sum += tmpNum;
61
- }
62
- } else {
63
- sum += tmpNum;
64
- }
65
- shouldDouble = !shouldDouble;
66
- }
67
- return !!(sum % 10 === 0 ? sanitized : false);
68
- };