arktype 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +1 -1
  2. package/dist/api.cjs +4 -3
  3. package/dist/api.d.ts +3 -2
  4. package/dist/api.mjs +2 -1
  5. package/dist/cjs/nodes/node.js +20 -2
  6. package/dist/cjs/nodes/predicate.js +2 -0
  7. package/dist/cjs/nodes/rules/regex.js +4 -42
  8. package/dist/cjs/nodes/rules/rules.js +16 -12
  9. package/dist/cjs/parse/definition.js +2 -2
  10. package/dist/cjs/parse/string/ast.js +1 -1
  11. package/dist/cjs/parse/tuple/keyof.js +6 -7
  12. package/dist/cjs/parse/tuple/morph.js +7 -6
  13. package/dist/cjs/scopes/cache.js +39 -0
  14. package/dist/cjs/scopes/jsObjects.js +133 -0
  15. package/dist/cjs/scopes/scope.js +244 -0
  16. package/dist/cjs/scopes/standard.js +44 -0
  17. package/dist/cjs/scopes/tsKeywords.js +113 -0
  18. package/dist/cjs/scopes/type.js +70 -0
  19. package/dist/cjs/scopes/validation.js +64 -0
  20. package/dist/cjs/traverse/problems.js +50 -74
  21. package/dist/cjs/traverse/traverse.js +25 -3
  22. package/dist/cjs/utils/numericLiterals.js +1 -0
  23. package/dist/mjs/nodes/node.js +20 -2
  24. package/dist/mjs/nodes/predicate.js +1 -0
  25. package/dist/mjs/nodes/rules/regex.js +3 -41
  26. package/dist/mjs/nodes/rules/rules.js +17 -13
  27. package/dist/mjs/parse/definition.js +1 -1
  28. package/dist/mjs/parse/string/ast.js +1 -1
  29. package/dist/mjs/parse/tuple/keyof.js +6 -7
  30. package/dist/mjs/parse/tuple/morph.js +7 -6
  31. package/dist/mjs/scopes/cache.js +25 -0
  32. package/dist/mjs/scopes/jsObjects.js +119 -0
  33. package/dist/mjs/{main.js → scopes/scope.js} +23 -349
  34. package/dist/mjs/scopes/standard.js +27 -0
  35. package/dist/mjs/scopes/tsKeywords.js +99 -0
  36. package/dist/mjs/scopes/type.js +56 -0
  37. package/dist/mjs/scopes/validation.js +51 -0
  38. package/dist/mjs/traverse/problems.js +49 -73
  39. package/dist/mjs/traverse/traverse.js +26 -4
  40. package/dist/mjs/utils/numericLiterals.js +1 -1
  41. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  42. package/dist/types/nodes/compose.d.ts +1 -1
  43. package/dist/types/nodes/infer.d.ts +4 -4
  44. package/dist/types/nodes/node.d.ts +11 -7
  45. package/dist/types/nodes/predicate.d.ts +43 -54
  46. package/dist/types/nodes/rules/range.d.ts +1 -1
  47. package/dist/types/nodes/rules/regex.d.ts +1 -11
  48. package/dist/types/nodes/rules/rules.d.ts +2 -10
  49. package/dist/types/parse/definition.d.ts +1 -1
  50. package/dist/types/parse/record.d.ts +2 -2
  51. package/dist/types/parse/string/ast.d.ts +5 -4
  52. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  53. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  54. package/dist/types/parse/string/reduce/static.d.ts +1 -1
  55. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  56. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  57. package/dist/types/parse/tuple/keyof.d.ts +8 -0
  58. package/dist/types/parse/tuple/morph.d.ts +1 -1
  59. package/dist/types/parse/tuple/narrow.d.ts +1 -1
  60. package/dist/types/parse/tuple/tuple.d.ts +7 -13
  61. package/dist/types/scopes/cache.d.ts +14 -0
  62. package/dist/types/scopes/jsObjects.d.ts +36 -0
  63. package/dist/types/scopes/scope.d.ts +76 -0
  64. package/dist/types/scopes/standard.d.ts +280 -0
  65. package/dist/types/scopes/tsKeywords.d.ts +32 -0
  66. package/dist/types/scopes/type.d.ts +53 -0
  67. package/dist/types/scopes/validation.d.ts +21 -0
  68. package/dist/types/traverse/problems.d.ts +29 -32
  69. package/dist/types/traverse/traverse.d.ts +10 -8
  70. package/dist/types/utils/generics.d.ts +2 -1
  71. package/dist/types/utils/numericLiterals.d.ts +1 -0
  72. package/package.json +2 -2
  73. package/dist/cjs/main.js +0 -574
  74. package/dist/types/main.d.ts +0 -321
