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
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { writeUndiscriminatableMorphUnionMessage } from "../parse/string/ast.js";
|
|
2
|
-
import { domainOf
|
|
2
|
+
import { domainOf } from "../utils/domains.js";
|
|
3
3
|
import { throwParseError } from "../utils/errors.js";
|
|
4
4
|
import { hasKey, isKeyOf, keysOf } from "../utils/generics.js";
|
|
5
|
+
import { isArray, objectKindOf } from "../utils/objectKinds.js";
|
|
5
6
|
import { Path } from "../utils/paths.js";
|
|
6
7
|
import { serializePrimitive } from "../utils/serialize.js";
|
|
7
8
|
import { IntersectionState } from "./compose.js";
|
|
@@ -44,7 +45,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
44
45
|
};
|
|
45
46
|
const discriminantKinds = {
|
|
46
47
|
domain: true,
|
|
47
|
-
|
|
48
|
+
objectKind: true,
|
|
48
49
|
value: true
|
|
49
50
|
};
|
|
50
51
|
const calculateDiscriminants = (branches, ctx)=>{
|
|
@@ -61,7 +62,7 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
61
62
|
const intersectionState = new IntersectionState(ctx.type, "|");
|
|
62
63
|
branchIntersection(branches[lIndex], branches[rIndex], intersectionState);
|
|
63
64
|
for(const path in intersectionState.disjoints){
|
|
64
|
-
// designed to match
|
|
65
|
+
// designed to match objectKind path segments like "${number}"
|
|
65
66
|
if (path.includes("${")) {
|
|
66
67
|
continue;
|
|
67
68
|
}
|
|
@@ -176,9 +177,9 @@ const serializeIfPrimitive = (data)=>{
|
|
|
176
177
|
};
|
|
177
178
|
const caseSerializers = {
|
|
178
179
|
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
179
|
-
|
|
180
|
+
objectKind: (data)=>objectKindOf(data) ?? "default",
|
|
180
181
|
domain: domainOf
|
|
181
182
|
};
|
|
182
183
|
export const serializeCase = (kind, data)=>caseSerializers[kind](data);
|
|
183
184
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(isOptional(prop) ? prop[1] : prop, $)) : false;
|
|
184
|
-
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false :
|
|
185
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const functors = {
|
|
2
2
|
Array: (node)=>({
|
|
3
3
|
object: {
|
|
4
|
-
|
|
4
|
+
objectKind: [
|
|
5
5
|
"Array",
|
|
6
6
|
node
|
|
7
7
|
]
|
|
@@ -9,7 +9,7 @@ export const functors = {
|
|
|
9
9
|
}),
|
|
10
10
|
Set: (node)=>({
|
|
11
11
|
object: {
|
|
12
|
-
|
|
12
|
+
objectKind: [
|
|
13
13
|
"Set",
|
|
14
14
|
node
|
|
15
15
|
]
|
|
@@ -17,7 +17,7 @@ export const functors = {
|
|
|
17
17
|
}),
|
|
18
18
|
Map: (k, v)=>({
|
|
19
19
|
object: {
|
|
20
|
-
|
|
20
|
+
objectKind: [
|
|
21
21
|
"Map",
|
|
22
22
|
k,
|
|
23
23
|
v
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -43,7 +43,7 @@ export const union = (l, r, type)=>{
|
|
|
43
43
|
}
|
|
44
44
|
return result;
|
|
45
45
|
};
|
|
46
|
-
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "
|
|
46
|
+
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "objectKind" || flatPredicate[0][0] === "value");
|
|
47
47
|
export const flattenType = (type)=>{
|
|
48
48
|
const ctx = {
|
|
49
49
|
type,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { keysOf, listFrom } from "../utils/generics.js";
|
|
2
|
+
import { isArray } from "../utils/objectKinds.js";
|
|
3
3
|
import { compareBranches, isBranchComparison } from "./branches.js";
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
@@ -9,7 +9,7 @@ export const comparePredicates = (l, r, context)=>{
|
|
|
9
9
|
if (l === true && r === true) {
|
|
10
10
|
return equality();
|
|
11
11
|
}
|
|
12
|
-
if (!
|
|
12
|
+
if (!isArray(l) && !isArray(r)) {
|
|
13
13
|
const result = branchIntersection(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
|
|
14
14
|
return result === l ? l : result === r ? r : result;
|
|
15
15
|
}
|
|
@@ -19,10 +19,10 @@ export const comparePredicates = (l, r, context)=>{
|
|
|
19
19
|
if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
|
|
20
20
|
return equality();
|
|
21
21
|
}
|
|
22
|
-
if (comparison.
|
|
22
|
+
if (comparison.lExtendsR.length + comparison.equalities.length === lBranches.length) {
|
|
23
23
|
return l;
|
|
24
24
|
}
|
|
25
|
-
if (comparison.
|
|
25
|
+
if (comparison.rExtendsL.length + comparison.equalities.length === rBranches.length) {
|
|
26
26
|
return r;
|
|
27
27
|
}
|
|
28
28
|
return comparison;
|
|
@@ -33,12 +33,16 @@ export const predicateIntersection = (domain, l, r, state)=>{
|
|
|
33
33
|
if (!isBranchComparison(comparison)) {
|
|
34
34
|
return comparison;
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
const resultBranches = [
|
|
37
37
|
...comparison.distinctIntersections,
|
|
38
|
-
...comparison.equalities.map((indices)=>comparison.
|
|
39
|
-
...comparison.
|
|
40
|
-
...comparison.
|
|
41
|
-
]
|
|
38
|
+
...comparison.equalities.map((indices)=>comparison.lBranches[indices[0]]),
|
|
39
|
+
...comparison.lExtendsR.map((lIndex)=>comparison.lBranches[lIndex]),
|
|
40
|
+
...comparison.rExtendsL.map((rIndex)=>comparison.rBranches[rIndex])
|
|
41
|
+
];
|
|
42
|
+
if (resultBranches.length === 0) {
|
|
43
|
+
state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
|
|
44
|
+
}
|
|
45
|
+
return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
|
|
42
46
|
};
|
|
43
47
|
export const predicateUnion = (domain, l, r, type)=>{
|
|
44
48
|
const state = new IntersectionState(type, "|");
|
|
@@ -51,16 +55,17 @@ export const predicateUnion = (domain, l, r, type)=>{
|
|
|
51
55
|
emptyRulesIfTrue(r)
|
|
52
56
|
];
|
|
53
57
|
}
|
|
54
|
-
|
|
55
|
-
...comparison.
|
|
56
|
-
...comparison.
|
|
57
|
-
]
|
|
58
|
+
const resultBranches = [
|
|
59
|
+
...comparison.lBranches.filter((_, lIndex)=>!comparison.lExtendsR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
|
|
60
|
+
...comparison.rBranches.filter((_, rIndex)=>!comparison.rExtendsL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
|
|
61
|
+
];
|
|
62
|
+
return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
|
|
58
63
|
};
|
|
59
64
|
export const flattenPredicate = (predicate, context)=>{
|
|
60
65
|
if (predicate === true) {
|
|
61
66
|
return [];
|
|
62
67
|
}
|
|
63
|
-
return
|
|
68
|
+
return isArray(predicate) ? flattenBranches(predicate, context) : flattenBranch(predicate, context);
|
|
64
69
|
};
|
|
65
70
|
export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
66
71
|
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { hasSubdomain } from "../../utils/domains.js";
|
|
2
1
|
import { equality } from "../compose.js";
|
|
3
2
|
export const collapsibleListUnion = (l, r)=>{
|
|
4
|
-
if (
|
|
5
|
-
if (
|
|
3
|
+
if (Array.isArray(l)) {
|
|
4
|
+
if (Array.isArray(r)) {
|
|
6
5
|
const result = listUnion(l, r);
|
|
7
6
|
return result.length === l.length ? result.length === r.length ? equality() : l : result.length === r.length ? r : result;
|
|
8
7
|
}
|
|
@@ -11,7 +10,7 @@ export const collapsibleListUnion = (l, r)=>{
|
|
|
11
10
|
r
|
|
12
11
|
];
|
|
13
12
|
}
|
|
14
|
-
if (
|
|
13
|
+
if (Array.isArray(r)) {
|
|
15
14
|
return r.includes(l) ? r : [
|
|
16
15
|
...r,
|
|
17
16
|
l
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { throwInternalError } from "../../utils/errors.js";
|
|
2
2
|
import { composeIntersection, equality, isDisjoint, isEquality } from "../compose.js";
|
|
3
3
|
import { flattenNode, nodeIntersection } from "../node.js";
|
|
4
|
-
export const
|
|
4
|
+
export const objectKindIntersection = composeIntersection((l, r, state)=>{
|
|
5
5
|
if (typeof l === "string") {
|
|
6
6
|
if (typeof r === "string") {
|
|
7
|
-
return l === r ? equality() : state.addDisjoint("
|
|
7
|
+
return l === r ? equality() : state.addDisjoint("objectKind", l, r);
|
|
8
8
|
}
|
|
9
|
-
return l === r[0] ? r : state.addDisjoint("
|
|
9
|
+
return l === r[0] ? r : state.addDisjoint("objectKind", l, r[0]);
|
|
10
10
|
}
|
|
11
11
|
if (typeof r === "string") {
|
|
12
|
-
return l[0] === r ? l : state.addDisjoint("
|
|
12
|
+
return l[0] === r ? l : state.addDisjoint("objectKind", l[0], r);
|
|
13
13
|
}
|
|
14
14
|
if (l[0] !== r[0]) {
|
|
15
|
-
return state.addDisjoint("
|
|
15
|
+
return state.addDisjoint("objectKind", l[0], r[0]);
|
|
16
16
|
}
|
|
17
17
|
const result = [
|
|
18
18
|
l[0]
|
|
@@ -22,7 +22,7 @@ export const subdomainIntersection = composeIntersection((l, r, state)=>{
|
|
|
22
22
|
for(let i = 1; i < l.length; i++){
|
|
23
23
|
const lNode = l[i];
|
|
24
24
|
const rNode = r[i];
|
|
25
|
-
state.path.push(
|
|
25
|
+
state.path.push(objectKindParameterToPathSegment(l[0], i));
|
|
26
26
|
const parameterResult = nodeIntersection(lNode, rNode, state);
|
|
27
27
|
state.path.pop();
|
|
28
28
|
if (isEquality(parameterResult)) {
|
|
@@ -43,15 +43,15 @@ export const subdomainIntersection = composeIntersection((l, r, state)=>{
|
|
|
43
43
|
}
|
|
44
44
|
return lImpliesR ? rImpliesL ? equality() : l : rImpliesL ? r : result;
|
|
45
45
|
});
|
|
46
|
-
const
|
|
47
|
-
export const
|
|
46
|
+
const objectKindParameterToPathSegment = (objectKind, i)=>objectKind === "Array" ? "${number}" : objectKind === "Set" ? "${item}" : objectKind === "Map" ? i === 1 ? "${key}" : "${value}" : throwInternalError(`Unexpected parameterized objectKind '${objectKind}'`);
|
|
47
|
+
export const objectKindPathSegments = {
|
|
48
48
|
"${number}": true,
|
|
49
49
|
"${item}": true,
|
|
50
50
|
"${key}": true,
|
|
51
51
|
"${value}": true
|
|
52
52
|
};
|
|
53
|
-
export const
|
|
54
|
-
"
|
|
53
|
+
export const flattenObjectKind = (entries, rule, ctx)=>entries.push([
|
|
54
|
+
"objectKind",
|
|
55
55
|
typeof rule === "string" ? rule : rule[0] === "Map" ? [
|
|
56
56
|
rule[0],
|
|
57
57
|
flattenNode(rule[1], ctx),
|
|
@@ -5,10 +5,10 @@ import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, is
|
|
|
5
5
|
import { classIntersection } from "./class.js";
|
|
6
6
|
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
7
7
|
import { divisorIntersection } from "./divisor.js";
|
|
8
|
+
import { flattenObjectKind, objectKindIntersection } from "./objectKind.js";
|
|
8
9
|
import { flattenProps, propsIntersection } from "./props.js";
|
|
9
10
|
import { flattenRange, rangeIntersection } from "./range.js";
|
|
10
11
|
import { getRegex, regexIntersection } from "./regex.js";
|
|
11
|
-
import { flattenSubdomain, subdomainIntersection } from "./subdomain.js";
|
|
12
12
|
const rulesOf = (branch)=>branch.input ?? branch;
|
|
13
13
|
export const branchIntersection = (l, r, state)=>{
|
|
14
14
|
const lRules = rulesOf(l);
|
|
@@ -40,7 +40,7 @@ export const branchIntersection = (l, r, state)=>{
|
|
|
40
40
|
export const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? equality() : state.addDisjoint("value", l.value, r.value) : literalSatisfiesRules(l.value, r, state) ? l : state.addDisjoint("leftAssignability", l, r) : "value" in r ? literalSatisfiesRules(r.value, l, state) ? r : state.addDisjoint("rightAssignability", l, r) : narrowableRulesIntersection(l, r, state);
|
|
41
41
|
const narrowIntersection = composeIntersection(collapsibleListUnion);
|
|
42
42
|
export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
43
|
-
|
|
43
|
+
objectKind: objectKindIntersection,
|
|
44
44
|
divisor: divisorIntersection,
|
|
45
45
|
regex: regexIntersection,
|
|
46
46
|
props: propsIntersection,
|
|
@@ -51,7 +51,7 @@ export const narrowableRulesIntersection = composeKeyedIntersection({
|
|
|
51
51
|
onEmpty: "bubble"
|
|
52
52
|
});
|
|
53
53
|
const ruleFlatteners = {
|
|
54
|
-
|
|
54
|
+
objectKind: flattenObjectKind,
|
|
55
55
|
regex: (entries, rule)=>{
|
|
56
56
|
for (const source of listFrom(rule)){
|
|
57
57
|
entries.push([
|
|
@@ -95,7 +95,7 @@ export const precedenceMap = {
|
|
|
95
95
|
value: 0,
|
|
96
96
|
domains: 0,
|
|
97
97
|
branches: 0,
|
|
98
|
-
|
|
98
|
+
objectKind: 0,
|
|
99
99
|
switch: 0,
|
|
100
100
|
alias: 0,
|
|
101
101
|
// Shallow: All shallow checks will be performed even if one or more fail
|
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
import { isType } from "../main.js";
|
|
2
|
-
import {
|
|
2
|
+
import { domainOf } from "../utils/domains.js";
|
|
3
3
|
import { throwParseError } from "../utils/errors.js";
|
|
4
|
+
import { objectKindOf } from "../utils/objectKinds.js";
|
|
5
|
+
import { stringify } from "../utils/serialize.js";
|
|
4
6
|
import { parseRecord } from "./record.js";
|
|
5
7
|
import { parseString } from "./string/string.js";
|
|
6
8
|
import { parseTuple } from "./tuple/tuple.js";
|
|
7
9
|
export const parseDefinition = (def, ctx)=>{
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
const domain = domainOf(def);
|
|
11
|
+
if (domain === "string") {
|
|
12
|
+
return parseString(def, ctx);
|
|
13
|
+
}
|
|
14
|
+
if (domain !== "object") {
|
|
15
|
+
return throwParseError(writeBadDefinitionTypeMessage(domain));
|
|
16
|
+
}
|
|
17
|
+
const objectKind = objectKindOf(def);
|
|
18
|
+
switch(objectKind){
|
|
19
|
+
case "Object":
|
|
20
|
+
return parseRecord(def, ctx);
|
|
21
|
+
case "Array":
|
|
22
|
+
return parseTuple(def, ctx);
|
|
23
|
+
case "RegExp":
|
|
24
|
+
return {
|
|
25
|
+
string: {
|
|
26
|
+
regex: def.source
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
case "Function":
|
|
30
|
+
return isType(def) ? def.node : throwParseError(writeBadDefinitionTypeMessage("Function"));
|
|
31
|
+
default:
|
|
32
|
+
return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? stringify(def)));
|
|
33
|
+
}
|
|
14
34
|
};
|
|
15
35
|
export const as = Symbol("as");
|
|
16
36
|
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
@@ -54,7 +54,7 @@ const distributeRange = (range, s)=>{
|
|
|
54
54
|
if (resolution.object === true) {
|
|
55
55
|
return false;
|
|
56
56
|
}
|
|
57
|
-
return listFrom(resolution.object).every((branch)=>hasKey(branch, "
|
|
57
|
+
return listFrom(resolution.object).every((branch)=>hasKey(branch, "objectKind") && (branch.objectKind === "Array" || branch.objectKind[0] === "Array"));
|
|
58
58
|
default:
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { hasSubdomain } from "../../utils/domains.js";
|
|
2
1
|
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
2
|
+
import { isArray } from "../../utils/objectKinds.js";
|
|
3
3
|
import { stringify } from "../../utils/serialize.js";
|
|
4
4
|
import { parseDefinition } from "../definition.js";
|
|
5
5
|
export const parseMorphTuple = (def, ctx)=>{
|
|
@@ -19,7 +19,7 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
19
19
|
morph
|
|
20
20
|
};
|
|
21
21
|
} else if (typeof predicate === "object") {
|
|
22
|
-
result[domain] =
|
|
22
|
+
result[domain] = isArray(predicate) ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
|
|
23
23
|
} else {
|
|
24
24
|
throwInternalError(`Unexpected predicate value for domain '${domain}': ${stringify(predicate)}`);
|
|
25
25
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
var _definitions_code, _definitions_code1, _definitions_code2, _clausesByPath, _pathKey;
|
|
2
2
|
import { Scanner } from "../parse/string/shift/scanner.js";
|
|
3
|
-
import {
|
|
3
|
+
import { domainDescriptions, domainOf } from "../utils/domains.js";
|
|
4
4
|
import { keysOf } from "../utils/generics.js";
|
|
5
|
+
import { objectKindDescriptions, objectKindOf } from "../utils/objectKinds.js";
|
|
5
6
|
import { Path } from "../utils/paths.js";
|
|
6
7
|
import { stringify } from "../utils/serialize.js";
|
|
7
8
|
export class ArkTypeError extends TypeError {
|
|
@@ -76,14 +77,14 @@ class ProblemArray extends Array {
|
|
|
76
77
|
}
|
|
77
78
|
export const Problems = ProblemArray;
|
|
78
79
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
79
|
-
export const
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
80
|
+
export const describeDomains = (domains)=>{
|
|
81
|
+
if (domains.length === 1) {
|
|
82
|
+
return domainDescriptions[domains[0]];
|
|
82
83
|
}
|
|
83
|
-
if (
|
|
84
|
+
if (domains.length === 0) {
|
|
84
85
|
return "never";
|
|
85
86
|
}
|
|
86
|
-
return describeBranches(
|
|
87
|
+
return describeBranches(domains.map((objectKind)=>domainDescriptions[objectKind]));
|
|
87
88
|
};
|
|
88
89
|
export const describeBranches = (descriptions)=>{
|
|
89
90
|
let description = "";
|
|
@@ -96,23 +97,6 @@ export const describeBranches = (descriptions)=>{
|
|
|
96
97
|
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
97
98
|
return description;
|
|
98
99
|
};
|
|
99
|
-
/** Each Subdomain's completion for the phrase "Must be _____" */ export const subdomainDescriptions = {
|
|
100
|
-
bigint: "a bigint",
|
|
101
|
-
boolean: "boolean",
|
|
102
|
-
null: "null",
|
|
103
|
-
number: "a number",
|
|
104
|
-
object: "an object",
|
|
105
|
-
string: "a string",
|
|
106
|
-
symbol: "a symbol",
|
|
107
|
-
undefined: "undefined",
|
|
108
|
-
Array: "an array",
|
|
109
|
-
Function: "a function",
|
|
110
|
-
Date: "a Date",
|
|
111
|
-
RegExp: "a RegExp",
|
|
112
|
-
Error: "an Error",
|
|
113
|
-
Map: "a Map",
|
|
114
|
-
Set: "a Set"
|
|
115
|
-
};
|
|
116
100
|
export class DataWrapper {
|
|
117
101
|
toString() {
|
|
118
102
|
return stringify(this.value);
|
|
@@ -120,18 +104,17 @@ export class DataWrapper {
|
|
|
120
104
|
get domain() {
|
|
121
105
|
return domainOf(this.value);
|
|
122
106
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return subdomainOf(this.value);
|
|
107
|
+
get objectKind() {
|
|
108
|
+
return objectKindOf(this.value) ?? "non-object";
|
|
126
109
|
}
|
|
127
110
|
get size() {
|
|
128
|
-
return
|
|
111
|
+
return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
|
|
129
112
|
}
|
|
130
113
|
get units() {
|
|
131
|
-
return
|
|
114
|
+
return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
|
|
132
115
|
}
|
|
133
116
|
get className() {
|
|
134
|
-
return
|
|
117
|
+
return Object(this.value).constructor.name;
|
|
135
118
|
}
|
|
136
119
|
constructor(value){
|
|
137
120
|
this.value = value;
|
|
@@ -158,13 +141,17 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
158
141
|
was: (data)=>data.className
|
|
159
142
|
},
|
|
160
143
|
domain: {
|
|
161
|
-
mustBe: (domain)=>
|
|
144
|
+
mustBe: (domain)=>domainDescriptions[domain],
|
|
162
145
|
was: (data)=>data.domain
|
|
163
146
|
},
|
|
164
147
|
domainBranches: {
|
|
165
|
-
mustBe: (domains)=>
|
|
148
|
+
mustBe: (domains)=>describeDomains(domains),
|
|
166
149
|
was: (data)=>data.domain
|
|
167
150
|
},
|
|
151
|
+
objectKind: {
|
|
152
|
+
mustBe: (kind)=>objectKindDescriptions[kind],
|
|
153
|
+
was: (data)=>data.objectKind
|
|
154
|
+
},
|
|
168
155
|
missing: {
|
|
169
156
|
mustBe: ()=>"defined",
|
|
170
157
|
was: ()=>""
|
|
@@ -44,9 +44,10 @@ import { checkDivisor } from "../nodes/rules/divisor.js";
|
|
|
44
44
|
import { checkBound } from "../nodes/rules/range.js";
|
|
45
45
|
import { checkRegex } from "../nodes/rules/regex.js";
|
|
46
46
|
import { precedenceMap } from "../nodes/rules/rules.js";
|
|
47
|
-
import { domainOf, hasDomain
|
|
47
|
+
import { domainOf, hasDomain } from "../utils/domains.js";
|
|
48
48
|
import { throwInternalError } from "../utils/errors.js";
|
|
49
49
|
import { hasKey, keysOf } from "../utils/generics.js";
|
|
50
|
+
import { objectKindOf } from "../utils/objectKinds.js";
|
|
50
51
|
import { getPath, Path } from "../utils/paths.js";
|
|
51
52
|
import { deserializePrimitive, stringify } from "../utils/serialize.js";
|
|
52
53
|
import { Problems } from "./problems.js";
|
|
@@ -157,7 +158,6 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
|
|
|
157
158
|
state.path.push(propKey);
|
|
158
159
|
if (!hasKey(data, propKey)) {
|
|
159
160
|
if (propKind !== "optionalProps") {
|
|
160
|
-
// TODO: update
|
|
161
161
|
state.problems.add("missing", undefined, undefined);
|
|
162
162
|
}
|
|
163
163
|
} else {
|
|
@@ -169,18 +169,18 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
|
|
|
169
169
|
};
|
|
170
170
|
const checkRequiredProps = createPropChecker("requiredProps");
|
|
171
171
|
const checkOptionalProps = createPropChecker("optionalProps");
|
|
172
|
-
export const
|
|
173
|
-
const
|
|
172
|
+
export const checkObjectKind = (data, rule, state)=>{
|
|
173
|
+
const dataObjectKind = objectKindOf(data);
|
|
174
174
|
if (typeof rule === "string") {
|
|
175
|
-
if (
|
|
176
|
-
return state.problems.add("
|
|
175
|
+
if (dataObjectKind !== rule) {
|
|
176
|
+
return state.problems.add("objectKind", data, rule);
|
|
177
177
|
}
|
|
178
178
|
return;
|
|
179
179
|
}
|
|
180
|
-
if (
|
|
181
|
-
return state.problems.add("
|
|
180
|
+
if (dataObjectKind !== rule[0]) {
|
|
181
|
+
return state.problems.add("objectKind", data, rule[0]);
|
|
182
182
|
}
|
|
183
|
-
if (
|
|
183
|
+
if (dataObjectKind === "Array" && typeof rule[2] === "number") {
|
|
184
184
|
const actual = data.length;
|
|
185
185
|
const expected = rule[2];
|
|
186
186
|
if (expected !== actual) {
|
|
@@ -191,7 +191,7 @@ export const checkSubdomain = (data, rule, state)=>{
|
|
|
191
191
|
});
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
|
-
if (
|
|
194
|
+
if (dataObjectKind === "Array" || dataObjectKind === "Set") {
|
|
195
195
|
let i = 0;
|
|
196
196
|
for (const item of data){
|
|
197
197
|
state.path.push(`${i}`);
|
|
@@ -200,7 +200,7 @@ export const checkSubdomain = (data, rule, state)=>{
|
|
|
200
200
|
i++;
|
|
201
201
|
}
|
|
202
202
|
} else {
|
|
203
|
-
return throwInternalError(`Unexpected
|
|
203
|
+
return throwInternalError(`Unexpected objectKind entry ${stringify(rule)}`);
|
|
204
204
|
}
|
|
205
205
|
return true;
|
|
206
206
|
};
|
|
@@ -220,7 +220,7 @@ const checkers = {
|
|
|
220
220
|
state.problems.add("domain", data, domain);
|
|
221
221
|
}
|
|
222
222
|
},
|
|
223
|
-
|
|
223
|
+
objectKind: checkObjectKind,
|
|
224
224
|
bound: checkBound,
|
|
225
225
|
requiredProps: checkRequiredProps,
|
|
226
226
|
optionalProps: checkOptionalProps,
|
|
@@ -1,48 +1,15 @@
|
|
|
1
|
-
import { isKeyOf } from "./generics.js";
|
|
2
1
|
export const hasDomain = (data, domain)=>domainOf(data) === domain;
|
|
3
2
|
export const domainOf = (data)=>{
|
|
4
3
|
const builtinType = typeof data;
|
|
5
4
|
return builtinType === "object" ? data === null ? "null" : "object" : builtinType === "function" ? "object" : builtinType;
|
|
6
5
|
};
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
/** Each domain's completion for the phrase "Must be _____" */ export const domainDescriptions = {
|
|
7
|
+
bigint: "a bigint",
|
|
8
|
+
boolean: "boolean",
|
|
9
|
+
null: "null",
|
|
10
|
+
number: "a number",
|
|
11
|
+
object: "an object",
|
|
12
|
+
string: "a string",
|
|
13
|
+
symbol: "a symbol",
|
|
14
|
+
undefined: "undefined"
|
|
16
15
|
};
|
|
17
|
-
export const subdomainOf = (data)=>{
|
|
18
|
-
const domain = domainOf(data);
|
|
19
|
-
if (domain !== "object") {
|
|
20
|
-
return domain;
|
|
21
|
-
}
|
|
22
|
-
if (Array.isArray(data)) {
|
|
23
|
-
return "Array";
|
|
24
|
-
}
|
|
25
|
-
// The raw result will be something like [object Date]
|
|
26
|
-
const prototypeName = Object.prototype.toString.call(data).slice(8, -1);
|
|
27
|
-
if (isKeyOf(prototypeName, objectSubdomains)) {
|
|
28
|
-
return data instanceof objectSubdomains[prototypeName] ? prototypeName : "object";
|
|
29
|
-
}
|
|
30
|
-
if (prototypeName.endsWith("Error")) {
|
|
31
|
-
return data instanceof Error ? "Error" : "object";
|
|
32
|
-
}
|
|
33
|
-
return "object";
|
|
34
|
-
};
|
|
35
|
-
export const hasSubdomain = (data, subdomain)=>subdomainOf(data) === subdomain;
|
|
36
|
-
export const sizeOf = (data)=>sizedSubdomains[subdomainOf(data)]?.(data) ?? 0;
|
|
37
|
-
const sizedSubdomains = {
|
|
38
|
-
number: (data)=>data,
|
|
39
|
-
string: (data)=>data.length,
|
|
40
|
-
Array: (data)=>data.length
|
|
41
|
-
};
|
|
42
|
-
export const unitsOf = (data)=>sizeUnits[subdomainOf(data)] ?? "";
|
|
43
|
-
const sizeUnits = {
|
|
44
|
-
number: "",
|
|
45
|
-
string: "characters",
|
|
46
|
-
Array: "items"
|
|
47
|
-
};
|
|
48
|
-
export const classNameOf = (data)=>Object(data).constructor.name;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { domainOf } from "./domains.js";
|
|
2
|
+
// Built-in object constructors based on a subset of:
|
|
3
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
|
4
|
+
const defaultObjectKinds = {
|
|
5
|
+
Array,
|
|
6
|
+
Date,
|
|
7
|
+
Error,
|
|
8
|
+
Function,
|
|
9
|
+
Map,
|
|
10
|
+
RegExp,
|
|
11
|
+
Set,
|
|
12
|
+
Object,
|
|
13
|
+
String,
|
|
14
|
+
Number,
|
|
15
|
+
Boolean,
|
|
16
|
+
WeakMap,
|
|
17
|
+
WeakSet,
|
|
18
|
+
Promise
|
|
19
|
+
};
|
|
20
|
+
export const objectKindOf = (data, kinds)=>{
|
|
21
|
+
if (domainOf(data) !== "object") {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const kindSet = kinds ?? defaultObjectKinds;
|
|
25
|
+
let prototype = Object.getPrototypeOf(data);
|
|
26
|
+
while(prototype?.constructor && (!kindSet[prototype.constructor.name] || !(data instanceof kindSet[prototype.constructor.name]))){
|
|
27
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
28
|
+
}
|
|
29
|
+
return prototype?.constructor?.name;
|
|
30
|
+
};
|
|
31
|
+
export const hasObjectKind = (data, kind, kinds)=>objectKindOf(data, kinds) === kind;
|
|
32
|
+
export const isArray = (data)=>Array.isArray(data);
|
|
33
|
+
/** Each defaultObjectKind's completion for the phrase "Must be _____" */ export const objectKindDescriptions = {
|
|
34
|
+
Object: "an object",
|
|
35
|
+
Array: "an array",
|
|
36
|
+
Function: "a function",
|
|
37
|
+
Date: "a Date",
|
|
38
|
+
RegExp: "a RegExp",
|
|
39
|
+
Error: "an Error",
|
|
40
|
+
Map: "a Map",
|
|
41
|
+
Set: "a Set",
|
|
42
|
+
String: "a String object",
|
|
43
|
+
Number: "a Number object",
|
|
44
|
+
Boolean: "a Boolean object",
|
|
45
|
+
Promise: "a Promise",
|
|
46
|
+
WeakMap: "a WeakMap",
|
|
47
|
+
WeakSet: "a WeakSet"
|
|
48
|
+
};
|