arktype 0.0.1 → 0.1.1

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 (147) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +101 -0
  3. package/dist/api.cjs +15 -0
  4. package/dist/api.d.ts +2 -0
  5. package/dist/api.mjs +1 -0
  6. package/dist/cjs/main.js +462 -0
  7. package/dist/cjs/nodes/branches.js +85 -0
  8. package/dist/cjs/nodes/compose.js +140 -0
  9. package/dist/cjs/nodes/discriminate.js +199 -0
  10. package/dist/cjs/nodes/functors.js +35 -0
  11. package/dist/cjs/nodes/infer.js +4 -0
  12. package/dist/cjs/nodes/node.js +108 -0
  13. package/dist/cjs/nodes/predicate.js +87 -0
  14. package/dist/cjs/nodes/rules/class.js +21 -0
  15. package/dist/cjs/nodes/rules/collapsibleSet.js +49 -0
  16. package/dist/cjs/nodes/rules/divisor.js +33 -0
  17. package/dist/cjs/nodes/rules/props.js +72 -0
  18. package/dist/cjs/nodes/rules/range.js +109 -0
  19. package/dist/cjs/nodes/rules/regex.js +68 -0
  20. package/dist/cjs/nodes/rules/rules.js +156 -0
  21. package/dist/cjs/nodes/rules/subdomain.js +78 -0
  22. package/dist/cjs/package.json +3 -0
  23. package/dist/cjs/parse/definition.js +33 -0
  24. package/dist/cjs/parse/record.js +37 -0
  25. package/dist/cjs/parse/string/ast.js +36 -0
  26. package/dist/cjs/parse/string/reduce/dynamic.js +150 -0
  27. package/dist/cjs/parse/string/reduce/shared.js +23 -0
  28. package/dist/cjs/parse/string/reduce/static.js +4 -0
  29. package/dist/cjs/parse/string/shift/operand/enclosed.js +54 -0
  30. package/dist/cjs/parse/string/shift/operand/operand.js +11 -0
  31. package/dist/cjs/parse/string/shift/operand/unenclosed.js +53 -0
  32. package/dist/cjs/parse/string/shift/operator/bounds.js +87 -0
  33. package/dist/cjs/parse/string/shift/operator/divisor.js +35 -0
  34. package/dist/cjs/parse/string/shift/operator/operator.js +26 -0
  35. package/dist/cjs/parse/string/shift/scanner.js +99 -0
  36. package/dist/cjs/parse/string/string.js +43 -0
  37. package/dist/cjs/parse/tuple/keyof.js +46 -0
  38. package/dist/cjs/parse/tuple/morph.js +56 -0
  39. package/dist/cjs/parse/tuple/narrow.js +15 -0
  40. package/dist/cjs/parse/tuple/tuple.js +85 -0
  41. package/dist/cjs/parse/tuple/utils.js +48 -0
  42. package/dist/cjs/traverse/problems.js +249 -0
  43. package/dist/cjs/traverse/traverse.js +268 -0
  44. package/dist/cjs/utils/chainableNoOpProxy.js +23 -0
  45. package/dist/cjs/utils/domains.js +67 -0
  46. package/dist/cjs/utils/errors.js +26 -0
  47. package/dist/cjs/utils/freeze.js +16 -0
  48. package/dist/cjs/utils/generics.js +36 -0
  49. package/dist/cjs/utils/numericLiterals.js +84 -0
  50. package/dist/cjs/utils/paths.js +32 -0
  51. package/dist/cjs/utils/serialize.js +98 -0
  52. package/dist/cjs/utils/unionToTuple.js +4 -0
  53. package/dist/mjs/main.js +442 -0
  54. package/dist/mjs/nodes/branches.js +71 -0
  55. package/dist/mjs/nodes/compose.js +118 -0
  56. package/dist/mjs/nodes/discriminate.js +184 -0
  57. package/dist/mjs/nodes/functors.js +27 -0
  58. package/dist/mjs/nodes/infer.js +1 -0
  59. package/dist/mjs/nodes/node.js +90 -0
  60. package/dist/mjs/nodes/predicate.js +69 -0
  61. package/dist/mjs/nodes/rules/class.js +7 -0
  62. package/dist/mjs/nodes/rules/collapsibleSet.js +35 -0
  63. package/dist/mjs/nodes/rules/divisor.js +19 -0
  64. package/dist/mjs/nodes/rules/props.js +57 -0
  65. package/dist/mjs/nodes/rules/range.js +90 -0
  66. package/dist/mjs/nodes/rules/regex.js +53 -0
  67. package/dist/mjs/nodes/rules/rules.js +138 -0
  68. package/dist/mjs/nodes/rules/subdomain.js +63 -0
  69. package/dist/mjs/package.json +3 -0
  70. package/dist/mjs/parse/definition.js +17 -0
  71. package/dist/mjs/parse/record.js +29 -0
  72. package/dist/mjs/parse/string/ast.js +19 -0
  73. package/dist/mjs/parse/string/reduce/dynamic.js +142 -0
  74. package/dist/mjs/parse/string/reduce/shared.js +6 -0
  75. package/dist/mjs/parse/string/reduce/static.js +1 -0
  76. package/dist/mjs/parse/string/shift/operand/enclosed.js +39 -0
  77. package/dist/mjs/parse/string/shift/operand/operand.js +3 -0
  78. package/dist/mjs/parse/string/shift/operand/unenclosed.js +36 -0
  79. package/dist/mjs/parse/string/shift/operator/bounds.js +70 -0
  80. package/dist/mjs/parse/string/shift/operator/divisor.js +21 -0
  81. package/dist/mjs/parse/string/shift/operator/operator.js +11 -0
  82. package/dist/mjs/parse/string/shift/scanner.js +91 -0
  83. package/dist/mjs/parse/string/string.js +28 -0
  84. package/dist/mjs/parse/tuple/keyof.js +38 -0
  85. package/dist/mjs/parse/tuple/morph.js +42 -0
  86. package/dist/mjs/parse/tuple/narrow.js +7 -0
  87. package/dist/mjs/parse/tuple/tuple.js +71 -0
  88. package/dist/mjs/parse/tuple/utils.js +34 -0
  89. package/dist/mjs/traverse/problems.js +229 -0
  90. package/dist/mjs/traverse/traverse.js +252 -0
  91. package/dist/mjs/utils/chainableNoOpProxy.js +15 -0
  92. package/dist/mjs/utils/domains.js +48 -0
  93. package/dist/mjs/utils/errors.js +10 -0
  94. package/dist/mjs/utils/freeze.js +8 -0
  95. package/dist/mjs/utils/generics.js +15 -0
  96. package/dist/mjs/utils/numericLiterals.js +71 -0
  97. package/dist/mjs/utils/paths.js +18 -0
  98. package/dist/mjs/utils/serialize.js +81 -0
  99. package/dist/mjs/utils/unionToTuple.js +1 -0
  100. package/dist/tsconfig.temp.tsbuildinfo +1 -0
  101. package/dist/types/main.d.ts +257 -0
  102. package/dist/types/nodes/branches.d.ts +14 -0
  103. package/dist/types/nodes/compose.d.ts +92 -0
  104. package/dist/types/nodes/discriminate.d.ts +27 -0
  105. package/dist/types/nodes/functors.d.ts +6 -0
  106. package/dist/types/nodes/infer.d.ts +35 -0
  107. package/dist/types/nodes/node.d.ts +51 -0
  108. package/dist/types/nodes/predicate.d.ts +88 -0
  109. package/dist/types/nodes/rules/class.d.ts +3 -0
  110. package/dist/types/nodes/rules/collapsibleSet.d.ts +4 -0
  111. package/dist/types/nodes/rules/divisor.d.ts +2 -0
  112. package/dist/types/nodes/rules/props.d.ts +20 -0
  113. package/dist/types/nodes/rules/range.d.ts +33 -0
  114. package/dist/types/nodes/rules/regex.d.ts +14 -0
  115. package/dist/types/nodes/rules/rules.d.ts +44 -0
  116. package/dist/types/nodes/rules/subdomain.d.ts +15 -0
  117. package/dist/types/parse/definition.d.ts +29 -0
  118. package/dist/types/parse/record.d.ts +24 -0
  119. package/dist/types/parse/string/ast.d.ts +42 -0
  120. package/dist/types/parse/string/reduce/dynamic.d.ts +34 -0
  121. package/dist/types/parse/string/reduce/shared.d.ts +24 -0
  122. package/dist/types/parse/string/reduce/static.d.ts +115 -0
  123. package/dist/types/parse/string/shift/operand/enclosed.d.ts +28 -0
  124. package/dist/types/parse/string/shift/operand/operand.d.ts +9 -0
  125. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +18 -0
  126. package/dist/types/parse/string/shift/operator/bounds.d.ts +20 -0
  127. package/dist/types/parse/string/shift/operator/divisor.d.ts +9 -0
  128. package/dist/types/parse/string/shift/operator/operator.d.ts +13 -0
  129. package/dist/types/parse/string/shift/scanner.d.ts +89 -0
  130. package/dist/types/parse/string/string.d.ts +27 -0
  131. package/dist/types/parse/tuple/keyof.d.ts +2 -0
  132. package/dist/types/parse/tuple/morph.d.ts +15 -0
  133. package/dist/types/parse/tuple/narrow.d.ts +13 -0
  134. package/dist/types/parse/tuple/tuple.d.ts +37 -0
  135. package/dist/types/parse/tuple/utils.d.ts +19 -0
  136. package/dist/types/traverse/problems.d.ts +123 -0
  137. package/dist/types/traverse/traverse.d.ts +48 -0
  138. package/dist/types/utils/chainableNoOpProxy.d.ts +14 -0
  139. package/dist/types/utils/domains.d.ts +58 -0
  140. package/dist/types/utils/errors.d.ts +6 -0
  141. package/dist/types/utils/freeze.d.ts +2 -0
  142. package/dist/types/utils/generics.d.ts +118 -0
  143. package/dist/types/utils/numericLiterals.d.ts +28 -0
  144. package/dist/types/utils/paths.d.ts +7 -0
  145. package/dist/types/utils/serialize.d.ts +38 -0
  146. package/dist/types/utils/unionToTuple.d.ts +11 -0
  147. package/package.json +102 -2
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 ArkType
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ <div align="center">
2
+ <img src="./dev/arktype.io/static/img/logo.svg" height="64px" />
3
+ <h1>ArkType</h1>
4
+ </div>
5
+ <div align="center">
6
+
7
+ Isomorphic type syntax for TS/JS
8
+
9
+ </div>
10
+
11
+ ## Installation 📦
12
+
13
+ `npm install arktype`
14
+
15
+ If you're using TypeScript, you'll need at least `4.8`
16
+
17
+ _Note: This package is under active development. APIs have largely stabilized, but may still change prior to our upcoming 1.0-beta release. Stay tuned!⛵_
18
+
19
+ ### Your first type
20
+
21
+ [Try it out.](https://arktype.io/docs/#your-first-type)
22
+
23
+ ```ts @blockFrom:examples/type.ts
24
+ import { type } from "arktype"
25
+
26
+ // Define a type...
27
+ export const user = type({
28
+ name: "string",
29
+ device: {
30
+ platform: "'android'|'ios'",
31
+ "version?": "number"
32
+ }
33
+ })
34
+
35
+ // Infer it...
36
+ export type User = typeof user.infer
37
+
38
+ // Validate your data anytime, anywhere, with the same clarity and precision you expect from TypeScript.
39
+ export const { data, problems } = user({
40
+ name: "Alan Turing",
41
+ device: {
42
+ platform: "enigma"
43
+ }
44
+ })
45
+
46
+ if (problems) {
47
+ // "device/platform must be 'android' or 'ios' (was 'enigma')"
48
+ console.log(problems.summary)
49
+ }
50
+ ```
51
+
52
+ ### Scopes
53
+
54
+ [Try it out.](https://arktype.io/docs/scopes)
55
+
56
+ ```ts @blockFrom:examples/scope.ts
57
+ import { scope } from "arktype"
58
+
59
+ // Scopes are collections of types that can reference each other.
60
+ export const types = scope({
61
+ package: {
62
+ name: "string",
63
+ "dependencies?": "package[]",
64
+ "contributors?": "contributor[]"
65
+ },
66
+ contributor: {
67
+ // Subtypes like 'email' are inferred like 'string' but provide additional validation at runtime.
68
+ email: "email",
69
+ "packages?": "package[]"
70
+ }
71
+ }).compile()
72
+
73
+ // Cyclic types are inferred to arbitrary depth...
74
+ export type Package = typeof types.package.infer
75
+
76
+ // And can validate cyclic data.
77
+ const packageData: Package = {
78
+ name: "arktype",
79
+ dependencies: [{ name: "typescript" }],
80
+ contributors: [{ email: "david@sharktypeio" }]
81
+ }
82
+ packageData.dependencies![0].dependencies = [packageData]
83
+
84
+ export const { data, problems } = types.package(packageData)
85
+ ```
86
+
87
+ ## API
88
+
89
+ ArkType supports many of TypeScript's built-in types and operators, as well as some new nes dedicated exclusively to runtime validation. In fact, we got a little ahead of ourselves and built a ton of cool features, but we're still working on getting caught up syntax and API docs. Keep an eye out for more in the next couple weeks! In the meantime, check out the examples from this README and use the type hints you get to learn how you can customize your types and scopes. If you have any questions, don't hesitate to reach out on the [dedicated Discord channel](https://discord.gg/WSNF3Kc4xh)!
90
+
91
+ ## Contributing
92
+
93
+ If you're interested in contributing to ArkType...
94
+
95
+ 1. Thank you! We'll do everything we can to make this as straightforward as possible, regardless of your level of experience.
96
+ 2. Check out our [guide](./CONTRIBUTING.md) to get started!
97
+
98
+ ## License
99
+
100
+ This project is licensed under the terms of the
101
+ [MIT license](./LICENSE).
package/dist/api.cjs ADDED
@@ -0,0 +1,15 @@
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: ()=>_mainTs.scope,
13
+ type: ()=>_mainTs.type
14
+ });
15
+ const _mainTs = require("./cjs/main.js");
package/dist/api.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { scope, type } from "./types/main.js"
2
+ export type { Type } from "./types/main.js"
package/dist/api.mjs ADDED
@@ -0,0 +1 @@
1
+ export { scope, type } from "./mjs/main.js";
@@ -0,0 +1,462 @@
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 `anonymousType${++_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 ?? scope.createAnonymousTypeName();
246
+ const meta = {
247
+ name,
248
+ id: `${scope.name}.${name}`,
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 ts = 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
+ // TODO: Add remaining JS object types
409
+ Function: [
410
+ "node",
411
+ {
412
+ object: {
413
+ subdomain: "Function"
414
+ }
415
+ }
416
+ ]
417
+ }, {
418
+ name: "ts",
419
+ standard: false
420
+ });
421
+ const tsTypes = ts.compile();
422
+ const validation = scope({
423
+ email: /^(.+)@(.+)\.(.+)$/,
424
+ alphanumeric: /^[dA-Za-z]+$/,
425
+ alpha: /^[A-Za-z]+$/,
426
+ lowercase: /^[a-z]*$/,
427
+ uppercase: /^[A-Z]*$/,
428
+ integer: [
429
+ "node",
430
+ {
431
+ number: {
432
+ divisor: 1
433
+ }
434
+ }
435
+ ]
436
+ }, {
437
+ name: "validation",
438
+ standard: false
439
+ });
440
+ const validationTypes = validation.compile();
441
+ const standard = scope({}, {
442
+ name: "standard",
443
+ includes: [
444
+ tsTypes,
445
+ validationTypes
446
+ ],
447
+ standard: false
448
+ });
449
+ const standardTypes = standard.compile();
450
+ const scopes = {
451
+ ts,
452
+ validation,
453
+ standard
454
+ };
455
+ const spaces = {
456
+ ts: tsTypes,
457
+ validation: validationTypes,
458
+ standard: standardTypes
459
+ };
460
+ const type = scopes.standard.type;
461
+ const writeDuplicateAliasMessage = (name)=>`Alias '${name}' is already defined`;
462
+ const isType = (value)=>value?.infer === _chainableNoOpProxyJs.chainableNoOpProxy;
@@ -0,0 +1,85 @@
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
+ isBranchComparison: ()=>isBranchComparison,
13
+ compareBranches: ()=>compareBranches
14
+ });
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");
19
+ const isBranchComparison = (comparison)=>comparison?.lConditions !== undefined;
20
+ const compareBranches = (lConditions, rConditions, state)=>{
21
+ const result = {
22
+ lConditions,
23
+ rConditions,
24
+ lSubconditionsOfR: [],
25
+ rSubconditionsOfL: [],
26
+ equalities: [],
27
+ distinctIntersections: []
28
+ };
29
+ const pairs = rConditions.map((condition)=>({
30
+ condition,
31
+ distinct: []
32
+ }));
33
+ lConditions.forEach((l, lIndex)=>{
34
+ let lImpliesR = false;
35
+ const distinct = pairs.map((rPairs, rIndex)=>{
36
+ if (lImpliesR || !rPairs.distinct) {
37
+ return null;
38
+ }
39
+ const r = rPairs.condition;
40
+ const subresult = (0, _rulesTs.branchIntersection)(l, r, state);
41
+ if ((0, _composeTs.isDisjoint)(subresult)) {
42
+ // doesn't tell us about any redundancies or add a distinct pair
43
+ return null;
44
+ } else if (subresult === l) {
45
+ result.lSubconditionsOfR.push(lIndex);
46
+ // If l is a subtype of the current r branch, intersections
47
+ // with the remaining branches of r won't lead to distinct
48
+ // branches, so we set a flag indicating we can skip them.
49
+ lImpliesR = true;
50
+ return null;
51
+ } else if (subresult === r) {
52
+ result.rSubconditionsOfL.push(rIndex);
53
+ // If r is a subtype of the current l branch, it is removed
54
+ // from pairsByR because future intersections won't lead to
55
+ // distinct branches.
56
+ rPairs.distinct = null;
57
+ return null;
58
+ } else if ((0, _composeTs.isEquality)(subresult)) {
59
+ // Combination of l and r subtype cases.
60
+ result.equalities.push([
61
+ lIndex,
62
+ rIndex
63
+ ]);
64
+ lImpliesR = true;
65
+ rPairs.distinct = null;
66
+ return null;
67
+ } else if ((0, _domainsTs.hasDomain)(subresult, "object")) {
68
+ // Neither branch is a subtype of the other, return
69
+ // the result of the intersection as a candidate
70
+ // branch for the final union
71
+ return subresult;
72
+ }
73
+ return (0, _errorsTs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsTs.domainOf)(subresult)}'`);
74
+ });
75
+ if (!lImpliesR) {
76
+ for(let i = 0; i < pairs.length; i++){
77
+ if (distinct[i]) {
78
+ pairs[i].distinct?.push(distinct[i]);
79
+ }
80
+ }
81
+ }
82
+ });
83
+ result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
84
+ return result;
85
+ };