arktype 1.0.15-alpha → 1.0.17-alpha
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/README.md +3 -3
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/src/main.js +38 -0
- package/dist/cjs/src/nodes/branch.js +140 -0
- package/dist/cjs/src/nodes/compose.js +157 -0
- package/dist/{nodes → cjs/src/nodes}/discriminate.js +92 -100
- package/dist/cjs/src/nodes/node.js +139 -0
- package/dist/cjs/src/nodes/predicate.js +86 -0
- package/dist/cjs/src/nodes/rules/class.js +25 -0
- package/dist/cjs/src/nodes/rules/collapsibleSet.js +48 -0
- package/dist/cjs/src/nodes/rules/divisor.js +29 -0
- package/dist/cjs/src/nodes/rules/props.js +160 -0
- package/dist/cjs/src/nodes/rules/range.js +105 -0
- package/dist/cjs/src/nodes/rules/regex.js +26 -0
- package/dist/cjs/src/nodes/rules/rules.js +123 -0
- package/dist/cjs/src/parse/ast/ast.js +4 -0
- package/dist/cjs/src/parse/ast/bound.js +16 -0
- package/dist/cjs/src/parse/ast/config.js +13 -0
- package/dist/cjs/src/parse/ast/distributableFunction.js +48 -0
- package/dist/cjs/src/parse/ast/divisor.js +9 -0
- package/dist/cjs/src/parse/ast/intersection.js +30 -0
- package/dist/cjs/src/parse/ast/keyof.js +111 -0
- package/dist/cjs/src/parse/ast/morph.js +58 -0
- package/dist/cjs/src/parse/ast/narrow.js +22 -0
- package/dist/cjs/src/parse/ast/node.js +4 -0
- package/dist/cjs/src/parse/ast/tuple.js +94 -0
- package/dist/cjs/src/parse/ast/union.js +9 -0
- package/dist/cjs/src/parse/ast/utils.js +4 -0
- package/dist/cjs/src/parse/definition.js +61 -0
- package/dist/cjs/src/parse/record.js +37 -0
- package/dist/cjs/src/parse/string/reduce/dynamic.js +167 -0
- package/dist/cjs/src/parse/string/reduce/shared.js +23 -0
- package/dist/cjs/src/parse/string/reduce/static.js +4 -0
- package/dist/cjs/src/parse/string/shift/operand/enclosed.js +54 -0
- package/dist/cjs/src/parse/string/shift/operand/operand.js +11 -0
- package/dist/cjs/src/parse/string/shift/operand/unenclosed.js +53 -0
- package/dist/cjs/src/parse/string/shift/operator/bounds.js +87 -0
- package/dist/cjs/src/parse/string/shift/operator/divisor.js +35 -0
- package/dist/cjs/src/parse/string/shift/operator/operator.js +26 -0
- package/dist/cjs/src/parse/string/shift/scanner.js +114 -0
- package/dist/cjs/src/parse/string/string.js +43 -0
- package/dist/cjs/src/scopes/ark.js +45 -0
- package/dist/cjs/src/scopes/cache.js +52 -0
- package/dist/cjs/src/scopes/expressions.js +29 -0
- package/dist/cjs/src/scopes/jsObjects.js +93 -0
- package/dist/cjs/src/scopes/scope.js +323 -0
- package/dist/cjs/src/scopes/tsKeywords.js +113 -0
- package/dist/cjs/src/scopes/type.js +50 -0
- package/dist/cjs/src/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/src/scopes/validation/date.js +62 -0
- package/dist/cjs/src/scopes/validation/validation.js +108 -0
- package/dist/cjs/src/traverse/problems.js +281 -0
- package/dist/cjs/src/traverse/traverse.js +390 -0
- package/dist/cjs/src/utils/chainableNoOpProxy.js +23 -0
- package/dist/cjs/src/utils/data.js +53 -0
- package/dist/cjs/src/utils/domains.js +30 -0
- package/dist/cjs/src/utils/errors.js +26 -0
- package/dist/cjs/src/utils/freeze.js +15 -0
- package/dist/cjs/src/utils/generics.js +63 -0
- package/dist/cjs/src/utils/numericLiterals.js +75 -0
- package/dist/cjs/src/utils/objectKinds.js +69 -0
- package/dist/cjs/src/utils/paths.js +32 -0
- package/dist/cjs/src/utils/serialize.js +74 -0
- package/dist/cjs/src/utils/unionToTuple.js +4 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/src/main.js +8 -0
- package/dist/mjs/src/nodes/branch.js +123 -0
- package/dist/mjs/src/nodes/compose.js +135 -0
- package/dist/mjs/src/nodes/discriminate.js +247 -0
- package/dist/mjs/src/nodes/node.js +117 -0
- package/dist/mjs/src/nodes/predicate.js +69 -0
- package/dist/mjs/src/nodes/rules/class.js +11 -0
- package/dist/mjs/src/nodes/rules/collapsibleSet.js +34 -0
- package/dist/mjs/src/nodes/rules/divisor.js +15 -0
- package/dist/mjs/src/nodes/rules/props.js +142 -0
- package/dist/mjs/src/nodes/rules/range.js +86 -0
- package/dist/mjs/src/nodes/rules/regex.js +11 -0
- package/dist/mjs/src/nodes/rules/rules.js +106 -0
- package/dist/mjs/src/parse/ast/ast.js +1 -0
- package/dist/mjs/src/parse/ast/bound.js +2 -0
- package/dist/mjs/src/parse/ast/config.js +9 -0
- package/dist/mjs/src/parse/ast/distributableFunction.js +34 -0
- package/dist/mjs/src/parse/ast/divisor.js +1 -0
- package/dist/mjs/src/parse/ast/intersection.js +16 -0
- package/dist/mjs/src/parse/ast/keyof.js +103 -0
- package/dist/mjs/src/parse/ast/morph.js +44 -0
- package/dist/mjs/src/parse/ast/narrow.js +14 -0
- package/dist/mjs/src/parse/ast/node.js +1 -0
- package/dist/mjs/src/parse/ast/tuple.js +80 -0
- package/dist/mjs/src/parse/ast/union.js +1 -0
- package/dist/mjs/src/parse/ast/utils.js +1 -0
- package/dist/mjs/src/parse/definition.js +46 -0
- package/dist/mjs/src/parse/record.js +29 -0
- package/dist/mjs/src/parse/string/reduce/dynamic.js +159 -0
- package/dist/mjs/src/parse/string/reduce/shared.js +6 -0
- package/dist/mjs/src/parse/string/reduce/static.js +1 -0
- package/dist/mjs/src/parse/string/shift/operand/enclosed.js +39 -0
- package/dist/mjs/src/parse/string/shift/operand/operand.js +3 -0
- package/dist/mjs/src/parse/string/shift/operand/unenclosed.js +36 -0
- package/dist/mjs/src/parse/string/shift/operator/bounds.js +70 -0
- package/dist/mjs/src/parse/string/shift/operator/divisor.js +21 -0
- package/dist/mjs/src/parse/string/shift/operator/operator.js +13 -0
- package/dist/mjs/src/parse/string/shift/scanner.js +106 -0
- package/dist/mjs/src/parse/string/string.js +28 -0
- package/dist/mjs/src/scopes/ark.js +28 -0
- package/dist/mjs/src/scopes/cache.js +38 -0
- package/dist/{scopes → mjs/src/scopes}/expressions.js +9 -20
- package/dist/mjs/src/scopes/jsObjects.js +82 -0
- package/dist/mjs/src/scopes/scope.js +303 -0
- package/dist/mjs/src/scopes/tsKeywords.js +116 -0
- package/dist/mjs/src/scopes/type.js +35 -0
- package/dist/{scopes → mjs/src/scopes}/validation/creditCard.js +11 -15
- package/dist/{scopes → mjs/src/scopes}/validation/date.js +16 -21
- package/dist/mjs/src/scopes/validation/validation.js +112 -0
- package/dist/mjs/src/traverse/problems.js +260 -0
- package/dist/mjs/src/traverse/traverse.js +373 -0
- package/dist/{utils → mjs/src/utils}/chainableNoOpProxy.js +2 -6
- package/dist/mjs/src/utils/data.js +38 -0
- package/dist/mjs/src/utils/domains.js +15 -0
- package/dist/mjs/src/utils/errors.js +10 -0
- package/dist/mjs/src/utils/freeze.js +7 -0
- package/dist/mjs/src/utils/generics.js +38 -0
- package/dist/mjs/src/utils/numericLiterals.js +72 -0
- package/dist/mjs/src/utils/objectKinds.js +55 -0
- package/dist/{utils → mjs/src/utils}/paths.js +3 -8
- package/dist/mjs/src/utils/serialize.js +58 -0
- package/dist/mjs/src/utils/unionToTuple.js +1 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/{utils → types/utils}/generics.d.ts +2 -2
- package/package.json +14 -5
- package/dist/main.js +0 -29
- package/dist/nodes/branch.js +0 -135
- package/dist/nodes/compose.js +0 -99
- package/dist/nodes/node.js +0 -134
- package/dist/nodes/predicate.js +0 -88
- package/dist/nodes/rules/class.js +0 -23
- package/dist/nodes/rules/collapsibleSet.js +0 -34
- package/dist/nodes/rules/divisor.js +0 -19
- package/dist/nodes/rules/props.js +0 -130
- package/dist/nodes/rules/range.js +0 -108
- package/dist/nodes/rules/regex.js +0 -17
- package/dist/nodes/rules/rules.js +0 -102
- package/dist/parse/ast/ast.js +0 -2
- package/dist/parse/ast/bound.js +0 -7
- package/dist/parse/ast/config.js +0 -14
- package/dist/parse/ast/distributableFunction.js +0 -40
- package/dist/parse/ast/divisor.js +0 -5
- package/dist/parse/ast/intersection.js +0 -21
- package/dist/parse/ast/keyof.js +0 -107
- package/dist/parse/ast/morph.js +0 -50
- package/dist/parse/ast/narrow.js +0 -17
- package/dist/parse/ast/node.js +0 -2
- package/dist/parse/ast/tuple.js +0 -72
- package/dist/parse/ast/union.js +0 -5
- package/dist/parse/ast/utils.js +0 -2
- package/dist/parse/definition.js +0 -47
- package/dist/parse/record.js +0 -31
- package/dist/parse/string/reduce/dynamic.js +0 -159
- package/dist/parse/string/reduce/shared.js +0 -13
- package/dist/parse/string/reduce/static.js +0 -2
- package/dist/parse/string/shift/operand/enclosed.js +0 -37
- package/dist/parse/string/shift/operand/operand.js +0 -15
- package/dist/parse/string/shift/operand/unenclosed.js +0 -45
- package/dist/parse/string/shift/operator/bounds.js +0 -83
- package/dist/parse/string/shift/operator/divisor.js +0 -23
- package/dist/parse/string/shift/operator/operator.js +0 -34
- package/dist/parse/string/shift/scanner.js +0 -97
- package/dist/parse/string/string.js +0 -35
- package/dist/scopes/ark.js +0 -27
- package/dist/scopes/cache.js +0 -28
- package/dist/scopes/jsObjects.js +0 -20
- package/dist/scopes/scope.js +0 -184
- package/dist/scopes/tsKeywords.js +0 -49
- package/dist/scopes/type.js +0 -40
- package/dist/scopes/validation/validation.js +0 -74
- package/dist/traverse/problems.js +0 -234
- package/dist/traverse/traverse.js +0 -330
- package/dist/utils/data.js +0 -39
- package/dist/utils/domains.js +0 -27
- package/dist/utils/errors.js +0 -17
- package/dist/utils/freeze.js +0 -16
- package/dist/utils/generics.js +0 -53
- package/dist/utils/numericLiterals.js +0 -88
- package/dist/utils/objectKinds.js +0 -69
- package/dist/utils/serialize.js +0 -71
- package/dist/utils/unionToTuple.js +0 -2
- /package/dist/{main.d.ts → types/main.d.ts} +0 -0
- /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
- /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
- /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
- /package/dist/{parse → types/parse}/definition.d.ts +0 -0
- /package/dist/{parse → types/parse}/record.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
- /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
- /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
- /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{utils → types/utils}/data.d.ts +0 -0
- /package/dist/{utils → types/utils}/domains.d.ts +0 -0
- /package/dist/{utils → types/utils}/errors.d.ts +0 -0
- /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
- /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
- /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
- /package/dist/{utils → types/utils}/paths.d.ts +0 -0
- /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
- /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
|
@@ -1,39 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
+
flattenBranches: ()=>flattenBranches,
|
|
13
|
+
serializeDefinitionIfAllowed: ()=>serializeDefinitionIfAllowed,
|
|
14
|
+
serializeCase: ()=>serializeCase
|
|
15
|
+
});
|
|
16
|
+
const _unionJs = require("../parse/ast/union.js");
|
|
17
|
+
const _domainsJs = require("../utils/domains.js");
|
|
18
|
+
const _errorsJs = require("../utils/errors.js");
|
|
19
|
+
const _genericsJs = require("../utils/generics.js");
|
|
20
|
+
const _objectKindsJs = require("../utils/objectKinds.js");
|
|
21
|
+
const _pathsJs = require("../utils/paths.js");
|
|
22
|
+
const _serializeJs = require("../utils/serialize.js");
|
|
23
|
+
const _branchJs = require("./branch.js");
|
|
24
|
+
const _composeJs = require("./compose.js");
|
|
25
|
+
const _propsJs = require("./rules/props.js");
|
|
26
|
+
const flattenBranches = (branches, ctx)=>{
|
|
15
27
|
const discriminants = calculateDiscriminants(branches, ctx);
|
|
16
|
-
const indices = branches.map((_, i)
|
|
28
|
+
const indices = branches.map((_, i)=>i);
|
|
17
29
|
return discriminate(branches, indices, discriminants, ctx);
|
|
18
30
|
};
|
|
19
|
-
|
|
20
|
-
const discriminate = (originalBranches, remainingIndices, discriminants, ctx) => {
|
|
31
|
+
const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
21
32
|
if (remainingIndices.length === 1) {
|
|
22
|
-
return (0,
|
|
33
|
+
return (0, _branchJs.flattenBranch)(originalBranches[remainingIndices[0]], ctx);
|
|
23
34
|
}
|
|
24
35
|
const bestDiscriminant = findBestDiscriminant(remainingIndices, discriminants);
|
|
25
36
|
if (!bestDiscriminant) {
|
|
26
37
|
return [
|
|
27
38
|
[
|
|
28
39
|
"branches",
|
|
29
|
-
remainingIndices.map((i)
|
|
30
|
-
? (0, errors_js_1.throwParseError)((0, union_js_1.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`))
|
|
31
|
-
: (0, branch_js_1.flattenBranch)(originalBranches[i], ctx))
|
|
40
|
+
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.scope) ? (0, _errorsJs.throwParseError)((0, _unionJs.writeUndiscriminatableMorphUnionMessage)(`${ctx.path}`)) : (0, _branchJs.flattenBranch)(originalBranches[i], ctx))
|
|
32
41
|
]
|
|
33
42
|
];
|
|
34
43
|
}
|
|
35
44
|
const cases = {};
|
|
36
|
-
for
|
|
45
|
+
for(const caseKey in bestDiscriminant.indexCases){
|
|
37
46
|
const nextIndices = bestDiscriminant.indexCases[caseKey];
|
|
38
47
|
cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
|
|
39
48
|
if (caseKey !== "default") {
|
|
@@ -51,30 +60,24 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx) =>
|
|
|
51
60
|
]
|
|
52
61
|
];
|
|
53
62
|
};
|
|
54
|
-
const pruneDiscriminant = (entries, segments, discriminant, ctx)
|
|
55
|
-
for
|
|
63
|
+
const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
|
|
64
|
+
for(let i = 0; i < entries.length; i++){
|
|
56
65
|
const [k, v] = entries[i];
|
|
57
66
|
if (!segments.length) {
|
|
58
67
|
if (discriminant.kind === "domain") {
|
|
59
68
|
if (k === "domain" || k === "domains") {
|
|
60
69
|
entries.splice(i, 1);
|
|
61
70
|
return;
|
|
62
|
-
}
|
|
63
|
-
else if (k === "class" || k === "value") {
|
|
71
|
+
} else if (k === "class" || k === "value") {
|
|
64
72
|
// these keys imply a domain, but also add additional
|
|
65
73
|
// information, so can't be pruned
|
|
66
74
|
return;
|
|
67
75
|
}
|
|
68
|
-
}
|
|
69
|
-
else if (discriminant.kind === k) {
|
|
76
|
+
} else if (discriminant.kind === k) {
|
|
70
77
|
entries.splice(i, 1);
|
|
71
78
|
return;
|
|
72
79
|
}
|
|
73
|
-
}
|
|
74
|
-
else if ((k === "requiredProp" ||
|
|
75
|
-
k === "prerequisiteProp" ||
|
|
76
|
-
k === "optionalProp") &&
|
|
77
|
-
v[0] === segments[0]) {
|
|
80
|
+
} else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
|
|
78
81
|
if (typeof v[1] === "string") {
|
|
79
82
|
if (discriminant.kind !== "domain") {
|
|
80
83
|
return throwInternalPruneFailure(discriminant);
|
|
@@ -91,21 +94,18 @@ const pruneDiscriminant = (entries, segments, discriminant, ctx) => {
|
|
|
91
94
|
// check for branch keys, which must be traversed even if there are no
|
|
92
95
|
// segments left
|
|
93
96
|
if (k === "domains") {
|
|
94
|
-
/* c8 ignore next */
|
|
95
|
-
if ((0, generics_js_1.keyCount)(v) !== 1 || !v.object) {
|
|
97
|
+
/* c8 ignore next */ if ((0, _genericsJs.keyCount)(v) !== 1 || !v.object) {
|
|
96
98
|
return throwInternalPruneFailure(discriminant);
|
|
97
99
|
}
|
|
98
100
|
pruneDiscriminant(v.object, segments, discriminant, ctx);
|
|
99
101
|
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
for (const caseKey in v.cases) {
|
|
102
|
+
} else if (k === "switch") {
|
|
103
|
+
for(const caseKey in v.cases){
|
|
103
104
|
pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
|
|
104
105
|
}
|
|
105
106
|
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
for (const branch of v) {
|
|
107
|
+
} else if (k === "branches") {
|
|
108
|
+
for (const branch of v){
|
|
109
109
|
pruneDiscriminant(branch, segments, discriminant, ctx);
|
|
110
110
|
}
|
|
111
111
|
return;
|
|
@@ -113,37 +113,34 @@ const pruneDiscriminant = (entries, segments, discriminant, ctx) => {
|
|
|
113
113
|
}
|
|
114
114
|
return throwInternalPruneFailure(discriminant);
|
|
115
115
|
};
|
|
116
|
-
const throwInternalPruneFailure = (discriminant)
|
|
116
|
+
const throwInternalPruneFailure = (discriminant)=>(0, _errorsJs.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
|
|
117
117
|
const discriminantKinds = {
|
|
118
118
|
domain: true,
|
|
119
119
|
class: true,
|
|
120
120
|
value: true
|
|
121
121
|
};
|
|
122
|
-
const calculateDiscriminants = (branches, ctx)
|
|
122
|
+
const calculateDiscriminants = (branches, ctx)=>{
|
|
123
123
|
const discriminants = {
|
|
124
124
|
disjointsByPair: {},
|
|
125
125
|
casesByDisjoint: {}
|
|
126
126
|
};
|
|
127
|
-
for
|
|
128
|
-
for
|
|
127
|
+
for(let lIndex = 0; lIndex < branches.length - 1; lIndex++){
|
|
128
|
+
for(let rIndex = lIndex + 1; rIndex < branches.length; rIndex++){
|
|
129
129
|
const pairKey = `${lIndex}/${rIndex}`;
|
|
130
130
|
const pairDisjoints = [];
|
|
131
131
|
discriminants.disjointsByPair[pairKey] = pairDisjoints;
|
|
132
|
-
const intersectionState = new
|
|
133
|
-
(0,
|
|
134
|
-
for
|
|
135
|
-
if (path.includes(
|
|
136
|
-
// containers could be empty and therefore their elements cannot be used to discriminate
|
|
137
|
-
// allowing this via a special case where both are length >0 tracked here:
|
|
138
|
-
// https://github.com/arktypeio/arktype/issues/593
|
|
132
|
+
const intersectionState = new _composeJs.IntersectionState(ctx.type, "|");
|
|
133
|
+
(0, _branchJs.branchIntersection)(branches[lIndex], branches[rIndex], intersectionState);
|
|
134
|
+
for(const path in intersectionState.disjoints){
|
|
135
|
+
if (path.includes(_propsJs.mappedKeys.index)) {
|
|
139
136
|
continue;
|
|
140
137
|
}
|
|
141
|
-
const { l, r, kind
|
|
142
|
-
if (!(0,
|
|
138
|
+
const { l , r , kind } = intersectionState.disjoints[path];
|
|
139
|
+
if (!(0, _genericsJs.isKeyOf)(kind, discriminantKinds)) {
|
|
143
140
|
continue;
|
|
144
141
|
}
|
|
145
|
-
const lSerialized =
|
|
146
|
-
const rSerialized =
|
|
142
|
+
const lSerialized = serializeDefinitionIfAllowed(kind, l);
|
|
143
|
+
const rSerialized = serializeDefinitionIfAllowed(kind, r);
|
|
147
144
|
if (lSerialized === undefined || rSerialized === undefined) {
|
|
148
145
|
continue;
|
|
149
146
|
}
|
|
@@ -151,24 +148,30 @@ const calculateDiscriminants = (branches, ctx) => {
|
|
|
151
148
|
pairDisjoints.push(qualifiedDisjoint);
|
|
152
149
|
if (!discriminants.casesByDisjoint[qualifiedDisjoint]) {
|
|
153
150
|
discriminants.casesByDisjoint[qualifiedDisjoint] = {
|
|
154
|
-
[lSerialized]: [
|
|
155
|
-
|
|
151
|
+
[lSerialized]: [
|
|
152
|
+
lIndex
|
|
153
|
+
],
|
|
154
|
+
[rSerialized]: [
|
|
155
|
+
rIndex
|
|
156
|
+
]
|
|
156
157
|
};
|
|
157
158
|
continue;
|
|
158
159
|
}
|
|
159
160
|
const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
|
|
160
161
|
const existingLBranch = cases[lSerialized];
|
|
161
162
|
if (!existingLBranch) {
|
|
162
|
-
cases[lSerialized] = [
|
|
163
|
-
|
|
164
|
-
|
|
163
|
+
cases[lSerialized] = [
|
|
164
|
+
lIndex
|
|
165
|
+
];
|
|
166
|
+
} else if (!existingLBranch.includes(lIndex)) {
|
|
165
167
|
existingLBranch.push(lIndex);
|
|
166
168
|
}
|
|
167
169
|
const existingRBranch = cases[rSerialized];
|
|
168
170
|
if (!existingRBranch) {
|
|
169
|
-
cases[rSerialized] = [
|
|
170
|
-
|
|
171
|
-
|
|
171
|
+
cases[rSerialized] = [
|
|
172
|
+
rIndex
|
|
173
|
+
];
|
|
174
|
+
} else if (!existingRBranch.includes(rIndex)) {
|
|
172
175
|
existingRBranch.push(rIndex);
|
|
173
176
|
}
|
|
174
177
|
}
|
|
@@ -176,26 +179,29 @@ const calculateDiscriminants = (branches, ctx) => {
|
|
|
176
179
|
}
|
|
177
180
|
return discriminants;
|
|
178
181
|
};
|
|
179
|
-
const parseQualifiedDisjoint = (qualifiedDisjoint)
|
|
180
|
-
const path =
|
|
181
|
-
return [
|
|
182
|
+
const parseQualifiedDisjoint = (qualifiedDisjoint)=>{
|
|
183
|
+
const path = _pathsJs.Path.fromString(qualifiedDisjoint);
|
|
184
|
+
return [
|
|
185
|
+
path,
|
|
186
|
+
path.pop()
|
|
187
|
+
];
|
|
182
188
|
};
|
|
183
|
-
const findBestDiscriminant = (remainingIndices, discriminants)
|
|
189
|
+
const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
184
190
|
let bestDiscriminant;
|
|
185
|
-
for
|
|
191
|
+
for(let i = 0; i < remainingIndices.length - 1; i++){
|
|
186
192
|
const lIndex = remainingIndices[i];
|
|
187
|
-
for
|
|
193
|
+
for(let j = i + 1; j < remainingIndices.length; j++){
|
|
188
194
|
const rIndex = remainingIndices[j];
|
|
189
195
|
const candidates = discriminants.disjointsByPair[`${lIndex}/${rIndex}`];
|
|
190
|
-
for (const qualifiedDisjoint of candidates)
|
|
196
|
+
for (const qualifiedDisjoint of candidates){
|
|
191
197
|
const indexCases = discriminants.casesByDisjoint[qualifiedDisjoint];
|
|
192
198
|
const filteredCases = {};
|
|
193
199
|
const defaultCases = [
|
|
194
200
|
...remainingIndices
|
|
195
201
|
];
|
|
196
202
|
let score = 0;
|
|
197
|
-
for
|
|
198
|
-
const filteredIndices = indexCases[caseKey].filter((i)
|
|
203
|
+
for(const caseKey in indexCases){
|
|
204
|
+
const filteredIndices = indexCases[caseKey].filter((i)=>{
|
|
199
205
|
const remainingIndex = remainingIndices.indexOf(i);
|
|
200
206
|
if (remainingIndex !== -1) {
|
|
201
207
|
delete defaultCases[remainingIndex];
|
|
@@ -208,9 +214,9 @@ const findBestDiscriminant = (remainingIndices, discriminants) => {
|
|
|
208
214
|
filteredCases[caseKey] = filteredIndices;
|
|
209
215
|
score++;
|
|
210
216
|
}
|
|
211
|
-
const defaultCaseKeys = (0,
|
|
217
|
+
const defaultCaseKeys = (0, _genericsJs.objectKeysOf)(defaultCases);
|
|
212
218
|
if (defaultCaseKeys.length) {
|
|
213
|
-
filteredCases["default"] = defaultCaseKeys.map((k)
|
|
219
|
+
filteredCases["default"] = defaultCaseKeys.map((k)=>parseInt(k));
|
|
214
220
|
}
|
|
215
221
|
if (!bestDiscriminant || score > bestDiscriminant.score) {
|
|
216
222
|
const [path, kind] = parseQualifiedDisjoint(qualifiedDisjoint);
|
|
@@ -230,41 +236,27 @@ const findBestDiscriminant = (remainingIndices, discriminants) => {
|
|
|
230
236
|
}
|
|
231
237
|
return bestDiscriminant;
|
|
232
238
|
};
|
|
233
|
-
const serializeDefinitionIfAllowed = (kind, definition)
|
|
234
|
-
switch
|
|
239
|
+
const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
240
|
+
switch(kind){
|
|
235
241
|
case "value":
|
|
236
242
|
return serializeIfPrimitive(definition);
|
|
237
243
|
case "domain":
|
|
238
244
|
return definition;
|
|
239
245
|
case "class":
|
|
240
|
-
return (0,
|
|
246
|
+
return (0, _objectKindsJs.getExactConstructorObjectKind)(definition);
|
|
241
247
|
default:
|
|
242
248
|
return;
|
|
243
249
|
}
|
|
244
250
|
};
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
return domain === "object" || domain === "symbol"
|
|
249
|
-
? undefined
|
|
250
|
-
: (0, serialize_js_1.serializePrimitive)(data);
|
|
251
|
+
const serializeIfPrimitive = (data)=>{
|
|
252
|
+
const domain = (0, _domainsJs.domainOf)(data);
|
|
253
|
+
return domain === "object" || domain === "symbol" ? undefined : (0, _serializeJs.serializePrimitive)(data);
|
|
251
254
|
};
|
|
252
255
|
const serializeData = {
|
|
253
|
-
value: (data)
|
|
254
|
-
class: (data)
|
|
255
|
-
domain:
|
|
256
|
+
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
257
|
+
class: (data)=>(0, _objectKindsJs.objectKindOf)(data) ?? "default",
|
|
258
|
+
domain: _domainsJs.domainOf
|
|
256
259
|
};
|
|
257
|
-
const serializeCase = (kind, data)
|
|
258
|
-
|
|
259
|
-
const
|
|
260
|
-
? true
|
|
261
|
-
: "props" in branch
|
|
262
|
-
? Object.values(branch.props).some((prop) => nodeIncludesMorph((0, props_js_1.propToNode)(prop), $))
|
|
263
|
-
: false;
|
|
264
|
-
const nodeIncludesMorph = (node, $) => typeof node === "string"
|
|
265
|
-
? $.resolve(node).includesMorph
|
|
266
|
-
: Object.values($.resolveTypeNode(node)).some((predicate) => predicate === true
|
|
267
|
-
? false
|
|
268
|
-
: (0, objectKinds_js_1.isArray)(predicate)
|
|
269
|
-
? predicate.some((branch) => branchIncludesMorph(branch, $))
|
|
270
|
-
: branchIncludesMorph(predicate, $));
|
|
260
|
+
const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
261
|
+
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsJs.propToNode)(prop), $)) : false;
|
|
262
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : (0, _objectKindsJs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
@@ -0,0 +1,139 @@
|
|
|
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
|
+
isConfigNode: ()=>isConfigNode,
|
|
13
|
+
nodeIntersection: ()=>nodeIntersection,
|
|
14
|
+
rootIntersection: ()=>rootIntersection,
|
|
15
|
+
rootUnion: ()=>rootUnion,
|
|
16
|
+
flattenType: ()=>flattenType,
|
|
17
|
+
flattenNode: ()=>flattenNode,
|
|
18
|
+
flattenTypeNode: ()=>flattenTypeNode,
|
|
19
|
+
isLiteralNode: ()=>isLiteralNode,
|
|
20
|
+
resolutionExtendsDomain: ()=>resolutionExtendsDomain,
|
|
21
|
+
toArrayNode: ()=>toArrayNode
|
|
22
|
+
});
|
|
23
|
+
const _intersectionJs = require("../parse/ast/intersection.js");
|
|
24
|
+
const _errorsJs = require("../utils/errors.js");
|
|
25
|
+
const _genericsJs = require("../utils/generics.js");
|
|
26
|
+
const _pathsJs = require("../utils/paths.js");
|
|
27
|
+
const _composeJs = require("./compose.js");
|
|
28
|
+
const _predicateJs = require("./predicate.js");
|
|
29
|
+
const _propsJs = require("./rules/props.js");
|
|
30
|
+
const isConfigNode = (node)=>"config" in node;
|
|
31
|
+
const nodeIntersection = (l, r, state)=>{
|
|
32
|
+
state.domain = undefined;
|
|
33
|
+
const lDomains = state.type.scope.resolveTypeNode(l);
|
|
34
|
+
const rDomains = state.type.scope.resolveTypeNode(r);
|
|
35
|
+
const result = typeNodeIntersection(lDomains, rDomains, state);
|
|
36
|
+
if (typeof result === "object" && !(0, _genericsJs.hasKeys)(result)) {
|
|
37
|
+
return (0, _genericsJs.hasKeys)(state.disjoints) ? (0, _composeJs.anonymousDisjoint)() : state.addDisjoint("domain", (0, _genericsJs.objectKeysOf)(lDomains), (0, _genericsJs.objectKeysOf)(rDomains));
|
|
38
|
+
}
|
|
39
|
+
return result === lDomains ? l : result === rDomains ? r : result;
|
|
40
|
+
};
|
|
41
|
+
const typeNodeIntersection = (0, _composeJs.composeKeyedIntersection)((domain, l, r, context)=>{
|
|
42
|
+
if (l === undefined) {
|
|
43
|
+
return r === undefined ? (0, _errorsJs.throwInternalError)(_composeJs.undefinedOperandsMessage) : undefined;
|
|
44
|
+
}
|
|
45
|
+
if (r === undefined) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return (0, _predicateJs.predicateIntersection)(domain, l, r, context);
|
|
49
|
+
}, {
|
|
50
|
+
onEmpty: "omit"
|
|
51
|
+
});
|
|
52
|
+
const rootIntersection = (l, r, type)=>{
|
|
53
|
+
const state = new _composeJs.IntersectionState(type, "&");
|
|
54
|
+
const result = nodeIntersection(l, r, state);
|
|
55
|
+
return (0, _composeJs.isDisjoint)(result) ? (0, _errorsJs.throwParseError)((0, _intersectionJs.compileDisjointReasonsMessage)(state.disjoints)) : (0, _composeJs.isEquality)(result) ? l : result;
|
|
56
|
+
};
|
|
57
|
+
const rootUnion = (l, r, type)=>{
|
|
58
|
+
const lDomains = type.scope.resolveTypeNode(l);
|
|
59
|
+
const rDomains = type.scope.resolveTypeNode(r);
|
|
60
|
+
const result = {};
|
|
61
|
+
const domains = (0, _genericsJs.objectKeysOf)({
|
|
62
|
+
...lDomains,
|
|
63
|
+
...rDomains
|
|
64
|
+
});
|
|
65
|
+
for (const domain of domains){
|
|
66
|
+
result[domain] = (0, _genericsJs.hasKey)(lDomains, domain) ? (0, _genericsJs.hasKey)(rDomains, domain) ? (0, _predicateJs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsJs.hasKey)(rDomains, domain) ? rDomains[domain] : (0, _errorsJs.throwInternalError)(_composeJs.undefinedOperandsMessage);
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
};
|
|
70
|
+
const hasImpliedDomain = (flatPredicate)=>flatPredicate[0] && (flatPredicate[0][0] === "value" || flatPredicate[0][0] === "class");
|
|
71
|
+
const flattenType = (type)=>{
|
|
72
|
+
const ctx = {
|
|
73
|
+
type,
|
|
74
|
+
path: new _pathsJs.Path(),
|
|
75
|
+
lastDomain: "undefined"
|
|
76
|
+
};
|
|
77
|
+
return flattenNode(type.node, ctx);
|
|
78
|
+
};
|
|
79
|
+
const flattenNode = (node, ctx)=>{
|
|
80
|
+
if (typeof node === "string") {
|
|
81
|
+
return ctx.type.scope.resolve(node).flat;
|
|
82
|
+
}
|
|
83
|
+
const hasConfig = isConfigNode(node);
|
|
84
|
+
const flattenedTypeNode = flattenTypeNode(hasConfig ? node.node : node, ctx);
|
|
85
|
+
return hasConfig ? [
|
|
86
|
+
[
|
|
87
|
+
"config",
|
|
88
|
+
{
|
|
89
|
+
config: (0, _genericsJs.entriesOf)(node.config),
|
|
90
|
+
node: flattenedTypeNode
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
] : flattenedTypeNode;
|
|
94
|
+
};
|
|
95
|
+
const flattenTypeNode = (node, ctx)=>{
|
|
96
|
+
const domains = (0, _genericsJs.objectKeysOf)(node);
|
|
97
|
+
if (domains.length === 1) {
|
|
98
|
+
const domain = domains[0];
|
|
99
|
+
const predicate = node[domain];
|
|
100
|
+
if (predicate === true) {
|
|
101
|
+
return domain;
|
|
102
|
+
}
|
|
103
|
+
ctx.lastDomain = domain;
|
|
104
|
+
const flatPredicate = (0, _predicateJs.flattenPredicate)(predicate, ctx);
|
|
105
|
+
return hasImpliedDomain(flatPredicate) ? flatPredicate : [
|
|
106
|
+
[
|
|
107
|
+
"domain",
|
|
108
|
+
domain
|
|
109
|
+
],
|
|
110
|
+
...flatPredicate
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
const result = {};
|
|
114
|
+
for (const domain of domains){
|
|
115
|
+
ctx.lastDomain = domain;
|
|
116
|
+
result[domain] = (0, _predicateJs.flattenPredicate)(node[domain], ctx);
|
|
117
|
+
}
|
|
118
|
+
return [
|
|
119
|
+
[
|
|
120
|
+
"domains",
|
|
121
|
+
result
|
|
122
|
+
]
|
|
123
|
+
];
|
|
124
|
+
};
|
|
125
|
+
const isLiteralNode = (node, domain)=>{
|
|
126
|
+
return resolutionExtendsDomain(node, domain) && (0, _predicateJs.isLiteralCondition)(node[domain]);
|
|
127
|
+
};
|
|
128
|
+
const resolutionExtendsDomain = (resolution, domain)=>{
|
|
129
|
+
const domains = (0, _genericsJs.objectKeysOf)(resolution);
|
|
130
|
+
return domains.length === 1 && domains[0] === domain;
|
|
131
|
+
};
|
|
132
|
+
const toArrayNode = (node)=>({
|
|
133
|
+
object: {
|
|
134
|
+
class: Array,
|
|
135
|
+
props: {
|
|
136
|
+
[_propsJs.mappedKeys.index]: node
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
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
|
+
comparePredicates: ()=>comparePredicates,
|
|
13
|
+
predicateIntersection: ()=>predicateIntersection,
|
|
14
|
+
predicateUnion: ()=>predicateUnion,
|
|
15
|
+
flattenPredicate: ()=>flattenPredicate,
|
|
16
|
+
isLiteralCondition: ()=>isLiteralCondition
|
|
17
|
+
});
|
|
18
|
+
const _genericsJs = require("../utils/generics.js");
|
|
19
|
+
const _objectKindsJs = require("../utils/objectKinds.js");
|
|
20
|
+
const _branchJs = require("./branch.js");
|
|
21
|
+
const _composeJs = require("./compose.js");
|
|
22
|
+
const _discriminateJs = require("./discriminate.js");
|
|
23
|
+
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
24
|
+
const comparePredicates = (l, r, context)=>{
|
|
25
|
+
if (l === true && r === true) {
|
|
26
|
+
return (0, _composeJs.equality)();
|
|
27
|
+
}
|
|
28
|
+
if (!(0, _objectKindsJs.isArray)(l) && !(0, _objectKindsJs.isArray)(r)) {
|
|
29
|
+
const result = (0, _branchJs.branchIntersection)(emptyRulesIfTrue(l), emptyRulesIfTrue(r), context);
|
|
30
|
+
return result === l ? l : result === r ? r : result;
|
|
31
|
+
}
|
|
32
|
+
const lBranches = (0, _genericsJs.listFrom)(emptyRulesIfTrue(l));
|
|
33
|
+
const rBranches = (0, _genericsJs.listFrom)(emptyRulesIfTrue(r));
|
|
34
|
+
const comparison = (0, _branchJs.compareBranches)(lBranches, rBranches, context);
|
|
35
|
+
if (comparison.equalities.length === lBranches.length && comparison.equalities.length === rBranches.length) {
|
|
36
|
+
return (0, _composeJs.equality)();
|
|
37
|
+
}
|
|
38
|
+
if (comparison.lExtendsR.length + comparison.equalities.length === lBranches.length) {
|
|
39
|
+
return l;
|
|
40
|
+
}
|
|
41
|
+
if (comparison.rExtendsL.length + comparison.equalities.length === rBranches.length) {
|
|
42
|
+
return r;
|
|
43
|
+
}
|
|
44
|
+
return comparison;
|
|
45
|
+
};
|
|
46
|
+
const predicateIntersection = (domain, l, r, state)=>{
|
|
47
|
+
state.domain = domain;
|
|
48
|
+
const comparison = comparePredicates(l, r, state);
|
|
49
|
+
if (!(0, _branchJs.isBranchComparison)(comparison)) {
|
|
50
|
+
return comparison;
|
|
51
|
+
}
|
|
52
|
+
const resultBranches = [
|
|
53
|
+
...comparison.distinctIntersections,
|
|
54
|
+
...comparison.equalities.map((indices)=>comparison.lBranches[indices[0]]),
|
|
55
|
+
...comparison.lExtendsR.map((lIndex)=>comparison.lBranches[lIndex]),
|
|
56
|
+
...comparison.rExtendsL.map((rIndex)=>comparison.rBranches[rIndex])
|
|
57
|
+
];
|
|
58
|
+
if (resultBranches.length === 0) {
|
|
59
|
+
state.addDisjoint("union", comparison.lBranches, comparison.rBranches);
|
|
60
|
+
}
|
|
61
|
+
return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
|
|
62
|
+
};
|
|
63
|
+
const predicateUnion = (domain, l, r, type)=>{
|
|
64
|
+
const state = new _composeJs.IntersectionState(type, "|");
|
|
65
|
+
const comparison = comparePredicates(l, r, state);
|
|
66
|
+
if (!(0, _branchJs.isBranchComparison)(comparison)) {
|
|
67
|
+
return (0, _composeJs.isEquality)(comparison) || comparison === l ? r : comparison === r ? l : // subtype of the other, it consists of two opposite literals
|
|
68
|
+
// and can be simplified to a non-literal boolean.
|
|
69
|
+
domain === "boolean" ? true : [
|
|
70
|
+
emptyRulesIfTrue(l),
|
|
71
|
+
emptyRulesIfTrue(r)
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
const resultBranches = [
|
|
75
|
+
...comparison.lBranches.filter((_, lIndex)=>!comparison.lExtendsR.includes(lIndex) && !comparison.equalities.some((indexPair)=>indexPair[0] === lIndex)),
|
|
76
|
+
...comparison.rBranches.filter((_, rIndex)=>!comparison.rExtendsL.includes(rIndex) && !comparison.equalities.some((indexPair)=>indexPair[1] === rIndex))
|
|
77
|
+
];
|
|
78
|
+
return resultBranches.length === 1 ? resultBranches[0] : resultBranches;
|
|
79
|
+
};
|
|
80
|
+
const flattenPredicate = (predicate, context)=>{
|
|
81
|
+
if (predicate === true) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
return (0, _objectKindsJs.isArray)(predicate) ? (0, _discriminateJs.flattenBranches)(predicate, context) : (0, _branchJs.flattenBranch)(predicate, context);
|
|
85
|
+
};
|
|
86
|
+
const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
classIntersection: ()=>classIntersection,
|
|
13
|
+
checkClass: ()=>checkClass
|
|
14
|
+
});
|
|
15
|
+
const _objectKindsJs = require("../../utils/objectKinds.js");
|
|
16
|
+
const _composeJs = require("../compose.js");
|
|
17
|
+
const classIntersection = (0, _composeJs.composeIntersection)((l, r, state)=>{
|
|
18
|
+
return l === r ? (0, _composeJs.equality)() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r);
|
|
19
|
+
});
|
|
20
|
+
const checkClass = (expectedClass, state)=>{
|
|
21
|
+
if (typeof expectedClass === "string") {
|
|
22
|
+
return (0, _objectKindsJs.objectKindOf)(state.data) === expectedClass || !state.problems.add("class", expectedClass);
|
|
23
|
+
}
|
|
24
|
+
return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
|
|
25
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
collapsibleListUnion: ()=>collapsibleListUnion,
|
|
13
|
+
listUnion: ()=>listUnion
|
|
14
|
+
});
|
|
15
|
+
const _composeJs = require("../compose.js");
|
|
16
|
+
const collapsibleListUnion = (l, r)=>{
|
|
17
|
+
if (Array.isArray(l)) {
|
|
18
|
+
if (Array.isArray(r)) {
|
|
19
|
+
const result = listUnion(l, r);
|
|
20
|
+
return result.length === l.length ? result.length === r.length ? (0, _composeJs.equality)() : l : result.length === r.length ? r : result;
|
|
21
|
+
}
|
|
22
|
+
return l.includes(r) ? l : [
|
|
23
|
+
...l,
|
|
24
|
+
r
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(r)) {
|
|
28
|
+
return r.includes(l) ? r : [
|
|
29
|
+
...r,
|
|
30
|
+
l
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
return l === r ? (0, _composeJs.equality)() : [
|
|
34
|
+
l,
|
|
35
|
+
r
|
|
36
|
+
];
|
|
37
|
+
};
|
|
38
|
+
const listUnion = (l, r)=>{
|
|
39
|
+
const result = [
|
|
40
|
+
...l
|
|
41
|
+
];
|
|
42
|
+
for (const expression of r){
|
|
43
|
+
if (!l.includes(expression)) {
|
|
44
|
+
result.push(expression);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
divisorIntersection: ()=>divisorIntersection,
|
|
13
|
+
checkDivisor: ()=>checkDivisor
|
|
14
|
+
});
|
|
15
|
+
const _composeJs = require("../compose.js");
|
|
16
|
+
const divisorIntersection = (0, _composeJs.composeIntersection)((l, r)=>l === r ? (0, _composeJs.equality)() : Math.abs(l * r / greatestCommonDivisor(l, r)));
|
|
17
|
+
// https://en.wikipedia.org/wiki/Euclidean_algorithm
|
|
18
|
+
const greatestCommonDivisor = (l, r)=>{
|
|
19
|
+
let previous;
|
|
20
|
+
let greatestCommonDivisor = l;
|
|
21
|
+
let current = r;
|
|
22
|
+
while(current !== 0){
|
|
23
|
+
previous = current;
|
|
24
|
+
current = greatestCommonDivisor % current;
|
|
25
|
+
greatestCommonDivisor = previous;
|
|
26
|
+
}
|
|
27
|
+
return greatestCommonDivisor;
|
|
28
|
+
};
|
|
29
|
+
const checkDivisor = (divisor, state)=>state.data % divisor === 0 || !state.problems.add("divisor", divisor);
|