package/dist/cjs/main.js DELETED
@@ -1,574 +0,0 @@
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
- Scope: ()=>Scope,
13
- writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
14
- scope: ()=>scope,
15
- scopes: ()=>scopes,
16
- spaces: ()=>spaces,
17
- type: ()=>type,
18
- writeDuplicateAliasMessage: ()=>writeDuplicateAliasMessage,
19
- isType: ()=>isType
20
- });
21
- const _nodeJs = require("./nodes/node.js");
22
- const _definitionJs = require("./parse/definition.js");
23
- const _problemsTs = require("./traverse/problems.js");
24
- const _traverseTs = require("./traverse/traverse.js");
25
- const _chainableNoOpProxyJs = require("./utils/chainableNoOpProxy.js");
26
- const _errorsJs = require("./utils/errors.js");
27
- const _freezeTs = require("./utils/freeze.js");
28
- const _pathsTs = require("./utils/paths.js");
29
- function _checkPrivateRedeclaration(obj, privateCollection) {
30
- if (privateCollection.has(obj)) {
31
- throw new TypeError("Cannot initialize the same private elements twice on an object");
32
- }
33
- }
34
- function _classApplyDescriptorGet(receiver, descriptor) {
35
- if (descriptor.get) {
36
- return descriptor.get.call(receiver);
37
- }
38
- return descriptor.value;
39
- }
40
- function _classApplyDescriptorSet(receiver, descriptor, value) {
41
- if (descriptor.set) {
42
- descriptor.set.call(receiver, value);
43
- } else {
44
- if (!descriptor.writable) {
45
- throw new TypeError("attempted to set read only private field");
46
- }
47
- descriptor.value = value;
48
- }
49
- }
50
- function _classApplyDescriptorUpdate(receiver, descriptor) {
51
- if (descriptor.set) {
52
- if (!descriptor.get) {
53
- throw new TypeError("attempted to read set only private field");
54
- }
55
- if (!("__destrWrapper" in descriptor)) {
56
- descriptor.__destrWrapper = {
57
- set value (v){
58
- descriptor.set.call(receiver, v);
59
- },
60
- get value () {
61
- return descriptor.get.call(receiver);
62
- }
63
- };
64
- }
65
- return descriptor.__destrWrapper;
66
- } else {
67
- if (!descriptor.writable) {
68
- throw new TypeError("attempted to set read only private field");
69
- }
70
- return descriptor;
71
- }
72
- }
73
- function _classExtractFieldDescriptor(receiver, privateMap, action) {
74
- if (!privateMap.has(receiver)) {
75
- throw new TypeError("attempted to " + action + " private field on non-instance");
76
- }
77
- return privateMap.get(receiver);
78
- }
79
- function _classPrivateFieldGet(receiver, privateMap) {
80
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
81
- return _classApplyDescriptorGet(receiver, descriptor);
82
- }
83
- function _classPrivateFieldInit(obj, privateMap, value) {
84
- _checkPrivateRedeclaration(obj, privateMap);
85
- privateMap.set(obj, value);
86
- }
87
- function _classPrivateFieldSet(receiver, privateMap, value) {
88
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
89
- _classApplyDescriptorSet(receiver, descriptor, value);
90
- return value;
91
- }
92
- function _classPrivateFieldUpdate(receiver, privateMap) {
93
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "update");
94
- return _classApplyDescriptorUpdate(receiver, descriptor);
95
- }
96
- function _classPrivateMethodGet(receiver, privateSet, fn) {
97
- if (!privateSet.has(receiver)) {
98
- throw new TypeError("attempted to get private field on non-instance");
99
- }
100
- return fn;
101
- }
102
- function _classPrivateMethodInit(obj, privateSet) {
103
- _checkPrivateRedeclaration(obj, privateSet);
104
- privateSet.add(obj);
105
- }
106
- var _ctx_type_meta;
107
- let anonymousScopeCount = 0;
108
- const scopeRegistry = {};
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
- class Scope {
111
- createAnonymousTypeName() {
112
- return `type${++_classPrivateFieldUpdate(this, _anonymousTypeCount).value}`;
113
- }
114
- get infer() {
115
- return _chainableNoOpProxyJs.chainableNoOpProxy;
116
- }
117
- compile() {
118
- if (!_classPrivateFieldGet(this, _compiled)) {
119
- for(const name in this.aliases){
120
- this.resolve(name);
121
- }
122
- _classPrivateFieldSet(this, _compiled, true);
123
- }
124
- return _classPrivateFieldGet(this, _exports).root;
125
- }
126
- addReferenceIfResolvable(name, ctx) {
127
- const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", []);
128
- if (!resolution) {
129
- return false;
130
- }
131
- (_ctx_type_meta = ctx.type.meta).includesMorph || (_ctx_type_meta.includesMorph = resolution.meta.includesMorph);
132
- return true;
133
- }
134
- resolve(name) {
135
- return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", []);
136
- }
137
- resolveNode(node) {
138
- if (typeof node === "object") {
139
- return node;
140
- }
141
- return this.resolveNode(this.resolve(node).node);
142
- }
143
- constructor(aliases, opts){
144
- _classPrivateMethodInit(this, _register);
145
- _classPrivateMethodInit(this, _cacheSpaces);
146
- _classPrivateMethodInit(this, _initializeContext);
147
- _classPrivateMethodInit(this, _resolveRecurse);
148
- _classPrivateFieldInit(this, _resolutions, {
149
- writable: true,
150
- value: void 0
151
- });
152
- _classPrivateFieldInit(this, _exports, {
153
- writable: true,
154
- value: void 0
155
- });
156
- _classPrivateFieldInit(this, _anonymousTypeCount, {
157
- writable: true,
158
- value: void 0
159
- });
160
- _classPrivateFieldInit(this, _compiled, {
161
- writable: true,
162
- value: void 0
163
- });
164
- this.aliases = aliases;
165
- this.opts = opts;
166
- this.parseCache = new FreezingCache();
167
- _classPrivateFieldSet(this, _resolutions, new Cache());
168
- _classPrivateFieldSet(this, _exports, new Cache());
169
- _classPrivateFieldSet(this, _anonymousTypeCount, 0);
170
- this.type = (def, opts = {})=>{
171
- const result = initializeType(def, opts, this);
172
- const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, result);
173
- result.node = this.resolveNode((0, _definitionJs.parseDefinition)(def, ctx));
174
- result.flat = (0, _nodeJs.flattenType)(result);
175
- return result;
176
- };
177
- _classPrivateFieldSet(this, _compiled, false);
178
- this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
179
- if (opts.standard !== false) {
180
- _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
181
- standardTypes
182
- ], "imports");
183
- }
184
- if (opts.imports) {
185
- _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.imports, "imports");
186
- }
187
- if (opts.includes) {
188
- _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.includes, "includes");
189
- }
190
- }
191
- }
192
- 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}`;
194
- scopeRegistry[name] = this;
195
- return name;
196
- }
197
- function cacheSpaces(spaces, kind) {
198
- for (const space of spaces){
199
- for(const name in space){
200
- if (_classPrivateFieldGet(this, _resolutions).has(name) || name in this.aliases) {
201
- (0, _errorsJs.throwParseError)(writeDuplicateAliasMessage(name));
202
- }
203
- _classPrivateFieldGet(this, _resolutions).set(name, space[name]);
204
- if (kind === "includes") {
205
- _classPrivateFieldGet(this, _exports).set(name, space[name]);
206
- }
207
- }
208
- }
209
- }
210
- function initializeContext(type) {
211
- return {
212
- type,
213
- path: new _pathsTs.Path()
214
- };
215
- }
216
- function resolveRecurse(name, onUnresolvable, seen) {
217
- const maybeCacheResult = _classPrivateFieldGet(this, _resolutions).get(name);
218
- if (maybeCacheResult) {
219
- return maybeCacheResult;
220
- }
221
- if (!this.aliases[name]) {
222
- return onUnresolvable === "throw" ? (0, _errorsJs.throwInternalError)(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
223
- }
224
- // TODO: opts?
225
- const type = initializeType(this.aliases[name], {
226
- name
227
- }, this);
228
- _classPrivateFieldGet(this, _resolutions).set(name, type);
229
- _classPrivateFieldGet(this, _exports).set(name, type);
230
- const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, type);
231
- let resolution = (0, _definitionJs.parseDefinition)(type.meta.definition, ctx);
232
- if (typeof resolution === "string") {
233
- if (seen.includes(resolution)) {
234
- return (0, _errorsJs.throwParseError)(writeShallowCycleErrorMessage(name, seen));
235
- }
236
- seen.push(resolution);
237
- resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen).node;
238
- }
239
- type.node = resolution;
240
- type.flat = (0, _nodeJs.flattenType)(type);
241
- return type;
242
- }
243
- // TODO: merge scope options
244
- const initializeType = (definition, opts, scope)=>{
245
- const name = opts.name ?? "type";
246
- const meta = {
247
- name,
248
- id: `${scope.name}.${opts.name ? name : scope.createAnonymousTypeName()}`,
249
- definition,
250
- scope,
251
- problems: (0, _problemsTs.compileProblemOptions)(opts.problems),
252
- includesMorph: false
253
- };
254
- const root = {
255
- // temporarily initialize node/flat to aliases that will be included in
256
- // the final type in case of cyclic resolutions
257
- node: name,
258
- flat: [
259
- [
260
- "alias",
261
- name
262
- ]
263
- ],
264
- meta,
265
- infer: _chainableNoOpProxyJs.chainableNoOpProxy
266
- };
267
- // dynamically assign a name to the primary traversal function
268
- const namedTraverse = {
269
- [name]: (data)=>{
270
- const state = new _traverseTs.TraversalState(type);
271
- const out = (0, _traverseTs.traverse)(data, type.flat, state);
272
- return state.problems.length ? {
273
- data,
274
- problems: state.problems
275
- } : {
276
- data,
277
- out
278
- };
279
- }
280
- }[name];
281
- // we need to assign this to a variable before returning so we can reference
282
- // it in namedTraverse
283
- const type = Object.assign(namedTraverse, root);
284
- return type;
285
- };
286
- class Cache {
287
- get root() {
288
- return this.cache;
289
- }
290
- has(name) {
291
- return name in this.cache;
292
- }
293
- get(name) {
294
- return this.cache[name];
295
- }
296
- set(name, item) {
297
- this.cache[name] = item;
298
- return item;
299
- }
300
- constructor(){
301
- this.cache = {};
302
- }
303
- }
304
- class FreezingCache extends Cache {
305
- set(name, item) {
306
- this.cache[name] = (0, _freezeTs.deepFreeze)(item);
307
- return item;
308
- }
309
- }
310
- const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
311
- ...seen,
312
- name
313
- ].join("=>")}`;
314
- const always = {
315
- bigint: true,
316
- boolean: true,
317
- null: true,
318
- number: true,
319
- object: true,
320
- string: true,
321
- symbol: true,
322
- undefined: true
323
- };
324
- const scope = (aliases, opts = {})=>new Scope(aliases, opts);
325
- const tsKeywords = scope({
326
- any: [
327
- "node",
328
- always
329
- ],
330
- bigint: [
331
- "node",
332
- {
333
- bigint: true
334
- }
335
- ],
336
- boolean: [
337
- "node",
338
- {
339
- boolean: true
340
- }
341
- ],
342
- false: [
343
- "node",
344
- {
345
- boolean: {
346
- value: false
347
- }
348
- }
349
- ],
350
- never: [
351
- "node",
352
- {}
353
- ],
354
- null: [
355
- "node",
356
- {
357
- null: true
358
- }
359
- ],
360
- number: [
361
- "node",
362
- {
363
- number: true
364
- }
365
- ],
366
- object: [
367
- "node",
368
- {
369
- object: true
370
- }
371
- ],
372
- string: [
373
- "node",
374
- {
375
- string: true
376
- }
377
- ],
378
- symbol: [
379
- "node",
380
- {
381
- symbol: true
382
- }
383
- ],
384
- true: [
385
- "node",
386
- {
387
- boolean: {
388
- value: true
389
- }
390
- }
391
- ],
392
- unknown: [
393
- "node",
394
- always
395
- ],
396
- void: [
397
- "node",
398
- {
399
- undefined: true
400
- }
401
- ],
402
- undefined: [
403
- "node",
404
- {
405
- undefined: true
406
- }
407
- ]
408
- }, {
409
- name: "ts",
410
- standard: false
411
- });
412
- const tsKeywordsSpace = tsKeywords.compile();
413
- const jsObjects = scope({
414
- Function: [
415
- "node",
416
- {
417
- object: {
418
- objectKind: "Function"
419
- }
420
- }
421
- ],
422
- Array: [
423
- "node",
424
- {
425
- object: {
426
- objectKind: "Array"
427
- }
428
- }
429
- ],
430
- Date: [
431
- "node",
432
- {
433
- object: {
434
- objectKind: "Date"
435
- }
436
- }
437
- ],
438
- Error: [
439
- "node",
440
- {
441
- object: {
442
- objectKind: "Error"
443
- }
444
- }
445
- ],
446
- Map: [
447
- "node",
448
- {
449
- object: {
450
- objectKind: "Map"
451
- }
452
- }
453
- ],
454
- RegExp: [
455
- "node",
456
- {
457
- object: {
458
- objectKind: "RegExp"
459
- }
460
- }
461
- ],
462
- Set: [
463
- "node",
464
- {
465
- object: {
466
- objectKind: "Set"
467
- }
468
- }
469
- ],
470
- Object: [
471
- "node",
472
- {
473
- object: {
474
- objectKind: "Object"
475
- }
476
- }
477
- ],
478
- String: [
479
- "node",
480
- {
481
- object: {
482
- objectKind: "String"
483
- }
484
- }
485
- ],
486
- Number: [
487
- "node",
488
- {
489
- object: {
490
- objectKind: "Number"
491
- }
492
- }
493
- ],
494
- Boolean: [
495
- "node",
496
- {
497
- object: {
498
- objectKind: "Boolean"
499
- }
500
- }
501
- ],
502
- WeakMap: [
503
- "node",
504
- {
505
- object: {
506
- objectKind: "WeakMap"
507
- }
508
- }
509
- ],
510
- WeakSet: [
511
- "node",
512
- {
513
- object: {
514
- objectKind: "WeakSet"
515
- }
516
- }
517
- ],
518
- Promise: [
519
- "node",
520
- {
521
- object: {
522
- objectKind: "Promise"
523
- }
524
- }
525
- ]
526
- }, {
527
- name: "jsObjects",
528
- standard: false
529
- });
530
- const jsObjectsSpace = jsObjects.compile();
531
- const validation = scope({
532
- email: /^(.+)@(.+)\.(.+)$/,
533
- alphanumeric: /^[dA-Za-z]+$/,
534
- alpha: /^[A-Za-z]+$/,
535
- lowercase: /^[a-z]*$/,
536
- uppercase: /^[A-Z]*$/,
537
- integer: [
538
- "node",
539
- {
540
- number: {
541
- divisor: 1
542
- }
543
- }
544
- ]
545
- }, {
546
- name: "validation",
547
- standard: false
548
- });
549
- const validationSpace = validation.compile();
550
- const standard = scope({}, {
551
- name: "standard",
552
- includes: [
553
- tsKeywordsSpace,
554
- jsObjectsSpace,
555
- validationSpace
556
- ],
557
- standard: false
558
- });
559
- const standardTypes = standard.compile();
560
- const scopes = {
561
- tsKeywords,
562
- jsObjects,
563
- validation,
564
- standard
565
- };
566
- const spaces = {
567
- tsKeywords: tsKeywordsSpace,
568
- jsObjects: jsObjectsSpace,
569
- validation: validationSpace,
570
- standard: standardTypes
571
- };
572
- const type = scopes.standard.type;
573
- const writeDuplicateAliasMessage = (name)=>`Alias '${name}' is already defined`;
574
- const isType = (value)=>value?.infer === _chainableNoOpProxyJs.chainableNoOpProxy;