arktype 0.1.1 → 0.1.3
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.
- package/dist/api.cjs +4 -1
- package/dist/api.d.ts +1 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/main.js +127 -15
- package/dist/cjs/nodes/branches.js +7 -7
- package/dist/cjs/nodes/compose.js +3 -2
- package/dist/cjs/nodes/discriminate.js +5 -4
- package/dist/cjs/nodes/functors.js +3 -3
- package/dist/cjs/nodes/node.js +1 -1
- package/dist/cjs/nodes/predicate.js +19 -14
- package/dist/cjs/nodes/rules/collapsibleSet.js +3 -4
- package/dist/cjs/nodes/rules/objectKind.js +78 -0
- package/dist/cjs/nodes/rules/range.js +2 -2
- package/dist/cjs/nodes/rules/rules.js +4 -4
- package/dist/cjs/parse/definition.js +26 -6
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/tuple/morph.js +2 -2
- package/dist/cjs/parse/tuple/tuple.js +1 -1
- package/dist/cjs/traverse/problems.js +18 -32
- package/dist/cjs/traverse/traverse.js +12 -12
- package/dist/cjs/utils/domains.js +10 -47
- package/dist/cjs/utils/generics.js +1 -3
- package/dist/cjs/utils/objectKinds.js +64 -0
- package/dist/cjs/utils/serialize.js +8 -8
- package/dist/cjs/utils/size.js +16 -0
- package/dist/mjs/main.js +127 -15
- package/dist/mjs/nodes/branches.js +7 -7
- package/dist/mjs/nodes/compose.js +3 -2
- package/dist/mjs/nodes/discriminate.js +6 -5
- package/dist/mjs/nodes/functors.js +3 -3
- package/dist/mjs/nodes/node.js +1 -1
- package/dist/mjs/nodes/predicate.js +20 -15
- package/dist/mjs/nodes/rules/collapsibleSet.js +3 -4
- package/dist/mjs/nodes/rules/{subdomain.js → objectKind.js} +10 -10
- package/dist/mjs/nodes/rules/range.js +1 -1
- package/dist/mjs/nodes/rules/rules.js +4 -4
- package/dist/mjs/parse/definition.js +27 -7
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/tuple/morph.js +2 -2
- package/dist/mjs/parse/tuple/tuple.js +1 -1
- package/dist/mjs/traverse/problems.js +18 -31
- package/dist/mjs/traverse/traverse.js +12 -12
- package/dist/mjs/utils/domains.js +9 -42
- package/dist/mjs/utils/generics.js +0 -1
- package/dist/mjs/utils/objectKinds.js +48 -0
- package/dist/mjs/utils/serialize.js +8 -8
- package/dist/mjs/utils/size.js +2 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +73 -9
- package/dist/types/nodes/branches.d.ts +4 -4
- package/dist/types/nodes/compose.d.ts +12 -5
- package/dist/types/nodes/discriminate.d.ts +3 -2
- package/dist/types/nodes/infer.d.ts +11 -10
- package/dist/types/nodes/predicate.d.ts +2 -2
- package/dist/types/nodes/rules/objectKind.d.ts +15 -0
- package/dist/types/nodes/rules/range.d.ts +1 -1
- package/dist/types/nodes/rules/rules.d.ts +4 -4
- package/dist/types/parse/definition.d.ts +6 -5
- package/dist/types/parse/string/ast.d.ts +4 -2
- package/dist/types/parse/string/reduce/shared.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
- package/dist/types/traverse/problems.d.ts +25 -41
- package/dist/types/traverse/traverse.d.ts +24 -4
- package/dist/types/utils/domains.d.ts +11 -37
- package/dist/types/utils/generics.d.ts +0 -1
- package/dist/types/utils/objectKinds.d.ts +83 -0
- package/dist/types/utils/serialize.d.ts +2 -2
- package/dist/types/utils/size.d.ts +3 -0
- package/package.json +10 -5
- package/dist/cjs/nodes/rules/subdomain.js +0 -78
- package/dist/types/nodes/rules/subdomain.d.ts +0 -15
|
@@ -12,9 +12,8 @@ _export(exports, {
|
|
|
12
12
|
ArkTypeError: ()=>ArkTypeError,
|
|
13
13
|
Problem: ()=>Problem,
|
|
14
14
|
Problems: ()=>Problems,
|
|
15
|
-
|
|
15
|
+
describeDomains: ()=>describeDomains,
|
|
16
16
|
describeBranches: ()=>describeBranches,
|
|
17
|
-
subdomainDescriptions: ()=>subdomainDescriptions,
|
|
18
17
|
DataWrapper: ()=>DataWrapper,
|
|
19
18
|
defaultProblemWriters: ()=>defaultProblemWriters,
|
|
20
19
|
compileProblemOptions: ()=>compileProblemOptions
|
|
@@ -22,6 +21,7 @@ _export(exports, {
|
|
|
22
21
|
const _scannerTs = require("../parse/string/shift/scanner.js");
|
|
23
22
|
const _domainsTs = require("../utils/domains.js");
|
|
24
23
|
const _genericsTs = require("../utils/generics.js");
|
|
24
|
+
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
25
25
|
const _pathsTs = require("../utils/paths.js");
|
|
26
26
|
const _serializeTs = require("../utils/serialize.js");
|
|
27
27
|
var _definitions_code, _definitions_code1, _definitions_code2, _clausesByPath, _pathKey;
|
|
@@ -97,14 +97,14 @@ class ProblemArray extends Array {
|
|
|
97
97
|
}
|
|
98
98
|
const Problems = ProblemArray;
|
|
99
99
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
return
|
|
100
|
+
const describeDomains = (domains)=>{
|
|
101
|
+
if (domains.length === 1) {
|
|
102
|
+
return _domainsTs.domainDescriptions[domains[0]];
|
|
103
103
|
}
|
|
104
|
-
if (
|
|
104
|
+
if (domains.length === 0) {
|
|
105
105
|
return "never";
|
|
106
106
|
}
|
|
107
|
-
return describeBranches(
|
|
107
|
+
return describeBranches(domains.map((objectKind)=>_domainsTs.domainDescriptions[objectKind]));
|
|
108
108
|
};
|
|
109
109
|
const describeBranches = (descriptions)=>{
|
|
110
110
|
let description = "";
|
|
@@ -117,23 +117,6 @@ const describeBranches = (descriptions)=>{
|
|
|
117
117
|
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
118
118
|
return description;
|
|
119
119
|
};
|
|
120
|
-
const subdomainDescriptions = {
|
|
121
|
-
bigint: "a bigint",
|
|
122
|
-
boolean: "boolean",
|
|
123
|
-
null: "null",
|
|
124
|
-
number: "a number",
|
|
125
|
-
object: "an object",
|
|
126
|
-
string: "a string",
|
|
127
|
-
symbol: "a symbol",
|
|
128
|
-
undefined: "undefined",
|
|
129
|
-
Array: "an array",
|
|
130
|
-
Function: "a function",
|
|
131
|
-
Date: "a Date",
|
|
132
|
-
RegExp: "a RegExp",
|
|
133
|
-
Error: "an Error",
|
|
134
|
-
Map: "a Map",
|
|
135
|
-
Set: "a Set"
|
|
136
|
-
};
|
|
137
120
|
class DataWrapper {
|
|
138
121
|
toString() {
|
|
139
122
|
return (0, _serializeTs.stringify)(this.value);
|
|
@@ -141,18 +124,17 @@ class DataWrapper {
|
|
|
141
124
|
get domain() {
|
|
142
125
|
return (0, _domainsTs.domainOf)(this.value);
|
|
143
126
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return (0, _domainsTs.subdomainOf)(this.value);
|
|
127
|
+
get objectKind() {
|
|
128
|
+
return (0, _objectKindsTs.objectKindOf)(this.value) ?? "non-object";
|
|
147
129
|
}
|
|
148
130
|
get size() {
|
|
149
|
-
return
|
|
131
|
+
return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
|
|
150
132
|
}
|
|
151
133
|
get units() {
|
|
152
|
-
return
|
|
134
|
+
return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
|
|
153
135
|
}
|
|
154
136
|
get className() {
|
|
155
|
-
return (
|
|
137
|
+
return Object(this.value).constructor.name;
|
|
156
138
|
}
|
|
157
139
|
constructor(value){
|
|
158
140
|
this.value = value;
|
|
@@ -179,13 +161,17 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
179
161
|
was: (data)=>data.className
|
|
180
162
|
},
|
|
181
163
|
domain: {
|
|
182
|
-
mustBe: (domain)=>
|
|
164
|
+
mustBe: (domain)=>_domainsTs.domainDescriptions[domain],
|
|
183
165
|
was: (data)=>data.domain
|
|
184
166
|
},
|
|
185
167
|
domainBranches: {
|
|
186
|
-
mustBe: (domains)=>
|
|
168
|
+
mustBe: (domains)=>describeDomains(domains),
|
|
187
169
|
was: (data)=>data.domain
|
|
188
170
|
},
|
|
171
|
+
objectKind: {
|
|
172
|
+
mustBe: (kind)=>_objectKindsTs.objectKindDescriptions[kind],
|
|
173
|
+
was: (data)=>data.objectKind
|
|
174
|
+
},
|
|
189
175
|
missing: {
|
|
190
176
|
mustBe: ()=>"defined",
|
|
191
177
|
was: ()=>""
|
|
@@ -12,7 +12,7 @@ _export(exports, {
|
|
|
12
12
|
TraversalState: ()=>TraversalState,
|
|
13
13
|
traverse: ()=>traverse,
|
|
14
14
|
checkEntries: ()=>checkEntries,
|
|
15
|
-
|
|
15
|
+
checkObjectKind: ()=>checkObjectKind
|
|
16
16
|
});
|
|
17
17
|
const _discriminateJs = require("../nodes/discriminate.js");
|
|
18
18
|
const _classJs = require("../nodes/rules/class.js");
|
|
@@ -23,6 +23,7 @@ const _rulesJs = require("../nodes/rules/rules.js");
|
|
|
23
23
|
const _domainsJs = require("../utils/domains.js");
|
|
24
24
|
const _errorsJs = require("../utils/errors.js");
|
|
25
25
|
const _genericsJs = require("../utils/generics.js");
|
|
26
|
+
const _objectKindsJs = require("../utils/objectKinds.js");
|
|
26
27
|
const _pathsJs = require("../utils/paths.js");
|
|
27
28
|
const _serializeJs = require("../utils/serialize.js");
|
|
28
29
|
const _problemsJs = require("./problems.js");
|
|
@@ -173,7 +174,6 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
|
|
|
173
174
|
state.path.push(propKey);
|
|
174
175
|
if (!(0, _genericsJs.hasKey)(data, propKey)) {
|
|
175
176
|
if (propKind !== "optionalProps") {
|
|
176
|
-
// TODO: update
|
|
177
177
|
state.problems.add("missing", undefined, undefined);
|
|
178
178
|
}
|
|
179
179
|
} else {
|
|
@@ -185,18 +185,18 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
|
|
|
185
185
|
};
|
|
186
186
|
const checkRequiredProps = createPropChecker("requiredProps");
|
|
187
187
|
const checkOptionalProps = createPropChecker("optionalProps");
|
|
188
|
-
const
|
|
189
|
-
const
|
|
188
|
+
const checkObjectKind = (data, rule, state)=>{
|
|
189
|
+
const dataObjectKind = (0, _objectKindsJs.objectKindOf)(data);
|
|
190
190
|
if (typeof rule === "string") {
|
|
191
|
-
if (
|
|
192
|
-
return state.problems.add("
|
|
191
|
+
if (dataObjectKind !== rule) {
|
|
192
|
+
return state.problems.add("objectKind", data, rule);
|
|
193
193
|
}
|
|
194
194
|
return;
|
|
195
195
|
}
|
|
196
|
-
if (
|
|
197
|
-
return state.problems.add("
|
|
196
|
+
if (dataObjectKind !== rule[0]) {
|
|
197
|
+
return state.problems.add("objectKind", data, rule[0]);
|
|
198
198
|
}
|
|
199
|
-
if (
|
|
199
|
+
if (dataObjectKind === "Array" && typeof rule[2] === "number") {
|
|
200
200
|
const actual = data.length;
|
|
201
201
|
const expected = rule[2];
|
|
202
202
|
if (expected !== actual) {
|
|
@@ -207,7 +207,7 @@ const checkSubdomain = (data, rule, state)=>{
|
|
|
207
207
|
});
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
|
-
if (
|
|
210
|
+
if (dataObjectKind === "Array" || dataObjectKind === "Set") {
|
|
211
211
|
let i = 0;
|
|
212
212
|
for (const item of data){
|
|
213
213
|
state.path.push(`${i}`);
|
|
@@ -216,7 +216,7 @@ const checkSubdomain = (data, rule, state)=>{
|
|
|
216
216
|
i++;
|
|
217
217
|
}
|
|
218
218
|
} else {
|
|
219
|
-
return (0, _errorsJs.throwInternalError)(`Unexpected
|
|
219
|
+
return (0, _errorsJs.throwInternalError)(`Unexpected objectKind entry ${(0, _serializeJs.stringify)(rule)}`);
|
|
220
220
|
}
|
|
221
221
|
return true;
|
|
222
222
|
};
|
|
@@ -236,7 +236,7 @@ const checkers = {
|
|
|
236
236
|
state.problems.add("domain", data, domain);
|
|
237
237
|
}
|
|
238
238
|
},
|
|
239
|
-
|
|
239
|
+
objectKind: checkObjectKind,
|
|
240
240
|
bound: _rangeJs.checkBound,
|
|
241
241
|
requiredProps: checkRequiredProps,
|
|
242
242
|
optionalProps: checkOptionalProps,
|
|
@@ -11,57 +11,20 @@ function _export(target, all) {
|
|
|
11
11
|
_export(exports, {
|
|
12
12
|
hasDomain: ()=>hasDomain,
|
|
13
13
|
domainOf: ()=>domainOf,
|
|
14
|
-
|
|
15
|
-
hasSubdomain: ()=>hasSubdomain,
|
|
16
|
-
sizeOf: ()=>sizeOf,
|
|
17
|
-
unitsOf: ()=>unitsOf,
|
|
18
|
-
classNameOf: ()=>classNameOf
|
|
14
|
+
domainDescriptions: ()=>domainDescriptions
|
|
19
15
|
});
|
|
20
|
-
const _genericsTs = require("./generics.js");
|
|
21
16
|
const hasDomain = (data, domain)=>domainOf(data) === domain;
|
|
22
17
|
const domainOf = (data)=>{
|
|
23
18
|
const builtinType = typeof data;
|
|
24
19
|
return builtinType === "object" ? data === null ? "null" : "object" : builtinType === "function" ? "object" : builtinType;
|
|
25
20
|
};
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
const domainDescriptions = {
|
|
22
|
+
bigint: "a bigint",
|
|
23
|
+
boolean: "boolean",
|
|
24
|
+
null: "null",
|
|
25
|
+
number: "a number",
|
|
26
|
+
object: "an object",
|
|
27
|
+
string: "a string",
|
|
28
|
+
symbol: "a symbol",
|
|
29
|
+
undefined: "undefined"
|
|
35
30
|
};
|
|
36
|
-
const subdomainOf = (data)=>{
|
|
37
|
-
const domain = domainOf(data);
|
|
38
|
-
if (domain !== "object") {
|
|
39
|
-
return domain;
|
|
40
|
-
}
|
|
41
|
-
if (Array.isArray(data)) {
|
|
42
|
-
return "Array";
|
|
43
|
-
}
|
|
44
|
-
// The raw result will be something like [object Date]
|
|
45
|
-
const prototypeName = Object.prototype.toString.call(data).slice(8, -1);
|
|
46
|
-
if ((0, _genericsTs.isKeyOf)(prototypeName, objectSubdomains)) {
|
|
47
|
-
return data instanceof objectSubdomains[prototypeName] ? prototypeName : "object";
|
|
48
|
-
}
|
|
49
|
-
if (prototypeName.endsWith("Error")) {
|
|
50
|
-
return data instanceof Error ? "Error" : "object";
|
|
51
|
-
}
|
|
52
|
-
return "object";
|
|
53
|
-
};
|
|
54
|
-
const hasSubdomain = (data, subdomain)=>subdomainOf(data) === subdomain;
|
|
55
|
-
const sizeOf = (data)=>sizedSubdomains[subdomainOf(data)]?.(data) ?? 0;
|
|
56
|
-
const sizedSubdomains = {
|
|
57
|
-
number: (data)=>data,
|
|
58
|
-
string: (data)=>data.length,
|
|
59
|
-
Array: (data)=>data.length
|
|
60
|
-
};
|
|
61
|
-
const unitsOf = (data)=>sizeUnits[subdomainOf(data)] ?? "";
|
|
62
|
-
const sizeUnits = {
|
|
63
|
-
number: "",
|
|
64
|
-
string: "characters",
|
|
65
|
-
Array: "items"
|
|
66
|
-
};
|
|
67
|
-
const classNameOf = (data)=>Object(data).constructor.name;
|
|
@@ -16,8 +16,7 @@ _export(exports, {
|
|
|
16
16
|
hasKey: ()=>hasKey,
|
|
17
17
|
keyCount: ()=>keyCount,
|
|
18
18
|
hasKeys: ()=>hasKeys,
|
|
19
|
-
listFrom: ()=>listFrom
|
|
20
|
-
collapseIfSingleton: ()=>collapseIfSingleton
|
|
19
|
+
listFrom: ()=>listFrom
|
|
21
20
|
});
|
|
22
21
|
const _domainsTs = require("./domains.js");
|
|
23
22
|
const asConst = (t)=>t;
|
|
@@ -33,4 +32,3 @@ const hasKeys = (value)=>(0, _domainsTs.hasDomain)(value, "object") ? Object.key
|
|
|
33
32
|
const listFrom = (data)=>Array.isArray(data) ? data : [
|
|
34
33
|
data
|
|
35
34
|
];
|
|
36
|
-
const collapseIfSingleton = (array)=>array.length === 1 ? array[0] : array;
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
objectKindOf: ()=>objectKindOf,
|
|
13
|
+
hasObjectKind: ()=>hasObjectKind,
|
|
14
|
+
isArray: ()=>isArray,
|
|
15
|
+
objectKindDescriptions: ()=>objectKindDescriptions
|
|
16
|
+
});
|
|
17
|
+
const _domainsTs = require("./domains.js");
|
|
18
|
+
// Built-in object constructors based on a subset of:
|
|
19
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
|
20
|
+
const defaultObjectKinds = {
|
|
21
|
+
Array,
|
|
22
|
+
Date,
|
|
23
|
+
Error,
|
|
24
|
+
Function,
|
|
25
|
+
Map,
|
|
26
|
+
RegExp,
|
|
27
|
+
Set,
|
|
28
|
+
Object,
|
|
29
|
+
String,
|
|
30
|
+
Number,
|
|
31
|
+
Boolean,
|
|
32
|
+
WeakMap,
|
|
33
|
+
WeakSet,
|
|
34
|
+
Promise
|
|
35
|
+
};
|
|
36
|
+
const objectKindOf = (data, kinds)=>{
|
|
37
|
+
if ((0, _domainsTs.domainOf)(data) !== "object") {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const kindSet = kinds ?? defaultObjectKinds;
|
|
41
|
+
let prototype = Object.getPrototypeOf(data);
|
|
42
|
+
while(prototype?.constructor && (!kindSet[prototype.constructor.name] || !(data instanceof kindSet[prototype.constructor.name]))){
|
|
43
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
44
|
+
}
|
|
45
|
+
return prototype?.constructor?.name;
|
|
46
|
+
};
|
|
47
|
+
const hasObjectKind = (data, kind, kinds)=>objectKindOf(data, kinds) === kind;
|
|
48
|
+
const isArray = (data)=>Array.isArray(data);
|
|
49
|
+
const objectKindDescriptions = {
|
|
50
|
+
Object: "an object",
|
|
51
|
+
Array: "an array",
|
|
52
|
+
Function: "a function",
|
|
53
|
+
Date: "a Date",
|
|
54
|
+
RegExp: "a RegExp",
|
|
55
|
+
Error: "an Error",
|
|
56
|
+
Map: "a Map",
|
|
57
|
+
Set: "a Set",
|
|
58
|
+
String: "a String object",
|
|
59
|
+
Number: "a Number object",
|
|
60
|
+
Boolean: "a Boolean object",
|
|
61
|
+
Promise: "a Promise",
|
|
62
|
+
WeakMap: "a WeakMap",
|
|
63
|
+
WeakSet: "a WeakSet"
|
|
64
|
+
};
|
|
@@ -23,7 +23,7 @@ const snapshotRecurse = (v, context, seen)=>{
|
|
|
23
23
|
switch((0, _domainsTs.domainOf)(v)){
|
|
24
24
|
case "object":
|
|
25
25
|
if (typeof v === "function") {
|
|
26
|
-
return
|
|
26
|
+
return stringifyOpts.onFunction(v);
|
|
27
27
|
}
|
|
28
28
|
if (seen.includes(v)) {
|
|
29
29
|
return "(cycle)";
|
|
@@ -41,7 +41,7 @@ const snapshotRecurse = (v, context, seen)=>{
|
|
|
41
41
|
}
|
|
42
42
|
return result;
|
|
43
43
|
case "symbol":
|
|
44
|
-
return
|
|
44
|
+
return stringifyOpts.onSymbol(v);
|
|
45
45
|
case "bigint":
|
|
46
46
|
return `${v}n`;
|
|
47
47
|
case "undefined":
|
|
@@ -50,7 +50,7 @@ const snapshotRecurse = (v, context, seen)=>{
|
|
|
50
50
|
return v;
|
|
51
51
|
}
|
|
52
52
|
};
|
|
53
|
-
const serialize = (data, opts = {})=>{
|
|
53
|
+
const serialize = (data, opts = {}, indent)=>{
|
|
54
54
|
const seen = [];
|
|
55
55
|
return JSON.stringify(data, (k, v)=>{
|
|
56
56
|
switch((0, _domainsTs.domainOf)(v)){
|
|
@@ -71,19 +71,19 @@ const serialize = (data, opts = {})=>{
|
|
|
71
71
|
default:
|
|
72
72
|
return v;
|
|
73
73
|
}
|
|
74
|
-
});
|
|
74
|
+
}, indent);
|
|
75
75
|
};
|
|
76
|
-
const
|
|
76
|
+
const stringifyOpts = {
|
|
77
77
|
onCycle: ()=>"(cycle)",
|
|
78
78
|
onSymbol: (v)=>`(symbol${v.description && ` ${v.description}`})`,
|
|
79
79
|
onFunction: (v)=>`(function${v.name && ` ${v.name}`})`
|
|
80
80
|
};
|
|
81
|
-
const stringify = (data)=>{
|
|
81
|
+
const stringify = (data, indent)=>{
|
|
82
82
|
switch((0, _domainsTs.domainOf)(data)){
|
|
83
83
|
case "object":
|
|
84
|
-
return serialize(data,
|
|
84
|
+
return serialize(data, stringifyOpts, indent);
|
|
85
85
|
case "symbol":
|
|
86
|
-
return
|
|
86
|
+
return stringifyOpts.onSymbol(data);
|
|
87
87
|
default:
|
|
88
88
|
return serializePrimitive(data);
|
|
89
89
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
sizeOf: ()=>sizeOf,
|
|
13
|
+
unitsOf: ()=>unitsOf
|
|
14
|
+
});
|
|
15
|
+
const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
|
|
16
|
+
const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items" : "";
|
package/dist/mjs/main.js
CHANGED
|
@@ -89,7 +89,7 @@ const scopeRegistry = {};
|
|
|
89
89
|
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();
|
|
90
90
|
export class Scope {
|
|
91
91
|
createAnonymousTypeName() {
|
|
92
|
-
return `
|
|
92
|
+
return `type${++_classPrivateFieldUpdate(this, _anonymousTypeCount).value}`;
|
|
93
93
|
}
|
|
94
94
|
get infer() {
|
|
95
95
|
return chainableNoOpProxy;
|
|
@@ -222,10 +222,10 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
222
222
|
}
|
|
223
223
|
// TODO: merge scope options
|
|
224
224
|
const initializeType = (definition, opts, scope)=>{
|
|
225
|
-
const name = opts.name ??
|
|
225
|
+
const name = opts.name ?? "type";
|
|
226
226
|
const meta = {
|
|
227
227
|
name,
|
|
228
|
-
id: `${scope.name}.${name}`,
|
|
228
|
+
id: `${scope.name}.${opts.name ? name : scope.createAnonymousTypeName()}`,
|
|
229
229
|
definition,
|
|
230
230
|
scope,
|
|
231
231
|
problems: compileProblemOptions(opts.problems),
|
|
@@ -302,7 +302,7 @@ const always = {
|
|
|
302
302
|
undefined: true
|
|
303
303
|
};
|
|
304
304
|
export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
305
|
-
const
|
|
305
|
+
const tsKeywords = scope({
|
|
306
306
|
any: [
|
|
307
307
|
"node",
|
|
308
308
|
always
|
|
@@ -384,21 +384,130 @@ const ts = scope({
|
|
|
384
384
|
{
|
|
385
385
|
undefined: true
|
|
386
386
|
}
|
|
387
|
-
]
|
|
388
|
-
|
|
387
|
+
]
|
|
388
|
+
}, {
|
|
389
|
+
name: "ts",
|
|
390
|
+
standard: false
|
|
391
|
+
});
|
|
392
|
+
const tsKeywordsSpace = tsKeywords.compile();
|
|
393
|
+
const jsObjects = scope({
|
|
389
394
|
Function: [
|
|
390
395
|
"node",
|
|
391
396
|
{
|
|
392
397
|
object: {
|
|
393
|
-
|
|
398
|
+
objectKind: "Function"
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
],
|
|
402
|
+
Array: [
|
|
403
|
+
"node",
|
|
404
|
+
{
|
|
405
|
+
object: {
|
|
406
|
+
objectKind: "Array"
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
],
|
|
410
|
+
Date: [
|
|
411
|
+
"node",
|
|
412
|
+
{
|
|
413
|
+
object: {
|
|
414
|
+
objectKind: "Date"
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
],
|
|
418
|
+
Error: [
|
|
419
|
+
"node",
|
|
420
|
+
{
|
|
421
|
+
object: {
|
|
422
|
+
objectKind: "Error"
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
],
|
|
426
|
+
Map: [
|
|
427
|
+
"node",
|
|
428
|
+
{
|
|
429
|
+
object: {
|
|
430
|
+
objectKind: "Map"
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
],
|
|
434
|
+
RegExp: [
|
|
435
|
+
"node",
|
|
436
|
+
{
|
|
437
|
+
object: {
|
|
438
|
+
objectKind: "RegExp"
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
],
|
|
442
|
+
Set: [
|
|
443
|
+
"node",
|
|
444
|
+
{
|
|
445
|
+
object: {
|
|
446
|
+
objectKind: "Set"
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
],
|
|
450
|
+
Object: [
|
|
451
|
+
"node",
|
|
452
|
+
{
|
|
453
|
+
object: {
|
|
454
|
+
objectKind: "Object"
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
],
|
|
458
|
+
String: [
|
|
459
|
+
"node",
|
|
460
|
+
{
|
|
461
|
+
object: {
|
|
462
|
+
objectKind: "String"
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
],
|
|
466
|
+
Number: [
|
|
467
|
+
"node",
|
|
468
|
+
{
|
|
469
|
+
object: {
|
|
470
|
+
objectKind: "Number"
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
],
|
|
474
|
+
Boolean: [
|
|
475
|
+
"node",
|
|
476
|
+
{
|
|
477
|
+
object: {
|
|
478
|
+
objectKind: "Boolean"
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
],
|
|
482
|
+
WeakMap: [
|
|
483
|
+
"node",
|
|
484
|
+
{
|
|
485
|
+
object: {
|
|
486
|
+
objectKind: "WeakMap"
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
],
|
|
490
|
+
WeakSet: [
|
|
491
|
+
"node",
|
|
492
|
+
{
|
|
493
|
+
object: {
|
|
494
|
+
objectKind: "WeakSet"
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
],
|
|
498
|
+
Promise: [
|
|
499
|
+
"node",
|
|
500
|
+
{
|
|
501
|
+
object: {
|
|
502
|
+
objectKind: "Promise"
|
|
394
503
|
}
|
|
395
504
|
}
|
|
396
505
|
]
|
|
397
506
|
}, {
|
|
398
|
-
name: "
|
|
507
|
+
name: "jsObjects",
|
|
399
508
|
standard: false
|
|
400
509
|
});
|
|
401
|
-
const
|
|
510
|
+
const jsObjectsSpace = jsObjects.compile();
|
|
402
511
|
const validation = scope({
|
|
403
512
|
email: /^(.+)@(.+)\.(.+)$/,
|
|
404
513
|
alphanumeric: /^[dA-Za-z]+$/,
|
|
@@ -417,24 +526,27 @@ const validation = scope({
|
|
|
417
526
|
name: "validation",
|
|
418
527
|
standard: false
|
|
419
528
|
});
|
|
420
|
-
const
|
|
529
|
+
const validationSpace = validation.compile();
|
|
421
530
|
const standard = scope({}, {
|
|
422
531
|
name: "standard",
|
|
423
532
|
includes: [
|
|
424
|
-
|
|
425
|
-
|
|
533
|
+
tsKeywordsSpace,
|
|
534
|
+
jsObjectsSpace,
|
|
535
|
+
validationSpace
|
|
426
536
|
],
|
|
427
537
|
standard: false
|
|
428
538
|
});
|
|
429
539
|
const standardTypes = standard.compile();
|
|
430
540
|
export const scopes = {
|
|
431
|
-
|
|
541
|
+
tsKeywords,
|
|
542
|
+
jsObjects,
|
|
432
543
|
validation,
|
|
433
544
|
standard
|
|
434
545
|
};
|
|
435
546
|
export const spaces = {
|
|
436
|
-
|
|
437
|
-
|
|
547
|
+
tsKeywords: tsKeywordsSpace,
|
|
548
|
+
jsObjects: jsObjectsSpace,
|
|
549
|
+
validation: validationSpace,
|
|
438
550
|
standard: standardTypes
|
|
439
551
|
};
|
|
440
552
|
export const type = scopes.standard.type;
|
|
@@ -2,13 +2,13 @@ import { domainOf, hasDomain } from "../utils/domains.js";
|
|
|
2
2
|
import { throwInternalError } from "../utils/errors.js";
|
|
3
3
|
import { isDisjoint, isEquality } from "./compose.js";
|
|
4
4
|
import { branchIntersection } from "./rules/rules.js";
|
|
5
|
-
export const isBranchComparison = (comparison)=>comparison?.
|
|
5
|
+
export const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
|
|
6
6
|
export const compareBranches = (lConditions, rConditions, state)=>{
|
|
7
7
|
const result = {
|
|
8
|
-
lConditions,
|
|
9
|
-
rConditions,
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
lBranches: lConditions,
|
|
9
|
+
rBranches: rConditions,
|
|
10
|
+
lExtendsR: [],
|
|
11
|
+
rExtendsL: [],
|
|
12
12
|
equalities: [],
|
|
13
13
|
distinctIntersections: []
|
|
14
14
|
};
|
|
@@ -28,14 +28,14 @@ export const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
28
28
|
// doesn't tell us about any redundancies or add a distinct pair
|
|
29
29
|
return null;
|
|
30
30
|
} else if (subresult === l) {
|
|
31
|
-
result.
|
|
31
|
+
result.lExtendsR.push(lIndex);
|
|
32
32
|
// If l is a subtype of the current r branch, intersections
|
|
33
33
|
// with the remaining branches of r won't lead to distinct
|
|
34
34
|
// branches, so we set a flag indicating we can skip them.
|
|
35
35
|
lImpliesR = true;
|
|
36
36
|
return null;
|
|
37
37
|
} else if (subresult === r) {
|
|
38
|
-
result.
|
|
38
|
+
result.rExtendsL.push(rIndex);
|
|
39
39
|
// If r is a subtype of the current l branch, it is removed
|
|
40
40
|
// from pairsByR because future intersections won't lead to
|
|
41
41
|
// distinct branches.
|
|
@@ -46,13 +46,14 @@ export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r
|
|
|
46
46
|
export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
48
48
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
49
|
-
|
|
49
|
+
objectKind: ({ l , r })=>`${l} and ${r}`,
|
|
50
50
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
51
51
|
class: ({ l , r })=>`classes ${l.name} and ${r.name}`,
|
|
52
52
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
53
53
|
value: ({ l , r })=>`literal values ${serialize(l)} and ${serialize(r)}`,
|
|
54
54
|
leftAssignability: ({ l , r })=>`literal value ${serialize(l.value)} and ${serialize(r)}`,
|
|
55
|
-
rightAssignability: ({ l , r })=>`literal value ${serialize(r.value)} and ${serialize(l)}
|
|
55
|
+
rightAssignability: ({ l , r })=>`literal value ${serialize(r.value)} and ${serialize(l)}`,
|
|
56
|
+
union: ({ l , r })=>`branches ${serialize(l)} and branches ${serialize(r)}`
|
|
56
57
|
};
|
|
57
58
|
export const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
|
|
58
59
|
var _disjoints = /*#__PURE__*/ new WeakMap();
|