arktype 2.1.22 → 2.1.23
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 +23 -29
- package/out/attributes.d.ts +22 -25
- package/out/declare.d.ts +30 -0
- package/out/fn.d.ts +61 -0
- package/out/fn.js +53 -0
- package/out/generic.d.ts +9 -10
- package/out/generic.js +4 -3
- package/out/index.d.ts +2 -1
- package/out/index.js +1 -0
- package/out/keywords/keywords.d.ts +6 -3
- package/out/keywords/keywords.js +1 -0
- package/out/keywords/string.js +1 -12
- package/out/match.d.ts +3 -8
- package/out/nary.d.ts +21 -20
- package/out/parser/ast/infer.d.ts +3 -3
- package/out/parser/ast/utils.d.ts +2 -2
- package/out/parser/definition.d.ts +2 -15
- package/out/parser/objectLiteral.d.ts +4 -4
- package/out/parser/objectLiteral.js +3 -3
- package/out/parser/reduce/dynamic.d.ts +12 -12
- package/out/parser/reduce/dynamic.js +7 -7
- package/out/parser/reduce/shared.d.ts +0 -4
- package/out/parser/reduce/shared.js +0 -2
- package/out/parser/reduce/static.d.ts +13 -13
- package/out/parser/shift/operand/enclosed.d.ts +17 -9
- package/out/parser/shift/operand/enclosed.js +6 -3
- package/out/parser/shift/operand/genericArgs.d.ts +7 -8
- package/out/parser/shift/operand/genericArgs.js +1 -1
- package/out/parser/shift/operand/operand.d.ts +5 -6
- package/out/parser/shift/operand/unenclosed.d.ts +14 -17
- package/out/parser/shift/operand/unenclosed.js +3 -2
- package/out/parser/shift/operator/bounds.d.ts +8 -9
- package/out/parser/shift/operator/brand.d.ts +6 -5
- package/out/parser/shift/operator/brand.js +2 -1
- package/out/parser/shift/operator/default.d.ts +16 -9
- package/out/parser/shift/operator/default.js +1 -1
- package/out/parser/shift/operator/divisor.d.ts +6 -5
- package/out/parser/shift/operator/divisor.js +3 -1
- package/out/parser/shift/operator/operator.d.ts +6 -6
- package/out/parser/shift/operator/operator.js +2 -2
- package/out/parser/shift/tokens.d.ts +33 -0
- package/out/parser/shift/tokens.js +37 -0
- package/out/parser/string.d.ts +6 -6
- package/out/parser/string.js +3 -4
- package/out/parser/tupleExpressions.d.ts +5 -8
- package/out/parser/tupleExpressions.js +2 -2
- package/out/scope.d.ts +5 -1
- package/out/scope.js +4 -3
- package/out/type.d.ts +18 -10
- package/out/type.js +3 -2
- package/out/{methods → variants}/base.d.ts +1 -1
- package/out/{methods → variants}/object.d.ts +1 -1
- package/out/{methods → variants}/string.d.ts +4 -1
- package/out/variants/string.js +1 -0
- package/package.json +4 -3
- package/out/parser/shift/scanner.d.ts +0 -47
- package/out/parser/shift/scanner.js +0 -43
- /package/out/{methods/array.js → declare.js} +0 -0
- /package/out/{methods → variants}/array.d.ts +0 -0
- /package/out/{methods/base.js → variants/array.js} +0 -0
- /package/out/{methods/date.js → variants/base.js} +0 -0
- /package/out/{methods → variants}/date.d.ts +0 -0
- /package/out/{methods/instantiate.js → variants/date.js} +0 -0
- /package/out/{methods → variants}/instantiate.d.ts +0 -0
- /package/out/{methods/number.js → variants/instantiate.js} +0 -0
- /package/out/{methods → variants}/number.d.ts +0 -0
- /package/out/{methods/object.js → variants/number.js} +0 -0
- /package/out/{methods/string.js → variants/object.js} +0 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
ArkType is a runtime validation library that parses optimized validators from familiar, type-safe syntax.
|
|
5
5
|
|
|
6
|
-
It can be used to check external data like JSON payloads or
|
|
6
|
+
It can be used to check external data like JSON payloads or forms at the boundaries of your code (similar to Zod or Yup).
|
|
7
7
|
|
|
8
8
|
<video
|
|
9
9
|
autoPlay
|
|
@@ -50,15 +50,16 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
50
50
|
|
|
51
51
|
<table>
|
|
52
52
|
<tr>
|
|
53
|
-
<th>
|
|
53
|
+
<th>get-convex</th>
|
|
54
54
|
<th>inspatiallabs</th>
|
|
55
|
+
<th>sam-goodwin</th>
|
|
55
56
|
</tr>
|
|
56
57
|
<tr>
|
|
57
58
|
<td>
|
|
58
|
-
<a href="https://github.com/
|
|
59
|
+
<a href="https://github.com/get-convex"
|
|
59
60
|
><img
|
|
60
61
|
height="64px"
|
|
61
|
-
src="https://avatars.githubusercontent.com/
|
|
62
|
+
src="https://avatars.githubusercontent.com/get-convex"
|
|
62
63
|
/></a>
|
|
63
64
|
</td>
|
|
64
65
|
<td>
|
|
@@ -68,6 +69,13 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
68
69
|
src="https://avatars.githubusercontent.com/inspatiallabs"
|
|
69
70
|
/></a>
|
|
70
71
|
</td>
|
|
72
|
+
<td>
|
|
73
|
+
<a href="https://github.com/sam-goodwin"
|
|
74
|
+
><img
|
|
75
|
+
height="64px"
|
|
76
|
+
src="https://avatars.githubusercontent.com/sam-goodwin"
|
|
77
|
+
/></a>
|
|
78
|
+
</td>
|
|
71
79
|
</tr>
|
|
72
80
|
</table>
|
|
73
81
|
|
|
@@ -75,20 +83,13 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
75
83
|
|
|
76
84
|
<table>
|
|
77
85
|
<tr>
|
|
78
|
-
<th>fubhy</th>
|
|
79
86
|
<th>tmm</th>
|
|
80
|
-
<th>mishushakov</th>
|
|
81
87
|
<th>mewhhaha</th>
|
|
82
88
|
<th>jahands</th>
|
|
89
|
+
<th>drwpwrs</th>
|
|
90
|
+
<th>Phalangers</th>
|
|
83
91
|
</tr>
|
|
84
92
|
<tr>
|
|
85
|
-
<td>
|
|
86
|
-
<a href="https://github.com/fubhy"
|
|
87
|
-
><img
|
|
88
|
-
height="64px"
|
|
89
|
-
src="https://avatars.githubusercontent.com/fubhy"
|
|
90
|
-
/></a>
|
|
91
|
-
</td>
|
|
92
93
|
<td>
|
|
93
94
|
<a href="https://github.com/tmm"
|
|
94
95
|
><img
|
|
@@ -96,11 +97,6 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
96
97
|
src="https://avatars.githubusercontent.com/tmm"
|
|
97
98
|
/></a>
|
|
98
99
|
</td>
|
|
99
|
-
<td>
|
|
100
|
-
<a href="https://github.com/mishushakov"
|
|
101
|
-
><img height="64px" src="https://avatars.githubusercontent.com/mishushakov"
|
|
102
|
-
/></a>
|
|
103
|
-
</td>
|
|
104
100
|
<td>
|
|
105
101
|
<a href="https://github.com/mewhhaha"
|
|
106
102
|
><img
|
|
@@ -108,22 +104,13 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
108
104
|
src="https://avatars.githubusercontent.com/mewhhaha"
|
|
109
105
|
/></a>
|
|
110
106
|
</td>
|
|
111
|
-
|
|
107
|
+
<td>
|
|
112
108
|
<a href="https://github.com/jahands"
|
|
113
109
|
><img
|
|
114
110
|
height="64px"
|
|
115
111
|
src="https://avatars.githubusercontent.com/jahands"
|
|
116
112
|
/></a>
|
|
117
113
|
</td>
|
|
118
|
-
</tr>
|
|
119
|
-
<tr>
|
|
120
|
-
<th>drwpwrs</th>
|
|
121
|
-
<th>Phalangers</th>
|
|
122
|
-
<th>WilliamConnatser</th>
|
|
123
|
-
<th>JameEnder</th>
|
|
124
|
-
<th>tylim88</th>
|
|
125
|
-
</tr>
|
|
126
|
-
<tr>
|
|
127
114
|
<td>
|
|
128
115
|
<a href="https://github.com/drwpwrs"
|
|
129
116
|
><img
|
|
@@ -138,6 +125,13 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
138
125
|
src="https://avatars.githubusercontent.com/Phalangers"
|
|
139
126
|
/></a>
|
|
140
127
|
</td>
|
|
128
|
+
</tr>
|
|
129
|
+
<tr>
|
|
130
|
+
<th>WilliamConnatser</th>
|
|
131
|
+
<th>JameEnder</th>
|
|
132
|
+
<th>tylim88</th>
|
|
133
|
+
</tr>
|
|
134
|
+
<tr>
|
|
141
135
|
<td>
|
|
142
136
|
<a href="https://github.com/WilliamConnatser"
|
|
143
137
|
><img
|
|
@@ -152,7 +146,7 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
152
146
|
src="https://avatars.githubusercontent.com/JameEnder"
|
|
153
147
|
/></a>
|
|
154
148
|
</td>
|
|
155
|
-
|
|
149
|
+
<td>
|
|
156
150
|
<a href="https://github.com/tylim88"
|
|
157
151
|
><img
|
|
158
152
|
height="64px"
|
package/out/attributes.d.ts
CHANGED
|
@@ -9,43 +9,40 @@ export type RegexLiteral<source extends string = string> = `/${source}/`;
|
|
|
9
9
|
export type DateLiteral<source extends string = string> = `d"${source}"` | `d'${source}'`;
|
|
10
10
|
export type LimitLiteral = number | DateLiteral;
|
|
11
11
|
export type normalizeLimit<limit> = limit extends DateLiteral<infer source> ? source : limit extends number | string ? limit : never;
|
|
12
|
-
export type distill<t,
|
|
12
|
+
export type distill<t, side extends distill.Side> = finalizeDistillation<t, _distill<t, side>>;
|
|
13
13
|
export declare namespace distill {
|
|
14
|
-
type
|
|
14
|
+
type Side = "in" | "out" | "introspectableOut";
|
|
15
15
|
type In<t> = distill<t, "in">;
|
|
16
16
|
type Out<t> = distill<t, "out">;
|
|
17
17
|
namespace introspectable {
|
|
18
|
-
type Out<t> = distill<t, "
|
|
18
|
+
type Out<t> = distill<t, "introspectableOut">;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
type finalizeDistillation<t, distilled> = equals<t, distilled> extends true ? t : distilled;
|
|
22
|
-
type _distill<t,
|
|
23
|
-
type distillMappable<o,
|
|
24
|
-
[k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: _distill<o[k],
|
|
22
|
+
type _distill<t, side extends distill.Side> = t extends undefined ? t : [t] extends [anyOrNever] ? t : unknown extends t ? unknown : t extends Brand<infer base> ? side extends "in" ? base : t : t extends TerminallyInferredObject | Primitive ? t : t extends Function ? t extends (...args: never) => anyOrNever ? t : t extends InferredMorph<infer i, infer o> ? distillIo<i, o, side> : t : t extends Default<infer constraint> ? _distill<constraint, side> : t extends array ? distillArray<t, side> : isSafelyMappable<t> extends true ? distillMappable<t, side> : t;
|
|
23
|
+
type distillMappable<o, side extends distill.Side> = side extends "in" ? show<{
|
|
24
|
+
[k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: _distill<o[k], side>;
|
|
25
25
|
} & {
|
|
26
|
-
[k in inferredDefaultKeyOf<o>]?: _distill<o[k],
|
|
26
|
+
[k in inferredDefaultKeyOf<o>]?: _distill<o[k], side>;
|
|
27
27
|
}> : {
|
|
28
|
-
[k in keyof o]: _distill<o[k],
|
|
28
|
+
[k in keyof o]: _distill<o[k], side>;
|
|
29
29
|
};
|
|
30
|
-
type distillIo<i, o extends Out,
|
|
30
|
+
type distillIo<i, o extends Out, side extends distill.Side> = side extends "out" ? _distill<o["t"], side> : side extends "in" ? _distill<i, side> : o extends To<infer validatedOut> ? _distill<validatedOut, side> : unknown;
|
|
31
31
|
type unwrapInput<t> = t extends InferredMorph<infer i> ? t extends anyOrNever ? t : i : t;
|
|
32
32
|
type inferredDefaultKeyOf<o> = keyof o extends infer k ? k extends keyof o ? unwrapInput<o[k]> extends Default<infer t> ? [
|
|
33
33
|
t
|
|
34
34
|
] extends [anyOrNever] ? never : k : never : never : never;
|
|
35
|
-
type distillArray<t extends array,
|
|
35
|
+
type distillArray<t extends array, side extends distill.Side> = t[number][] extends t ? alignReadonly<_distill<t[number], side>[], t> : distillNonArraykeys<t, alignReadonly<distillArrayFromPrefix<[...t], side, []>, t>, side>;
|
|
36
36
|
type alignReadonly<result extends unknown[], original extends array> = original extends unknown[] ? result : Readonly<result>;
|
|
37
|
-
type distillNonArraykeys<originalArray extends array, distilledArray,
|
|
37
|
+
type distillNonArraykeys<originalArray extends array, distilledArray, side extends distill.Side> = keyof originalArray extends keyof distilledArray ? distilledArray : distilledArray & _distill<{
|
|
38
38
|
[k in keyof originalArray as k extends keyof distilledArray ? never : k]: originalArray[k];
|
|
39
|
-
},
|
|
40
|
-
type distillArrayFromPrefix<t extends array,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
]> : [...prefix, ...distillArrayFromPostfix<t,
|
|
44
|
-
type distillArrayFromPostfix<t extends array,
|
|
45
|
-
_distill<
|
|
46
|
-
...postfix
|
|
47
|
-
]> : [...{
|
|
48
|
-
[i in keyof t]: _distill<t[i], endpoint>;
|
|
39
|
+
}, side>;
|
|
40
|
+
type distillArrayFromPrefix<t extends array, side extends distill.Side, prefix extends array> = t extends readonly [infer head, ...infer tail] ? distillArrayFromPrefix<tail, side, [
|
|
41
|
+
side,
|
|
42
|
+
head
|
|
43
|
+
] extends ["in", Default] ? [...prefix, _distill<head, side>?] : [...prefix, _distill<head, side>]> : [...prefix, ...distillArrayFromPostfix<t, side, []>];
|
|
44
|
+
type distillArrayFromPostfix<t extends array, side extends distill.Side, postfix extends array> = t extends readonly [...infer init, infer last] ? distillArrayFromPostfix<init, side, [_distill<last, side>, ...postfix]> : [...{
|
|
45
|
+
[i in keyof t]: _distill<t[i], side>;
|
|
49
46
|
}, ...postfix];
|
|
50
47
|
type BuiltinTerminalObjectKind = Exclude<arkPrototypes.NonDegenerateName, "Array" | "Function">;
|
|
51
48
|
/** Objects we don't want to expand during inference like Date or Promise */
|
|
@@ -74,7 +71,7 @@ export interface Out<o = any> {
|
|
|
74
71
|
export interface To<o = any> extends Out<o> {
|
|
75
72
|
introspectable: true;
|
|
76
73
|
}
|
|
77
|
-
export type InferredMorph<i =
|
|
74
|
+
export type InferredMorph<i = never, o extends Out = Out> = (In: i) => o;
|
|
78
75
|
declare const defaultsToKey: " defaultsTo";
|
|
79
76
|
export type Default<t = unknown, v = unknown> = {
|
|
80
77
|
[defaultsToKey]: [t, v];
|
|
@@ -83,7 +80,7 @@ export type withDefault<t, v, undistributed = t> = t extends InferredMorph ? add
|
|
|
83
80
|
type addDefaultToMorph<t extends InferredMorph, v> = [
|
|
84
81
|
normalizeMorphDistribution<t>
|
|
85
82
|
] extends [InferredMorph<infer i, infer o>] ? (In: Default<i, v>) => o : never;
|
|
86
|
-
type normalizeMorphDistribution<t, undistributedIn = t extends InferredMorph<infer i> ? i : never, undistributedOut extends Out = t extends InferredMorph<
|
|
83
|
+
type normalizeMorphDistribution<t, undistributedIn = t extends InferredMorph<infer i> ? i : never, undistributedOut extends Out = t extends InferredMorph<never, infer o> ? [
|
|
87
84
|
o
|
|
88
85
|
] extends [To<infer unwrappedOut>] ? To<unwrappedOut> : o : never> = (Extract<t, InferredMorph> extends anyOrNever ? never : Extract<t, InferredMorph> extends InferredMorph<infer i, infer o> ? [
|
|
89
86
|
undistributedOut
|
|
@@ -95,12 +92,12 @@ export type inferMorph<t, morph extends Morph> = morph extends type.cast<infer t
|
|
|
95
92
|
export type inferPipe<l, r> = normalizeMorphDistribution<_inferIntersection<l, r, true>>;
|
|
96
93
|
type _inferIntersection<l, r, piped extends boolean> = [
|
|
97
94
|
l & r
|
|
98
|
-
] extends [infer t extends anyOrNever] ? t : l extends InferredMorph<infer lIn, infer lOut> ? r extends InferredMorph<
|
|
95
|
+
] extends [infer t extends anyOrNever] ? t : l extends InferredMorph<infer lIn, infer lOut> ? r extends InferredMorph<never, infer rOut> ? piped extends true ? (In: lIn) => rOut : never : piped extends true ? (In: lIn) => To<r> : (In: _inferIntersection<lIn, r, false>) => lOut : r extends InferredMorph<infer rIn, infer rOut> ? (In: _inferIntersection<rIn, l, false>) => rOut : [l, r] extends [object, object] ? intersectObjects<l, r, piped> extends infer result ? result : never : l & r;
|
|
99
96
|
interface MorphableIntersection<piped extends boolean> extends Hkt<[unknown, unknown]> {
|
|
100
97
|
body: _inferIntersection<this[0], this[1], piped>;
|
|
101
98
|
}
|
|
102
99
|
type intersectObjects<l, r, piped extends boolean> = l extends array ? r extends array ? intersectArrays<l, r, MorphableIntersection<piped>> : // for an intersection with exactly one array operand like { name: string } & string[],
|
|
103
|
-
l & r : r extends array ? l & r : show<{
|
|
100
|
+
l & r : r extends array ? l & r : keyof l & keyof r extends never ? show<l & r> : show<{
|
|
104
101
|
[k in keyof l]: k extends keyof r ? _inferIntersection<l[k], r[k], piped> : l[k];
|
|
105
102
|
} & {
|
|
106
103
|
[k in keyof r]: k extends keyof l ? _inferIntersection<l[k], r[k], piped> : r[k];
|
package/out/declare.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { anyOrNever, array, defined, equals, ErrorMessage, ErrorType, optionalKeyOf, requiredKeyOf, show, unset } from "@ark/util";
|
|
2
|
+
import type { distill } from "./attributes.ts";
|
|
3
|
+
import type { type } from "./keywords/keywords.ts";
|
|
4
|
+
import type { inferDefinition, ThunkCast } from "./parser/definition.ts";
|
|
5
|
+
import type { TupleExpression } from "./parser/tupleExpressions.ts";
|
|
6
|
+
import type { bindThis } from "./scope.ts";
|
|
7
|
+
import type { Type } from "./type.ts";
|
|
8
|
+
export type DeclarationParser<$> = <preinferred = unset, ctx extends DeclareContext = {}>() => {
|
|
9
|
+
type: <const def>(def: [preinferred] extends [unset] ? [
|
|
10
|
+
preinferred
|
|
11
|
+
] extends [anyOrNever] ? validateDeclared<preinferred, def, $, ctx> : ErrorMessage<`declare<ExternalType>() requires a generic argument`> : validateDeclared<preinferred, def, $, ctx>) => Type<finalizePreinferred<preinferred, def, $, ctx>, $>;
|
|
12
|
+
};
|
|
13
|
+
type finalizePreinferred<preinferred, def, $, ctx extends DeclareContext> = ctx["side"] extends distill.Side ? ctx["side"] extends "in" ? (In: preinferred) => type.infer.Out<def, $> : (In: type.infer.In<def, $>) => preinferred : preinferred;
|
|
14
|
+
export type DeclareContext = {
|
|
15
|
+
side?: "in" | "out";
|
|
16
|
+
};
|
|
17
|
+
export type validateDeclared<declared, def, $, ctx extends DeclareContext> = def extends type.validate<def, $> ? validateInference<def, declared, $, bindThis<def>, ctx> : type.validate<def, $>;
|
|
18
|
+
type validateInference<def, declared, $, args, ctx extends DeclareContext> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<inferDefinition<def, $, args>, declared, ctx> : def extends array ? declared extends array ? {
|
|
19
|
+
[i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args, ctx> : declared[i];
|
|
20
|
+
} : show<declarationMismatch<inferDefinition<def, $, args>, declared>> : def extends object ? show<{
|
|
21
|
+
[k in requiredKeyOf<declared>]: k extends keyof def ? validateInference<def[k], declared[k], $, args, ctx> : declared[k];
|
|
22
|
+
} & {
|
|
23
|
+
[k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (keyof def) ? validateInference<def[`${k}?`], defined<declared[k]>, $, args, ctx> : declared[k];
|
|
24
|
+
}> : validateShallowInference<inferDefinition<def, $, args>, declared, ctx>;
|
|
25
|
+
type validateShallowInference<t, declared, ctx extends DeclareContext, inferred = ctx["side"] extends distill.Side ? distill<t, ctx["side"]> : t> = equals<inferred, declared> extends true ? unknown : show<declarationMismatch<inferred, declared>>;
|
|
26
|
+
type declarationMismatch<inferred, declared> = ErrorType<{
|
|
27
|
+
declared: declared;
|
|
28
|
+
inferred: inferred;
|
|
29
|
+
}>;
|
|
30
|
+
export {};
|
package/out/fn.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { BaseRoot, IntersectionNode } from "@ark/schema";
|
|
2
|
+
import { Callable, type applyElementLabels, type conform, type Fn, type get } from "@ark/util";
|
|
3
|
+
import type { distill } from "./attributes.ts";
|
|
4
|
+
import type { type } from "./keywords/keywords.ts";
|
|
5
|
+
import type { validateInnerDefinition } from "./parser/definition.ts";
|
|
6
|
+
import type { inferTupleLiteral, validateTupleLiteral } from "./parser/tupleLiteral.ts";
|
|
7
|
+
import type { InternalScope, Scope } from "./scope.ts";
|
|
8
|
+
import type { Type } from "./type.ts";
|
|
9
|
+
export interface FnParser<$ = {}> {
|
|
10
|
+
<const args extends readonly unknown[], paramsT extends readonly unknown[] = inferTupleLiteral<args extends readonly [...infer params, ":", unknown] ? params : args, $, {}>, returnT = args extends readonly [...unknown[], ":", infer returnDef] ? type.infer<returnDef, $> : unknown>(...args: {
|
|
11
|
+
[i in keyof args]: conform<args[i], get<validateFnArgs<args, $>, i>>;
|
|
12
|
+
}): <internalSignature extends (...args: distill.Out<paramsT>) => distill.In<returnT>, externalSignature extends Fn = (...args: applyElementLabels<distill.In<paramsT>, Parameters<internalSignature>>) => args extends readonly [...unknown[], ":", unknown] ? distill.Out<returnT> : ReturnType<internalSignature>>(implementation: internalSignature) => TypedFn<externalSignature, $, args extends readonly [...unknown[], ":", unknown] ? Return.introspectable : {}>;
|
|
13
|
+
/**
|
|
14
|
+
* The {@link Scope} in which definitions passed to this function will be parsed.
|
|
15
|
+
*/
|
|
16
|
+
$: Scope<$>;
|
|
17
|
+
/**
|
|
18
|
+
* An alias of `fn` with no type-level validation or inference.
|
|
19
|
+
*
|
|
20
|
+
* Useful when wrapping `fn` or using it to parse a dynamic definition.
|
|
21
|
+
*/
|
|
22
|
+
raw: RawFnParser;
|
|
23
|
+
}
|
|
24
|
+
export type RawFnParser = (...args: unknown[]) => (...args: unknown[]) => unknown;
|
|
25
|
+
export declare class InternalFnParser extends Callable<(...args: unknown[]) => Fn> {
|
|
26
|
+
constructor($: InternalScope);
|
|
27
|
+
}
|
|
28
|
+
export declare namespace TypedFn {
|
|
29
|
+
type meta = {
|
|
30
|
+
introspectableReturn?: true;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface TypedFn<signature extends Fn = Fn, $ = {}, meta extends TypedFn.meta = {}> extends Callable<signature> {
|
|
34
|
+
expression: string;
|
|
35
|
+
params: signature extends Fn<infer params> ? Type<params, $> : never;
|
|
36
|
+
returns: Type<meta extends Return.introspectable ? ReturnType<signature> : unknown, $>;
|
|
37
|
+
}
|
|
38
|
+
export declare class InternalTypedFn extends Callable<(...args: unknown[]) => unknown> {
|
|
39
|
+
raw: Fn;
|
|
40
|
+
params: IntersectionNode;
|
|
41
|
+
returns: BaseRoot;
|
|
42
|
+
expression: string;
|
|
43
|
+
constructor(raw: Fn, params: IntersectionNode, returns: BaseRoot);
|
|
44
|
+
}
|
|
45
|
+
export declare namespace Return {
|
|
46
|
+
interface introspectable {
|
|
47
|
+
introspectableReturn: true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
type validateFnArgs<args, $> = args extends readonly unknown[] ? args extends readonly [...infer paramDefs, ":", infer returnDef] ? readonly [
|
|
51
|
+
...validateFnParamDefs<paramDefs, $>,
|
|
52
|
+
":",
|
|
53
|
+
type.validate<returnDef, $>
|
|
54
|
+
] : validateFnParamDefs<args, $> : never;
|
|
55
|
+
type validateFnParamDefs<paramDefs extends readonly unknown[], $> = paramDefs extends validateTupleLiteral<paramDefs, $, {}> ? paramDefs : paramDefs extends {
|
|
56
|
+
[i in keyof paramDefs]: paramDefs[i] extends "..." ? paramDefs[i] : validateInnerDefinition<paramDefs[i], $, {}>;
|
|
57
|
+
} ? validateTupleLiteral<paramDefs, $, {}> : {
|
|
58
|
+
[i in keyof paramDefs]: validateInnerDefinition<paramDefs[i], $, {}>;
|
|
59
|
+
};
|
|
60
|
+
export declare const badFnReturnTypeMessage = "\":\" must be followed by exactly one return type e.g:\nfn(\"string\", \":\", \"number\")(s => s.length)";
|
|
61
|
+
export {};
|
package/out/fn.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Callable, throwParseError } from "@ark/util";
|
|
2
|
+
export class InternalFnParser extends Callable {
|
|
3
|
+
constructor($) {
|
|
4
|
+
const attach = {
|
|
5
|
+
$: $,
|
|
6
|
+
raw: $.fn
|
|
7
|
+
};
|
|
8
|
+
super((...signature) => {
|
|
9
|
+
const returnOperatorIndex = signature.indexOf(":");
|
|
10
|
+
const lastParamIndex = returnOperatorIndex === -1 ?
|
|
11
|
+
signature.length - 1
|
|
12
|
+
: returnOperatorIndex - 1;
|
|
13
|
+
const paramDefs = signature.slice(0, lastParamIndex + 1);
|
|
14
|
+
const paramTuple = $.parse(paramDefs).assertHasKind("intersection");
|
|
15
|
+
let returnType = $.intrinsic.unknown;
|
|
16
|
+
if (returnOperatorIndex !== -1) {
|
|
17
|
+
if (returnOperatorIndex !== signature.length - 2)
|
|
18
|
+
return throwParseError(badFnReturnTypeMessage);
|
|
19
|
+
returnType = $.parse(signature[returnOperatorIndex + 1]);
|
|
20
|
+
}
|
|
21
|
+
return (impl) => new InternalTypedFn(impl, paramTuple, returnType);
|
|
22
|
+
}, { attach });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class InternalTypedFn extends Callable {
|
|
26
|
+
raw;
|
|
27
|
+
params;
|
|
28
|
+
returns;
|
|
29
|
+
expression;
|
|
30
|
+
constructor(raw, params, returns) {
|
|
31
|
+
const typedName = `typed ${raw.name}`;
|
|
32
|
+
const typed = {
|
|
33
|
+
// assign to a key with the expected name to force it to be created that way
|
|
34
|
+
[typedName]: (...args) => {
|
|
35
|
+
const validatedArgs = params.assert(args);
|
|
36
|
+
const returned = raw(...validatedArgs);
|
|
37
|
+
return returns.assert(returned);
|
|
38
|
+
}
|
|
39
|
+
}[typedName];
|
|
40
|
+
super(typed);
|
|
41
|
+
this.raw = raw;
|
|
42
|
+
this.params = params;
|
|
43
|
+
this.returns = returns;
|
|
44
|
+
let argsExpression = params.expression;
|
|
45
|
+
if (argsExpression[0] === "[" && argsExpression.at(-1) === "]")
|
|
46
|
+
argsExpression = argsExpression.slice(1, -1);
|
|
47
|
+
else if (argsExpression.endsWith("[]"))
|
|
48
|
+
argsExpression = `...${argsExpression}`;
|
|
49
|
+
this.expression = `(${argsExpression}) => ${returns?.expression ?? "unknown"}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export const badFnReturnTypeMessage = `":" must be followed by exactly one return type e.g:
|
|
53
|
+
fn("string", ":", "number")(s => s.length)`;
|
package/out/generic.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import { GenericRoot, type arkKind, type BaseParseContext, type GenericAst, type GenericParamAst, type GenericParamDef, type genericParamNames, type LazyGenericBody } from "@ark/schema";
|
|
2
|
-
import { type array, type Callable, type conform, type ErrorMessage, type ErrorType, type Hkt, type JsonStructure, type WhitespaceChar } from "@ark/util";
|
|
2
|
+
import { type array, type Callable, type conform, type ErrorMessage, type ErrorType, type Hkt, type JsonStructure, type Scanner, type WhitespaceChar } from "@ark/util";
|
|
3
3
|
import type { type } from "./keywords/keywords.ts";
|
|
4
4
|
import type { inferAstRoot } from "./parser/ast/infer.ts";
|
|
5
5
|
import type { validateAst } from "./parser/ast/validate.ts";
|
|
6
6
|
import type { inferDefinition } from "./parser/definition.ts";
|
|
7
|
-
import type {
|
|
8
|
-
import type { ArkTypeScanner } from "./parser/shift/scanner.ts";
|
|
7
|
+
import type { s, StaticState } from "./parser/reduce/static.ts";
|
|
9
8
|
import { parseUntilFinalizer } from "./parser/string.ts";
|
|
10
9
|
import type { Scope } from "./scope.ts";
|
|
11
10
|
import type { Type } from "./type.ts";
|
|
12
11
|
export type ParameterString<params extends string = string> = `<${params}>`;
|
|
13
12
|
export type extractParams<s extends ParameterString> = s extends ParameterString<infer params> ? params : never;
|
|
14
13
|
export type validateParameterString<s extends ParameterString, $> = parseGenericParams<extractParams<s>, $> extends infer e extends ErrorMessage ? e : s;
|
|
15
|
-
export type validateGenericArg<arg, param extends GenericParamAst, $> = type.infer<arg, $> extends param[1] ? unknown : ErrorType
|
|
14
|
+
export type validateGenericArg<arg, param extends GenericParamAst, $> = type.infer<arg, $> extends param[1] ? unknown : ErrorType<[`Invalid argument for ${param[0]}`, expected: param[1]]>;
|
|
16
15
|
export type GenericInstantiator<params extends array<GenericParamAst>, def, $, args$> = params["length"] extends 1 ? {
|
|
17
16
|
<const a, r = instantiateGeneric<def, params, [a], $, args$>>(a: type.validate<a, args$> & validateGenericArg<a, params[0], args$>): r extends infer _ ? _ : never;
|
|
18
17
|
} : params["length"] extends 2 ? {
|
|
@@ -87,19 +86,19 @@ export type GenericDeclaration<name extends string = string, params extends Para
|
|
|
87
86
|
export type parseValidGenericParams<def extends ParameterString, $> = conform<parseGenericParams<extractParams<def>, $>, array<GenericParamAst>>;
|
|
88
87
|
export declare const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
|
|
89
88
|
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage;
|
|
90
|
-
export type parseGenericParams<def extends string, $> = parseNextNameChar<
|
|
89
|
+
export type parseGenericParams<def extends string, $> = parseNextNameChar<Scanner.skipWhitespace<def>, "", [
|
|
91
90
|
], $>;
|
|
92
91
|
type ParamsTerminator = WhitespaceChar | ",";
|
|
93
|
-
export declare const parseGenericParamName: (scanner:
|
|
94
|
-
type parseName<unscanned extends string, result extends array<GenericParamAst>, $> = parseNextNameChar<
|
|
92
|
+
export declare const parseGenericParamName: (scanner: Scanner, result: GenericParamDef[], ctx: BaseParseContext) => GenericParamDef[];
|
|
93
|
+
type parseName<unscanned extends string, result extends array<GenericParamAst>, $> = parseNextNameChar<Scanner.skipWhitespace<unscanned>, "", result, $>;
|
|
95
94
|
type parseNextNameChar<unscanned extends string, name extends string, result extends array<GenericParamAst>, $> = unscanned extends `${infer lookahead}${infer nextUnscanned}` ? lookahead extends ParamsTerminator ? name extends "" ? ErrorMessage<emptyGenericParameterMessage> : lookahead extends "," ? parseName<nextUnscanned, [...result, [name, unknown]], $> : lookahead extends WhitespaceChar ? _parseOptionalConstraint<nextUnscanned, name, result, $> : never : parseNextNameChar<nextUnscanned, `${name}${lookahead}`, result, $> : name extends "" ? result : [...result, [name, unknown]];
|
|
96
95
|
declare const extendsToken = "extends ";
|
|
97
96
|
type extendsToken = typeof extendsToken;
|
|
98
|
-
declare const _parseOptionalConstraint: (scanner:
|
|
99
|
-
type _parseOptionalConstraint<unscanned extends string, name extends string, result extends array<GenericParamAst>, $> =
|
|
97
|
+
declare const _parseOptionalConstraint: (scanner: Scanner, name: string, result: GenericParamDef[], ctx: BaseParseContext) => GenericParamDef[];
|
|
98
|
+
type _parseOptionalConstraint<unscanned extends string, name extends string, result extends array<GenericParamAst>, $> = Scanner.skipWhitespace<unscanned> extends (`${extendsToken}${infer nextUnscanned}`) ? parseUntilFinalizer<s.initialize<nextUnscanned>, $, {}> extends (infer finalArgState extends StaticState) ? validateAst<finalArgState["root"], $, {}> extends (infer e extends ErrorMessage) ? e : parseName<finalArgState["unscanned"], [
|
|
100
99
|
...result,
|
|
101
100
|
[name, inferAstRoot<finalArgState["root"], $, {}>]
|
|
102
|
-
], $> : never : parseName<
|
|
101
|
+
], $> : never : parseName<Scanner.skipWhitespace<unscanned> extends `,${infer nextUnscanned}` ? nextUnscanned : unscanned, [
|
|
103
102
|
...result,
|
|
104
103
|
[name, unknown]
|
|
105
104
|
], $>;
|
package/out/generic.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { GenericRoot } from "@ark/schema";
|
|
2
2
|
import { throwParseError } from "@ark/util";
|
|
3
|
-
import {
|
|
3
|
+
import { RuntimeState } from "./parser/reduce/dynamic.js";
|
|
4
|
+
import { terminatingChars } from "./parser/shift/tokens.js";
|
|
4
5
|
import { parseUntilFinalizer } from "./parser/string.js";
|
|
5
6
|
export const Generic = GenericRoot;
|
|
6
7
|
export const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
|
|
7
8
|
export const parseGenericParamName = (scanner, result, ctx) => {
|
|
8
9
|
scanner.shiftUntilNonWhitespace();
|
|
9
|
-
const name = scanner.
|
|
10
|
+
const name = scanner.shiftUntilLookahead(terminatingChars);
|
|
10
11
|
if (name === "") {
|
|
11
12
|
// if we've reached the end of the string and have parsed at least one
|
|
12
13
|
// param, return the valid result
|
|
@@ -30,7 +31,7 @@ const _parseOptionalConstraint = (scanner, name, result, ctx) => {
|
|
|
30
31
|
result.push(name);
|
|
31
32
|
return parseGenericParamName(scanner, result, ctx);
|
|
32
33
|
}
|
|
33
|
-
const s = parseUntilFinalizer(new
|
|
34
|
+
const s = parseUntilFinalizer(new RuntimeState(scanner, ctx));
|
|
34
35
|
result.push([name, s.root]);
|
|
35
36
|
return parseGenericParamName(scanner, result, ctx);
|
|
36
37
|
};
|
package/out/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
export { regex } from "@ark/regex";
|
|
1
2
|
export { ArkError, ArkErrors, Traversal, TraversalError, type ArkSchemaConfig, type ArkSchemaScopeConfig, type JsonSchema } from "@ark/schema";
|
|
2
3
|
export { Hkt, inferred, ParseError } from "@ark/util";
|
|
3
4
|
export type { distill, Out } from "./attributes.ts";
|
|
4
5
|
export * from "./config.ts";
|
|
5
6
|
export { Generic } from "./generic.ts";
|
|
6
7
|
export { ark, declare, define, generic, keywords, match, type, type Ark } from "./keywords/keywords.ts";
|
|
7
|
-
export type { BaseType } from "./methods/base.ts";
|
|
8
8
|
export { Module, type BoundModule, type Submodule } from "./module.ts";
|
|
9
9
|
export type { inferDefinition, validateDefinition } from "./parser/definition.ts";
|
|
10
10
|
export { scope, type bindThis, type Scope } from "./scope.ts";
|
|
11
11
|
export { Type } from "./type.ts";
|
|
12
|
+
export type { BaseType } from "./variants/base.ts";
|
package/out/index.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { ArkErrors, arkKind, flatResolutionsOf } from "@ark/schema";
|
|
2
2
|
import type { Brand, inferred } from "@ark/util";
|
|
3
3
|
import type { distill, InferredMorph, Out, To } from "../attributes.ts";
|
|
4
|
+
import type { DeclarationParser } from "../declare.ts";
|
|
5
|
+
import type { FnParser } from "../fn.ts";
|
|
4
6
|
import type { GenericParser } from "../generic.ts";
|
|
5
7
|
import type { MatchParser } from "../match.ts";
|
|
6
|
-
import type { BaseType } from "../methods/base.ts";
|
|
7
|
-
import type { instantiateType } from "../methods/instantiate.ts";
|
|
8
8
|
import type { BoundModule, Module } from "../module.ts";
|
|
9
9
|
import type { inferDefinition, validateDefinition } from "../parser/definition.ts";
|
|
10
10
|
import { type bindThis, type Scope } from "../scope.ts";
|
|
11
|
-
import type {
|
|
11
|
+
import type { DefinitionParser, SchemaParser, Type, TypeParser } from "../type.ts";
|
|
12
|
+
import type { BaseType } from "../variants/base.ts";
|
|
13
|
+
import type { instantiateType } from "../variants/instantiate.ts";
|
|
12
14
|
import { arkBuiltins } from "./builtins.ts";
|
|
13
15
|
import { arkPrototypes } from "./constructors.ts";
|
|
14
16
|
import { number } from "./number.ts";
|
|
@@ -61,6 +63,7 @@ export declare namespace type {
|
|
|
61
63
|
}
|
|
62
64
|
export type type<t = unknown, $ = {}> = Type<t, $>;
|
|
63
65
|
export declare const match: MatchParser<{}>;
|
|
66
|
+
export declare const fn: FnParser<{}>;
|
|
64
67
|
export declare const generic: GenericParser<{}>;
|
|
65
68
|
export declare const schema: SchemaParser<{}>;
|
|
66
69
|
export declare const define: DefinitionParser<{}>;
|
package/out/keywords/keywords.js
CHANGED
|
@@ -41,6 +41,7 @@ export const type = Object.assign(ark.type,
|
|
|
41
41
|
// registry when their TypeParsers are instantiated
|
|
42
42
|
$arkTypeRegistry.typeAttachments);
|
|
43
43
|
export const match = ark.match;
|
|
44
|
+
export const fn = ark.fn;
|
|
44
45
|
export const generic = ark.generic;
|
|
45
46
|
export const schema = ark.schema;
|
|
46
47
|
export const define = ark.define;
|
package/out/keywords/string.js
CHANGED
|
@@ -383,18 +383,7 @@ const upper = Scope.module({
|
|
|
383
383
|
}, {
|
|
384
384
|
name: "string.upper"
|
|
385
385
|
});
|
|
386
|
-
const isParsableUrl = (s) =>
|
|
387
|
-
if (URL.canParse)
|
|
388
|
-
return URL.canParse(s);
|
|
389
|
-
// TODO[2025-04-30] remove once Node 18 is EOL and we can rely on URL.canParse
|
|
390
|
-
try {
|
|
391
|
-
new URL(s);
|
|
392
|
-
return true;
|
|
393
|
-
}
|
|
394
|
-
catch {
|
|
395
|
-
return false;
|
|
396
|
-
}
|
|
397
|
-
};
|
|
386
|
+
const isParsableUrl = (s) => URL.canParse(s);
|
|
398
387
|
const urlRoot = rootSchema({
|
|
399
388
|
domain: "string",
|
|
400
389
|
predicate: {
|
package/out/match.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type ArkErrors, type BaseRoot, type Morph } from "@ark/schema";
|
|
2
|
-
import { Callable, type conform, type ErrorMessage, type ErrorType, type isDisjoint, type Key, type numericStringKeyOf, type propValueOf, type unionToTuple } from "@ark/util";
|
|
2
|
+
import { Callable, type conform, type ErrorMessage, type ErrorType, type isDisjoint, type Key, type numericStringKeyOf, type propValueOf, type show, type unionToTuple } from "@ark/util";
|
|
3
3
|
import type { distill, Out } from "./attributes.ts";
|
|
4
4
|
import type { type } from "./keywords/keywords.ts";
|
|
5
|
-
import type { Inferred } from "./methods/base.ts";
|
|
6
5
|
import type { BaseCompletions } from "./parser/string.ts";
|
|
7
6
|
import type { InternalScope } from "./scope.ts";
|
|
7
|
+
import type { Inferred } from "./variants/base.ts";
|
|
8
8
|
type MatchParserContext<input = unknown> = {
|
|
9
9
|
cases: Morph[];
|
|
10
10
|
$: unknown;
|
|
@@ -70,12 +70,7 @@ type inferCaseArg<def, ctx extends MatchParserContext, endpoint extends "in" | "
|
|
|
70
70
|
type maybeLiftToKey<t, ctx extends MatchParserContext> = ctx["key"] extends PropertyKey ? {
|
|
71
71
|
[k in ctx["key"]]: t;
|
|
72
72
|
} : t;
|
|
73
|
-
type _finalizeCaseArg<t, ctx extends MatchParserContext, endpoint extends "in" | "out"> =
|
|
74
|
-
distill<t, "in">,
|
|
75
|
-
distill<t, endpoint>
|
|
76
|
-
] extends [infer i, infer result] ? [
|
|
77
|
-
i
|
|
78
|
-
] extends [ctx["input"]] ? result : Extract<ctx["input"], i> extends never ? result : Extract<ctx["input"], result> : never;
|
|
73
|
+
type _finalizeCaseArg<t, ctx extends MatchParserContext, endpoint extends "in" | "out", ctxInput = ctx["input"]> = ctxInput extends unknown ? t extends unknown ? distill<t, endpoint> extends infer result ? ctxInput extends result ? ctxInput : show<ctxInput & result> : never : never : never;
|
|
79
74
|
type CaseParser<ctx extends MatchParserContext> = <const def, ret>(def: type.validate<def, ctx["$"]>, resolve: (In: inferCaseArg<def, ctx, "out">) => ret) => ChainableMatchParser<addCasesToContext<ctx, [(In: inferCaseArg<def, ctx, "in">) => ret]>>;
|
|
80
75
|
type validateKey<key extends Key, ctx extends MatchParserContext> = ctx["key"] extends Key ? ErrorMessage<doubleAtMessage> : ctx["cases"]["length"] extends 0 ? keyof ctx["input"] extends never ? key : conform<key, keyof ctx["input"]> : ErrorMessage<chainedAtMessage>;
|
|
81
76
|
interface StringsParser<ctx extends MatchParserContext> {
|