arktype 1.0.14-alpha → 1.0.15-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/README.md +68 -11
  2. package/dist/main.js +29 -0
  3. package/dist/nodes/branch.js +135 -0
  4. package/dist/nodes/compose.js +99 -0
  5. package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
  6. package/dist/nodes/node.js +134 -0
  7. package/dist/nodes/predicate.js +88 -0
  8. package/dist/nodes/rules/class.js +23 -0
  9. package/dist/nodes/rules/collapsibleSet.js +34 -0
  10. package/dist/nodes/rules/divisor.js +19 -0
  11. package/dist/nodes/rules/props.js +130 -0
  12. package/dist/nodes/rules/range.js +108 -0
  13. package/dist/nodes/rules/regex.js +17 -0
  14. package/dist/nodes/rules/rules.js +102 -0
  15. package/dist/parse/ast/ast.js +2 -0
  16. package/dist/parse/ast/bound.js +7 -0
  17. package/dist/parse/ast/config.js +14 -0
  18. package/dist/parse/ast/distributableFunction.js +40 -0
  19. package/dist/parse/ast/divisor.js +5 -0
  20. package/dist/parse/ast/intersection.js +21 -0
  21. package/dist/parse/ast/keyof.js +107 -0
  22. package/dist/parse/ast/morph.js +50 -0
  23. package/dist/parse/ast/narrow.js +17 -0
  24. package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
  25. package/dist/parse/ast/node.js +2 -0
  26. package/dist/parse/ast/tuple.js +72 -0
  27. package/dist/parse/ast/union.js +5 -0
  28. package/dist/parse/ast/utils.js +2 -0
  29. package/dist/parse/definition.js +47 -0
  30. package/dist/parse/record.js +31 -0
  31. package/dist/parse/string/reduce/dynamic.js +159 -0
  32. package/dist/parse/string/reduce/shared.js +13 -0
  33. package/dist/parse/string/reduce/static.js +2 -0
  34. package/dist/parse/string/shift/operand/enclosed.js +37 -0
  35. package/dist/parse/string/shift/operand/operand.js +15 -0
  36. package/dist/parse/string/shift/operand/unenclosed.js +45 -0
  37. package/dist/parse/string/shift/operator/bounds.js +83 -0
  38. package/dist/parse/string/shift/operator/divisor.js +23 -0
  39. package/dist/parse/string/shift/operator/operator.js +34 -0
  40. package/dist/parse/string/shift/scanner.js +97 -0
  41. package/dist/parse/string/string.js +35 -0
  42. package/dist/scopes/ark.js +27 -0
  43. package/dist/scopes/cache.js +28 -0
  44. package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
  45. package/dist/scopes/jsObjects.js +20 -0
  46. package/dist/scopes/scope.js +184 -0
  47. package/dist/scopes/tsKeywords.js +49 -0
  48. package/dist/scopes/type.js +40 -0
  49. package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
  50. package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
  51. package/dist/scopes/validation/validation.js +74 -0
  52. package/dist/traverse/problems.js +234 -0
  53. package/dist/traverse/traverse.js +330 -0
  54. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  55. package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
  56. package/dist/utils/data.js +39 -0
  57. package/dist/utils/domains.js +27 -0
  58. package/dist/utils/errors.js +17 -0
  59. package/dist/utils/freeze.js +16 -0
  60. package/dist/utils/generics.js +53 -0
  61. package/dist/utils/numericLiterals.js +88 -0
  62. package/dist/utils/objectKinds.js +69 -0
  63. package/dist/{mjs/utils → utils}/paths.js +8 -3
  64. package/dist/utils/serialize.js +71 -0
  65. package/dist/utils/unionToTuple.js +2 -0
  66. package/package.json +25 -39
  67. package/dist/cjs/main.js +0 -38
  68. package/dist/cjs/nodes/branch.js +0 -140
  69. package/dist/cjs/nodes/compose.js +0 -157
  70. package/dist/cjs/nodes/node.js +0 -139
  71. package/dist/cjs/nodes/predicate.js +0 -86
  72. package/dist/cjs/nodes/rules/class.js +0 -25
  73. package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
  74. package/dist/cjs/nodes/rules/divisor.js +0 -29
  75. package/dist/cjs/nodes/rules/props.js +0 -160
  76. package/dist/cjs/nodes/rules/range.js +0 -105
  77. package/dist/cjs/nodes/rules/regex.js +0 -26
  78. package/dist/cjs/nodes/rules/rules.js +0 -123
  79. package/dist/cjs/package.json +0 -3
  80. package/dist/cjs/parse/ast/ast.js +0 -4
  81. package/dist/cjs/parse/ast/bound.js +0 -16
  82. package/dist/cjs/parse/ast/config.js +0 -13
  83. package/dist/cjs/parse/ast/distributableFunction.js +0 -48
  84. package/dist/cjs/parse/ast/divisor.js +0 -9
  85. package/dist/cjs/parse/ast/intersection.js +0 -30
  86. package/dist/cjs/parse/ast/keyof.js +0 -111
  87. package/dist/cjs/parse/ast/morph.js +0 -58
  88. package/dist/cjs/parse/ast/narrow.js +0 -22
  89. package/dist/cjs/parse/ast/node.js +0 -4
  90. package/dist/cjs/parse/ast/tuple.js +0 -94
  91. package/dist/cjs/parse/ast/union.js +0 -9
  92. package/dist/cjs/parse/ast/utils.js +0 -4
  93. package/dist/cjs/parse/definition.js +0 -61
  94. package/dist/cjs/parse/record.js +0 -37
  95. package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
  96. package/dist/cjs/parse/string/reduce/shared.js +0 -23
  97. package/dist/cjs/parse/string/reduce/static.js +0 -4
  98. package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
  99. package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
  100. package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
  101. package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
  102. package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
  103. package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
  104. package/dist/cjs/parse/string/shift/scanner.js +0 -114
  105. package/dist/cjs/parse/string/string.js +0 -43
  106. package/dist/cjs/scopes/ark.js +0 -45
  107. package/dist/cjs/scopes/cache.js +0 -52
  108. package/dist/cjs/scopes/expressions.js +0 -29
  109. package/dist/cjs/scopes/jsObjects.js +0 -93
  110. package/dist/cjs/scopes/scope.js +0 -323
  111. package/dist/cjs/scopes/tsKeywords.js +0 -113
  112. package/dist/cjs/scopes/type.js +0 -50
  113. package/dist/cjs/scopes/validation/creditCard.js +0 -47
  114. package/dist/cjs/scopes/validation/date.js +0 -62
  115. package/dist/cjs/scopes/validation/validation.js +0 -104
  116. package/dist/cjs/traverse/problems.js +0 -281
  117. package/dist/cjs/traverse/traverse.js +0 -390
  118. package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
  119. package/dist/cjs/utils/data.js +0 -53
  120. package/dist/cjs/utils/domains.js +0 -30
  121. package/dist/cjs/utils/errors.js +0 -26
  122. package/dist/cjs/utils/freeze.js +0 -15
  123. package/dist/cjs/utils/generics.js +0 -63
  124. package/dist/cjs/utils/numericLiterals.js +0 -75
  125. package/dist/cjs/utils/objectKinds.js +0 -69
  126. package/dist/cjs/utils/paths.js +0 -32
  127. package/dist/cjs/utils/serialize.js +0 -74
  128. package/dist/cjs/utils/unionToTuple.js +0 -4
  129. package/dist/mjs/main.js +0 -8
  130. package/dist/mjs/nodes/branch.js +0 -123
  131. package/dist/mjs/nodes/compose.js +0 -135
  132. package/dist/mjs/nodes/discriminate.js +0 -247
  133. package/dist/mjs/nodes/node.js +0 -117
  134. package/dist/mjs/nodes/predicate.js +0 -69
  135. package/dist/mjs/nodes/rules/class.js +0 -11
  136. package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
  137. package/dist/mjs/nodes/rules/divisor.js +0 -15
  138. package/dist/mjs/nodes/rules/props.js +0 -142
  139. package/dist/mjs/nodes/rules/range.js +0 -86
  140. package/dist/mjs/nodes/rules/regex.js +0 -11
  141. package/dist/mjs/nodes/rules/rules.js +0 -106
  142. package/dist/mjs/package.json +0 -3
  143. package/dist/mjs/parse/ast/ast.js +0 -1
  144. package/dist/mjs/parse/ast/bound.js +0 -2
  145. package/dist/mjs/parse/ast/config.js +0 -9
  146. package/dist/mjs/parse/ast/distributableFunction.js +0 -34
  147. package/dist/mjs/parse/ast/divisor.js +0 -1
  148. package/dist/mjs/parse/ast/intersection.js +0 -16
  149. package/dist/mjs/parse/ast/keyof.js +0 -103
  150. package/dist/mjs/parse/ast/morph.js +0 -44
  151. package/dist/mjs/parse/ast/narrow.js +0 -14
  152. package/dist/mjs/parse/ast/node.js +0 -1
  153. package/dist/mjs/parse/ast/tuple.js +0 -80
  154. package/dist/mjs/parse/ast/union.js +0 -1
  155. package/dist/mjs/parse/ast/utils.js +0 -1
  156. package/dist/mjs/parse/definition.js +0 -46
  157. package/dist/mjs/parse/record.js +0 -29
  158. package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
  159. package/dist/mjs/parse/string/reduce/shared.js +0 -6
  160. package/dist/mjs/parse/string/reduce/static.js +0 -1
  161. package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
  162. package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
  163. package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
  164. package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
  165. package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
  166. package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
  167. package/dist/mjs/parse/string/shift/scanner.js +0 -106
  168. package/dist/mjs/parse/string/string.js +0 -28
  169. package/dist/mjs/scopes/ark.js +0 -28
  170. package/dist/mjs/scopes/cache.js +0 -38
  171. package/dist/mjs/scopes/jsObjects.js +0 -82
  172. package/dist/mjs/scopes/scope.js +0 -303
  173. package/dist/mjs/scopes/tsKeywords.js +0 -116
  174. package/dist/mjs/scopes/type.js +0 -35
  175. package/dist/mjs/scopes/validation/validation.js +0 -108
  176. package/dist/mjs/traverse/problems.js +0 -260
  177. package/dist/mjs/traverse/traverse.js +0 -373
  178. package/dist/mjs/utils/data.js +0 -38
  179. package/dist/mjs/utils/domains.js +0 -15
  180. package/dist/mjs/utils/errors.js +0 -10
  181. package/dist/mjs/utils/freeze.js +0 -7
  182. package/dist/mjs/utils/generics.js +0 -38
  183. package/dist/mjs/utils/numericLiterals.js +0 -72
  184. package/dist/mjs/utils/objectKinds.js +0 -55
  185. package/dist/mjs/utils/serialize.js +0 -58
  186. package/dist/mjs/utils/unionToTuple.js +0 -1
  187. /package/dist/{types/main.d.ts → main.d.ts} +0 -0
  188. /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
  189. /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
  190. /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
  191. /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
  192. /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
  193. /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
  194. /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
  195. /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
  196. /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
  197. /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
  198. /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
  199. /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
  200. /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
  201. /package/dist/{types/parse → parse}/ast/bound.d.ts +0 -0
  202. /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
  203. /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
  204. /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
  205. /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
  206. /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
  207. /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
  208. /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
  209. /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
  211. /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{types/parse → parse}/definition.d.ts +0 -0
  213. /package/dist/{types/parse → parse}/record.d.ts +0 -0
  214. /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
  225. /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
  226. /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
  227. /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
  228. /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
  230. /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
  232. /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
  236. /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
  237. /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{types/utils → utils}/data.d.ts +0 -0
  239. /package/dist/{types/utils → utils}/domains.d.ts +0 -0
  240. /package/dist/{types/utils → utils}/errors.d.ts +0 -0
  241. /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
  242. /package/dist/{types/utils → utils}/generics.d.ts +0 -0
  243. /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{types/utils → utils}/paths.d.ts +0 -0
  246. /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
  247. /package/dist/{types/utils → utils}/unionToTuple.d.ts +0 -0
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Scanner = void 0;
4
+ class Scanner {
5
+ chars;
6
+ i;
7
+ finalized = false;
8
+ constructor(def) {
9
+ this.chars = [...def];
10
+ this.i = 0;
11
+ }
12
+ /** Get lookahead and advance scanner by one */
13
+ shift() {
14
+ return (this.chars[this.i++] ?? "");
15
+ }
16
+ get lookahead() {
17
+ return (this.chars[this.i] ?? "");
18
+ }
19
+ shiftUntil(condition) {
20
+ let shifted = "";
21
+ while (this.lookahead) {
22
+ if (condition(this, shifted)) {
23
+ if (shifted[shifted.length - 1] === Scanner.escapeToken) {
24
+ shifted = shifted.slice(0, -1);
25
+ }
26
+ else {
27
+ break;
28
+ }
29
+ }
30
+ shifted += this.shift();
31
+ }
32
+ return shifted;
33
+ }
34
+ shiftUntilNextTerminator() {
35
+ this.shiftUntil(Scanner.lookaheadIsNotWhitespace);
36
+ return this.shiftUntil(Scanner.lookaheadIsTerminator);
37
+ }
38
+ get unscanned() {
39
+ return this.chars.slice(this.i, this.chars.length).join("");
40
+ }
41
+ lookaheadIs(char) {
42
+ return this.lookahead === char;
43
+ }
44
+ lookaheadIsIn(tokens) {
45
+ return this.lookahead in tokens;
46
+ }
47
+ }
48
+ exports.Scanner = Scanner;
49
+ (function (Scanner) {
50
+ Scanner.lookaheadIsTerminator = (scanner) => scanner.lookahead in Scanner.terminatingChars;
51
+ Scanner.lookaheadIsNotWhitespace = (scanner) => scanner.lookahead !== Scanner.whiteSpaceToken;
52
+ Scanner.comparatorStartChars = {
53
+ "<": true,
54
+ ">": true,
55
+ "=": true
56
+ };
57
+ Scanner.terminatingChars = {
58
+ ...Scanner.comparatorStartChars,
59
+ "|": true,
60
+ "&": true,
61
+ ")": true,
62
+ "[": true,
63
+ "%": true,
64
+ " ": true
65
+ };
66
+ Scanner.comparators = {
67
+ "<": true,
68
+ ">": true,
69
+ "<=": true,
70
+ ">=": true,
71
+ "==": true
72
+ };
73
+ Scanner.oneCharComparators = {
74
+ "<": true,
75
+ ">": true
76
+ };
77
+ Scanner.comparatorDescriptions = {
78
+ "<": "less than",
79
+ ">": "more than",
80
+ "<=": "at most",
81
+ ">=": "at least",
82
+ "==": "exactly"
83
+ };
84
+ Scanner.invertedComparators = {
85
+ "<": ">",
86
+ ">": "<",
87
+ "<=": ">=",
88
+ ">=": "<=",
89
+ "==": "=="
90
+ };
91
+ Scanner.branchTokens = {
92
+ "|": true,
93
+ "&": true
94
+ };
95
+ Scanner.escapeToken = "\\";
96
+ Scanner.whiteSpaceToken = " ";
97
+ })(Scanner = exports.Scanner || (exports.Scanner = {}));
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fullStringParse = exports.maybeNaiveParse = exports.parseString = void 0;
4
+ const node_js_1 = require("../../nodes/node.js");
5
+ const dynamic_js_1 = require("./reduce/dynamic.js");
6
+ const operand_js_1 = require("./shift/operand/operand.js");
7
+ const operator_js_1 = require("./shift/operator/operator.js");
8
+ const parseString = (def, ctx) => ctx.type.scope.parseCache.get(def) ??
9
+ ctx.type.scope.parseCache.set(def, (0, exports.maybeNaiveParse)(def, ctx) ?? (0, exports.fullStringParse)(def, ctx));
10
+ exports.parseString = parseString;
11
+ const maybeNaiveParse = (def, ctx) => {
12
+ if (ctx.type.scope.addParsedReferenceIfResolvable(def, ctx)) {
13
+ return def;
14
+ }
15
+ if (def.endsWith("[]")) {
16
+ const elementDef = def.slice(0, -2);
17
+ if (ctx.type.scope.addParsedReferenceIfResolvable(def, ctx)) {
18
+ return (0, node_js_1.toArrayNode)(elementDef);
19
+ }
20
+ }
21
+ };
22
+ exports.maybeNaiveParse = maybeNaiveParse;
23
+ const fullStringParse = (def, ctx) => {
24
+ const s = new dynamic_js_1.DynamicState(def, ctx);
25
+ (0, operand_js_1.parseOperand)(s);
26
+ return loop(s);
27
+ };
28
+ exports.fullStringParse = fullStringParse;
29
+ const loop = (s) => {
30
+ while (!s.scanner.finalized) {
31
+ next(s);
32
+ }
33
+ return s.ejectFinalizedRoot();
34
+ };
35
+ const next = (s) => s.hasRoot() ? (0, operator_js_1.parseOperator)(s) : (0, operand_js_1.parseOperand)(s);
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.type = exports.spaces = exports.scopes = exports.ark = exports.arkScope = void 0;
4
+ const jsObjects_js_1 = require("./jsObjects.js");
5
+ const scope_js_1 = require("./scope.js");
6
+ const tsKeywords_js_1 = require("./tsKeywords.js");
7
+ const validation_js_1 = require("./validation/validation.js");
8
+ exports.arkScope = (0, scope_js_1.scope)({}, {
9
+ name: "standard",
10
+ includes: [tsKeywords_js_1.tsKeywords, jsObjects_js_1.jsObjects, validation_js_1.validation],
11
+ standard: false
12
+ });
13
+ exports.ark = exports.arkScope.compile();
14
+ exports.scopes = {
15
+ root: scope_js_1.rootScope,
16
+ tsKeywords: tsKeywords_js_1.tsKeywordsScope,
17
+ jsObjects: jsObjects_js_1.jsObjectsScope,
18
+ validation: validation_js_1.validationScope,
19
+ ark: exports.arkScope
20
+ };
21
+ exports.spaces = {
22
+ tsKeywords: tsKeywords_js_1.tsKeywords,
23
+ jsObjects: jsObjects_js_1.jsObjects,
24
+ validation: validation_js_1.validation,
25
+ ark: exports.ark
26
+ };
27
+ exports.type = exports.arkScope.type;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FreezingCache = exports.Cache = void 0;
4
+ const freeze_js_1 = require("../utils/freeze.js");
5
+ class Cache {
6
+ cache = {};
7
+ get root() {
8
+ return this.cache;
9
+ }
10
+ has(name) {
11
+ return name in this.cache;
12
+ }
13
+ get(name) {
14
+ return this.cache[name];
15
+ }
16
+ set(name, item) {
17
+ this.cache[name] = item;
18
+ return item;
19
+ }
20
+ }
21
+ exports.Cache = Cache;
22
+ class FreezingCache extends Cache {
23
+ set(name, item) {
24
+ this.cache[name] = (0, freeze_js_1.deepFreeze)(item);
25
+ return item;
26
+ }
27
+ }
28
+ exports.FreezingCache = FreezingCache;
@@ -1,4 +1,7 @@
1
- import { scopes } from "./ark.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.morph = exports.narrow = exports.valueOf = exports.instanceOf = exports.keyOf = exports.arrayOf = exports.union = exports.intersection = void 0;
4
+ const ark_js_1 = require("./ark.js");
2
5
  /**
3
6
  * @operator {@link intersection | &}
4
7
  * @docgenTable
@@ -11,7 +14,8 @@ import { scopes } from "./ark.js";
11
14
  * const tupleIntersection = type(["/@arktype\.io$/", "&", "email"])
12
15
  * @example helper
13
16
  * const helperIntersection = intersection("/@arktype\.io$/","email")
14
- */ export const intersection = scopes.ark.intersection;
17
+ */
18
+ exports.intersection = ark_js_1.scopes.ark.intersection;
15
19
  /**
16
20
  * @operator {@link union | |}
17
21
  * @docgenTable
@@ -24,7 +28,8 @@ import { scopes } from "./ark.js";
24
28
  * const tupleUnion = type(["string", "|", "number"])
25
29
  * @example helper
26
30
  * const helperUnion = union("string", "number")
27
- */ export const union = scopes.ark.union;
31
+ */
32
+ exports.union = ark_js_1.scopes.ark.union;
28
33
  /**
29
34
  * @operator {@link arrayOf}
30
35
  * @docgenTable
@@ -37,7 +42,8 @@ import { scopes } from "./ark.js";
37
42
  * const tupleArray = type(["number", "[]"])
38
43
  * @example helper
39
44
  * const helperArray = arrayOf("number")
40
- */ export const arrayOf = scopes.ark.arrayOf;
45
+ */
46
+ exports.arrayOf = ark_js_1.scopes.ark.arrayOf;
41
47
  /**
42
48
  * @operator {@link keyOf}
43
49
  * @docgenTable
@@ -47,7 +53,8 @@ import { scopes } from "./ark.js";
47
53
  * const tupleKeyOf = type(["keyOf", {a:"string"}])
48
54
  * @example helper
49
55
  * const helperKeyOf = keyOf({a:"string"})
50
- */ export const keyOf = scopes.ark.keyOf;
56
+ */
57
+ exports.keyOf = ark_js_1.scopes.ark.keyOf;
51
58
  /**
52
59
  * @operator {@link instanceOf}
53
60
  * @docgenTable
@@ -57,7 +64,8 @@ import { scopes } from "./ark.js";
57
64
  * const tupleInstanceOf = type(["instanceOf", Date])
58
65
  * @example helper
59
66
  * const helperInstanceOf = instanceOf(Date)
60
- */ export const instanceOf = scopes.ark.instanceOf;
67
+ */
68
+ exports.instanceOf = ark_js_1.scopes.ark.instanceOf;
61
69
  /**
62
70
  * @operator {@link valueOf | ===}
63
71
  * @docgenTable
@@ -67,7 +75,8 @@ import { scopes } from "./ark.js";
67
75
  * const tupleValueOf = type(["valueOf", {a:"string"}])
68
76
  * @example helper
69
77
  * const helperValueOf = valueOf({a:"string"})
70
- */ export const valueOf = scopes.ark.valueOf;
78
+ */
79
+ exports.valueOf = ark_js_1.scopes.ark.valueOf;
71
80
  /**
72
81
  * @operator {@link narrow | =>}
73
82
  * @docgenTable
@@ -76,7 +85,8 @@ import { scopes } from "./ark.js";
76
85
  * const narrow = type( ["number", "=>" , (n) => n % 2 === 0])
77
86
  * @example
78
87
  * const isEven = (x: unknown): x is number => x % 2 === 0
79
- */ export const narrow = scopes.ark.narrow;
88
+ */
89
+ exports.narrow = ark_js_1.scopes.ark.narrow;
80
90
  /**
81
91
  * @operator {@link morph | |>}
82
92
  * @docgenTable
@@ -86,4 +96,5 @@ import { scopes } from "./ark.js";
86
96
  * const tupleMorph = type( ["string", "|>" , (data) => `morphed ${data}`])
87
97
  * @example helper
88
98
  * const helperMorph = morph("string", (data) => `morphed ${data}`)
89
- */ export const morph = scopes.ark.morph;
99
+ */
100
+ exports.morph = ark_js_1.scopes.ark.morph;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsObjects = exports.jsObjectsScope = void 0;
4
+ const scope_js_1 = require("./scope.js");
5
+ /**
6
+ * @docgenScope
7
+ * @docgenTable
8
+ */
9
+ exports.jsObjectsScope = (0, scope_js_1.scope)({
10
+ Function: ["node", { object: { class: Function } }],
11
+ Date: ["node", { object: { class: Date } }],
12
+ Error: ["node", { object: { class: Error } }],
13
+ Map: ["node", { object: { class: Map } }],
14
+ RegExp: ["node", { object: { class: RegExp } }],
15
+ Set: ["node", { object: { class: Set } }],
16
+ WeakMap: ["node", { object: { class: WeakMap } }],
17
+ WeakSet: ["node", { object: { class: WeakSet } }],
18
+ Promise: ["node", { object: { class: Promise } }]
19
+ }, { name: "jsObjects", standard: false });
20
+ exports.jsObjects = exports.jsObjectsScope.compile();
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeDuplicateAliasesMessage = exports.writeShallowCycleErrorMessage = exports.rootType = exports.rootScope = exports.scope = exports.Scope = exports.isConfigTuple = exports.compileScopeOptions = void 0;
4
+ const node_js_1 = require("../nodes/node.js");
5
+ const definition_js_1 = require("../parse/definition.js");
6
+ const problems_js_1 = require("../traverse/problems.js");
7
+ const chainableNoOpProxy_js_1 = require("../utils/chainableNoOpProxy.js");
8
+ const errors_js_1 = require("../utils/errors.js");
9
+ const freeze_js_1 = require("../utils/freeze.js");
10
+ const generics_js_1 = require("../utils/generics.js");
11
+ const paths_js_1 = require("../utils/paths.js");
12
+ const cache_js_1 = require("./cache.js");
13
+ const type_js_1 = require("./type.js");
14
+ const compileScopeOptions = (opts) => ({
15
+ codes: (0, problems_js_1.compileProblemWriters)(opts.codes),
16
+ keys: opts.keys ?? "loose"
17
+ });
18
+ exports.compileScopeOptions = compileScopeOptions;
19
+ let anonymousScopeCount = 0;
20
+ const scopeRegistry = {};
21
+ const spaceRegistry = {};
22
+ const isConfigTuple = (def) => Array.isArray(def) && def[1] === ":";
23
+ exports.isConfigTuple = isConfigTuple;
24
+ class Scope {
25
+ aliases;
26
+ name;
27
+ config;
28
+ parseCache = new cache_js_1.FreezingCache();
29
+ #resolutions = new cache_js_1.Cache();
30
+ #exports = new cache_js_1.Cache();
31
+ constructor(aliases, opts = {}) {
32
+ this.aliases = aliases;
33
+ this.name = this.#register(opts);
34
+ if (opts.standard !== false) {
35
+ this.#cacheSpaces([spaceRegistry["standard"]], "imports");
36
+ }
37
+ if (opts.imports) {
38
+ this.#cacheSpaces(opts.imports, "imports");
39
+ }
40
+ if (opts.includes) {
41
+ this.#cacheSpaces(opts.includes, "includes");
42
+ }
43
+ this.config = (0, exports.compileScopeOptions)(opts);
44
+ }
45
+ #register(opts) {
46
+ const name = opts.name
47
+ ? scopeRegistry[opts.name]
48
+ ? (0, errors_js_1.throwParseError)(`A scope named '${opts.name}' already exists`)
49
+ : opts.name
50
+ : `scope${++anonymousScopeCount}`;
51
+ scopeRegistry[name] = this;
52
+ return name;
53
+ }
54
+ #cacheSpaces(spaces, kind) {
55
+ for (const space of spaces) {
56
+ for (const name in space) {
57
+ if (this.#resolutions.has(name) || name in this.aliases) {
58
+ (0, errors_js_1.throwParseError)((0, exports.writeDuplicateAliasesMessage)(name));
59
+ }
60
+ this.#resolutions.set(name, space[name]);
61
+ if (kind === "includes") {
62
+ this.#exports.set(name, space[name]);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ #initializeContext(type) {
68
+ return {
69
+ type,
70
+ path: new paths_js_1.Path()
71
+ };
72
+ }
73
+ getAnonymousQualifiedName(base) {
74
+ let increment = 0;
75
+ let id = base;
76
+ while (this.isResolvable(id)) {
77
+ id = `${base}${increment++}`;
78
+ }
79
+ return `${this.name}.${id}`;
80
+ }
81
+ addAnonymousTypeReference(referencedType, ctx) {
82
+ ctx.type.includesMorph ||= referencedType.includesMorph;
83
+ return referencedType.node;
84
+ }
85
+ get infer() {
86
+ return chainableNoOpProxy_js_1.chainableNoOpProxy;
87
+ }
88
+ compile() {
89
+ if (!spaceRegistry[this.name]) {
90
+ for (const name in this.aliases) {
91
+ this.resolve(name);
92
+ }
93
+ spaceRegistry[this.name] = this.#exports.root;
94
+ }
95
+ return this.#exports.root;
96
+ }
97
+ addParsedReferenceIfResolvable(name, ctx) {
98
+ const resolution = this.#resolveRecurse(name, "undefined", [name]);
99
+ if (!resolution) {
100
+ return false;
101
+ }
102
+ ctx.type.includesMorph ||= resolution.includesMorph;
103
+ return true;
104
+ }
105
+ resolve(name) {
106
+ return this.#resolveRecurse(name, "throw", [name]);
107
+ }
108
+ #resolveRecurse(name, onUnresolvable, seen) {
109
+ const maybeCacheResult = this.#resolutions.get(name);
110
+ if (maybeCacheResult) {
111
+ return maybeCacheResult;
112
+ }
113
+ const aliasDef = this.aliases[name];
114
+ if (!aliasDef) {
115
+ return (onUnresolvable === "throw"
116
+ ? (0, errors_js_1.throwInternalError)(`Unexpectedly failed to resolve alias '${name}'`)
117
+ : undefined);
118
+ }
119
+ const t = (0, type_js_1.initializeType)(name, aliasDef, {}, this);
120
+ const ctx = this.#initializeContext(t);
121
+ this.#resolutions.set(name, t);
122
+ this.#exports.set(name, t);
123
+ let node = (0, definition_js_1.parseDefinition)(aliasDef, ctx);
124
+ if (typeof node === "string") {
125
+ if (seen.includes(node)) {
126
+ return (0, errors_js_1.throwParseError)((0, exports.writeShallowCycleErrorMessage)(name, seen));
127
+ }
128
+ seen.push(node);
129
+ node = this.#resolveRecurse(node, "throw", seen).node;
130
+ }
131
+ t.node = (0, freeze_js_1.deepFreeze)(node);
132
+ t.flat = (0, freeze_js_1.deepFreeze)((0, node_js_1.flattenType)(t));
133
+ return t;
134
+ }
135
+ resolveNode(node) {
136
+ return typeof node === "string"
137
+ ? this.resolveNode(this.resolve(node).node)
138
+ : node;
139
+ }
140
+ resolveTypeNode(node) {
141
+ const resolution = this.resolveNode(node);
142
+ return (0, node_js_1.isConfigNode)(resolution) ? resolution.node : resolution;
143
+ }
144
+ expressions = {
145
+ intersection: (l, r, opts) => this.type([l, "&", r], opts),
146
+ union: (l, r, opts) => this.type([l, "|", r], opts),
147
+ arrayOf: (def, opts) => this.type([def, "[]"], opts),
148
+ keyOf: (def, opts) => this.type(["keyof", def], opts),
149
+ node: (def, opts) => this.type(["node", def], opts),
150
+ instanceOf: (def, opts) => this.type(["instanceof", def], opts),
151
+ valueOf: (def, opts) => this.type(["===", def], opts),
152
+ narrow: (def, fn, opts) => this.type([def, "=>", fn], opts),
153
+ morph: (def, fn, opts) => this.type([def, "|>", fn], opts)
154
+ };
155
+ intersection = this.expressions.intersection;
156
+ union = this.expressions.union;
157
+ arrayOf = this.expressions.arrayOf;
158
+ keyOf = this.expressions.keyOf;
159
+ valueOf = this.expressions.valueOf;
160
+ instanceOf = this.expressions.instanceOf;
161
+ narrow = this.expressions.narrow;
162
+ morph = this.expressions.morph;
163
+ type = Object.assign((def, config = {}) => {
164
+ const t = (0, type_js_1.initializeType)("λtype", def, config, this);
165
+ const ctx = this.#initializeContext(t);
166
+ const root = (0, definition_js_1.parseDefinition)(def, ctx);
167
+ t.node = (0, freeze_js_1.deepFreeze)((0, generics_js_1.hasKeys)(config)
168
+ ? { config, node: this.resolveTypeNode(root) }
169
+ : root);
170
+ t.flat = (0, freeze_js_1.deepFreeze)((0, node_js_1.flattenType)(t));
171
+ return t;
172
+ }, { from: this.expressions.node });
173
+ isResolvable(name) {
174
+ return this.#resolutions.has(name) || this.aliases[name];
175
+ }
176
+ }
177
+ exports.Scope = Scope;
178
+ exports.scope = ((aliases, opts = {}) => new Scope(aliases, opts));
179
+ exports.rootScope = (0, exports.scope)({}, { name: "root", standard: false });
180
+ exports.rootType = exports.rootScope.type;
181
+ const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join("=>")}`;
182
+ exports.writeShallowCycleErrorMessage = writeShallowCycleErrorMessage;
183
+ const writeDuplicateAliasesMessage = (name) => `Alias '${name}' is already defined`;
184
+ exports.writeDuplicateAliasesMessage = writeDuplicateAliasesMessage;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tsKeywords = exports.tsKeywordsScope = void 0;
4
+ const scope_js_1 = require("./scope.js");
5
+ const always = {
6
+ bigint: true,
7
+ boolean: true,
8
+ null: true,
9
+ number: true,
10
+ object: true,
11
+ string: true,
12
+ symbol: true,
13
+ undefined: true
14
+ };
15
+ /**
16
+ * @keywords keywords: {"any": "any",
17
+ "bigint": "a bigint",
18
+ "boolean": "a boolean",
19
+ "false": "false",
20
+ "never": "never",
21
+ "null": "null",
22
+ "number": "a number",
23
+ "object": "an object",
24
+ "string": "a string",
25
+ "symbol": "a symbol",
26
+ "true": "true",
27
+ "unknown": "unknown",
28
+ "void": "void",
29
+ "undefined": "undefined"}
30
+ * @docgenScope
31
+ * @docgenTable
32
+ */
33
+ exports.tsKeywordsScope = (0, scope_js_1.scope)({
34
+ any: ["node", always],
35
+ bigint: ["node", { bigint: true }],
36
+ boolean: ["node", { boolean: true }],
37
+ false: ["node", { boolean: { value: false } }],
38
+ never: ["node", {}],
39
+ null: ["node", { null: true }],
40
+ number: ["node", { number: true }],
41
+ object: ["node", { object: true }],
42
+ string: ["node", { string: true }],
43
+ symbol: ["node", { symbol: true }],
44
+ true: ["node", { boolean: { value: true } }],
45
+ unknown: ["node", always],
46
+ void: ["node", { undefined: true }],
47
+ undefined: ["node", { undefined: true }]
48
+ }, { name: "ts", standard: false });
49
+ exports.tsKeywords = exports.tsKeywordsScope.compile();
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAnonymousName = exports.isType = exports.initializeType = void 0;
4
+ const traverse_js_1 = require("../traverse/traverse.js");
5
+ const chainableNoOpProxy_js_1 = require("../utils/chainableNoOpProxy.js");
6
+ const initializeType = (name, definition, config, scope) => {
7
+ const root = {
8
+ // temporarily initialize node/flat to aliases that will be included in
9
+ // the final type in case of cyclic resolutions
10
+ node: name,
11
+ flat: [["alias", name]],
12
+ allows: (data) => !namedTraverse(data).problems,
13
+ assert: (data) => {
14
+ const result = namedTraverse(data);
15
+ return result.problems ? result.problems.throw() : result.data;
16
+ },
17
+ infer: chainableNoOpProxy_js_1.chainableNoOpProxy,
18
+ inferIn: chainableNoOpProxy_js_1.chainableNoOpProxy,
19
+ qualifiedName: (0, exports.isAnonymousName)(name)
20
+ ? scope.getAnonymousQualifiedName(name)
21
+ : `${scope.name}.${name}`,
22
+ definition,
23
+ scope,
24
+ includesMorph: false,
25
+ config
26
+ // the "as" symbol from inferred is not used at runtime, so we check
27
+ // that the rest of the type is correct then cast it
28
+ };
29
+ // define within a key to dynamically assign a name to the function
30
+ const namedTraverse = {
31
+ [name]: (data) => (0, traverse_js_1.traverseRoot)(namedTraverse, data)
32
+ }[name];
33
+ const t = Object.assign(namedTraverse, root);
34
+ return t;
35
+ };
36
+ exports.initializeType = initializeType;
37
+ const isType = (value) => value?.infer === chainableNoOpProxy_js_1.chainableNoOpProxy;
38
+ exports.isType = isType;
39
+ const isAnonymousName = (name) => name[0] === "λ";
40
+ exports.isAnonymousName = isAnonymousName;
@@ -1,34 +1,38 @@
1
- import { rootType } from "../scope.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.creditCard = exports.isLuhnValid = void 0;
4
+ const scope_js_1 = require("../scope.js");
2
5
  // https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
3
- export const isLuhnValid = (creditCardInput)=>{
6
+ const isLuhnValid = (creditCardInput) => {
4
7
  const sanitized = creditCardInput.replace(/[- ]+/g, "");
5
8
  let sum = 0;
6
9
  let digit;
7
10
  let tmpNum;
8
11
  let shouldDouble;
9
- for(let i = sanitized.length - 1; i >= 0; i--){
12
+ for (let i = sanitized.length - 1; i >= 0; i--) {
10
13
  digit = sanitized.substring(i, i + 1);
11
14
  tmpNum = parseInt(digit, 10);
12
15
  if (shouldDouble) {
13
16
  tmpNum *= 2;
14
17
  if (tmpNum >= 10) {
15
- sum += tmpNum % 10 + 1;
16
- } else {
18
+ sum += (tmpNum % 10) + 1;
19
+ }
20
+ else {
17
21
  sum += tmpNum;
18
22
  }
19
- } else {
23
+ }
24
+ else {
20
25
  sum += tmpNum;
21
26
  }
22
27
  shouldDouble = !shouldDouble;
23
28
  }
24
29
  return !!(sum % 10 === 0 ? sanitized : false);
25
30
  };
31
+ exports.isLuhnValid = isLuhnValid;
26
32
  // https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
27
33
  const creditCardMatcher = /^(?: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}))$/;
28
- export const creditCard = rootType([
34
+ exports.creditCard = (0, scope_js_1.rootType)([
29
35
  creditCardMatcher,
30
36
  "=>",
31
- (s, problems)=>isLuhnValid(s) || !problems.mustBe("a valid credit card number")
32
- ], {
33
- mustBe: "a valid credit card number"
34
- });
37
+ (s, problems) => (0, exports.isLuhnValid)(s) || !problems.mustBe("a valid credit card number")
38
+ ], { mustBe: "a valid credit card number" });