arktype 1.1.0-dev-1 → 2.0.0-beta.0
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/out/ark.d.ts +24 -0
- package/out/ark.js +8 -0
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +54 -0
- package/out/generic.js +39 -0
- package/out/index.d.ts +6 -0
- package/out/index.js +5 -0
- package/out/match.d.ts +71 -0
- package/out/match.js +50 -0
- package/out/module.d.ts +11 -0
- package/out/module.js +2 -0
- package/out/parser/definition.d.ts +32 -0
- package/out/parser/definition.js +29 -0
- package/out/parser/objectLiteral.d.ts +79 -0
- package/out/parser/objectLiteral.js +100 -0
- package/out/parser/semantic/bounds.d.ts +15 -0
- package/out/parser/semantic/bounds.js +1 -0
- package/out/parser/semantic/default.d.ts +8 -0
- package/out/parser/semantic/default.js +1 -0
- package/out/parser/semantic/divisor.d.ts +7 -0
- package/out/parser/semantic/divisor.js +1 -0
- package/out/parser/semantic/infer.d.ts +25 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/utils.d.ts +4 -0
- package/out/parser/semantic/utils.js +1 -0
- package/out/parser/semantic/validate.d.ts +30 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/string/reduce/dynamic.d.ts +43 -0
- package/out/parser/string/reduce/dynamic.js +139 -0
- package/out/parser/string/reduce/shared.d.ts +42 -0
- package/out/parser/string/reduce/shared.js +27 -0
- package/out/parser/string/reduce/static.d.ts +179 -0
- package/out/parser/string/reduce/static.js +1 -0
- package/out/parser/string/shift/operand/date.d.ts +9 -0
- package/out/parser/string/shift/operand/date.js +23 -0
- package/out/parser/string/shift/operand/enclosed.d.ts +38 -0
- package/out/parser/string/shift/operand/enclosed.js +50 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
- package/out/parser/string/shift/operand/genericArgs.js +16 -0
- package/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.js +12 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
- package/out/parser/string/shift/operand/unenclosed.js +53 -0
- package/out/parser/string/shift/operator/bounds.d.ts +27 -0
- package/out/parser/string/shift/operator/bounds.js +94 -0
- package/out/parser/string/shift/operator/default.d.ts +24 -0
- package/out/parser/string/shift/operator/default.js +23 -0
- package/out/parser/string/shift/operator/divisor.d.ts +7 -0
- package/out/parser/string/shift/operator/divisor.js +11 -0
- package/out/parser/string/shift/operator/operator.d.ts +13 -0
- package/out/parser/string/shift/operator/operator.js +22 -0
- package/out/parser/string/shift/scanner.d.ts +67 -0
- package/out/parser/string/shift/scanner.js +102 -0
- package/out/parser/string/string.d.ts +28 -0
- package/out/parser/string/string.js +24 -0
- package/out/parser/tuple.d.ts +130 -0
- package/out/parser/tuple.js +157 -0
- package/out/scope.d.ts +84 -0
- package/out/scope.js +131 -0
- package/out/type.d.ts +79 -0
- package/out/type.js +37 -0
- package/package.json +37 -36
- package/dist/main.d.ts +0 -1018
- package/dist/main.js +0 -1450
package/dist/main.js
DELETED
|
@@ -1,1450 +0,0 @@
|
|
|
1
|
-
// main.ts
|
|
2
|
-
import { Problem, Problems } from "@arktype/schema";
|
|
3
|
-
|
|
4
|
-
// scope.ts
|
|
5
|
-
import { builtins as builtins3 } from "@arktype/schema";
|
|
6
|
-
import {
|
|
7
|
-
domainOf,
|
|
8
|
-
hasDomain,
|
|
9
|
-
isThunk as isThunk2,
|
|
10
|
-
throwParseError as throwParseError8,
|
|
11
|
-
transform as transform2
|
|
12
|
-
} from "@arktype/util";
|
|
13
|
-
|
|
14
|
-
// type.ts
|
|
15
|
-
import {
|
|
16
|
-
In,
|
|
17
|
-
TraversalState,
|
|
18
|
-
arkKind,
|
|
19
|
-
builtins,
|
|
20
|
-
registry
|
|
21
|
-
} from "@arktype/schema";
|
|
22
|
-
import {
|
|
23
|
-
CompiledFunction,
|
|
24
|
-
transform
|
|
25
|
-
} from "@arktype/util";
|
|
26
|
-
|
|
27
|
-
// parser/generic.ts
|
|
28
|
-
import { throwParseError as throwParseError5 } from "@arktype/util";
|
|
29
|
-
|
|
30
|
-
// parser/string/reduce/dynamic.ts
|
|
31
|
-
import {
|
|
32
|
-
isKeyOf as isKeyOf4,
|
|
33
|
-
throwInternalError,
|
|
34
|
-
throwParseError as throwParseError3
|
|
35
|
-
} from "@arktype/util";
|
|
36
|
-
|
|
37
|
-
// parser/string/shift/operator/bounds.ts
|
|
38
|
-
import { isKeyOf as isKeyOf3, tryParseNumber as tryParseNumber3 } from "@arktype/util";
|
|
39
|
-
|
|
40
|
-
// parser/string/reduce/shared.ts
|
|
41
|
-
var writeUnmatchedGroupCloseMessage = (unscanned) => `Unmatched )${unscanned === "" ? "" : ` before ${unscanned}`}`;
|
|
42
|
-
var writeUnclosedGroupMessage = (missingChar) => `Missing ${missingChar}`;
|
|
43
|
-
var writeOpenRangeMessage = (min, comparator) => `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`;
|
|
44
|
-
var writeUnpairableComparatorMessage = (comparator) => `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`;
|
|
45
|
-
var writeMultipleLeftBoundsMessage = (openLimit, openComparator, limit, comparator) => `An expression may have at most one left bound (parsed ${openLimit}${invertedComparators[openComparator]}, ${limit}${invertedComparators[comparator]})`;
|
|
46
|
-
|
|
47
|
-
// parser/string/shift/operand/date.ts
|
|
48
|
-
import { throwParseError, tryParseNumber } from "@arktype/util";
|
|
49
|
-
var isDateLiteral = (value) => typeof value === "string" && value[0] === "d" && (value[1] === "'" || value[1] === '"') && value.at(-1) === value[1];
|
|
50
|
-
var isValidDate = (d) => d.toString() !== "Invalid Date";
|
|
51
|
-
var extractDateLiteralSource = (literal) => literal.slice(2, -1);
|
|
52
|
-
var writeInvalidDateMessage = (source) => `'${source}' could not be parsed by the Date constructor`;
|
|
53
|
-
var tryParseDate = (source, errorOnFail) => maybeParseDate(source, errorOnFail);
|
|
54
|
-
var maybeParseDate = (source, errorOnFail) => {
|
|
55
|
-
const stringParsedDate = new Date(source);
|
|
56
|
-
if (isValidDate(stringParsedDate)) {
|
|
57
|
-
return stringParsedDate;
|
|
58
|
-
}
|
|
59
|
-
const epochMillis = tryParseNumber(source);
|
|
60
|
-
if (epochMillis !== void 0) {
|
|
61
|
-
const numberParsedDate = new Date(epochMillis);
|
|
62
|
-
if (isValidDate(numberParsedDate)) {
|
|
63
|
-
return numberParsedDate;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return errorOnFail ? throwParseError(
|
|
67
|
-
errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail
|
|
68
|
-
) : void 0;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// parser/string/shift/scanner.ts
|
|
72
|
-
import { isKeyOf } from "@arktype/util";
|
|
73
|
-
var Scanner = class _Scanner {
|
|
74
|
-
chars;
|
|
75
|
-
i;
|
|
76
|
-
constructor(def) {
|
|
77
|
-
this.chars = [...def];
|
|
78
|
-
this.i = 0;
|
|
79
|
-
}
|
|
80
|
-
/** Get lookahead and advance scanner by one */
|
|
81
|
-
shift() {
|
|
82
|
-
return this.chars[this.i++] ?? "";
|
|
83
|
-
}
|
|
84
|
-
get lookahead() {
|
|
85
|
-
return this.chars[this.i] ?? "";
|
|
86
|
-
}
|
|
87
|
-
get length() {
|
|
88
|
-
return this.chars.length;
|
|
89
|
-
}
|
|
90
|
-
shiftUntil(condition) {
|
|
91
|
-
let shifted = "";
|
|
92
|
-
while (this.lookahead) {
|
|
93
|
-
if (condition(this, shifted)) {
|
|
94
|
-
if (shifted[shifted.length - 1] === _Scanner.escapeToken) {
|
|
95
|
-
shifted = shifted.slice(0, -1);
|
|
96
|
-
} else {
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
shifted += this.shift();
|
|
101
|
-
}
|
|
102
|
-
return shifted;
|
|
103
|
-
}
|
|
104
|
-
shiftUntilNextTerminator() {
|
|
105
|
-
this.shiftUntilNonWhitespace();
|
|
106
|
-
return this.shiftUntil(_Scanner.lookaheadIsTerminator);
|
|
107
|
-
}
|
|
108
|
-
shiftUntilNonWhitespace() {
|
|
109
|
-
return this.shiftUntil(_Scanner.lookaheadIsNotWhitespace);
|
|
110
|
-
}
|
|
111
|
-
jumpToIndex(i) {
|
|
112
|
-
this.i = i < 0 ? this.length + i : i;
|
|
113
|
-
}
|
|
114
|
-
get location() {
|
|
115
|
-
return this.i;
|
|
116
|
-
}
|
|
117
|
-
get unscanned() {
|
|
118
|
-
return this.chars.slice(this.i, this.length).join("");
|
|
119
|
-
}
|
|
120
|
-
get scanned() {
|
|
121
|
-
return this.chars.slice(0, this.i).join("");
|
|
122
|
-
}
|
|
123
|
-
sliceChars(start, end) {
|
|
124
|
-
return this.chars.slice(start, end).join("");
|
|
125
|
-
}
|
|
126
|
-
lookaheadIs(char) {
|
|
127
|
-
return this.lookahead === char;
|
|
128
|
-
}
|
|
129
|
-
lookaheadIsIn(tokens) {
|
|
130
|
-
return this.lookahead in tokens;
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
((Scanner2) => {
|
|
134
|
-
Scanner2.lookaheadIsTerminator = (scanner) => scanner.lookahead in Scanner2.terminatingChars;
|
|
135
|
-
Scanner2.lookaheadIsNotWhitespace = (scanner) => !(scanner.lookahead in Scanner2.whiteSpaceTokens);
|
|
136
|
-
Scanner2.terminatingChars = {
|
|
137
|
-
"<": true,
|
|
138
|
-
">": true,
|
|
139
|
-
"=": true,
|
|
140
|
-
"|": true,
|
|
141
|
-
"&": true,
|
|
142
|
-
")": true,
|
|
143
|
-
"[": true,
|
|
144
|
-
"%": true,
|
|
145
|
-
" ": true,
|
|
146
|
-
",": true
|
|
147
|
-
};
|
|
148
|
-
Scanner2.finalizingLookaheads = {
|
|
149
|
-
">": true,
|
|
150
|
-
",": true,
|
|
151
|
-
"": true
|
|
152
|
-
};
|
|
153
|
-
Scanner2.escapeToken = "\\";
|
|
154
|
-
Scanner2.whiteSpaceTokens = {
|
|
155
|
-
" ": true,
|
|
156
|
-
"\n": true
|
|
157
|
-
};
|
|
158
|
-
Scanner2.lookaheadIsFinalizing = (lookahead, unscanned) => lookahead === ">" ? unscanned[0] === "=" ? (
|
|
159
|
-
// >== would only occur in an expression like Array<number>==5
|
|
160
|
-
// otherwise, >= would only occur as part of a bound like number>=5
|
|
161
|
-
unscanned[1] === "="
|
|
162
|
-
) : (
|
|
163
|
-
// if > is the end of a generic instantiation, the next token will be an operator or the end of the string
|
|
164
|
-
unscanned.trimStart() === "" || isKeyOf(unscanned.trimStart()[0], Scanner2.terminatingChars)
|
|
165
|
-
) : (
|
|
166
|
-
// if the lookahead is a finalizing token but not >, it's unambiguously a finalizer (currently just ",")
|
|
167
|
-
lookahead === ","
|
|
168
|
-
);
|
|
169
|
-
})(Scanner || (Scanner = {}));
|
|
170
|
-
|
|
171
|
-
// parser/string/shift/operand/enclosed.ts
|
|
172
|
-
import { node } from "@arktype/schema";
|
|
173
|
-
import { isKeyOf as isKeyOf2 } from "@arktype/util";
|
|
174
|
-
var parseEnclosed = (s, enclosing) => {
|
|
175
|
-
const enclosed = s.scanner.shiftUntil(
|
|
176
|
-
untilLookaheadIsClosing[enclosingTokens[enclosing]]
|
|
177
|
-
);
|
|
178
|
-
if (s.scanner.lookahead === "") {
|
|
179
|
-
return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing));
|
|
180
|
-
}
|
|
181
|
-
const token = `${enclosing}${enclosed}${s.scanner.shift()}`;
|
|
182
|
-
if (enclosing === "/") {
|
|
183
|
-
new RegExp(enclosed);
|
|
184
|
-
s.root = node({ basis: "string", pattern: token });
|
|
185
|
-
} else if (isKeyOf2(enclosing, enclosingQuote)) {
|
|
186
|
-
s.root = node({ is: enclosed });
|
|
187
|
-
} else {
|
|
188
|
-
const date2 = tryParseDate(enclosed, writeInvalidDateMessage(enclosed));
|
|
189
|
-
s.root = node({ is: date2, description: token });
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
var enclosingQuote = {
|
|
193
|
-
"'": 1,
|
|
194
|
-
'"': 1
|
|
195
|
-
};
|
|
196
|
-
var enclosingChar = {
|
|
197
|
-
"/": 1,
|
|
198
|
-
...enclosingQuote
|
|
199
|
-
};
|
|
200
|
-
var enclosingTokens = {
|
|
201
|
-
"d'": "'",
|
|
202
|
-
'd"': '"',
|
|
203
|
-
"'": "'",
|
|
204
|
-
'"': '"',
|
|
205
|
-
"/": "/"
|
|
206
|
-
};
|
|
207
|
-
var untilLookaheadIsClosing = {
|
|
208
|
-
"'": (scanner) => scanner.lookahead === `'`,
|
|
209
|
-
'"': (scanner) => scanner.lookahead === `"`,
|
|
210
|
-
"/": (scanner) => scanner.lookahead === `/`
|
|
211
|
-
};
|
|
212
|
-
var enclosingCharDescriptions = {
|
|
213
|
-
'"': "double-quote",
|
|
214
|
-
"'": "single-quote",
|
|
215
|
-
"/": "forward slash"
|
|
216
|
-
};
|
|
217
|
-
var writeUnterminatedEnclosedMessage = (fragment, enclosingStart) => `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[enclosingTokens[enclosingStart]]}`;
|
|
218
|
-
|
|
219
|
-
// parser/string/shift/operand/unenclosed.ts
|
|
220
|
-
import { isNode, node as node2 } from "@arktype/schema";
|
|
221
|
-
import {
|
|
222
|
-
stringify,
|
|
223
|
-
throwParseError as throwParseError2,
|
|
224
|
-
tryParseNumber as tryParseNumber2,
|
|
225
|
-
tryParseWellFormedBigint
|
|
226
|
-
} from "@arktype/util";
|
|
227
|
-
var parseUnenclosed = (s) => {
|
|
228
|
-
const token = s.scanner.shiftUntilNextTerminator();
|
|
229
|
-
if (token === "keyof") {
|
|
230
|
-
s.addPrefix("keyof");
|
|
231
|
-
} else {
|
|
232
|
-
s.root = unenclosedToNode(s, token);
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
var parseGenericInstantiation = (name, g, s) => {
|
|
236
|
-
s.scanner.shiftUntilNonWhitespace();
|
|
237
|
-
const lookahead = s.scanner.shift();
|
|
238
|
-
if (lookahead !== "<") {
|
|
239
|
-
return s.error(writeInvalidGenericArgsMessage(name, g.parameters, []));
|
|
240
|
-
}
|
|
241
|
-
const parsedArgs = parseGenericArgs(
|
|
242
|
-
name,
|
|
243
|
-
g.parameters,
|
|
244
|
-
s.scanner.unscanned,
|
|
245
|
-
s.ctx
|
|
246
|
-
);
|
|
247
|
-
const remainingChars = parsedArgs.unscanned.length;
|
|
248
|
-
s.scanner.jumpToIndex(
|
|
249
|
-
remainingChars === 0 ? s.scanner.length : -remainingChars
|
|
250
|
-
);
|
|
251
|
-
return g(...parsedArgs.result).root;
|
|
252
|
-
};
|
|
253
|
-
var unenclosedToNode = (s, token) => maybeParseReference(s, token) ?? maybeParseUnenclosedLiteral(s, token) ?? s.error(
|
|
254
|
-
token === "" ? writeMissingOperandMessage(s) : writeUnresolvableMessage(token)
|
|
255
|
-
);
|
|
256
|
-
var maybeParseReference = (s, token) => {
|
|
257
|
-
if (s.ctx.args?.[token]) {
|
|
258
|
-
return s.ctx.args[token];
|
|
259
|
-
}
|
|
260
|
-
const resolution = s.ctx.scope.maybeResolve(token);
|
|
261
|
-
if (isNode(resolution)) {
|
|
262
|
-
return resolution;
|
|
263
|
-
}
|
|
264
|
-
if (resolution === void 0) {
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
if (hasArkKind(resolution, "generic")) {
|
|
268
|
-
return parseGenericInstantiation(token, resolution, s);
|
|
269
|
-
}
|
|
270
|
-
return throwParseError2(`Unexpected resolution ${stringify(resolution)}`);
|
|
271
|
-
};
|
|
272
|
-
var maybeParseUnenclosedLiteral = (s, token) => {
|
|
273
|
-
const maybeNumber = tryParseNumber2(token, { strict: true });
|
|
274
|
-
if (maybeNumber !== void 0) {
|
|
275
|
-
return node2({ is: maybeNumber });
|
|
276
|
-
}
|
|
277
|
-
const maybeBigint = tryParseWellFormedBigint(token);
|
|
278
|
-
if (maybeBigint !== void 0) {
|
|
279
|
-
return node2({ is: maybeBigint });
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
var writeNonSubmoduleDotMessage = (name) => `'${name}' must reference a scope to be accessed using dot syntax`;
|
|
283
|
-
var writeMissingSubmoduleAccessMessage = (name) => `Reference to submodule '${name}' must specify an alias`;
|
|
284
|
-
var writeUnresolvableMessage = (token) => `'${token}' is unresolvable`;
|
|
285
|
-
var writeMissingOperandMessage = (s) => {
|
|
286
|
-
const operator = s.previousOperator();
|
|
287
|
-
return operator ? writeMissingRightOperandMessage(operator, s.scanner.unscanned) : writeExpressionExpectedMessage(s.scanner.unscanned);
|
|
288
|
-
};
|
|
289
|
-
var writeMissingRightOperandMessage = (token, unscanned = "") => `Token '${token}' requires a right operand${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
290
|
-
var writeExpressionExpectedMessage = (unscanned) => `Expected an expression${unscanned ? ` before '${unscanned}'` : ""}`;
|
|
291
|
-
|
|
292
|
-
// parser/string/shift/operand/operand.ts
|
|
293
|
-
var parseOperand = (s) => s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift()) : s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens) ? parseOperand(s.shiftedByOne()) : s.scanner.lookahead === "d" ? s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, `d${s.scanner.shift()}`) : parseUnenclosed(s) : parseUnenclosed(s);
|
|
294
|
-
|
|
295
|
-
// parser/string/shift/operator/bounds.ts
|
|
296
|
-
var parseBound = (s, start) => {
|
|
297
|
-
const comparator = shiftComparator(s, start);
|
|
298
|
-
if (s.root.kind === "unit") {
|
|
299
|
-
if (typeof s.root.is === "number") {
|
|
300
|
-
s.unsetRoot();
|
|
301
|
-
return s.reduceLeftBound(s.root.is, comparator);
|
|
302
|
-
}
|
|
303
|
-
if (s.root.is instanceof Date) {
|
|
304
|
-
s.unsetRoot();
|
|
305
|
-
const literal = `d'${s.root.description ?? s.root.is.toISOString()}'`;
|
|
306
|
-
return s.reduceLeftBound(literal, comparator);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
return parseRightBound(s, comparator);
|
|
310
|
-
};
|
|
311
|
-
var minComparators = {
|
|
312
|
-
">": true,
|
|
313
|
-
">=": true
|
|
314
|
-
};
|
|
315
|
-
var maxComparators = {
|
|
316
|
-
"<": true,
|
|
317
|
-
"<=": true
|
|
318
|
-
};
|
|
319
|
-
var comparators = {
|
|
320
|
-
...minComparators,
|
|
321
|
-
...maxComparators,
|
|
322
|
-
"==": true
|
|
323
|
-
};
|
|
324
|
-
var oneCharComparators = {
|
|
325
|
-
"<": true,
|
|
326
|
-
">": true
|
|
327
|
-
};
|
|
328
|
-
var comparatorStartChars = {
|
|
329
|
-
"<": 1,
|
|
330
|
-
">": 1,
|
|
331
|
-
"=": 1
|
|
332
|
-
};
|
|
333
|
-
var shiftComparator = (s, start) => s.scanner.lookaheadIs("=") ? `${start}${s.scanner.shift()}` : isKeyOf3(start, oneCharComparators) ? start : s.error(singleEqualsMessage);
|
|
334
|
-
var singleEqualsMessage = `= is not a valid comparator. Use == to check for equality`;
|
|
335
|
-
var openLeftBoundToSchema = (leftBound) => ({
|
|
336
|
-
min: isDateLiteral(leftBound.limit) ? extractDateLiteralSource(leftBound.limit) : leftBound.limit,
|
|
337
|
-
exclusive: leftBound.comparator.length === 1
|
|
338
|
-
});
|
|
339
|
-
var parseRightBound = (s, comparator) => {
|
|
340
|
-
const previousRoot = s.unsetRoot();
|
|
341
|
-
const previousScannerIndex = s.scanner.location;
|
|
342
|
-
parseOperand(s);
|
|
343
|
-
const limitToken = s.scanner.sliceChars(
|
|
344
|
-
previousScannerIndex,
|
|
345
|
-
s.scanner.location
|
|
346
|
-
);
|
|
347
|
-
s.setRoot(previousRoot);
|
|
348
|
-
const limit = tryParseNumber3(limitToken) ?? (isDateLiteral(limitToken) ? extractDateLiteralSource(limitToken) : s.error(writeInvalidLimitMessage(comparator, limitToken, "right")));
|
|
349
|
-
const exclusive = comparator.length === 1;
|
|
350
|
-
if (comparator[0] !== ">") {
|
|
351
|
-
s.constrainRoot("max", { max: limit, exclusive });
|
|
352
|
-
}
|
|
353
|
-
if (comparator[0] !== "<") {
|
|
354
|
-
s.constrainRoot("min", { min: limit, exclusive });
|
|
355
|
-
}
|
|
356
|
-
if (!s.branches.leftBound) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
if (!isKeyOf3(comparator, maxComparators)) {
|
|
360
|
-
return s.error(writeUnpairableComparatorMessage(comparator));
|
|
361
|
-
}
|
|
362
|
-
s.constrainRoot("min", openLeftBoundToSchema(s.branches.leftBound));
|
|
363
|
-
delete s.branches.leftBound;
|
|
364
|
-
};
|
|
365
|
-
var writeInvalidLimitMessage = (comparator, limit, boundKind) => `Comparator ${comparator} must be ${boundKind === "left" ? "preceded" : "followed"} by a corresponding literal (was '${limit}')`;
|
|
366
|
-
var invertedComparators = {
|
|
367
|
-
"<": ">",
|
|
368
|
-
">": "<",
|
|
369
|
-
"<=": ">=",
|
|
370
|
-
">=": "<=",
|
|
371
|
-
"==": "=="
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
// parser/string/reduce/dynamic.ts
|
|
375
|
-
var DynamicState = class {
|
|
376
|
-
constructor(def, ctx) {
|
|
377
|
-
this.ctx = ctx;
|
|
378
|
-
this.scanner = new Scanner(def);
|
|
379
|
-
}
|
|
380
|
-
scanner;
|
|
381
|
-
root;
|
|
382
|
-
branches = {
|
|
383
|
-
prefixes: []
|
|
384
|
-
};
|
|
385
|
-
finalizer;
|
|
386
|
-
groups = [];
|
|
387
|
-
error(message) {
|
|
388
|
-
return throwParseError3(message);
|
|
389
|
-
}
|
|
390
|
-
hasRoot() {
|
|
391
|
-
return this.root !== void 0;
|
|
392
|
-
}
|
|
393
|
-
unsetRoot() {
|
|
394
|
-
const value = this.root;
|
|
395
|
-
this.root = void 0;
|
|
396
|
-
return value;
|
|
397
|
-
}
|
|
398
|
-
constrainRoot(...args) {
|
|
399
|
-
this.root = this.root.constrain(...args);
|
|
400
|
-
}
|
|
401
|
-
setRoot(root) {
|
|
402
|
-
this.root = root;
|
|
403
|
-
}
|
|
404
|
-
finalize(finalizer) {
|
|
405
|
-
if (this.groups.length) {
|
|
406
|
-
return this.error(writeUnclosedGroupMessage(")"));
|
|
407
|
-
}
|
|
408
|
-
this.finalizeBranches();
|
|
409
|
-
this.finalizer = finalizer;
|
|
410
|
-
}
|
|
411
|
-
reduceLeftBound(limit, comparator) {
|
|
412
|
-
const invertedComparator = invertedComparators[comparator];
|
|
413
|
-
if (!isKeyOf4(invertedComparator, minComparators)) {
|
|
414
|
-
return this.error(writeUnpairableComparatorMessage(comparator));
|
|
415
|
-
}
|
|
416
|
-
if (this.branches.leftBound) {
|
|
417
|
-
return this.error(
|
|
418
|
-
writeMultipleLeftBoundsMessage(
|
|
419
|
-
this.branches.leftBound.limit,
|
|
420
|
-
this.branches.leftBound.comparator,
|
|
421
|
-
limit,
|
|
422
|
-
invertedComparator
|
|
423
|
-
)
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
this.branches.leftBound = {
|
|
427
|
-
comparator: invertedComparator,
|
|
428
|
-
limit
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
finalizeBranches() {
|
|
432
|
-
this.assertRangeUnset();
|
|
433
|
-
if (this.branches["|"]) {
|
|
434
|
-
this.pushRootToBranch("|");
|
|
435
|
-
this.root = this.branches["|"];
|
|
436
|
-
} else if (this.branches["&"]) {
|
|
437
|
-
this.pushRootToBranch("&");
|
|
438
|
-
this.root = this.branches["&"];
|
|
439
|
-
} else {
|
|
440
|
-
this.applyPrefixes();
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
finalizeGroup() {
|
|
444
|
-
this.finalizeBranches();
|
|
445
|
-
const topBranchState = this.groups.pop();
|
|
446
|
-
if (!topBranchState) {
|
|
447
|
-
return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned));
|
|
448
|
-
}
|
|
449
|
-
this.branches = topBranchState;
|
|
450
|
-
}
|
|
451
|
-
addPrefix(prefix) {
|
|
452
|
-
this.branches.prefixes.push(prefix);
|
|
453
|
-
}
|
|
454
|
-
applyPrefixes() {
|
|
455
|
-
while (this.branches.prefixes.length) {
|
|
456
|
-
const lastPrefix = this.branches.prefixes.pop();
|
|
457
|
-
this.root = lastPrefix === "keyof" ? this.root.keyof() : throwInternalError(`Unexpected prefix '${lastPrefix}'`);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
pushRootToBranch(token) {
|
|
461
|
-
this.assertRangeUnset();
|
|
462
|
-
this.applyPrefixes();
|
|
463
|
-
const root = this.root;
|
|
464
|
-
this.branches["&"] = this.branches["&"]?.and(root) ?? root;
|
|
465
|
-
if (token === "|") {
|
|
466
|
-
this.branches["|"] = this.branches["|"]?.or(this.branches["&"]) ?? this.branches["&"];
|
|
467
|
-
delete this.branches["&"];
|
|
468
|
-
}
|
|
469
|
-
this.root = void 0;
|
|
470
|
-
}
|
|
471
|
-
assertRangeUnset() {
|
|
472
|
-
if (this.branches.leftBound) {
|
|
473
|
-
return this.error(
|
|
474
|
-
writeOpenRangeMessage(
|
|
475
|
-
this.branches.leftBound.limit,
|
|
476
|
-
this.branches.leftBound.comparator
|
|
477
|
-
)
|
|
478
|
-
);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
reduceGroupOpen() {
|
|
482
|
-
this.groups.push(this.branches);
|
|
483
|
-
this.branches = {
|
|
484
|
-
prefixes: []
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
previousOperator() {
|
|
488
|
-
return this.branches.leftBound?.comparator ?? this.branches.prefixes.at(-1) ?? (this.branches["&"] ? "&" : this.branches["|"] ? "|" : void 0);
|
|
489
|
-
}
|
|
490
|
-
shiftedByOne() {
|
|
491
|
-
this.scanner.shift();
|
|
492
|
-
return this;
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
// parser/string/shift/operator/operator.ts
|
|
497
|
-
import { isKeyOf as isKeyOf5 } from "@arktype/util";
|
|
498
|
-
|
|
499
|
-
// parser/string/shift/operator/divisor.ts
|
|
500
|
-
import { tryParseInteger } from "@arktype/util";
|
|
501
|
-
var parseDivisor = (s) => {
|
|
502
|
-
const divisorToken = s.scanner.shiftUntilNextTerminator();
|
|
503
|
-
const divisor = tryParseInteger(divisorToken, {
|
|
504
|
-
errorOnFail: writeInvalidDivisorMessage(divisorToken)
|
|
505
|
-
});
|
|
506
|
-
if (divisor === 0) {
|
|
507
|
-
s.error(writeInvalidDivisorMessage(0));
|
|
508
|
-
}
|
|
509
|
-
s.root = s.root.constrain("divisor", divisor);
|
|
510
|
-
};
|
|
511
|
-
var writeInvalidDivisorMessage = (divisor) => `% operator must be followed by a non-zero integer literal (was ${divisor})`;
|
|
512
|
-
|
|
513
|
-
// parser/string/shift/operator/operator.ts
|
|
514
|
-
var parseOperator = (s) => {
|
|
515
|
-
const lookahead = s.scanner.shift();
|
|
516
|
-
return lookahead === "" ? s.finalize("") : lookahead === "[" ? s.scanner.shift() === "]" ? s.setRoot(s.root.array()) : s.error(incompleteArrayTokenMessage) : lookahead === "|" || lookahead === "&" ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : Scanner.lookaheadIsFinalizing(lookahead, s.scanner.unscanned) ? s.finalize(lookahead) : isKeyOf5(lookahead, comparatorStartChars) ? parseBound(s, lookahead) : lookahead === "%" ? parseDivisor(s) : lookahead === " " ? parseOperator(s) : s.error(writeUnexpectedCharacterMessage(lookahead));
|
|
517
|
-
};
|
|
518
|
-
var writeUnexpectedCharacterMessage = (char, shouldBe = "") => `'${char}' is not allowed here${shouldBe && ` (should be ${shouldBe})`}`;
|
|
519
|
-
var incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
520
|
-
|
|
521
|
-
// parser/string/string.ts
|
|
522
|
-
import { throwParseError as throwParseError4 } from "@arktype/util";
|
|
523
|
-
|
|
524
|
-
// parser/semantic/validate.ts
|
|
525
|
-
var writeUnsatisfiableExpressionError = (expression) => `${expression} results in an unsatisfiable type`;
|
|
526
|
-
|
|
527
|
-
// parser/string/string.ts
|
|
528
|
-
var parseString = (def, ctx) => ctx.scope.maybeResolveNode(def) ?? (def.endsWith("[]") && ctx.scope.maybeResolveNode(def.slice(0, -2))?.array() || fullStringParse(def, ctx));
|
|
529
|
-
var fullStringParse = (def, ctx) => {
|
|
530
|
-
const s = new DynamicState(def, ctx);
|
|
531
|
-
parseOperand(s);
|
|
532
|
-
const result = parseUntilFinalizer(s).root;
|
|
533
|
-
s.scanner.shiftUntilNonWhitespace();
|
|
534
|
-
if (s.scanner.lookahead) {
|
|
535
|
-
throwParseError4(writeUnexpectedCharacterMessage(s.scanner.lookahead));
|
|
536
|
-
}
|
|
537
|
-
return result.isNever() ? throwParseError4(writeUnsatisfiableExpressionError(def)) : result;
|
|
538
|
-
};
|
|
539
|
-
var parseUntilFinalizer = (s) => {
|
|
540
|
-
while (s.finalizer === void 0) {
|
|
541
|
-
next(s);
|
|
542
|
-
}
|
|
543
|
-
return s;
|
|
544
|
-
};
|
|
545
|
-
var next = (s) => s.hasRoot() ? parseOperator(s) : parseOperand(s);
|
|
546
|
-
|
|
547
|
-
// parser/generic.ts
|
|
548
|
-
var parseGenericParams = (def) => parseGenericParamsRecurse(new Scanner(def));
|
|
549
|
-
var emptyGenericParameterMessage = `An empty string is not a valid generic parameter name`;
|
|
550
|
-
var parseGenericParamsRecurse = (scanner) => {
|
|
551
|
-
const param = scanner.shiftUntilNextTerminator();
|
|
552
|
-
if (param === "") {
|
|
553
|
-
throwParseError5(emptyGenericParameterMessage);
|
|
554
|
-
}
|
|
555
|
-
scanner.shiftUntilNonWhitespace();
|
|
556
|
-
const nextNonWhitespace = scanner.shift();
|
|
557
|
-
return nextNonWhitespace === "" ? [param] : nextNonWhitespace === "," ? [param, ...parseGenericParamsRecurse(scanner)] : throwParseError5(writeUnexpectedCharacterMessage(nextNonWhitespace, ","));
|
|
558
|
-
};
|
|
559
|
-
var parseGenericArgs = (name, params, unscanned, ctx) => parseGenericArgsRecurse(name, params, unscanned, ctx, [], []);
|
|
560
|
-
var parseGenericArgsRecurse = (name, params, unscanned, ctx, argDefs, argNodes) => {
|
|
561
|
-
const s = parseUntilFinalizer(new DynamicState(unscanned, ctx));
|
|
562
|
-
argDefs.push(s.scanner.scanned.slice(0, -1));
|
|
563
|
-
argNodes.push(s.root);
|
|
564
|
-
const nextUnscanned = s.scanner.unscanned;
|
|
565
|
-
if (s.finalizer === ">") {
|
|
566
|
-
if (argNodes.length === params.length) {
|
|
567
|
-
return {
|
|
568
|
-
result: argNodes,
|
|
569
|
-
unscanned: nextUnscanned
|
|
570
|
-
};
|
|
571
|
-
} else {
|
|
572
|
-
return s.error(writeInvalidGenericArgsMessage(name, params, argDefs));
|
|
573
|
-
}
|
|
574
|
-
} else if (s.finalizer === ",") {
|
|
575
|
-
return parseGenericArgsRecurse(
|
|
576
|
-
name,
|
|
577
|
-
params,
|
|
578
|
-
nextUnscanned,
|
|
579
|
-
ctx,
|
|
580
|
-
argDefs,
|
|
581
|
-
argNodes
|
|
582
|
-
);
|
|
583
|
-
}
|
|
584
|
-
return s.error(writeUnclosedGroupMessage(">"));
|
|
585
|
-
};
|
|
586
|
-
var writeInvalidGenericArgsMessage = (name, params, argDefs) => `${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${argDefs.length}${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`;
|
|
587
|
-
|
|
588
|
-
// type.ts
|
|
589
|
-
var createTypeParser = (scope2) => {
|
|
590
|
-
const parser = (...args) => {
|
|
591
|
-
if (args.length === 1) {
|
|
592
|
-
return new Type(args[0], scope2);
|
|
593
|
-
}
|
|
594
|
-
if (args.length === 2 && typeof args[0] === "string" && args[0][0] === "<" && args[0].at(-1) === ">") {
|
|
595
|
-
const params = parseGenericParams(args[0].slice(1, -1));
|
|
596
|
-
const def = args[1];
|
|
597
|
-
return validateUninstantiatedGeneric(generic(params, def, scope2));
|
|
598
|
-
}
|
|
599
|
-
return new Type(args, scope2);
|
|
600
|
-
};
|
|
601
|
-
return parser;
|
|
602
|
-
};
|
|
603
|
-
var addArkKind = (value, kind) => Object.defineProperty(value, arkKind, {
|
|
604
|
-
value: kind,
|
|
605
|
-
enumerable: false
|
|
606
|
-
});
|
|
607
|
-
var hasArkKind = (value, kind) => value?.[arkKind] === kind;
|
|
608
|
-
registry().register(TraversalState, "state");
|
|
609
|
-
var Type = class _Type extends CompiledFunction {
|
|
610
|
-
constructor(definition, scope2) {
|
|
611
|
-
const root = parseTypeRoot(definition, scope2);
|
|
612
|
-
super(In, `return true ? { data: ${In} } : { problems: [] } `);
|
|
613
|
-
this.definition = definition;
|
|
614
|
-
this.scope = scope2;
|
|
615
|
-
this.root = root;
|
|
616
|
-
this.allows = root.allows;
|
|
617
|
-
this.config = scope2.config;
|
|
618
|
-
this.json = this.root.json;
|
|
619
|
-
this.alias = this.root.alias;
|
|
620
|
-
}
|
|
621
|
-
config;
|
|
622
|
-
root;
|
|
623
|
-
condition = "";
|
|
624
|
-
alias;
|
|
625
|
-
allows;
|
|
626
|
-
json;
|
|
627
|
-
configure(config) {
|
|
628
|
-
this.config = { ...this.config, ...config };
|
|
629
|
-
return this;
|
|
630
|
-
}
|
|
631
|
-
// TODO: should return out
|
|
632
|
-
from(literal) {
|
|
633
|
-
return literal;
|
|
634
|
-
}
|
|
635
|
-
fromIn(literal) {
|
|
636
|
-
return literal;
|
|
637
|
-
}
|
|
638
|
-
// TODO: Morph intersections, ordering
|
|
639
|
-
and(def) {
|
|
640
|
-
return new _Type(
|
|
641
|
-
this.root.and(parseTypeRoot(def, this.scope)),
|
|
642
|
-
this.scope
|
|
643
|
-
);
|
|
644
|
-
}
|
|
645
|
-
or(def) {
|
|
646
|
-
return new _Type(
|
|
647
|
-
this.root.or(parseTypeRoot(def, this.scope)),
|
|
648
|
-
this.scope
|
|
649
|
-
);
|
|
650
|
-
}
|
|
651
|
-
morph(morph, outValidator) {
|
|
652
|
-
outValidator;
|
|
653
|
-
return this;
|
|
654
|
-
}
|
|
655
|
-
// TODO: based on below, should maybe narrow morph output if used after
|
|
656
|
-
narrow(def) {
|
|
657
|
-
return new _Type(this.root.constrain("predicate", def), this.scope);
|
|
658
|
-
}
|
|
659
|
-
array() {
|
|
660
|
-
return new _Type(this.root.array(), this.scope);
|
|
661
|
-
}
|
|
662
|
-
keyof() {
|
|
663
|
-
return new _Type(this.root.keyof(), this.scope);
|
|
664
|
-
}
|
|
665
|
-
assert(data) {
|
|
666
|
-
const result = this.call(null, data);
|
|
667
|
-
return result.problems ? result.problems.throw() : result.data;
|
|
668
|
-
}
|
|
669
|
-
// TODO: parse these
|
|
670
|
-
equals(other) {
|
|
671
|
-
return this.root === other.root;
|
|
672
|
-
}
|
|
673
|
-
extends(other) {
|
|
674
|
-
return this.root.extends(other.root);
|
|
675
|
-
}
|
|
676
|
-
};
|
|
677
|
-
var parseTypeRoot = (def, scope2, args) => scope2.parseDefinition(def, { args: args ?? bindThis(), baseName: "type" });
|
|
678
|
-
var validateUninstantiatedGeneric = (g) => {
|
|
679
|
-
g.scope.parseDefinition(
|
|
680
|
-
g.definition,
|
|
681
|
-
// once we support constraints on generic parameters, we'd use
|
|
682
|
-
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
683
|
-
{
|
|
684
|
-
baseName: "generic",
|
|
685
|
-
args: transform(g.parameters, ([, name]) => [name, builtins.unknown])
|
|
686
|
-
}
|
|
687
|
-
);
|
|
688
|
-
return g;
|
|
689
|
-
};
|
|
690
|
-
var generic = (parameters, definition, scope2) => {
|
|
691
|
-
return Object.assign(
|
|
692
|
-
(...args) => {
|
|
693
|
-
const argNodes = transform(parameters, ([i, param]) => [
|
|
694
|
-
param,
|
|
695
|
-
parseTypeRoot(args[i], scope2)
|
|
696
|
-
]);
|
|
697
|
-
const root = parseTypeRoot(definition, scope2, argNodes);
|
|
698
|
-
return new Type(root, scope2);
|
|
699
|
-
},
|
|
700
|
-
{
|
|
701
|
-
[arkKind]: "generic",
|
|
702
|
-
parameters,
|
|
703
|
-
definition,
|
|
704
|
-
scope: scope2
|
|
705
|
-
// $ is only needed at compile-time
|
|
706
|
-
}
|
|
707
|
-
);
|
|
708
|
-
};
|
|
709
|
-
|
|
710
|
-
// match.ts
|
|
711
|
-
var createWhenParser = (scope2) => {
|
|
712
|
-
const parser = (def) => new Type(def, scope2).alias;
|
|
713
|
-
return parser;
|
|
714
|
-
};
|
|
715
|
-
var createMatchParser = (scope2) => {
|
|
716
|
-
const parser = (cases) => {
|
|
717
|
-
const caseArray = Object.entries(cases).map(([def, morph]) => ({
|
|
718
|
-
when: new Type(def, scope2).allows,
|
|
719
|
-
then: morph
|
|
720
|
-
}));
|
|
721
|
-
return (data) => {
|
|
722
|
-
for (const c of caseArray) {
|
|
723
|
-
if (c.when(data)) {
|
|
724
|
-
return c.then(data, {});
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
};
|
|
728
|
-
};
|
|
729
|
-
return parser;
|
|
730
|
-
};
|
|
731
|
-
|
|
732
|
-
// parser/definition.ts
|
|
733
|
-
import { isNode as isNode2, node as node5 } from "@arktype/schema";
|
|
734
|
-
import {
|
|
735
|
-
isThunk,
|
|
736
|
-
objectKindOf,
|
|
737
|
-
stringify as stringify4,
|
|
738
|
-
throwParseError as throwParseError7
|
|
739
|
-
} from "@arktype/util";
|
|
740
|
-
|
|
741
|
-
// parser/objectLiteral.ts
|
|
742
|
-
import { node as node3 } from "@arktype/schema";
|
|
743
|
-
import {
|
|
744
|
-
stringify as stringify2
|
|
745
|
-
} from "@arktype/util";
|
|
746
|
-
|
|
747
|
-
// parser/shared.ts
|
|
748
|
-
var getInnerValue = (value) => {
|
|
749
|
-
if (typeof value === "string") {
|
|
750
|
-
if (value[value.length - 1] === "?") {
|
|
751
|
-
return {
|
|
752
|
-
kind: "optional",
|
|
753
|
-
innerValue: value.slice(0, -1)
|
|
754
|
-
};
|
|
755
|
-
}
|
|
756
|
-
} else if (Array.isArray(value)) {
|
|
757
|
-
if (value.length === 2 && value[1] === "?") {
|
|
758
|
-
return {
|
|
759
|
-
kind: "optional",
|
|
760
|
-
innerValue: getInnerValue(value[0]).innerValue
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
return {
|
|
765
|
-
kind: "required",
|
|
766
|
-
innerValue: value
|
|
767
|
-
};
|
|
768
|
-
};
|
|
769
|
-
var parseEntry = ([key, value]) => {
|
|
770
|
-
const keyParseResult = typeof key === "string" && key.at(-1) === "?" ? key.at(-2) === Scanner.escapeToken ? { innerKey: `${key.slice(0, -2)}?`, kind: "required" } : { innerKey: key.slice(0, -1), kind: "optional" } : { innerKey: key, kind: "required" };
|
|
771
|
-
const valueParseResult = getInnerValue(value);
|
|
772
|
-
return {
|
|
773
|
-
innerKey: keyParseResult.innerKey,
|
|
774
|
-
innerValue: valueParseResult.innerValue,
|
|
775
|
-
kind: keyParseResult.kind === "indexed" ? "indexed" : valueParseResult.kind === "optional" ? "optional" : keyParseResult.kind
|
|
776
|
-
};
|
|
777
|
-
};
|
|
778
|
-
|
|
779
|
-
// parser/objectLiteral.ts
|
|
780
|
-
var stringAndSymbolicEntriesOf = (o) => [
|
|
781
|
-
...Object.entries(o),
|
|
782
|
-
...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]])
|
|
783
|
-
];
|
|
784
|
-
var parseObjectLiteral = (def, ctx) => {
|
|
785
|
-
const required = [];
|
|
786
|
-
const optional = [];
|
|
787
|
-
for (const entry of stringAndSymbolicEntriesOf(def)) {
|
|
788
|
-
const result = parseEntry(entry);
|
|
789
|
-
ctx.path.push(
|
|
790
|
-
`${typeof result.innerKey === "symbol" ? `[${stringify2(result.innerKey)}]` : result.innerKey}`
|
|
791
|
-
);
|
|
792
|
-
const valueNode = ctx.scope.parse(result.innerValue, ctx);
|
|
793
|
-
if (result.kind === "optional") {
|
|
794
|
-
optional.push({
|
|
795
|
-
key: result.innerKey,
|
|
796
|
-
value: valueNode
|
|
797
|
-
});
|
|
798
|
-
} else {
|
|
799
|
-
required.push({
|
|
800
|
-
key: result.innerKey,
|
|
801
|
-
value: valueNode
|
|
802
|
-
});
|
|
803
|
-
}
|
|
804
|
-
ctx.path.pop();
|
|
805
|
-
}
|
|
806
|
-
return node3({
|
|
807
|
-
basis: "object",
|
|
808
|
-
required,
|
|
809
|
-
optional
|
|
810
|
-
});
|
|
811
|
-
};
|
|
812
|
-
|
|
813
|
-
// parser/tuple.ts
|
|
814
|
-
import {
|
|
815
|
-
builtins as builtins2,
|
|
816
|
-
node as node4
|
|
817
|
-
} from "@arktype/schema";
|
|
818
|
-
import {
|
|
819
|
-
isArray,
|
|
820
|
-
objectKindOrDomainOf,
|
|
821
|
-
stringify as stringify3,
|
|
822
|
-
throwParseError as throwParseError6
|
|
823
|
-
} from "@arktype/util";
|
|
824
|
-
var parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
825
|
-
var parseTupleLiteral = (def, ctx) => {
|
|
826
|
-
const props = [];
|
|
827
|
-
let isVariadic = false;
|
|
828
|
-
for (let i = 0; i < def.length; i++) {
|
|
829
|
-
let elementDef = def[i];
|
|
830
|
-
ctx.path.push(`${i}`);
|
|
831
|
-
if (typeof elementDef === "string" && elementDef.startsWith("...")) {
|
|
832
|
-
elementDef = elementDef.slice(3);
|
|
833
|
-
isVariadic = true;
|
|
834
|
-
} else if (isArray(elementDef) && elementDef.length === 2 && elementDef[0] === "...") {
|
|
835
|
-
elementDef = elementDef[1];
|
|
836
|
-
isVariadic = true;
|
|
837
|
-
}
|
|
838
|
-
const parsedEntry = parseEntry([`${i}`, elementDef]);
|
|
839
|
-
const value = ctx.scope.parse(parsedEntry.innerValue, ctx);
|
|
840
|
-
if (isVariadic) {
|
|
841
|
-
if (!value.extends(builtins2.array)) {
|
|
842
|
-
return throwParseError6(writeNonArrayRestMessage(elementDef));
|
|
843
|
-
}
|
|
844
|
-
if (i !== def.length - 1) {
|
|
845
|
-
return throwParseError6(prematureRestMessage);
|
|
846
|
-
}
|
|
847
|
-
const elementType = value.getPath();
|
|
848
|
-
props.push({ key: builtins2.number, value: elementType });
|
|
849
|
-
} else {
|
|
850
|
-
props.push({
|
|
851
|
-
key: {
|
|
852
|
-
name: `${i}`,
|
|
853
|
-
prerequisite: false,
|
|
854
|
-
optional: parsedEntry.kind === "optional"
|
|
855
|
-
},
|
|
856
|
-
value
|
|
857
|
-
});
|
|
858
|
-
}
|
|
859
|
-
ctx.path.pop();
|
|
860
|
-
}
|
|
861
|
-
if (!isVariadic) {
|
|
862
|
-
props.push({
|
|
863
|
-
key: {
|
|
864
|
-
name: "length",
|
|
865
|
-
prerequisite: true,
|
|
866
|
-
optional: false
|
|
867
|
-
},
|
|
868
|
-
// , ctx
|
|
869
|
-
value: node4({ is: def.length })
|
|
870
|
-
});
|
|
871
|
-
}
|
|
872
|
-
return node4(Array);
|
|
873
|
-
};
|
|
874
|
-
var maybeParseTupleExpression = (def, ctx) => {
|
|
875
|
-
const tupleExpressionResult = isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx) : isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx) : void 0;
|
|
876
|
-
if (tupleExpressionResult) {
|
|
877
|
-
return tupleExpressionResult.isNever() ? throwParseError6(
|
|
878
|
-
writeUnsatisfiableExpressionError(
|
|
879
|
-
def.map((def2) => typeof def2 === "string" ? def2 : stringify3(def2)).join(" ")
|
|
880
|
-
)
|
|
881
|
-
) : tupleExpressionResult;
|
|
882
|
-
}
|
|
883
|
-
};
|
|
884
|
-
var writeNonArrayRestMessage = (operand) => `Rest element ${typeof operand === "string" ? `'${operand}'` : ""} must be an array`;
|
|
885
|
-
var prematureRestMessage = `Rest elements are only allowed at the end of a tuple`;
|
|
886
|
-
var parseKeyOfTuple = (def, ctx) => ctx.scope.parse(def[1], ctx).keyof();
|
|
887
|
-
var parseBranchTuple = (def, ctx) => {
|
|
888
|
-
if (def[2] === void 0) {
|
|
889
|
-
return throwParseError6(writeMissingRightOperandMessage(def[1], ""));
|
|
890
|
-
}
|
|
891
|
-
const l = ctx.scope.parse(def[0], ctx);
|
|
892
|
-
const r = ctx.scope.parse(def[2], ctx);
|
|
893
|
-
return def[1] === "&" ? l.and(r) : l.or(r);
|
|
894
|
-
};
|
|
895
|
-
var parseArrayTuple = (def, ctx) => ctx.scope.parse(def[0], ctx).array();
|
|
896
|
-
var isIndexOneExpression = (def) => indexOneParsers[def[1]] !== void 0;
|
|
897
|
-
var parseMorphTuple = (def, ctx) => {
|
|
898
|
-
if (typeof def[2] !== "function") {
|
|
899
|
-
return throwParseError6(
|
|
900
|
-
writeMalformedFunctionalExpressionMessage("=>", def[2])
|
|
901
|
-
);
|
|
902
|
-
}
|
|
903
|
-
return ctx.scope.parse(def[0], ctx);
|
|
904
|
-
};
|
|
905
|
-
var writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
906
|
-
var parseNarrowTuple = (def, ctx) => {
|
|
907
|
-
if (typeof def[2] !== "function") {
|
|
908
|
-
return throwParseError6(
|
|
909
|
-
writeMalformedFunctionalExpressionMessage(":", def[2])
|
|
910
|
-
);
|
|
911
|
-
}
|
|
912
|
-
return ctx.scope.parse(def[0], ctx).constrain("predicate", def[2]);
|
|
913
|
-
};
|
|
914
|
-
var parseAttributeTuple = (def, ctx) => {
|
|
915
|
-
return ctx.scope.parse(def[0], ctx);
|
|
916
|
-
};
|
|
917
|
-
var indexOneParsers = {
|
|
918
|
-
"|": parseBranchTuple,
|
|
919
|
-
"&": parseBranchTuple,
|
|
920
|
-
"[]": parseArrayTuple,
|
|
921
|
-
":": parseNarrowTuple,
|
|
922
|
-
"=>": parseMorphTuple,
|
|
923
|
-
"@": parseAttributeTuple
|
|
924
|
-
};
|
|
925
|
-
var prefixParsers = {
|
|
926
|
-
keyof: parseKeyOfTuple,
|
|
927
|
-
instanceof: (def, ctx) => {
|
|
928
|
-
if (typeof def[1] !== "function") {
|
|
929
|
-
return throwParseError6(
|
|
930
|
-
writeInvalidConstructorMessage(objectKindOrDomainOf(def[1]))
|
|
931
|
-
);
|
|
932
|
-
}
|
|
933
|
-
const branches = def.slice(1).map(
|
|
934
|
-
(ctor) => typeof ctor === "function" ? { basis: ctor } : throwParseError6(
|
|
935
|
-
writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))
|
|
936
|
-
)
|
|
937
|
-
);
|
|
938
|
-
return node4(...branches);
|
|
939
|
-
},
|
|
940
|
-
"===": (def) => node4({ is: def.slice(1) })
|
|
941
|
-
};
|
|
942
|
-
var isIndexZeroExpression = (def) => prefixParsers[def[0]] !== void 0;
|
|
943
|
-
var writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual}).`;
|
|
944
|
-
|
|
945
|
-
// parser/definition.ts
|
|
946
|
-
var parseObject = (def, ctx) => {
|
|
947
|
-
const objectKind = objectKindOf(def);
|
|
948
|
-
switch (objectKind) {
|
|
949
|
-
case void 0:
|
|
950
|
-
if (isNode2(def)) {
|
|
951
|
-
return def;
|
|
952
|
-
}
|
|
953
|
-
return parseObjectLiteral(def, ctx);
|
|
954
|
-
case "Array":
|
|
955
|
-
return parseTuple(def, ctx);
|
|
956
|
-
case "RegExp":
|
|
957
|
-
return node5({ basis: "string", pattern: def });
|
|
958
|
-
case "Function":
|
|
959
|
-
const resolvedDef = isThunk(def) ? def() : def;
|
|
960
|
-
if (resolvedDef instanceof Type) {
|
|
961
|
-
return resolvedDef.root;
|
|
962
|
-
}
|
|
963
|
-
return throwParseError7(writeBadDefinitionTypeMessage("Function"));
|
|
964
|
-
default:
|
|
965
|
-
return throwParseError7(
|
|
966
|
-
writeBadDefinitionTypeMessage(objectKind ?? stringify4(def))
|
|
967
|
-
);
|
|
968
|
-
}
|
|
969
|
-
};
|
|
970
|
-
var writeBadDefinitionTypeMessage = (actual) => `Type definitions must be strings or objects (was ${actual})`;
|
|
971
|
-
|
|
972
|
-
// scope.ts
|
|
973
|
-
var bindThis = () => ({
|
|
974
|
-
// TODO: fix
|
|
975
|
-
this: builtins3.unknown
|
|
976
|
-
});
|
|
977
|
-
var Scope = class _Scope {
|
|
978
|
-
config;
|
|
979
|
-
parseCache = {};
|
|
980
|
-
resolutions;
|
|
981
|
-
/** The set of names defined at the root-level of the scope mapped to their
|
|
982
|
-
* corresponding definitions.**/
|
|
983
|
-
aliases = {};
|
|
984
|
-
exportedNames = [];
|
|
985
|
-
ambient;
|
|
986
|
-
references = [];
|
|
987
|
-
constructor(def, config) {
|
|
988
|
-
for (const k in def) {
|
|
989
|
-
const parsedKey = parseScopeKey(k);
|
|
990
|
-
this.aliases[parsedKey.name] = parsedKey.params.length ? generic(parsedKey.params, def[k], this) : def[k];
|
|
991
|
-
if (!parsedKey.isLocal) {
|
|
992
|
-
this.exportedNames.push(parsedKey.name);
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
this.ambient = config.ambient ?? null;
|
|
996
|
-
if (this.ambient) {
|
|
997
|
-
this.ambient.export();
|
|
998
|
-
this.resolutions = { ...this.ambient.exportedResolutions };
|
|
999
|
-
} else {
|
|
1000
|
-
this.resolutions = {};
|
|
1001
|
-
}
|
|
1002
|
-
this.config = config;
|
|
1003
|
-
}
|
|
1004
|
-
static root = (aliases) => {
|
|
1005
|
-
return new _Scope(aliases, {});
|
|
1006
|
-
};
|
|
1007
|
-
type = createTypeParser(this);
|
|
1008
|
-
match = createMatchParser(this);
|
|
1009
|
-
when = createWhenParser(this);
|
|
1010
|
-
// TODO: decide if this API will be used for non-validated types
|
|
1011
|
-
declare = () => ({ type: this.type });
|
|
1012
|
-
scope = (def, config = {}) => {
|
|
1013
|
-
return new _Scope(def, {
|
|
1014
|
-
ambient: this.ambient,
|
|
1015
|
-
...this.config,
|
|
1016
|
-
...config
|
|
1017
|
-
});
|
|
1018
|
-
};
|
|
1019
|
-
define = (def) => def;
|
|
1020
|
-
toAmbient() {
|
|
1021
|
-
return new _Scope(this.aliases, {
|
|
1022
|
-
ambient: this,
|
|
1023
|
-
...this.config
|
|
1024
|
-
});
|
|
1025
|
-
}
|
|
1026
|
-
// TODO: name?
|
|
1027
|
-
get(name) {
|
|
1028
|
-
return this.export()[name];
|
|
1029
|
-
}
|
|
1030
|
-
createRootContext(input) {
|
|
1031
|
-
return {
|
|
1032
|
-
path: [],
|
|
1033
|
-
scope: this,
|
|
1034
|
-
...input
|
|
1035
|
-
};
|
|
1036
|
-
}
|
|
1037
|
-
parseDefinition(def, input) {
|
|
1038
|
-
return this.parse(def, this.createRootContext(input));
|
|
1039
|
-
}
|
|
1040
|
-
parse(def, ctx) {
|
|
1041
|
-
if (typeof def === "string") {
|
|
1042
|
-
if (ctx.args !== void 0) {
|
|
1043
|
-
return parseString(def, ctx);
|
|
1044
|
-
}
|
|
1045
|
-
if (!this.parseCache[def]) {
|
|
1046
|
-
this.parseCache[def] = parseString(def, ctx);
|
|
1047
|
-
}
|
|
1048
|
-
return this.parseCache[def];
|
|
1049
|
-
}
|
|
1050
|
-
return hasDomain(def, "object") ? parseObject(def, ctx) : throwParseError8(writeBadDefinitionTypeMessage(domainOf(def)));
|
|
1051
|
-
}
|
|
1052
|
-
maybeResolve(name) {
|
|
1053
|
-
const cached = this.resolutions[name];
|
|
1054
|
-
if (cached) {
|
|
1055
|
-
return cached;
|
|
1056
|
-
}
|
|
1057
|
-
let def = this.aliases[name];
|
|
1058
|
-
if (!def) {
|
|
1059
|
-
return this.maybeResolveSubalias(name);
|
|
1060
|
-
}
|
|
1061
|
-
if (isThunk2(def) && !hasArkKind(def, "generic")) {
|
|
1062
|
-
def = def();
|
|
1063
|
-
}
|
|
1064
|
-
const resolution = hasArkKind(def, "generic") ? validateUninstantiatedGeneric(def) : hasArkKind(def, "module") ? throwParseError8(writeMissingSubmoduleAccessMessage(name)) : this.parseDefinition(
|
|
1065
|
-
def,
|
|
1066
|
-
this.createRootContext({ baseName: name, args: {} })
|
|
1067
|
-
);
|
|
1068
|
-
this.resolutions[name] = resolution;
|
|
1069
|
-
return resolution;
|
|
1070
|
-
}
|
|
1071
|
-
/** If name is a valid reference to a submodule alias, return its resolution */
|
|
1072
|
-
maybeResolveSubalias(name) {
|
|
1073
|
-
const dotIndex = name.indexOf(".");
|
|
1074
|
-
if (dotIndex === -1) {
|
|
1075
|
-
return;
|
|
1076
|
-
}
|
|
1077
|
-
const dotPrefix = name.slice(0, dotIndex);
|
|
1078
|
-
const prefixDef = this.aliases[dotPrefix];
|
|
1079
|
-
if (hasArkKind(prefixDef, "module")) {
|
|
1080
|
-
const resolution = prefixDef[name.slice(dotIndex + 1)]?.root;
|
|
1081
|
-
if (!resolution) {
|
|
1082
|
-
return throwParseError8(writeUnresolvableMessage(name));
|
|
1083
|
-
}
|
|
1084
|
-
this.resolutions[name] = resolution;
|
|
1085
|
-
return resolution;
|
|
1086
|
-
}
|
|
1087
|
-
if (prefixDef !== void 0) {
|
|
1088
|
-
return throwParseError8(writeNonSubmoduleDotMessage(dotPrefix));
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
maybeResolveNode(name) {
|
|
1092
|
-
const result = this.maybeResolve(name);
|
|
1093
|
-
return hasArkKind(result, "node") ? result : void 0;
|
|
1094
|
-
}
|
|
1095
|
-
import(...names) {
|
|
1096
|
-
return addArkKind(
|
|
1097
|
-
transform2(this.export(...names), ([alias, value]) => [
|
|
1098
|
-
`#${alias}`,
|
|
1099
|
-
value
|
|
1100
|
-
]),
|
|
1101
|
-
"module"
|
|
1102
|
-
);
|
|
1103
|
-
}
|
|
1104
|
-
compile() {
|
|
1105
|
-
return "";
|
|
1106
|
-
}
|
|
1107
|
-
exportedResolutions;
|
|
1108
|
-
exportCache;
|
|
1109
|
-
export(...names) {
|
|
1110
|
-
if (!this.exportCache) {
|
|
1111
|
-
this.exportCache = {};
|
|
1112
|
-
for (const name of this.exportedNames) {
|
|
1113
|
-
let def = this.aliases[name];
|
|
1114
|
-
if (hasArkKind(def, "generic")) {
|
|
1115
|
-
this.exportCache[name] = def;
|
|
1116
|
-
continue;
|
|
1117
|
-
}
|
|
1118
|
-
if (isThunk2(def)) {
|
|
1119
|
-
def = def();
|
|
1120
|
-
}
|
|
1121
|
-
if (hasArkKind(def, "module")) {
|
|
1122
|
-
this.exportCache[name] = def;
|
|
1123
|
-
} else {
|
|
1124
|
-
this.exportCache[name] = new Type(this.maybeResolve(name), this);
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
this.exportedResolutions = resolutionsOfModule(this.exportCache);
|
|
1128
|
-
Object.assign(this.resolutions, this.exportedResolutions);
|
|
1129
|
-
}
|
|
1130
|
-
const namesToExport = names.length ? names : this.exportedNames;
|
|
1131
|
-
return addArkKind(
|
|
1132
|
-
transform2(namesToExport, ([, name]) => [
|
|
1133
|
-
name,
|
|
1134
|
-
this.exportCache[name]
|
|
1135
|
-
]),
|
|
1136
|
-
"module"
|
|
1137
|
-
);
|
|
1138
|
-
}
|
|
1139
|
-
};
|
|
1140
|
-
var resolutionsOfModule = (typeSet) => {
|
|
1141
|
-
const result = {};
|
|
1142
|
-
for (const k in typeSet) {
|
|
1143
|
-
const v = typeSet[k];
|
|
1144
|
-
if (hasArkKind(v, "module")) {
|
|
1145
|
-
const innerResolutions = resolutionsOfModule(v);
|
|
1146
|
-
const prefixedResolutions = transform2(
|
|
1147
|
-
innerResolutions,
|
|
1148
|
-
([innerK, innerV]) => [`${k}.${innerK}`, innerV]
|
|
1149
|
-
);
|
|
1150
|
-
Object.assign(result, prefixedResolutions);
|
|
1151
|
-
} else if (hasArkKind(v, "generic")) {
|
|
1152
|
-
result[k] = v;
|
|
1153
|
-
} else {
|
|
1154
|
-
result[k] = v.root;
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
return result;
|
|
1158
|
-
};
|
|
1159
|
-
var parseScopeKey = (k) => {
|
|
1160
|
-
const isLocal = k[0] === "#";
|
|
1161
|
-
const name = isLocal ? k.slice(1) : k;
|
|
1162
|
-
const firstParamIndex = k.indexOf("<");
|
|
1163
|
-
if (firstParamIndex === -1) {
|
|
1164
|
-
return {
|
|
1165
|
-
isLocal,
|
|
1166
|
-
name,
|
|
1167
|
-
params: []
|
|
1168
|
-
};
|
|
1169
|
-
}
|
|
1170
|
-
if (k.at(-1) !== ">") {
|
|
1171
|
-
throwParseError8(
|
|
1172
|
-
`'>' must be the last character of a generic declaration in a scope`
|
|
1173
|
-
);
|
|
1174
|
-
}
|
|
1175
|
-
return {
|
|
1176
|
-
isLocal,
|
|
1177
|
-
name: name.slice(0, firstParamIndex),
|
|
1178
|
-
params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
|
|
1179
|
-
};
|
|
1180
|
-
};
|
|
1181
|
-
|
|
1182
|
-
// scopes/jsObjects.ts
|
|
1183
|
-
import { node as node6 } from "@arktype/schema";
|
|
1184
|
-
var jsObjects = Scope.root({
|
|
1185
|
-
Function: node6(Function),
|
|
1186
|
-
Date: node6(Date),
|
|
1187
|
-
Error: node6(Error),
|
|
1188
|
-
Map: node6(Map),
|
|
1189
|
-
RegExp: node6(RegExp),
|
|
1190
|
-
Set: node6(Set),
|
|
1191
|
-
WeakMap: node6(WeakMap),
|
|
1192
|
-
WeakSet: node6(WeakSet),
|
|
1193
|
-
Promise: node6(Promise)
|
|
1194
|
-
});
|
|
1195
|
-
var jsObjectsModule = jsObjects.export();
|
|
1196
|
-
|
|
1197
|
-
// scopes/parsing.ts
|
|
1198
|
-
import { node as node8 } from "@arktype/schema";
|
|
1199
|
-
import {
|
|
1200
|
-
wellFormedIntegerMatcher,
|
|
1201
|
-
wellFormedNumberMatcher
|
|
1202
|
-
} from "@arktype/util";
|
|
1203
|
-
|
|
1204
|
-
// scopes/utils/date.ts
|
|
1205
|
-
import { node as node7 } from "@arktype/schema";
|
|
1206
|
-
import { throwInternalError as throwInternalError2 } from "@arktype/util";
|
|
1207
|
-
var dayDelimiterMatcher = /^[./-]$/;
|
|
1208
|
-
var iso8601Matcher = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
|
1209
|
-
var isValidDateInstance = (date2) => !isNaN(date2);
|
|
1210
|
-
var writeFormattedMustBe = (format) => `a ${format}-formatted date`;
|
|
1211
|
-
var tryParseDatePattern = (data, opts) => {
|
|
1212
|
-
if (!opts?.format) {
|
|
1213
|
-
const result = new Date(data);
|
|
1214
|
-
return isValidDateInstance(result) ? result : "a valid date";
|
|
1215
|
-
}
|
|
1216
|
-
if (opts.format === "iso8601") {
|
|
1217
|
-
return iso8601Matcher.test(data) ? new Date(data) : writeFormattedMustBe("iso8601");
|
|
1218
|
-
}
|
|
1219
|
-
const dataParts = data.split(dayDelimiterMatcher);
|
|
1220
|
-
const delimiter = data[dataParts[0].length];
|
|
1221
|
-
const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format];
|
|
1222
|
-
if (dataParts.length !== formatParts.length) {
|
|
1223
|
-
return writeFormattedMustBe(opts.format);
|
|
1224
|
-
}
|
|
1225
|
-
const parsedParts = {};
|
|
1226
|
-
for (let i = 0; i < formatParts.length; i++) {
|
|
1227
|
-
if (dataParts[i].length !== formatParts[i].length && // if format is "m" or "d", data is allowed to be 1 or 2 characters
|
|
1228
|
-
!(formatParts[i].length === 1 && dataParts[i].length === 2)) {
|
|
1229
|
-
return writeFormattedMustBe(opts.format);
|
|
1230
|
-
}
|
|
1231
|
-
parsedParts[formatParts[i][0]] = dataParts[i];
|
|
1232
|
-
}
|
|
1233
|
-
const date2 = /* @__PURE__ */ new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`);
|
|
1234
|
-
if (`${date2.getDate()}` === parsedParts.d) {
|
|
1235
|
-
return date2;
|
|
1236
|
-
}
|
|
1237
|
-
return writeFormattedMustBe(opts.format);
|
|
1238
|
-
};
|
|
1239
|
-
var parsedDate = node7({
|
|
1240
|
-
in: "string",
|
|
1241
|
-
morph: (s) => {
|
|
1242
|
-
const result = tryParseDatePattern(s);
|
|
1243
|
-
return typeof result === "string" ? (
|
|
1244
|
-
// TODO: Fix
|
|
1245
|
-
throwInternalError2("Unsupported")
|
|
1246
|
-
) : (
|
|
1247
|
-
//state.mustBe(result, s, state.basePath)
|
|
1248
|
-
result
|
|
1249
|
-
);
|
|
1250
|
-
}
|
|
1251
|
-
});
|
|
1252
|
-
|
|
1253
|
-
// scopes/parsing.ts
|
|
1254
|
-
var number = node8({
|
|
1255
|
-
in: {
|
|
1256
|
-
basis: "string",
|
|
1257
|
-
pattern: wellFormedNumberMatcher,
|
|
1258
|
-
description: "a well-formed numeric string"
|
|
1259
|
-
},
|
|
1260
|
-
morph: (s) => parseFloat(s)
|
|
1261
|
-
});
|
|
1262
|
-
var integer = node8({
|
|
1263
|
-
in: {
|
|
1264
|
-
basis: "string",
|
|
1265
|
-
pattern: wellFormedIntegerMatcher
|
|
1266
|
-
},
|
|
1267
|
-
morph: (s) => {
|
|
1268
|
-
return parseInt(s);
|
|
1269
|
-
}
|
|
1270
|
-
});
|
|
1271
|
-
var url = node8({
|
|
1272
|
-
in: {
|
|
1273
|
-
basis: "string",
|
|
1274
|
-
description: "a valid URL"
|
|
1275
|
-
},
|
|
1276
|
-
morph: (s) => {
|
|
1277
|
-
return new URL(s);
|
|
1278
|
-
}
|
|
1279
|
-
});
|
|
1280
|
-
var json = node8({
|
|
1281
|
-
in: {
|
|
1282
|
-
basis: "string",
|
|
1283
|
-
description: "a JSON-parsable string"
|
|
1284
|
-
},
|
|
1285
|
-
morph: (s) => JSON.parse(s)
|
|
1286
|
-
});
|
|
1287
|
-
var date = parsedDate;
|
|
1288
|
-
var parsing = Scope.root({
|
|
1289
|
-
url,
|
|
1290
|
-
number,
|
|
1291
|
-
integer,
|
|
1292
|
-
date,
|
|
1293
|
-
json
|
|
1294
|
-
});
|
|
1295
|
-
var parsingModule = parsing.export();
|
|
1296
|
-
|
|
1297
|
-
// scopes/tsGenerics.ts
|
|
1298
|
-
var tsGenerics = Scope.root({
|
|
1299
|
-
// "Record<K, V>": {
|
|
1300
|
-
// // Remove this once we support constraints on generic parameters:
|
|
1301
|
-
// // https://github.com/arktypeio/arktype/issues/796
|
|
1302
|
-
// /** @ts-expect-error */
|
|
1303
|
-
// "[K]": "V"
|
|
1304
|
-
// }
|
|
1305
|
-
// unfortunately TS won't let us assign this directly, so we need to be
|
|
1306
|
-
// careful to keep the inferred types in sync
|
|
1307
|
-
});
|
|
1308
|
-
var tsGenericsModule = tsGenerics.export();
|
|
1309
|
-
|
|
1310
|
-
// scopes/tsKeywords.ts
|
|
1311
|
-
import { node as node9 } from "@arktype/schema";
|
|
1312
|
-
var tsKeywords = Scope.root({
|
|
1313
|
-
any: "unknown",
|
|
1314
|
-
bigint: node9("bigint"),
|
|
1315
|
-
boolean: "true|false",
|
|
1316
|
-
false: node9.units(false),
|
|
1317
|
-
never: node9(),
|
|
1318
|
-
null: node9.units(null),
|
|
1319
|
-
number: node9("number"),
|
|
1320
|
-
object: node9("object"),
|
|
1321
|
-
string: node9("string"),
|
|
1322
|
-
symbol: node9("symbol"),
|
|
1323
|
-
true: node9.units(true),
|
|
1324
|
-
unknown: node9({}),
|
|
1325
|
-
void: "undefined",
|
|
1326
|
-
undefined: node9.units(void 0)
|
|
1327
|
-
});
|
|
1328
|
-
var tsKeywordsModule = tsKeywords.export();
|
|
1329
|
-
|
|
1330
|
-
// scopes/validation.ts
|
|
1331
|
-
import { node as node11 } from "@arktype/schema";
|
|
1332
|
-
|
|
1333
|
-
// scopes/utils/creditCard.ts
|
|
1334
|
-
import { node as node10 } from "@arktype/schema";
|
|
1335
|
-
var isLuhnValid = (creditCardInput) => {
|
|
1336
|
-
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
1337
|
-
let sum = 0;
|
|
1338
|
-
let digit;
|
|
1339
|
-
let tmpNum;
|
|
1340
|
-
let shouldDouble;
|
|
1341
|
-
for (let i = sanitized.length - 1; i >= 0; i--) {
|
|
1342
|
-
digit = sanitized.substring(i, i + 1);
|
|
1343
|
-
tmpNum = parseInt(digit, 10);
|
|
1344
|
-
if (shouldDouble) {
|
|
1345
|
-
tmpNum *= 2;
|
|
1346
|
-
if (tmpNum >= 10) {
|
|
1347
|
-
sum += tmpNum % 10 + 1;
|
|
1348
|
-
} else {
|
|
1349
|
-
sum += tmpNum;
|
|
1350
|
-
}
|
|
1351
|
-
} else {
|
|
1352
|
-
sum += tmpNum;
|
|
1353
|
-
}
|
|
1354
|
-
shouldDouble = !shouldDouble;
|
|
1355
|
-
}
|
|
1356
|
-
return !!(sum % 10 === 0 ? sanitized : false);
|
|
1357
|
-
};
|
|
1358
|
-
var creditCardMatcher = /^(?: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}))$/;
|
|
1359
|
-
var creditCard = node10({
|
|
1360
|
-
basis: "string",
|
|
1361
|
-
pattern: creditCardMatcher,
|
|
1362
|
-
predicate: isLuhnValid,
|
|
1363
|
-
description: "a valid credit card number"
|
|
1364
|
-
});
|
|
1365
|
-
|
|
1366
|
-
// scopes/validation.ts
|
|
1367
|
-
var url2 = node11({
|
|
1368
|
-
basis: "string",
|
|
1369
|
-
predicate: (s) => {
|
|
1370
|
-
try {
|
|
1371
|
-
new URL(s);
|
|
1372
|
-
} catch {
|
|
1373
|
-
return false;
|
|
1374
|
-
}
|
|
1375
|
-
return true;
|
|
1376
|
-
},
|
|
1377
|
-
description: "a valid URL"
|
|
1378
|
-
});
|
|
1379
|
-
var emailMatcher = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
|
|
1380
|
-
var email = node11({
|
|
1381
|
-
basis: "string",
|
|
1382
|
-
pattern: emailMatcher,
|
|
1383
|
-
description: "a valid email"
|
|
1384
|
-
});
|
|
1385
|
-
var uuidMatcher = /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/;
|
|
1386
|
-
var uuid = node11({
|
|
1387
|
-
basis: "string",
|
|
1388
|
-
pattern: uuidMatcher,
|
|
1389
|
-
description: "a valid UUID"
|
|
1390
|
-
});
|
|
1391
|
-
var semverMatcher = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
1392
|
-
var semver = node11({
|
|
1393
|
-
basis: "string",
|
|
1394
|
-
pattern: semverMatcher,
|
|
1395
|
-
description: "a valid semantic version (see https://semver.org/)"
|
|
1396
|
-
});
|
|
1397
|
-
var validation = Scope.root({
|
|
1398
|
-
// Character sets
|
|
1399
|
-
alpha: [/^[A-Za-z]*$/, "@", "only letters"],
|
|
1400
|
-
alphanumeric: [/^[A-Za-z\d]*$/, "@", "only letters and digits"],
|
|
1401
|
-
lowercase: [/^[a-z]*$/, "@", "only lowercase letters"],
|
|
1402
|
-
uppercase: [/^[A-Z]*$/, "@", "only uppercase letters"],
|
|
1403
|
-
creditCard,
|
|
1404
|
-
email,
|
|
1405
|
-
uuid,
|
|
1406
|
-
url: url2,
|
|
1407
|
-
semver,
|
|
1408
|
-
integer: node11({
|
|
1409
|
-
basis: "number",
|
|
1410
|
-
divisor: 1,
|
|
1411
|
-
description: "an integer"
|
|
1412
|
-
})
|
|
1413
|
-
});
|
|
1414
|
-
var validationModule = validation.export();
|
|
1415
|
-
|
|
1416
|
-
// scopes/ark.ts
|
|
1417
|
-
var ark = Scope.root({
|
|
1418
|
-
...tsKeywordsModule,
|
|
1419
|
-
...jsObjectsModule,
|
|
1420
|
-
...validationModule,
|
|
1421
|
-
parse: parsingModule,
|
|
1422
|
-
// again, unfortunately TS won't handle comparing generics well here, so we
|
|
1423
|
-
// have to cast. that said, since each individual root scope is checked,
|
|
1424
|
-
// this is low risk
|
|
1425
|
-
...tsGenericsModule
|
|
1426
|
-
}).toAmbient();
|
|
1427
|
-
var arktypes = ark.export();
|
|
1428
|
-
var scope = ark.scope;
|
|
1429
|
-
var type = ark.type;
|
|
1430
|
-
var match = ark.match;
|
|
1431
|
-
var when = ark.when;
|
|
1432
|
-
var define = ark.define;
|
|
1433
|
-
var declare = ark.declare;
|
|
1434
|
-
export {
|
|
1435
|
-
Problem,
|
|
1436
|
-
Problems,
|
|
1437
|
-
Type,
|
|
1438
|
-
ark,
|
|
1439
|
-
arktypes,
|
|
1440
|
-
declare,
|
|
1441
|
-
define,
|
|
1442
|
-
jsObjects,
|
|
1443
|
-
match,
|
|
1444
|
-
scope,
|
|
1445
|
-
tsGenerics,
|
|
1446
|
-
tsKeywords,
|
|
1447
|
-
type,
|
|
1448
|
-
validation,
|
|
1449
|
-
when
|
|
1450
|
-
};
|