arktype 0.5.1 → 1.0.0-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 +1 -1
- package/dist/api.cjs +4 -2
- package/dist/api.d.ts +1 -1
- package/dist/api.mjs +1 -1
- package/dist/cjs/nodes/branch.js +3 -3
- package/dist/cjs/nodes/compose.js +4 -4
- package/dist/cjs/nodes/discriminate.js +2 -2
- package/dist/cjs/nodes/node.js +33 -30
- package/dist/cjs/nodes/predicate.js +1 -7
- package/dist/cjs/nodes/rules/props.js +39 -0
- package/dist/cjs/nodes/rules/rules.js +5 -2
- package/dist/cjs/parse/ast/config.js +13 -0
- package/dist/cjs/parse/ast/distributableFunction.js +2 -2
- package/dist/cjs/parse/ast/keyof.js +1 -1
- package/dist/cjs/parse/ast/morph.js +4 -3
- package/dist/cjs/parse/ast/narrow.js +8 -1
- package/dist/cjs/{nodes/infer.js → parse/ast/node.js} +0 -0
- package/dist/cjs/parse/ast/tuple.js +2 -1
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +2 -2
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/cjs/parse/string/string.js +3 -3
- package/dist/cjs/scopes/{standard.js → ark.js} +9 -8
- package/dist/cjs/scopes/expressions.js +9 -9
- package/dist/cjs/scopes/scope.js +73 -75
- package/dist/cjs/scopes/type.js +24 -38
- package/dist/cjs/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/scopes/validation/date.js +63 -0
- package/dist/cjs/scopes/validation/validation.js +95 -0
- package/dist/cjs/traverse/problems.js +48 -19
- package/dist/cjs/traverse/traverse.js +80 -25
- package/dist/cjs/utils/generics.js +16 -0
- package/dist/cjs/utils/serialize.js +29 -53
- package/dist/mjs/nodes/branch.js +2 -2
- package/dist/mjs/nodes/compose.js +5 -5
- package/dist/mjs/nodes/discriminate.js +2 -2
- package/dist/mjs/nodes/node.js +31 -31
- package/dist/mjs/nodes/predicate.js +1 -6
- package/dist/mjs/nodes/rules/props.js +39 -0
- package/dist/mjs/nodes/rules/rules.js +5 -2
- package/dist/mjs/parse/ast/config.js +5 -0
- package/dist/mjs/parse/ast/distributableFunction.js +2 -2
- package/dist/mjs/parse/ast/keyof.js +1 -1
- package/dist/mjs/parse/ast/morph.js +5 -4
- package/dist/mjs/parse/ast/narrow.js +9 -2
- package/dist/mjs/{nodes/infer.js → parse/ast/node.js} +0 -0
- package/dist/mjs/parse/ast/tuple.js +2 -1
- package/dist/mjs/parse/definition.js +2 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +2 -2
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/mjs/parse/string/string.js +3 -3
- package/dist/mjs/scopes/{standard.js → ark.js} +8 -7
- package/dist/mjs/scopes/expressions.js +9 -9
- package/dist/mjs/scopes/scope.js +70 -76
- package/dist/mjs/scopes/type.js +22 -38
- package/dist/mjs/scopes/validation/creditCard.js +34 -0
- package/dist/mjs/scopes/validation/date.js +49 -0
- package/dist/mjs/scopes/validation/validation.js +81 -0
- package/dist/mjs/traverse/problems.js +45 -18
- package/dist/mjs/traverse/traverse.js +82 -27
- package/dist/mjs/utils/generics.js +11 -0
- package/dist/mjs/utils/serialize.js +29 -52
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +8 -6
- package/dist/types/nodes/node.d.ts +24 -12
- package/dist/types/nodes/predicate.d.ts +4 -8
- package/dist/types/nodes/rules/props.d.ts +23 -8
- package/dist/types/nodes/rules/rules.d.ts +2 -2
- package/dist/types/parse/ast/ast.d.ts +1 -1
- package/dist/types/parse/ast/config.d.ts +10 -0
- package/dist/types/parse/ast/distributableFunction.d.ts +1 -1
- package/dist/types/parse/ast/morph.d.ts +2 -2
- package/dist/types/parse/ast/narrow.d.ts +1 -1
- package/dist/types/{nodes/infer.d.ts → parse/ast/node.d.ts} +14 -14
- package/dist/types/parse/ast/tuple.d.ts +8 -7
- package/dist/types/parse/definition.d.ts +2 -2
- package/dist/types/parse/string/reduce/dynamic.d.ts +5 -5
- package/dist/types/parse/string/string.d.ts +3 -3
- package/dist/types/scopes/{standard.d.ts → ark.d.ts} +15 -86
- package/dist/types/scopes/expressions.d.ts +3 -3
- package/dist/types/scopes/scope.d.ts +24 -11
- package/dist/types/scopes/type.d.ts +37 -20
- package/dist/types/scopes/validation/creditCard.d.ts +2 -0
- package/dist/types/scopes/validation/date.d.ts +23 -0
- package/dist/types/scopes/validation/validation.d.ts +28 -0
- package/dist/types/traverse/problems.d.ts +14 -8
- package/dist/types/traverse/traverse.d.ts +18 -3
- package/dist/types/utils/generics.d.ts +10 -5
- package/dist/types/utils/serialize.d.ts +0 -1
- package/package.json +2 -2
- package/dist/cjs/scopes/validation.js +0 -79
- package/dist/mjs/scopes/validation.js +0 -66
- package/dist/types/scopes/validation.d.ts +0 -25
|
@@ -15,11 +15,14 @@ _export(exports, {
|
|
|
15
15
|
domainsToDescriptions: ()=>domainsToDescriptions,
|
|
16
16
|
objectKindsToDescriptions: ()=>objectKindsToDescriptions,
|
|
17
17
|
describeBranches: ()=>describeBranches,
|
|
18
|
-
|
|
18
|
+
problemCodes: ()=>problemCodes,
|
|
19
|
+
defaultProblemWriters: ()=>defaultProblemWriters,
|
|
20
|
+
compileProblemWriters: ()=>compileProblemWriters
|
|
19
21
|
});
|
|
20
22
|
const _scannerTs = require("../parse/string/shift/scanner.js");
|
|
21
23
|
const _dataTs = require("../utils/data.js");
|
|
22
24
|
const _domainsTs = require("../utils/domains.js");
|
|
25
|
+
const _genericsTs = require("../utils/generics.js");
|
|
23
26
|
const _numericLiteralsTs = require("../utils/numericLiterals.js");
|
|
24
27
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
25
28
|
const _pathsTs = require("../utils/paths.js");
|
|
@@ -64,10 +67,9 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
64
67
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
65
68
|
return value;
|
|
66
69
|
}
|
|
67
|
-
var _definitions_code, _definitions_code1;
|
|
68
70
|
class ArkTypeError extends TypeError {
|
|
69
71
|
constructor(problems){
|
|
70
|
-
super(problems
|
|
72
|
+
super(`${problems}`);
|
|
71
73
|
this.cause = problems;
|
|
72
74
|
}
|
|
73
75
|
}
|
|
@@ -82,7 +84,7 @@ class Problem {
|
|
|
82
84
|
return this.writers.writeReason(this.mustBe, new _dataTs.DataWrapper(this.data));
|
|
83
85
|
}
|
|
84
86
|
get mustBe() {
|
|
85
|
-
return this.writers.mustBe(this.source);
|
|
87
|
+
return typeof this.writers.mustBe === "string" ? this.writers.mustBe : this.writers.mustBe(this.source);
|
|
86
88
|
}
|
|
87
89
|
constructor(code, path, data, source, writers){
|
|
88
90
|
this.code = code;
|
|
@@ -97,13 +99,16 @@ class Problem {
|
|
|
97
99
|
}
|
|
98
100
|
var _state = /*#__PURE__*/ new WeakMap();
|
|
99
101
|
class ProblemArray extends Array {
|
|
102
|
+
mustBe(description, opts) {
|
|
103
|
+
return this.add("custom", description, opts);
|
|
104
|
+
}
|
|
100
105
|
add(code, source, opts) {
|
|
101
106
|
// copy the path to avoid future mutations affecting it
|
|
102
107
|
const path = opts?.path ?? _pathsTs.Path.from(_classPrivateFieldGet(this, _state).path);
|
|
103
108
|
const data = // we have to check for the presence of the key explicitly since the
|
|
104
109
|
// data could be undefined or null
|
|
105
110
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
106
|
-
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).
|
|
111
|
+
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
|
|
107
112
|
this.addProblem(problem);
|
|
108
113
|
return problem;
|
|
109
114
|
}
|
|
@@ -117,7 +122,7 @@ class ProblemArray extends Array {
|
|
|
117
122
|
const problemIntersection = new Problem("multi", existing.path, existing.data, [
|
|
118
123
|
existing,
|
|
119
124
|
problem
|
|
120
|
-
], _classPrivateFieldGet(this, _state).
|
|
125
|
+
], _classPrivateFieldGet(this, _state).getProblemConfig("multi"));
|
|
121
126
|
const existingIndex = this.indexOf(existing);
|
|
122
127
|
// If existing is found (which it always should be unless this was externally mutated),
|
|
123
128
|
// replace it with the new problem intersection. In case it isn't for whatever reason,
|
|
@@ -132,10 +137,10 @@ class ProblemArray extends Array {
|
|
|
132
137
|
this.count++;
|
|
133
138
|
}
|
|
134
139
|
get summary() {
|
|
135
|
-
return this
|
|
140
|
+
return `${this}`;
|
|
136
141
|
}
|
|
137
142
|
toString() {
|
|
138
|
-
return this.
|
|
143
|
+
return this.join("\n");
|
|
139
144
|
}
|
|
140
145
|
throw() {
|
|
141
146
|
throw new ArkTypeError(this);
|
|
@@ -174,15 +179,7 @@ const describeBranches = (descriptions)=>{
|
|
|
174
179
|
};
|
|
175
180
|
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
176
181
|
const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
|
|
177
|
-
const
|
|
178
|
-
let code;
|
|
179
|
-
for(code in definitions){
|
|
180
|
-
(_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
|
|
181
|
-
(_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = addDefaultContext);
|
|
182
|
-
}
|
|
183
|
-
return definitions;
|
|
184
|
-
};
|
|
185
|
-
const defaultProblemWriters = compileDefaultProblemWriters({
|
|
182
|
+
const defaultProblemConfig = {
|
|
186
183
|
divisor: {
|
|
187
184
|
mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
|
|
188
185
|
},
|
|
@@ -198,6 +195,10 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
198
195
|
mustBe: ()=>"defined",
|
|
199
196
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
200
197
|
},
|
|
198
|
+
extraneous: {
|
|
199
|
+
mustBe: ()=>"removed",
|
|
200
|
+
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
201
|
+
},
|
|
201
202
|
bound: {
|
|
202
203
|
mustBe: (bound)=>`${_scannerTs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
|
|
203
204
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
|
|
@@ -218,10 +219,38 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
218
219
|
writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
|
|
219
220
|
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
220
221
|
},
|
|
221
|
-
|
|
222
|
+
custom: {
|
|
222
223
|
mustBe: (mustBe)=>mustBe
|
|
223
224
|
},
|
|
224
225
|
cases: {
|
|
225
226
|
mustBe: (cases)=>describeBranches(cases)
|
|
226
227
|
}
|
|
227
|
-
}
|
|
228
|
+
};
|
|
229
|
+
const problemCodes = (0, _genericsTs.objectKeysOf)(defaultProblemConfig);
|
|
230
|
+
const compileDefaultProblemWriters = ()=>{
|
|
231
|
+
const result = {};
|
|
232
|
+
let code;
|
|
233
|
+
for (code of problemCodes){
|
|
234
|
+
result[code] = {
|
|
235
|
+
mustBe: defaultProblemConfig[code].mustBe,
|
|
236
|
+
writeReason: defaultProblemConfig[code].writeReason ?? writeDefaultReason,
|
|
237
|
+
addContext: defaultProblemConfig[code].addContext ?? addDefaultContext
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return result;
|
|
241
|
+
};
|
|
242
|
+
const defaultProblemWriters = compileDefaultProblemWriters();
|
|
243
|
+
const compileProblemWriters = (input)=>{
|
|
244
|
+
if (!input) {
|
|
245
|
+
return defaultProblemWriters;
|
|
246
|
+
}
|
|
247
|
+
const result = {};
|
|
248
|
+
for (const code of problemCodes){
|
|
249
|
+
result[code] = {
|
|
250
|
+
mustBe: input[code]?.mustBe ?? defaultProblemConfig[code].mustBe,
|
|
251
|
+
writeReason: input[code]?.writeReason ?? defaultProblemConfig[code].writeReason ?? input.writeReason ?? writeDefaultReason,
|
|
252
|
+
addContext: input[code]?.addContext ?? defaultProblemConfig[code].addContext ?? input.addContext ?? addDefaultContext
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
};
|
|
@@ -20,6 +20,7 @@ const _divisorTs = require("../nodes/rules/divisor.js");
|
|
|
20
20
|
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
21
|
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
22
|
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
+
const _typeTs = require("../scopes/type.js");
|
|
23
24
|
const _domainsTs = require("../utils/domains.js");
|
|
24
25
|
const _errorsTs = require("../utils/errors.js");
|
|
25
26
|
const _genericsTs = require("../utils/generics.js");
|
|
@@ -65,22 +66,38 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
65
66
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
66
67
|
return value;
|
|
67
68
|
}
|
|
69
|
+
const initializeTraversalConfig = ()=>({
|
|
70
|
+
mustBe: [],
|
|
71
|
+
writeReason: [],
|
|
72
|
+
addContext: [],
|
|
73
|
+
keys: []
|
|
74
|
+
});
|
|
75
|
+
const problemWriterKeys = [
|
|
76
|
+
"mustBe",
|
|
77
|
+
"writeReason",
|
|
78
|
+
"addContext"
|
|
79
|
+
];
|
|
68
80
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
69
81
|
class TraversalState {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
82
|
+
getProblemConfig(code) {
|
|
83
|
+
const result = {};
|
|
84
|
+
for (const k of problemWriterKeys){
|
|
85
|
+
result[k] = this.traversalConfig[k][0] ?? this.rootScope.config.codes[code][k];
|
|
73
86
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
getConfigKey(k) {
|
|
90
|
+
return this.traversalConfig[k][0];
|
|
91
|
+
}
|
|
92
|
+
traverseConfig(configEntries, node) {
|
|
93
|
+
for (const entry of configEntries){
|
|
94
|
+
this.traversalConfig[entry[0]].unshift(entry[1]);
|
|
95
|
+
}
|
|
96
|
+
const isValid = traverse(node, this);
|
|
97
|
+
for (const entry1 of configEntries){
|
|
98
|
+
this.traversalConfig[entry1[0]].shift();
|
|
82
99
|
}
|
|
83
|
-
return
|
|
100
|
+
return isValid;
|
|
84
101
|
}
|
|
85
102
|
traverseKey(key, node) {
|
|
86
103
|
const lastData = this.data;
|
|
@@ -93,8 +110,8 @@ class TraversalState {
|
|
|
93
110
|
return isValid;
|
|
94
111
|
}
|
|
95
112
|
traverseResolution(name) {
|
|
96
|
-
const resolution = this.type.
|
|
97
|
-
const id = resolution.
|
|
113
|
+
const resolution = this.type.scope.resolve(name);
|
|
114
|
+
const id = resolution.qualifiedName;
|
|
98
115
|
// this assignment helps with narrowing
|
|
99
116
|
const data = this.data;
|
|
100
117
|
const isObject = (0, _domainsTs.hasDomain)(data, "object");
|
|
@@ -114,10 +131,10 @@ class TraversalState {
|
|
|
114
131
|
];
|
|
115
132
|
}
|
|
116
133
|
}
|
|
117
|
-
const
|
|
134
|
+
const lastType = this.type;
|
|
118
135
|
this.type = resolution;
|
|
119
136
|
const isValid = traverse(resolution.flat, this);
|
|
120
|
-
this.type =
|
|
137
|
+
this.type = lastType;
|
|
121
138
|
if (isObject) {
|
|
122
139
|
_classPrivateFieldGet(this, _seen)[id].pop();
|
|
123
140
|
}
|
|
@@ -153,10 +170,9 @@ class TraversalState {
|
|
|
153
170
|
this.path = new _pathsTs.Path();
|
|
154
171
|
this.problems = new _problemsTs.Problems(this);
|
|
155
172
|
this.failFast = false;
|
|
173
|
+
this.traversalConfig = initializeTraversalConfig();
|
|
156
174
|
_classPrivateFieldSet(this, _seen, {});
|
|
157
|
-
this.
|
|
158
|
-
type.meta.scope.config
|
|
159
|
-
] : [];
|
|
175
|
+
this.rootScope = type.scope;
|
|
160
176
|
}
|
|
161
177
|
}
|
|
162
178
|
const traverse = (node, state)=>typeof node === "string" ? (0, _domainsTs.domainOf)(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
|
|
@@ -190,6 +206,31 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
190
206
|
});
|
|
191
207
|
return false;
|
|
192
208
|
};
|
|
209
|
+
const createPropChecker = (kind)=>(props, state)=>{
|
|
210
|
+
let isValid = true;
|
|
211
|
+
const unseenRequired = {
|
|
212
|
+
...props.required
|
|
213
|
+
};
|
|
214
|
+
for(const k in state.data){
|
|
215
|
+
if (props.required[k]) {
|
|
216
|
+
isValid || (isValid = state.traverseKey(k, props.required[k]));
|
|
217
|
+
delete unseenRequired[k];
|
|
218
|
+
} else if (props.optional[k]) {
|
|
219
|
+
isValid || (isValid = state.traverseKey(k, props.optional[k]));
|
|
220
|
+
} else if (kind === "distilledProps") {
|
|
221
|
+
delete state.data[k];
|
|
222
|
+
} else {
|
|
223
|
+
state.problems.add("extraneous", state.data[k], {
|
|
224
|
+
path: state.path.concat(k)
|
|
225
|
+
});
|
|
226
|
+
isValid = false;
|
|
227
|
+
}
|
|
228
|
+
if (!isValid && state.failFast) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return isValid;
|
|
233
|
+
};
|
|
193
234
|
const entryCheckers = {
|
|
194
235
|
regex: _regexTs.checkRegex,
|
|
195
236
|
divisor: _divisorTs.checkDivisor,
|
|
@@ -241,13 +282,15 @@ const entryCheckers = {
|
|
|
241
282
|
},
|
|
242
283
|
alias: (name, state)=>state.traverseResolution(name),
|
|
243
284
|
class: _classTs.checkClass,
|
|
244
|
-
narrow: (narrow, state)=>
|
|
245
|
-
|
|
246
|
-
state.
|
|
247
|
-
|
|
248
|
-
|
|
285
|
+
narrow: (narrow, state)=>{
|
|
286
|
+
const lastProblemsCount = state.problems.count;
|
|
287
|
+
const result = narrow(state.data, state.problems);
|
|
288
|
+
if (!result && state.problems.count === lastProblemsCount) {
|
|
289
|
+
state.problems.mustBe(narrow.name ? `valid according to ${narrow.name}` : "valid");
|
|
290
|
+
}
|
|
249
291
|
return result;
|
|
250
292
|
},
|
|
293
|
+
config: ({ config , node }, state)=>state.traverseConfig(config, node),
|
|
251
294
|
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
252
295
|
morph: (morph, state)=>{
|
|
253
296
|
const out = morph(state.data, state.problems);
|
|
@@ -259,7 +302,19 @@ const entryCheckers = {
|
|
|
259
302
|
state.problems.addProblem(out);
|
|
260
303
|
return false;
|
|
261
304
|
}
|
|
305
|
+
if ((0, _typeTs.isCheckResult)(out)) {
|
|
306
|
+
if (out.problems) {
|
|
307
|
+
for (const problem of out.problems){
|
|
308
|
+
state.problems.addProblem(problem);
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
state.data = out.data;
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
262
315
|
state.data = out;
|
|
263
316
|
return true;
|
|
264
|
-
}
|
|
317
|
+
},
|
|
318
|
+
distilledProps: createPropChecker("distilledProps"),
|
|
319
|
+
strictProps: createPropChecker("strictProps")
|
|
265
320
|
};
|
|
@@ -15,8 +15,13 @@ _export(exports, {
|
|
|
15
15
|
objectKeysOf: ()=>objectKeysOf,
|
|
16
16
|
prototypeKeysOf: ()=>prototypeKeysOf,
|
|
17
17
|
hasKey: ()=>hasKey,
|
|
18
|
+
hasSingleKey: ()=>hasSingleKey,
|
|
18
19
|
keyCount: ()=>keyCount,
|
|
19
20
|
hasKeys: ()=>hasKeys,
|
|
21
|
+
nominal: ()=>nominal,
|
|
22
|
+
composeNamed: ()=>composeNamed,
|
|
23
|
+
getNominalId: ()=>getNominalId,
|
|
24
|
+
hasNominalId: ()=>hasNominalId,
|
|
20
25
|
listFrom: ()=>listFrom
|
|
21
26
|
});
|
|
22
27
|
const _domainsTs = require("./domains.js");
|
|
@@ -45,8 +50,19 @@ const hasKey = (o, k)=>{
|
|
|
45
50
|
const valueAtKey = o?.[k];
|
|
46
51
|
return valueAtKey !== undefined && valueAtKey !== null;
|
|
47
52
|
};
|
|
53
|
+
const hasSingleKey = (o, k)=>k in o && Object.keys(o).length === 1;
|
|
48
54
|
const keyCount = (o)=>Object.keys(o).length;
|
|
49
55
|
const hasKeys = (value)=>(0, _domainsTs.hasDomain)(value, "object") ? Object.keys(value).length !== 0 : false;
|
|
56
|
+
const id = Symbol("id");
|
|
57
|
+
const nominal = (o, name)=>Object.assign(o, {
|
|
58
|
+
[id]: name
|
|
59
|
+
});
|
|
60
|
+
const composeNamed = (f, name)=>// define within a key to dynamically assign a name to the function
|
|
61
|
+
({
|
|
62
|
+
[name]: (...args)=>nominal(f(...args), name)
|
|
63
|
+
})[name];
|
|
64
|
+
const getNominalId = (data)=>(0, _domainsTs.hasDomain)(data, "object") && id in data ? data[id] : undefined;
|
|
65
|
+
const hasNominalId = (data, name)=>getNominalId(data) === name;
|
|
50
66
|
const listFrom = (data)=>Array.isArray(data) ? data : [
|
|
51
67
|
data
|
|
52
68
|
];
|
|
@@ -10,7 +10,6 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
snapshot: ()=>snapshot,
|
|
13
|
-
serialize: ()=>serialize,
|
|
14
13
|
stringify: ()=>stringify,
|
|
15
14
|
serializePrimitive: ()=>serializePrimitive,
|
|
16
15
|
deserializePrimitive: ()=>deserializePrimitive
|
|
@@ -18,74 +17,51 @@ _export(exports, {
|
|
|
18
17
|
const _domainsTs = require("./domains.js");
|
|
19
18
|
const _genericsTs = require("./generics.js");
|
|
20
19
|
const _numericLiteralsTs = require("./numericLiterals.js");
|
|
21
|
-
const snapshot = (data, opts = {})=>
|
|
22
|
-
const
|
|
23
|
-
switch((0, _domainsTs.domainOf)(
|
|
20
|
+
const snapshot = (data, opts = {})=>serializeRecurse(data, opts, []);
|
|
21
|
+
const stringify = (data, indent)=>{
|
|
22
|
+
switch((0, _domainsTs.domainOf)(data)){
|
|
23
|
+
case "object":
|
|
24
|
+
return JSON.stringify(serializeRecurse(data, stringifyOpts, []), null, indent);
|
|
25
|
+
case "symbol":
|
|
26
|
+
return stringifyOpts.onSymbol(data);
|
|
27
|
+
default:
|
|
28
|
+
return serializePrimitive(data);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const stringifyOpts = {
|
|
32
|
+
onCycle: ()=>"(cycle)",
|
|
33
|
+
onSymbol: (v)=>`(symbol${v.description && ` ${v.description}`})`,
|
|
34
|
+
onFunction: (v)=>`(function${v.name && ` ${v.name}`})`
|
|
35
|
+
};
|
|
36
|
+
const serializeRecurse = (data, context, seen)=>{
|
|
37
|
+
switch((0, _domainsTs.domainOf)(data)){
|
|
24
38
|
case "object":
|
|
25
|
-
if (typeof
|
|
26
|
-
return stringifyOpts.onFunction(
|
|
39
|
+
if (typeof data === "function") {
|
|
40
|
+
return stringifyOpts.onFunction(data);
|
|
27
41
|
}
|
|
28
|
-
if (seen.includes(
|
|
42
|
+
if (seen.includes(data)) {
|
|
29
43
|
return "(cycle)";
|
|
30
44
|
}
|
|
31
45
|
const nextSeen = [
|
|
32
46
|
...seen,
|
|
33
|
-
|
|
47
|
+
data
|
|
34
48
|
];
|
|
35
|
-
if (Array.isArray(
|
|
36
|
-
return
|
|
49
|
+
if (Array.isArray(data)) {
|
|
50
|
+
return data.map((item)=>serializeRecurse(item, context, nextSeen));
|
|
37
51
|
}
|
|
38
52
|
const result = {};
|
|
39
|
-
for(const k in
|
|
40
|
-
result[k] =
|
|
53
|
+
for(const k in data){
|
|
54
|
+
result[k] = serializeRecurse(data[k], context, nextSeen);
|
|
41
55
|
}
|
|
42
56
|
return result;
|
|
43
57
|
case "symbol":
|
|
44
|
-
return stringifyOpts.onSymbol(
|
|
58
|
+
return stringifyOpts.onSymbol(data);
|
|
45
59
|
case "bigint":
|
|
46
|
-
return `${
|
|
60
|
+
return `${data}n`;
|
|
47
61
|
case "undefined":
|
|
48
62
|
return "undefined";
|
|
49
63
|
default:
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const serialize = (data, opts = {}, indent)=>{
|
|
54
|
-
const seen = [];
|
|
55
|
-
return JSON.stringify(data, (k, v)=>{
|
|
56
|
-
switch((0, _domainsTs.domainOf)(v)){
|
|
57
|
-
case "object":
|
|
58
|
-
if (seen.includes(v)) {
|
|
59
|
-
return opts.onCycle?.(v);
|
|
60
|
-
}
|
|
61
|
-
if (typeof v === "function") {
|
|
62
|
-
return opts.onFunction?.(v);
|
|
63
|
-
}
|
|
64
|
-
return v;
|
|
65
|
-
case "undefined":
|
|
66
|
-
return "undefined";
|
|
67
|
-
case "symbol":
|
|
68
|
-
return opts.onSymbol?.(v);
|
|
69
|
-
case "bigint":
|
|
70
|
-
return `${v}n`;
|
|
71
|
-
default:
|
|
72
|
-
return v;
|
|
73
|
-
}
|
|
74
|
-
}, indent);
|
|
75
|
-
};
|
|
76
|
-
const stringifyOpts = {
|
|
77
|
-
onCycle: ()=>"(cycle)",
|
|
78
|
-
onSymbol: (v)=>`(symbol${v.description && ` ${v.description}`})`,
|
|
79
|
-
onFunction: (v)=>`(function${v.name && ` ${v.name}`})`
|
|
80
|
-
};
|
|
81
|
-
const stringify = (data, indent)=>{
|
|
82
|
-
switch((0, _domainsTs.domainOf)(data)){
|
|
83
|
-
case "object":
|
|
84
|
-
return serialize(data, stringifyOpts, indent);
|
|
85
|
-
case "symbol":
|
|
86
|
-
return stringifyOpts.onSymbol(data);
|
|
87
|
-
default:
|
|
88
|
-
return serializePrimitive(data);
|
|
64
|
+
return data;
|
|
89
65
|
}
|
|
90
66
|
};
|
|
91
67
|
const serializePrimitive = (value)=>typeof value === "string" ? `'${value}'` : typeof value === "bigint" ? `${value}n` : `${value}`;
|
package/dist/mjs/nodes/branch.js
CHANGED
|
@@ -70,9 +70,9 @@ export const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
70
70
|
result.distinctIntersections = pairs.flatMap((pairs)=>pairs.distinct ?? []);
|
|
71
71
|
return result;
|
|
72
72
|
};
|
|
73
|
-
export const
|
|
73
|
+
export const isTransformationBranch = (branch)=>"rules" in branch;
|
|
74
74
|
export const flattenBranch = (branch, ctx)=>{
|
|
75
|
-
if (
|
|
75
|
+
if (isTransformationBranch(branch)) {
|
|
76
76
|
const result = flattenRules(branch.rules, ctx);
|
|
77
77
|
if (branch.morph) {
|
|
78
78
|
if (typeof branch.morph === "function") {
|
|
@@ -41,7 +41,7 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
41
41
|
import { throwInternalError } from "../utils/errors.js";
|
|
42
42
|
import { objectKeysOf } from "../utils/generics.js";
|
|
43
43
|
import { Path } from "../utils/paths.js";
|
|
44
|
-
import {
|
|
44
|
+
import { stringify } from "../utils/serialize.js";
|
|
45
45
|
export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwUndefinedOperandsError() : r : r === undefined ? l : reducer(l, r, state);
|
|
46
46
|
export const throwUndefinedOperandsError = ()=>throwInternalError(`Unexpected operation two undefined operands`);
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
@@ -49,10 +49,10 @@ export const disjointDescriptionWriters = {
|
|
|
49
49
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
50
50
|
class: ({ l , r })=>`classes ${typeof l === "string" ? l : l.name} and ${typeof r === "string" ? r : r.name}`,
|
|
51
51
|
tupleLength: ({ l , r })=>`tuples of length ${l} and ${r}`,
|
|
52
|
-
value: ({ l , r })=>`literal values ${
|
|
53
|
-
leftAssignability: ({ l , r })=>`literal value ${
|
|
54
|
-
rightAssignability: ({ l , r })=>`literal value ${
|
|
55
|
-
union: ({ l , r })=>`branches ${
|
|
52
|
+
value: ({ l , r })=>`literal values ${stringify(l)} and ${stringify(r)}`,
|
|
53
|
+
leftAssignability: ({ l , r })=>`literal value ${stringify(l.value)} and ${stringify(r)}`,
|
|
54
|
+
rightAssignability: ({ l , r })=>`literal value ${stringify(r.value)} and ${stringify(l)}`,
|
|
55
|
+
union: ({ l , r })=>`branches ${stringify(l)} and branches ${stringify(r)}`
|
|
56
56
|
};
|
|
57
57
|
export const stringifyRange = (range)=>"limit" in range ? `the range of exactly ${range.limit}` : range.min ? range.max ? `the range bounded by ${range.min.comparator}${range.min.limit} and ${range.max.comparator}${range.max.limit}` : `${range.min.comparator}${range.min.limit}` : range.max ? `${range.max.comparator}${range.max.limit}` : "the unbounded range";
|
|
58
58
|
var _disjoints = /*#__PURE__*/ new WeakMap();
|
|
@@ -22,7 +22,7 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
22
22
|
return [
|
|
23
23
|
[
|
|
24
24
|
"branches",
|
|
25
|
-
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.
|
|
25
|
+
remainingIndices.map((i)=>branchIncludesMorph(originalBranches[i], ctx.type.scope) ? throwParseError(writeUndiscriminatableMorphUnionMessage(`${ctx.path}`)) : flattenBranch(originalBranches[i], ctx))
|
|
26
26
|
]
|
|
27
27
|
];
|
|
28
28
|
}
|
|
@@ -244,4 +244,4 @@ const serializeData = {
|
|
|
244
244
|
};
|
|
245
245
|
export const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
246
246
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
|
|
247
|
-
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).
|
|
247
|
+
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).includesMorph : Object.values($.resolveTypeNode(node)).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
|
|
2
2
|
import { throwParseError } from "../utils/errors.js";
|
|
3
|
-
import { hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
|
|
3
|
+
import { entriesOf, hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
|
|
4
4
|
import { Path } from "../utils/paths.js";
|
|
5
5
|
import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality, throwUndefinedOperandsError } from "./compose.js";
|
|
6
|
-
import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion
|
|
6
|
+
import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion } from "./predicate.js";
|
|
7
7
|
import { mappedKeys } from "./rules/props.js";
|
|
8
|
+
export const isConfigNode = (node)=>"config" in node;
|
|
8
9
|
export const nodeIntersection = (l, r, state)=>{
|
|
9
10
|
state.domain = undefined;
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const result =
|
|
11
|
+
const lDomains = state.type.scope.resolveTypeNode(l);
|
|
12
|
+
const rDomains = state.type.scope.resolveTypeNode(r);
|
|
13
|
+
const result = typeNodeIntersection(lDomains, rDomains, state);
|
|
13
14
|
if (typeof result === "object" && !hasKeys(result)) {
|
|
14
|
-
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(
|
|
15
|
+
return hasKeys(state.disjoints) ? anonymousDisjoint() : state.addDisjoint("domain", objectKeysOf(lDomains), objectKeysOf(rDomains));
|
|
15
16
|
}
|
|
16
|
-
return result ===
|
|
17
|
+
return result === lDomains ? l : result === rDomains ? r : result;
|
|
17
18
|
};
|
|
18
|
-
const
|
|
19
|
+
const typeNodeIntersection = composeKeyedIntersection((domain, l, r, context)=>{
|
|
19
20
|
if (l === undefined) {
|
|
20
21
|
return r === undefined ? throwUndefinedOperandsError() : undefined;
|
|
21
22
|
}
|
|
@@ -32,15 +33,15 @@ export const rootIntersection = (l, r, type)=>{
|
|
|
32
33
|
return isDisjoint(result) ? throwParseError(compileDisjointReasonsMessage(state.disjoints)) : isEquality(result) ? l : result;
|
|
33
34
|
};
|
|
34
35
|
export const rootUnion = (l, r, type)=>{
|
|
35
|
-
const
|
|
36
|
-
const
|
|
36
|
+
const lDomains = type.scope.resolveTypeNode(l);
|
|
37
|
+
const rDomains = type.scope.resolveTypeNode(r);
|
|
37
38
|
const result = {};
|
|
38
39
|
const domains = objectKeysOf({
|
|
39
|
-
...
|
|
40
|
-
...
|
|
40
|
+
...lDomains,
|
|
41
|
+
...rDomains
|
|
41
42
|
});
|
|
42
43
|
for (const domain of domains){
|
|
43
|
-
result[domain] = hasKey(
|
|
44
|
+
result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] : throwUndefinedOperandsError();
|
|
44
45
|
}
|
|
45
46
|
return result;
|
|
46
47
|
};
|
|
@@ -51,30 +52,25 @@ export const flattenType = (type)=>{
|
|
|
51
52
|
path: new Path(),
|
|
52
53
|
lastDomain: "undefined"
|
|
53
54
|
};
|
|
54
|
-
return type.
|
|
55
|
+
return flattenNode(type.node, ctx);
|
|
56
|
+
};
|
|
57
|
+
export const flattenNode = (node, ctx)=>{
|
|
58
|
+
if (typeof node === "string") {
|
|
59
|
+
return ctx.type.scope.resolve(node).flat;
|
|
60
|
+
}
|
|
61
|
+
const hasConfig = isConfigNode(node);
|
|
62
|
+
const flattenedTypeNode = flattenTypeNode(hasConfig ? node.node : node, ctx);
|
|
63
|
+
return hasConfig ? [
|
|
55
64
|
[
|
|
56
65
|
"config",
|
|
57
66
|
{
|
|
58
|
-
config:
|
|
59
|
-
node:
|
|
67
|
+
config: entriesOf(node.config),
|
|
68
|
+
node: flattenedTypeNode
|
|
60
69
|
}
|
|
61
70
|
]
|
|
62
|
-
] :
|
|
71
|
+
] : flattenedTypeNode;
|
|
63
72
|
};
|
|
64
|
-
export const
|
|
65
|
-
if (typeof node === "string") {
|
|
66
|
-
const resolution = ctx.type.meta.scope.resolve(node);
|
|
67
|
-
const updatedScopeConfig = resolution.meta.scope !== ctx.type.meta.scope && resolution.meta.scope.config;
|
|
68
|
-
return updatedScopeConfig ? [
|
|
69
|
-
[
|
|
70
|
-
"config",
|
|
71
|
-
{
|
|
72
|
-
config: updatedScopeConfig,
|
|
73
|
-
node: resolution.flat
|
|
74
|
-
}
|
|
75
|
-
]
|
|
76
|
-
] : resolution.flat;
|
|
77
|
-
}
|
|
73
|
+
export const flattenTypeNode = (node, ctx)=>{
|
|
78
74
|
const domains = objectKeysOf(node);
|
|
79
75
|
if (domains.length === 1) {
|
|
80
76
|
const domain = domains[0];
|
|
@@ -107,6 +103,10 @@ export const flattenNode = (node, ctx)=>{
|
|
|
107
103
|
export const isLiteralNode = (node, domain)=>{
|
|
108
104
|
return resolutionExtendsDomain(node, domain) && isLiteralCondition(node[domain]);
|
|
109
105
|
};
|
|
106
|
+
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
107
|
+
const domains = objectKeysOf(resolution);
|
|
108
|
+
return domains.length === 1 && domains[0] === domain;
|
|
109
|
+
};
|
|
110
110
|
export const toArrayNode = (node)=>({
|
|
111
111
|
object: {
|
|
112
112
|
class: "Array",
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { listFrom
|
|
1
|
+
import { listFrom } from "../utils/generics.js";
|
|
2
2
|
import { isArray } from "../utils/objectKinds.js";
|
|
3
3
|
import { branchIntersection, compareBranches, flattenBranch, isBranchComparison } from "./branch.js";
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
6
|
-
import "./rules/rules.js";
|
|
7
6
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
8
7
|
export const comparePredicates = (l, r, context)=>{
|
|
9
8
|
if (l === true && r === true) {
|
|
@@ -68,7 +67,3 @@ export const flattenPredicate = (predicate, context)=>{
|
|
|
68
67
|
return isArray(predicate) ? flattenBranches(predicate, context) : flattenBranch(predicate, context);
|
|
69
68
|
};
|
|
70
69
|
export const isLiteralCondition = (predicate)=>typeof predicate === "object" && "value" in predicate;
|
|
71
|
-
export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
72
|
-
const domains = objectKeysOf(resolution);
|
|
73
|
-
return domains.length === 1 && domains[0] === domain;
|
|
74
|
-
};
|