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.
- package/LICENSE +7 -0
- package/README.md +101 -0
- package/dist/api.cjs +15 -0
- package/dist/api.d.ts +2 -0
- package/dist/api.mjs +1 -0
- package/dist/cjs/main.js +462 -0
- package/dist/cjs/nodes/branches.js +85 -0
- package/dist/cjs/nodes/compose.js +140 -0
- package/dist/cjs/nodes/discriminate.js +199 -0
- package/dist/cjs/nodes/functors.js +35 -0
- package/dist/cjs/nodes/infer.js +4 -0
- package/dist/cjs/nodes/node.js +108 -0
- package/dist/cjs/nodes/predicate.js +87 -0
- package/dist/cjs/nodes/rules/class.js +21 -0
- package/dist/cjs/nodes/rules/collapsibleSet.js +49 -0
- package/dist/cjs/nodes/rules/divisor.js +33 -0
- package/dist/cjs/nodes/rules/props.js +72 -0
- package/dist/cjs/nodes/rules/range.js +109 -0
- package/dist/cjs/nodes/rules/regex.js +68 -0
- package/dist/cjs/nodes/rules/rules.js +156 -0
- package/dist/cjs/nodes/rules/subdomain.js +78 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parse/definition.js +33 -0
- package/dist/cjs/parse/record.js +37 -0
- package/dist/cjs/parse/string/ast.js +36 -0
- package/dist/cjs/parse/string/reduce/dynamic.js +150 -0
- package/dist/cjs/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/parse/string/reduce/static.js +4 -0
- package/dist/cjs/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/parse/string/shift/scanner.js +99 -0
- package/dist/cjs/parse/string/string.js +43 -0
- package/dist/cjs/parse/tuple/keyof.js +46 -0
- package/dist/cjs/parse/tuple/morph.js +56 -0
- package/dist/cjs/parse/tuple/narrow.js +15 -0
- package/dist/cjs/parse/tuple/tuple.js +85 -0
- package/dist/cjs/parse/tuple/utils.js +48 -0
- package/dist/cjs/traverse/problems.js +249 -0
- package/dist/cjs/traverse/traverse.js +268 -0
- package/dist/cjs/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/utils/domains.js +67 -0
- package/dist/cjs/utils/errors.js +26 -0
- package/dist/cjs/utils/freeze.js +16 -0
- package/dist/cjs/utils/generics.js +36 -0
- package/dist/cjs/utils/numericLiterals.js +84 -0
- package/dist/cjs/utils/paths.js +32 -0
- package/dist/cjs/utils/serialize.js +98 -0
- package/dist/cjs/utils/unionToTuple.js +4 -0
- package/dist/mjs/main.js +442 -0
- package/dist/mjs/nodes/branches.js +71 -0
- package/dist/mjs/nodes/compose.js +118 -0
- package/dist/mjs/nodes/discriminate.js +184 -0
- package/dist/mjs/nodes/functors.js +27 -0
- package/dist/mjs/nodes/infer.js +1 -0
- package/dist/mjs/nodes/node.js +90 -0
- package/dist/mjs/nodes/predicate.js +69 -0
- package/dist/mjs/nodes/rules/class.js +7 -0
- package/dist/mjs/nodes/rules/collapsibleSet.js +35 -0
- package/dist/mjs/nodes/rules/divisor.js +19 -0
- package/dist/mjs/nodes/rules/props.js +57 -0
- package/dist/mjs/nodes/rules/range.js +90 -0
- package/dist/mjs/nodes/rules/regex.js +53 -0
- package/dist/mjs/nodes/rules/rules.js +138 -0
- package/dist/mjs/nodes/rules/subdomain.js +63 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/parse/definition.js +17 -0
- package/dist/mjs/parse/record.js +29 -0
- package/dist/mjs/parse/string/ast.js +19 -0
- package/dist/mjs/parse/string/reduce/dynamic.js +142 -0
- package/dist/mjs/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/parse/string/reduce/static.js +1 -0
- package/dist/mjs/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/parse/string/shift/operator/operator.js +11 -0
- package/dist/mjs/parse/string/shift/scanner.js +91 -0
- package/dist/mjs/parse/string/string.js +28 -0
- package/dist/mjs/parse/tuple/keyof.js +38 -0
- package/dist/mjs/parse/tuple/morph.js +42 -0
- package/dist/mjs/parse/tuple/narrow.js +7 -0
- package/dist/mjs/parse/tuple/tuple.js +71 -0
- package/dist/mjs/parse/tuple/utils.js +34 -0
- package/dist/mjs/traverse/problems.js +229 -0
- package/dist/mjs/traverse/traverse.js +252 -0
- package/dist/mjs/utils/chainableNoOpProxy.js +15 -0
- package/dist/mjs/utils/domains.js +48 -0
- package/dist/mjs/utils/errors.js +10 -0
- package/dist/mjs/utils/freeze.js +8 -0
- package/dist/mjs/utils/generics.js +15 -0
- package/dist/mjs/utils/numericLiterals.js +71 -0
- package/dist/mjs/utils/paths.js +18 -0
- package/dist/mjs/utils/serialize.js +81 -0
- package/dist/mjs/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -0
- package/dist/types/main.d.ts +257 -0
- package/dist/types/nodes/branches.d.ts +14 -0
- package/dist/types/nodes/compose.d.ts +92 -0
- package/dist/types/nodes/discriminate.d.ts +27 -0
- package/dist/types/nodes/functors.d.ts +6 -0
- package/dist/types/nodes/infer.d.ts +35 -0
- package/dist/types/nodes/node.d.ts +51 -0
- package/dist/types/nodes/predicate.d.ts +88 -0
- package/dist/types/nodes/rules/class.d.ts +3 -0
- package/dist/types/nodes/rules/collapsibleSet.d.ts +4 -0
- package/dist/types/nodes/rules/divisor.d.ts +2 -0
- package/dist/types/nodes/rules/props.d.ts +20 -0
- package/dist/types/nodes/rules/range.d.ts +33 -0
- package/dist/types/nodes/rules/regex.d.ts +14 -0
- package/dist/types/nodes/rules/rules.d.ts +44 -0
- package/dist/types/nodes/rules/subdomain.d.ts +15 -0
- package/dist/types/parse/definition.d.ts +29 -0
- package/dist/types/parse/record.d.ts +24 -0
- package/dist/types/parse/string/ast.d.ts +42 -0
- package/dist/types/parse/string/reduce/dynamic.d.ts +34 -0
- package/dist/types/parse/string/reduce/shared.d.ts +24 -0
- package/dist/types/parse/string/reduce/static.d.ts +115 -0
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +28 -0
- package/dist/types/parse/string/shift/operand/operand.d.ts +9 -0
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +18 -0
- package/dist/types/parse/string/shift/operator/bounds.d.ts +20 -0
- package/dist/types/parse/string/shift/operator/divisor.d.ts +9 -0
- package/dist/types/parse/string/shift/operator/operator.d.ts +13 -0
- package/dist/types/parse/string/shift/scanner.d.ts +89 -0
- package/dist/types/parse/string/string.d.ts +27 -0
- package/dist/types/parse/tuple/keyof.d.ts +2 -0
- package/dist/types/parse/tuple/morph.d.ts +15 -0
- package/dist/types/parse/tuple/narrow.d.ts +13 -0
- package/dist/types/parse/tuple/tuple.d.ts +37 -0
- package/dist/types/parse/tuple/utils.d.ts +19 -0
- package/dist/types/traverse/problems.d.ts +123 -0
- package/dist/types/traverse/traverse.d.ts +48 -0
- package/dist/types/utils/chainableNoOpProxy.d.ts +14 -0
- package/dist/types/utils/domains.d.ts +58 -0
- package/dist/types/utils/errors.d.ts +6 -0
- package/dist/types/utils/freeze.d.ts +2 -0
- package/dist/types/utils/generics.d.ts +118 -0
- package/dist/types/utils/numericLiterals.d.ts +28 -0
- package/dist/types/utils/paths.d.ts +7 -0
- package/dist/types/utils/serialize.d.ts +38 -0
- package/dist/types/utils/unionToTuple.d.ts +11 -0
- package/package.json +102 -2
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
isOptional: ()=>isOptional,
|
|
13
|
+
propsIntersection: ()=>propsIntersection,
|
|
14
|
+
flattenProps: ()=>flattenProps
|
|
15
|
+
});
|
|
16
|
+
const _composeTs = require("../compose.js");
|
|
17
|
+
const _nodeTs = require("../node.js");
|
|
18
|
+
const isOptional = (prop)=>prop[0] === "?";
|
|
19
|
+
const nodeFrom = (prop)=>isOptional(prop) ? prop[1] : prop;
|
|
20
|
+
const propsIntersection = (0, _composeTs.composeIntersection)((0, _composeTs.composeKeyedIntersection)((propKey, l, r, context)=>{
|
|
21
|
+
if (l === undefined) {
|
|
22
|
+
return r === undefined ? (0, _composeTs.equality)() : r;
|
|
23
|
+
}
|
|
24
|
+
if (r === undefined) {
|
|
25
|
+
return l;
|
|
26
|
+
}
|
|
27
|
+
context.path.push(propKey);
|
|
28
|
+
const result = (0, _nodeTs.nodeIntersection)(nodeFrom(l), nodeFrom(r), context);
|
|
29
|
+
context.path.pop();
|
|
30
|
+
const resultIsOptional = isOptional(l) && isOptional(r);
|
|
31
|
+
if ((0, _composeTs.isDisjoint)(result) && resultIsOptional) {
|
|
32
|
+
// If an optional key has an empty intersection, the type can
|
|
33
|
+
// still be satisfied as long as the key is not included. Set
|
|
34
|
+
// the node to never rather than invalidating the type.
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}, {
|
|
39
|
+
onEmpty: "bubble"
|
|
40
|
+
}));
|
|
41
|
+
const flattenProps = (entries, props, ctx)=>{
|
|
42
|
+
const requiredProps = [];
|
|
43
|
+
const optionalProps = [];
|
|
44
|
+
for(const k in props){
|
|
45
|
+
const prop = props[k];
|
|
46
|
+
ctx.path.push(k);
|
|
47
|
+
if (isOptional(prop)) {
|
|
48
|
+
optionalProps.push([
|
|
49
|
+
k,
|
|
50
|
+
(0, _nodeTs.flattenNode)(prop[1], ctx)
|
|
51
|
+
]);
|
|
52
|
+
} else {
|
|
53
|
+
requiredProps.push([
|
|
54
|
+
k,
|
|
55
|
+
(0, _nodeTs.flattenNode)(prop, ctx)
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
ctx.path.pop();
|
|
59
|
+
}
|
|
60
|
+
if (requiredProps.length) {
|
|
61
|
+
entries.push([
|
|
62
|
+
"requiredProps",
|
|
63
|
+
requiredProps
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
if (optionalProps.length) {
|
|
67
|
+
entries.push([
|
|
68
|
+
"optionalProps",
|
|
69
|
+
optionalProps
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
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
|
+
minComparators: ()=>minComparators,
|
|
13
|
+
maxComparators: ()=>maxComparators,
|
|
14
|
+
isEqualityRange: ()=>isEqualityRange,
|
|
15
|
+
rangeIntersection: ()=>rangeIntersection,
|
|
16
|
+
flattenRange: ()=>flattenRange,
|
|
17
|
+
checkBound: ()=>checkBound,
|
|
18
|
+
compareStrictness: ()=>compareStrictness
|
|
19
|
+
});
|
|
20
|
+
const _domainsTs = require("../../utils/domains.js");
|
|
21
|
+
const _composeTs = require("../compose.js");
|
|
22
|
+
const minComparators = {
|
|
23
|
+
">": true,
|
|
24
|
+
">=": true
|
|
25
|
+
};
|
|
26
|
+
const maxComparators = {
|
|
27
|
+
"<": true,
|
|
28
|
+
"<=": true
|
|
29
|
+
};
|
|
30
|
+
const isEqualityRange = (range)=>"comparator" in range;
|
|
31
|
+
const rangeIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
32
|
+
if (isEqualityRange(l)) {
|
|
33
|
+
if (isEqualityRange(r)) {
|
|
34
|
+
return l.limit === r.limit ? (0, _composeTs.equality)() : state.addDisjoint("range", l, r);
|
|
35
|
+
}
|
|
36
|
+
return rangeAllows(r, l.limit) ? l : state.addDisjoint("range", l, r);
|
|
37
|
+
}
|
|
38
|
+
if (isEqualityRange(r)) {
|
|
39
|
+
return rangeAllows(l, r.limit) ? r : state.addDisjoint("range", l, r);
|
|
40
|
+
}
|
|
41
|
+
const stricterMin = compareStrictness("min", l.min, r.min);
|
|
42
|
+
const stricterMax = compareStrictness("max", l.max, r.max);
|
|
43
|
+
if (stricterMin === "l") {
|
|
44
|
+
if (stricterMax === "r") {
|
|
45
|
+
return compareStrictness("min", l.min, r.max) === "l" ? state.addDisjoint("range", l, r) : {
|
|
46
|
+
min: l.min,
|
|
47
|
+
max: r.max
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return l;
|
|
51
|
+
}
|
|
52
|
+
if (stricterMin === "r") {
|
|
53
|
+
if (stricterMax === "l") {
|
|
54
|
+
return compareStrictness("max", l.max, r.min) === "l" ? state.addDisjoint("range", l, r) : {
|
|
55
|
+
min: r.min,
|
|
56
|
+
max: l.max
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return r;
|
|
60
|
+
}
|
|
61
|
+
return stricterMax === "l" ? l : stricterMax === "r" ? r : (0, _composeTs.equality)();
|
|
62
|
+
});
|
|
63
|
+
const rangeAllows = (range, n)=>isEqualityRange(range) ? n === range.limit : minAllows(range.min, n) && maxAllows(range.max, n);
|
|
64
|
+
const minAllows = (min, n)=>!min || n < min.limit || n === min.limit && !isExclusive(min.comparator);
|
|
65
|
+
const maxAllows = (max, n)=>!max || n > max.limit || n === max.limit && !isExclusive(max.comparator);
|
|
66
|
+
const flattenRange = (entries, range, ctx)=>{
|
|
67
|
+
const units = ctx.lastDomain === "string" ? "characters" : ctx.lastDomain === "object" ? "items" : undefined;
|
|
68
|
+
if (isEqualityRange(range)) {
|
|
69
|
+
return entries.push([
|
|
70
|
+
"bound",
|
|
71
|
+
units ? {
|
|
72
|
+
...range,
|
|
73
|
+
units
|
|
74
|
+
} : range
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
if (range.min) {
|
|
78
|
+
entries.push([
|
|
79
|
+
"bound",
|
|
80
|
+
units ? {
|
|
81
|
+
...range.min,
|
|
82
|
+
units
|
|
83
|
+
} : range.min
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
if (range.max) {
|
|
87
|
+
entries.push([
|
|
88
|
+
"bound",
|
|
89
|
+
units ? {
|
|
90
|
+
...range.max,
|
|
91
|
+
units
|
|
92
|
+
} : range.max
|
|
93
|
+
]);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const checkBound = (data, bound, state)=>{
|
|
97
|
+
if (!comparatorCheckers[bound.comparator]((0, _domainsTs.sizeOf)(data), bound.limit)) {
|
|
98
|
+
state.problems.add("bound", data, bound);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const comparatorCheckers = {
|
|
102
|
+
"<": (size, limit)=>size < limit,
|
|
103
|
+
">": (size, limit)=>size > limit,
|
|
104
|
+
"<=": (size, limit)=>size <= limit,
|
|
105
|
+
">=": (size, limit)=>size >= limit,
|
|
106
|
+
"==": (size, limit)=>size === limit
|
|
107
|
+
};
|
|
108
|
+
const compareStrictness = (kind, l, r)=>!l ? !r ? "=" : "r" : !r ? "l" : l.limit === r.limit ? isExclusive(l.comparator) ? isExclusive(r.comparator) ? "=" : "l" : isExclusive(r.comparator) ? "r" : "=" : kind === "min" ? l.limit > r.limit ? "l" : "r" : l.limit < r.limit ? "l" : "r";
|
|
109
|
+
const isExclusive = (comparator)=>comparator.length === 1;
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
sources: ()=>sources,
|
|
13
|
+
getRegex: ()=>getRegex,
|
|
14
|
+
checkRegex: ()=>checkRegex,
|
|
15
|
+
regexIntersection: ()=>regexIntersection,
|
|
16
|
+
isLuhnValid: ()=>isLuhnValid
|
|
17
|
+
});
|
|
18
|
+
const _composeTs = require("../compose.js");
|
|
19
|
+
const _collapsibleSetTs = require("./collapsibleSet.js");
|
|
20
|
+
const regexCache = {};
|
|
21
|
+
const sources = {
|
|
22
|
+
// Character sets
|
|
23
|
+
alpha: /^[A-Z]*$/i,
|
|
24
|
+
alphanumeric: /^[A-Z\d]*$/i,
|
|
25
|
+
lowercase: /^[a-z]*$/,
|
|
26
|
+
uppercase: /^[A-Z]*$/,
|
|
27
|
+
// https://github.com/validatorjs/validator.js
|
|
28
|
+
creditCard: /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
|
|
29
|
+
// https://www.regular-expressions.info
|
|
30
|
+
email: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
|
|
31
|
+
// https://github.com/validatorjs/validator.js
|
|
32
|
+
uuid: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
|
33
|
+
};
|
|
34
|
+
const getRegex = (source)=>{
|
|
35
|
+
if (!regexCache[source]) {
|
|
36
|
+
regexCache[source] = new RegExp(source);
|
|
37
|
+
}
|
|
38
|
+
return regexCache[source];
|
|
39
|
+
};
|
|
40
|
+
const checkRegex = (data, regex, state)=>{
|
|
41
|
+
if (!regex.test(data)) {
|
|
42
|
+
state.problems.add("regex", data, regex);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
|
|
46
|
+
const isLuhnValid = (creditCardInput)=>{
|
|
47
|
+
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
48
|
+
let sum = 0;
|
|
49
|
+
let digit;
|
|
50
|
+
let tmpNum;
|
|
51
|
+
let shouldDouble;
|
|
52
|
+
for(let i = sanitized.length - 1; i >= 0; i--){
|
|
53
|
+
digit = sanitized.substring(i, i + 1);
|
|
54
|
+
tmpNum = parseInt(digit, 10);
|
|
55
|
+
if (shouldDouble) {
|
|
56
|
+
tmpNum *= 2;
|
|
57
|
+
if (tmpNum >= 10) {
|
|
58
|
+
sum += tmpNum % 10 + 1;
|
|
59
|
+
} else {
|
|
60
|
+
sum += tmpNum;
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
sum += tmpNum;
|
|
64
|
+
}
|
|
65
|
+
shouldDouble = !shouldDouble;
|
|
66
|
+
}
|
|
67
|
+
return !!(sum % 10 === 0 ? sanitized : false);
|
|
68
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
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
|
+
branchIntersection: ()=>branchIntersection,
|
|
13
|
+
rulesIntersection: ()=>rulesIntersection,
|
|
14
|
+
narrowableRulesIntersection: ()=>narrowableRulesIntersection,
|
|
15
|
+
precedenceMap: ()=>precedenceMap,
|
|
16
|
+
flattenBranch: ()=>flattenBranch,
|
|
17
|
+
literalSatisfiesRules: ()=>literalSatisfiesRules
|
|
18
|
+
});
|
|
19
|
+
const _astTs = require("../../parse/string/ast.js");
|
|
20
|
+
const _errorsTs = require("../../utils/errors.js");
|
|
21
|
+
const _genericsTs = require("../../utils/generics.js");
|
|
22
|
+
const _composeTs = require("../compose.js");
|
|
23
|
+
const _classTs = require("./class.js");
|
|
24
|
+
const _collapsibleSetTs = require("./collapsibleSet.js");
|
|
25
|
+
const _divisorTs = require("./divisor.js");
|
|
26
|
+
const _propsTs = require("./props.js");
|
|
27
|
+
const _rangeTs = require("./range.js");
|
|
28
|
+
const _regexTs = require("./regex.js");
|
|
29
|
+
const _subdomainTs = require("./subdomain.js");
|
|
30
|
+
const rulesOf = (branch)=>branch.input ?? branch;
|
|
31
|
+
const branchIntersection = (l, r, state)=>{
|
|
32
|
+
const lRules = rulesOf(l);
|
|
33
|
+
const rRules = rulesOf(r);
|
|
34
|
+
const rulesResult = rulesIntersection(lRules, rRules, state);
|
|
35
|
+
if ("morph" in l) {
|
|
36
|
+
if ("morph" in r) {
|
|
37
|
+
if (l.morph === r.morph) {
|
|
38
|
+
return (0, _composeTs.isEquality)(rulesResult) || (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
39
|
+
input: rulesResult,
|
|
40
|
+
morph: l.morph
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return state.lastOperator === "&" ? (0, _errorsTs.throwParseError)((0, _astTs.writeImplicitNeverMessage)(state.path, "of morphs")) : {};
|
|
44
|
+
}
|
|
45
|
+
return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
46
|
+
input: (0, _composeTs.isEquality)(rulesResult) ? l.input : rulesResult,
|
|
47
|
+
morph: l.morph
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if ("morph" in r) {
|
|
51
|
+
return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
52
|
+
input: (0, _composeTs.isEquality)(rulesResult) ? r.input : rulesResult,
|
|
53
|
+
morph: r.morph
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return rulesResult;
|
|
57
|
+
};
|
|
58
|
+
const rulesIntersection = (l, r, state)=>"value" in l ? "value" in r ? l.value === r.value ? (0, _composeTs.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);
|
|
59
|
+
const narrowIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
|
|
60
|
+
const narrowableRulesIntersection = (0, _composeTs.composeKeyedIntersection)({
|
|
61
|
+
subdomain: _subdomainTs.subdomainIntersection,
|
|
62
|
+
divisor: _divisorTs.divisorIntersection,
|
|
63
|
+
regex: _regexTs.regexIntersection,
|
|
64
|
+
props: _propsTs.propsIntersection,
|
|
65
|
+
class: _classTs.classIntersection,
|
|
66
|
+
range: _rangeTs.rangeIntersection,
|
|
67
|
+
narrow: narrowIntersection
|
|
68
|
+
}, {
|
|
69
|
+
onEmpty: "bubble"
|
|
70
|
+
});
|
|
71
|
+
const ruleFlatteners = {
|
|
72
|
+
subdomain: _subdomainTs.flattenSubdomain,
|
|
73
|
+
regex: (entries, rule)=>{
|
|
74
|
+
for (const source of (0, _genericsTs.listFrom)(rule)){
|
|
75
|
+
entries.push([
|
|
76
|
+
"regex",
|
|
77
|
+
(0, _regexTs.getRegex)(source)
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
divisor: (entries, rule)=>{
|
|
82
|
+
entries.push([
|
|
83
|
+
"divisor",
|
|
84
|
+
rule
|
|
85
|
+
]);
|
|
86
|
+
},
|
|
87
|
+
range: _rangeTs.flattenRange,
|
|
88
|
+
class: (entries, rule)=>{
|
|
89
|
+
entries.push([
|
|
90
|
+
"class",
|
|
91
|
+
rule
|
|
92
|
+
]);
|
|
93
|
+
},
|
|
94
|
+
props: _propsTs.flattenProps,
|
|
95
|
+
narrow: (entries, rule)=>{
|
|
96
|
+
for (const narrow of (0, _genericsTs.listFrom)(rule)){
|
|
97
|
+
entries.push([
|
|
98
|
+
"narrow",
|
|
99
|
+
narrow
|
|
100
|
+
]);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
value: (entries, rule)=>{
|
|
104
|
+
entries.push([
|
|
105
|
+
"value",
|
|
106
|
+
rule
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const precedenceMap = {
|
|
111
|
+
// Critical: No other checks are performed if these fail
|
|
112
|
+
domain: 0,
|
|
113
|
+
value: 0,
|
|
114
|
+
domains: 0,
|
|
115
|
+
branches: 0,
|
|
116
|
+
subdomain: 0,
|
|
117
|
+
switch: 0,
|
|
118
|
+
alias: 0,
|
|
119
|
+
// Shallow: All shallow checks will be performed even if one or more fail
|
|
120
|
+
class: 1,
|
|
121
|
+
regex: 1,
|
|
122
|
+
divisor: 1,
|
|
123
|
+
bound: 1,
|
|
124
|
+
// Deep: Performed if all shallow checks pass, even if one or more deep checks fail
|
|
125
|
+
requiredProps: 2,
|
|
126
|
+
optionalProps: 2,
|
|
127
|
+
// Narrow: Only performed if all shallow and deep checks pass
|
|
128
|
+
narrow: 3,
|
|
129
|
+
// Morph: Only performed if all validation passes
|
|
130
|
+
morph: 4
|
|
131
|
+
};
|
|
132
|
+
const flattenBranch = (branch, ctx)=>{
|
|
133
|
+
if ("morph" in branch) {
|
|
134
|
+
const result = flattenRules(branch.input, ctx);
|
|
135
|
+
result.push([
|
|
136
|
+
"morph",
|
|
137
|
+
branch.morph
|
|
138
|
+
]);
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
return flattenRules(branch, ctx);
|
|
142
|
+
};
|
|
143
|
+
const flattenRules = (rules, ctx)=>{
|
|
144
|
+
const entries = [];
|
|
145
|
+
let k;
|
|
146
|
+
for(k in rules){
|
|
147
|
+
ruleFlatteners[k](entries, rules[k], ctx);
|
|
148
|
+
}
|
|
149
|
+
return entries.sort((l, r)=>precedenceMap[l[0]] - precedenceMap[r[0]]);
|
|
150
|
+
};
|
|
151
|
+
const literalSatisfiesRules = (data, rules, state)=>!state.type.meta.scope.type([
|
|
152
|
+
"node",
|
|
153
|
+
{
|
|
154
|
+
[state.domain]: rules
|
|
155
|
+
}
|
|
156
|
+
])(data).problems;
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
subdomainIntersection: ()=>subdomainIntersection,
|
|
13
|
+
subdomainPathSegments: ()=>subdomainPathSegments,
|
|
14
|
+
flattenSubdomain: ()=>flattenSubdomain
|
|
15
|
+
});
|
|
16
|
+
const _errorsTs = require("../../utils/errors.js");
|
|
17
|
+
const _composeTs = require("../compose.js");
|
|
18
|
+
const _nodeTs = require("../node.js");
|
|
19
|
+
const subdomainIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
20
|
+
if (typeof l === "string") {
|
|
21
|
+
if (typeof r === "string") {
|
|
22
|
+
return l === r ? (0, _composeTs.equality)() : state.addDisjoint("subdomain", l, r);
|
|
23
|
+
}
|
|
24
|
+
return l === r[0] ? r : state.addDisjoint("subdomain", l, r[0]);
|
|
25
|
+
}
|
|
26
|
+
if (typeof r === "string") {
|
|
27
|
+
return l[0] === r ? l : state.addDisjoint("subdomain", l[0], r);
|
|
28
|
+
}
|
|
29
|
+
if (l[0] !== r[0]) {
|
|
30
|
+
return state.addDisjoint("subdomain", l[0], r[0]);
|
|
31
|
+
}
|
|
32
|
+
const result = [
|
|
33
|
+
l[0]
|
|
34
|
+
];
|
|
35
|
+
let lImpliesR = true;
|
|
36
|
+
let rImpliesL = true;
|
|
37
|
+
for(let i = 1; i < l.length; i++){
|
|
38
|
+
const lNode = l[i];
|
|
39
|
+
const rNode = r[i];
|
|
40
|
+
state.path.push(subdomainParameterToPathSegment(l[0], i));
|
|
41
|
+
const parameterResult = (0, _nodeTs.nodeIntersection)(lNode, rNode, state);
|
|
42
|
+
state.path.pop();
|
|
43
|
+
if ((0, _composeTs.isEquality)(parameterResult)) {
|
|
44
|
+
result[i] = lNode;
|
|
45
|
+
} else if (parameterResult === l) {
|
|
46
|
+
result[i] = lNode;
|
|
47
|
+
rImpliesL = false;
|
|
48
|
+
} else if (parameterResult === r) {
|
|
49
|
+
result[i] = rNode;
|
|
50
|
+
lImpliesR = false;
|
|
51
|
+
} else if ((0, _composeTs.isDisjoint)(parameterResult)) {
|
|
52
|
+
return parameterResult;
|
|
53
|
+
} else {
|
|
54
|
+
result[i] = parameterResult;
|
|
55
|
+
lImpliesR = false;
|
|
56
|
+
rImpliesL = false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return lImpliesR ? rImpliesL ? (0, _composeTs.equality)() : l : rImpliesL ? r : result;
|
|
60
|
+
});
|
|
61
|
+
const subdomainParameterToPathSegment = (subdomain, i)=>subdomain === "Array" ? "${number}" : subdomain === "Set" ? "${item}" : subdomain === "Map" ? i === 1 ? "${key}" : "${value}" : (0, _errorsTs.throwInternalError)(`Unexpected parameterized subdomain '${subdomain}'`);
|
|
62
|
+
const subdomainPathSegments = {
|
|
63
|
+
"${number}": true,
|
|
64
|
+
"${item}": true,
|
|
65
|
+
"${key}": true,
|
|
66
|
+
"${value}": true
|
|
67
|
+
};
|
|
68
|
+
const flattenSubdomain = (entries, rule, ctx)=>entries.push([
|
|
69
|
+
"subdomain",
|
|
70
|
+
typeof rule === "string" ? rule : rule[0] === "Map" ? [
|
|
71
|
+
rule[0],
|
|
72
|
+
(0, _nodeTs.flattenNode)(rule[1], ctx),
|
|
73
|
+
(0, _nodeTs.flattenNode)(rule[2], ctx)
|
|
74
|
+
] : [
|
|
75
|
+
rule[0],
|
|
76
|
+
(0, _nodeTs.flattenNode)(rule[1], ctx)
|
|
77
|
+
]
|
|
78
|
+
]);
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
parseDefinition: ()=>parseDefinition,
|
|
13
|
+
as: ()=>as,
|
|
14
|
+
unknownDefinitionMessage: ()=>unknownDefinitionMessage,
|
|
15
|
+
writeBadDefinitionTypeMessage: ()=>writeBadDefinitionTypeMessage
|
|
16
|
+
});
|
|
17
|
+
const _mainTs = require("../main.js");
|
|
18
|
+
const _domainsTs = require("../utils/domains.js");
|
|
19
|
+
const _errorsTs = require("../utils/errors.js");
|
|
20
|
+
const _recordTs = require("./record.js");
|
|
21
|
+
const _stringTs = require("./string/string.js");
|
|
22
|
+
const _tupleTs = require("./tuple/tuple.js");
|
|
23
|
+
const parseDefinition = (def, ctx)=>{
|
|
24
|
+
const subdomain = (0, _domainsTs.subdomainOf)(def);
|
|
25
|
+
return subdomain === "string" ? (0, _stringTs.parseString)(def, ctx) : subdomain === "object" ? (0, _recordTs.parseRecord)(def, ctx) : subdomain === "Array" ? (0, _tupleTs.parseTuple)(def, ctx) : subdomain === "RegExp" ? {
|
|
26
|
+
string: {
|
|
27
|
+
regex: def.source
|
|
28
|
+
}
|
|
29
|
+
} : (0, _mainTs.isType)(def) ? def.node : (0, _errorsTs.throwParseError)(writeBadDefinitionTypeMessage(subdomain));
|
|
30
|
+
};
|
|
31
|
+
const as = Symbol("as");
|
|
32
|
+
const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as inferred<...>' to cast it.";
|
|
33
|
+
const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "parseRecord", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>parseRecord
|
|
8
|
+
});
|
|
9
|
+
const _definitionTs = require("./definition.js");
|
|
10
|
+
const _scannerTs = require("./string/shift/scanner.js");
|
|
11
|
+
const parseRecord = (def, ctx)=>{
|
|
12
|
+
const props = {};
|
|
13
|
+
for(const definitionKey in def){
|
|
14
|
+
let keyName = definitionKey;
|
|
15
|
+
let isOptional = false;
|
|
16
|
+
if (definitionKey[definitionKey.length - 1] === "?") {
|
|
17
|
+
if (definitionKey[definitionKey.length - 2] === _scannerTs.Scanner.escapeToken) {
|
|
18
|
+
keyName = `${definitionKey.slice(0, -2)}?`;
|
|
19
|
+
} else {
|
|
20
|
+
keyName = definitionKey.slice(0, -1);
|
|
21
|
+
isOptional = true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
ctx.path.push(keyName);
|
|
25
|
+
const propNode = (0, _definitionTs.parseDefinition)(def[definitionKey], ctx);
|
|
26
|
+
ctx.path.pop();
|
|
27
|
+
props[keyName] = isOptional ? [
|
|
28
|
+
"?",
|
|
29
|
+
propNode
|
|
30
|
+
] : propNode;
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
object: {
|
|
34
|
+
props
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
compileDisjointReasonsMessage: ()=>compileDisjointReasonsMessage,
|
|
13
|
+
writeImplicitNeverMessage: ()=>writeImplicitNeverMessage,
|
|
14
|
+
writeUndiscriminatableMorphUnionMessage: ()=>writeUndiscriminatableMorphUnionMessage,
|
|
15
|
+
writeIndivisibleMessage: ()=>writeIndivisibleMessage,
|
|
16
|
+
writeUnboundableMessage: ()=>writeUnboundableMessage
|
|
17
|
+
});
|
|
18
|
+
const _composeTs = require("../../nodes/compose.js");
|
|
19
|
+
const _genericsTs = require("../../utils/generics.js");
|
|
20
|
+
const compileDisjointReasonsMessage = (disjoints)=>{
|
|
21
|
+
const paths = (0, _genericsTs.keysOf)(disjoints);
|
|
22
|
+
if (paths.length === 1) {
|
|
23
|
+
const path = paths[0];
|
|
24
|
+
return `${path === "/" ? "" : `At ${path}: `}Intersection of ${_composeTs.disjointDescriptionWriters[disjoints[path].kind](disjoints[path])} results in an unsatisfiable type`;
|
|
25
|
+
}
|
|
26
|
+
let message = `
|
|
27
|
+
"Intersection results in unsatisfiable types at the following paths:\n`;
|
|
28
|
+
for(const path1 in disjoints){
|
|
29
|
+
message += ` ${path1}: ${_composeTs.disjointDescriptionWriters[disjoints[path1].kind](disjoints[path1])}\n`;
|
|
30
|
+
}
|
|
31
|
+
return message;
|
|
32
|
+
};
|
|
33
|
+
const writeImplicitNeverMessage = (path, description)=>`${path.length ? `At ${path}: ` : ""}Intersection ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
34
|
+
const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
35
|
+
const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
36
|
+
const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|