@sinclair/typebox 0.32.0-dev-20 → 0.32.0-dev-21
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/build/import/compiler/compiler.mjs +62 -62
- package/build/import/errors/errors.mjs +62 -62
- package/build/import/index.d.mts +4 -4
- package/build/import/index.mjs +1 -1
- package/build/import/type/indexed/indexed-property-keys.d.mts +2 -2
- package/build/import/type/indexed/indexed-property-keys.mjs +6 -11
- package/build/import/type/intrinsic/intrinsic.mjs +3 -3
- package/build/import/type/record/record.d.mts +2 -2
- package/build/import/type/record/record.mjs +2 -3
- package/build/import/type/schema/schema.d.mts +1 -1
- package/build/import/type/symbol/symbol.d.mts +1 -1
- package/build/import/type/template-literal/finite.d.mts +7 -5
- package/build/import/type/template-literal/finite.mjs +14 -5
- package/build/import/type/template-literal/generate.d.mts +14 -8
- package/build/import/type/template-literal/generate.mjs +19 -9
- package/build/import/type/template-literal/index.d.mts +1 -1
- package/build/import/type/template-literal/index.mjs +1 -1
- package/build/import/type/template-literal/{parser.mjs → parse.mjs} +7 -2
- package/build/import/type/template-literal/syntax.d.mts +1 -1
- package/build/import/type/template-literal/syntax.mjs +0 -1
- package/build/import/type/template-literal/template-literal.d.mts +2 -2
- package/build/import/type/template-literal/template-literal.mjs +1 -1
- package/build/import/type/template-literal/union.d.mts +2 -2
- package/build/import/type/template-literal/union.mjs +5 -10
- package/build/import/type/type/json.d.mts +2 -2
- package/build/import/value/cast/cast.mjs +20 -20
- package/build/import/value/check/check.mjs +62 -62
- package/build/import/value/clean/clean.mjs +16 -16
- package/build/import/value/convert/convert.mjs +36 -36
- package/build/import/value/create/create.mjs +66 -67
- package/build/import/value/default/default.mjs +16 -16
- package/build/import/value/transform/decode.mjs +18 -18
- package/build/import/value/transform/encode.mjs +18 -18
- package/build/import/value/transform/has.mjs +28 -28
- package/build/require/compiler/compiler.js +62 -62
- package/build/require/errors/errors.js +62 -62
- package/build/require/index.d.ts +4 -4
- package/build/require/index.js +4 -2
- package/build/require/type/indexed/indexed-property-keys.d.ts +2 -2
- package/build/require/type/indexed/indexed-property-keys.js +4 -9
- package/build/require/type/intrinsic/intrinsic.js +2 -2
- package/build/require/type/record/record.d.ts +2 -2
- package/build/require/type/record/record.js +1 -2
- package/build/require/type/schema/schema.d.ts +1 -1
- package/build/require/type/symbol/symbol.d.ts +1 -1
- package/build/require/type/template-literal/finite.d.ts +7 -5
- package/build/require/type/template-literal/finite.js +16 -6
- package/build/require/type/template-literal/generate.d.ts +14 -8
- package/build/require/type/template-literal/generate.js +22 -11
- package/build/require/type/template-literal/index.d.ts +1 -1
- package/build/require/type/template-literal/index.js +1 -1
- package/build/require/type/template-literal/{parser.js → parse.js} +7 -2
- package/build/require/type/template-literal/syntax.d.ts +1 -1
- package/build/require/type/template-literal/syntax.js +0 -1
- package/build/require/type/template-literal/template-literal.d.ts +2 -2
- package/build/require/type/template-literal/template-literal.js +1 -1
- package/build/require/type/template-literal/union.d.ts +2 -2
- package/build/require/type/template-literal/union.js +5 -10
- package/build/require/type/type/json.d.ts +2 -2
- package/build/require/value/cast/cast.js +20 -20
- package/build/require/value/check/check.js +62 -62
- package/build/require/value/clean/clean.js +16 -16
- package/build/require/value/convert/convert.js +36 -36
- package/build/require/value/create/create.js +65 -66
- package/build/require/value/default/default.js +16 -16
- package/build/require/value/transform/decode.js +18 -18
- package/build/require/value/transform/encode.js +18 -18
- package/build/require/value/transform/has.js +28 -28
- package/package.json +1 -1
- package/readme.md +47 -47
- /package/build/import/type/template-literal/{parser.d.mts → parse.d.mts} +0 -0
- /package/build/require/type/template-literal/{parser.d.ts → parse.d.ts} +0 -0
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TemplateLiteralGenerate } from '../template-literal/index.mjs';
|
|
2
2
|
// ------------------------------------------------------------------
|
|
3
3
|
// TypeGuard
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
// prettier-ignore
|
|
6
|
-
import { IsTemplateLiteral,
|
|
6
|
+
import { IsTemplateLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../guard/type.mjs';
|
|
7
7
|
// prettier-ignore
|
|
8
8
|
function FromTemplateLiteral(T) {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const S = TemplateLiteralToUnion(T);
|
|
12
|
-
return (F === true
|
|
13
|
-
? IsUnionLiteral(S)
|
|
14
|
-
? S.anyOf.map(S => S.const.toString())
|
|
15
|
-
: []
|
|
16
|
-
: []);
|
|
9
|
+
const R = TemplateLiteralGenerate(T);
|
|
10
|
+
return R.map(S => S.toString());
|
|
17
11
|
}
|
|
18
12
|
// prettier-ignore
|
|
19
13
|
function FromUnion(T) {
|
|
@@ -24,7 +18,8 @@ function FromUnion(T) {
|
|
|
24
18
|
}
|
|
25
19
|
// prettier-ignore
|
|
26
20
|
function FromLiteral(T) {
|
|
27
|
-
return ([T.toString()])
|
|
21
|
+
return ([T.toString()] // TS 5.4 observes TLiteralValue as not having a toString()
|
|
22
|
+
);
|
|
28
23
|
}
|
|
29
24
|
/** Returns a tuple of PropertyKeys derived from the given TSchema */
|
|
30
25
|
// prettier-ignore
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TemplateLiteral, TemplateLiteralParseExact,
|
|
1
|
+
import { TemplateLiteral, TemplateLiteralParseExact, IsTemplateLiteralExpressionFinite, TemplateLiteralExpressionGenerate } from '../template-literal/index.mjs';
|
|
2
2
|
import { IntrinsicFromMappedKey } from './intrinsic-from-mapped-key.mjs';
|
|
3
3
|
import { Literal } from '../literal/index.mjs';
|
|
4
4
|
import { Union } from '../union/index.mjs';
|
|
@@ -28,10 +28,10 @@ function FromTemplateLiteral(schema, mode, options) {
|
|
|
28
28
|
// note: template literals require special runtime handling as they are encoded in string patterns.
|
|
29
29
|
// This diverges from the mapped type which would otherwise map on the template literal kind.
|
|
30
30
|
const expression = TemplateLiteralParseExact(schema.pattern);
|
|
31
|
-
const finite =
|
|
31
|
+
const finite = IsTemplateLiteralExpressionFinite(expression);
|
|
32
32
|
if (!finite)
|
|
33
33
|
return { ...schema, pattern: FromLiteralValue(schema.pattern, mode) };
|
|
34
|
-
const strings = [...
|
|
34
|
+
const strings = [...TemplateLiteralExpressionGenerate(expression)];
|
|
35
35
|
const literals = strings.map((value) => Literal(value));
|
|
36
36
|
const mapped = FromRest(literals, mode);
|
|
37
37
|
const union = Union(mapped);
|
|
@@ -9,13 +9,13 @@ import { type TString } from '../string/index.mjs';
|
|
|
9
9
|
import { type TInteger } from '../integer/index.mjs';
|
|
10
10
|
import { type TNumber } from '../number/index.mjs';
|
|
11
11
|
import { type TEnum } from '../enum/index.mjs';
|
|
12
|
-
import { TTemplateLiteral,
|
|
12
|
+
import { TTemplateLiteral, TIsTemplateLiteralFinite } from '../template-literal/index.mjs';
|
|
13
13
|
import { Kind } from '../symbols/index.mjs';
|
|
14
14
|
type FromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
|
|
15
15
|
type FromTemplateLiteralKeyFinite<K extends TTemplateLiteral, T extends TSchema, I extends string = Static<K>> = (Ensure<TObject<Evaluate<{
|
|
16
16
|
[_ in I]: T;
|
|
17
17
|
}>>>);
|
|
18
|
-
type FromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema> =
|
|
18
|
+
type FromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema> = TIsTemplateLiteralFinite<K> extends false ? FromTemplateLiteralKeyInfinite<K, T> : FromTemplateLiteralKeyFinite<K, T>;
|
|
19
19
|
declare function FromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema>(K: K, T: T, options: ObjectOptions): FromTemplateLiteralKey<K, T>;
|
|
20
20
|
type FromEnumKey<K extends Record<string, string | number>, T extends TSchema> = Ensure<TObject<{
|
|
21
21
|
[_ in K[keyof K]]: T;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Object } from '../object/index.mjs';
|
|
2
2
|
import { Never } from '../never/index.mjs';
|
|
3
3
|
import { Union } from '../union/index.mjs';
|
|
4
|
-
import { IsTemplateLiteralFinite
|
|
4
|
+
import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
|
|
5
5
|
import { PatternStringExact, PatternNumberExact } from '../patterns/index.mjs';
|
|
6
6
|
import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
7
7
|
import { Kind, Hint } from '../symbols/index.mjs';
|
|
@@ -34,8 +34,7 @@ function RecordCreateFromKeys(K, T, options) {
|
|
|
34
34
|
}
|
|
35
35
|
// prettier-ignore
|
|
36
36
|
function FromTemplateLiteralKey(K, T, options) {
|
|
37
|
-
|
|
38
|
-
return (IsTemplateLiteralFinite(expression)
|
|
37
|
+
return (IsTemplateLiteralFinite(K)
|
|
39
38
|
? RecordCreateFromKeys(IndexPropertyKeys(K), T, options)
|
|
40
39
|
: RecordCreateFromPattern(K.pattern, T, options));
|
|
41
40
|
}
|
|
@@ -20,7 +20,7 @@ export interface SchemaOptions {
|
|
|
20
20
|
export interface TKind {
|
|
21
21
|
[Kind]: string;
|
|
22
22
|
}
|
|
23
|
-
export interface TSchema extends
|
|
23
|
+
export interface TSchema extends TKind, SchemaOptions {
|
|
24
24
|
[ReadonlyKind]?: string;
|
|
25
25
|
[OptionalKind]?: string;
|
|
26
26
|
[Hint]?: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TSchema, SchemaOptions } from '../schema/index.mjs';
|
|
2
2
|
import { Kind } from '../symbols/index.mjs';
|
|
3
|
-
export type
|
|
3
|
+
export type TSymbolValue = string | number | undefined;
|
|
4
4
|
export interface TSymbol extends TSchema, SchemaOptions {
|
|
5
5
|
[Kind]: 'Symbol';
|
|
6
6
|
static: symbol;
|
|
@@ -6,11 +6,13 @@ import type { TNumber } from '../number/index.mjs';
|
|
|
6
6
|
import type { TInteger } from '../integer/index.mjs';
|
|
7
7
|
import type { TBigInt } from '../bigint/index.mjs';
|
|
8
8
|
import type { TLiteral } from '../literal/index.mjs';
|
|
9
|
-
import type { Expression } from './
|
|
9
|
+
import type { Expression } from './parse.mjs';
|
|
10
10
|
export declare class TemplateLiteralFiniteError extends Error {
|
|
11
11
|
}
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
export
|
|
15
|
-
export
|
|
12
|
+
type FromTemplateLiteralKind<T> = T extends TTemplateLiteral<infer U extends TTemplateLiteralKind[]> ? FromTemplateLiteralKinds<U> : T extends TUnion<infer U extends TTemplateLiteralKind[]> ? FromTemplateLiteralKinds<U> : T extends TString ? false : T extends TNumber ? false : T extends TInteger ? false : T extends TBigInt ? false : T extends TBoolean ? true : T extends TLiteral ? true : false;
|
|
13
|
+
type FromTemplateLiteralKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? FromTemplateLiteralKind<L> extends false ? false : FromTemplateLiteralKinds<R> : true;
|
|
14
|
+
export declare function IsTemplateLiteralExpressionFinite(expression: Expression): boolean;
|
|
15
|
+
export type TIsTemplateLiteralFinite<T> = T extends TTemplateLiteral<infer U> ? FromTemplateLiteralKinds<U> : false;
|
|
16
|
+
/** Returns true if this TemplateLiteral resolves to a finite set of values */
|
|
17
|
+
export declare function IsTemplateLiteralFinite<T extends TTemplateLiteral>(schema: T): boolean;
|
|
16
18
|
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TemplateLiteralParseExact } from './parse.mjs';
|
|
1
2
|
// ------------------------------------------------------------------
|
|
2
3
|
// TemplateLiteralFiniteError
|
|
3
4
|
// ------------------------------------------------------------------
|
|
@@ -28,12 +29,20 @@ function IsBooleanExpression(expression) {
|
|
|
28
29
|
function IsStringExpression(expression) {
|
|
29
30
|
return expression.type === 'const' && expression.const === '.*';
|
|
30
31
|
}
|
|
32
|
+
// ------------------------------------------------------------------
|
|
33
|
+
// IsTemplateLiteralExpressionFinite
|
|
34
|
+
// ------------------------------------------------------------------
|
|
31
35
|
// prettier-ignore
|
|
32
|
-
export function
|
|
33
|
-
return (
|
|
34
|
-
|
|
35
|
-
(expression.type === 'and') ? expression.expr.every((expr) =>
|
|
36
|
-
(expression.type === 'or') ? expression.expr.every((expr) =>
|
|
36
|
+
export function IsTemplateLiteralExpressionFinite(expression) {
|
|
37
|
+
return (IsNumberExpression(expression) || IsStringExpression(expression) ? false :
|
|
38
|
+
IsBooleanExpression(expression) ? true :
|
|
39
|
+
(expression.type === 'and') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
|
|
40
|
+
(expression.type === 'or') ? expression.expr.every((expr) => IsTemplateLiteralExpressionFinite(expr)) :
|
|
37
41
|
(expression.type === 'const') ? true :
|
|
38
42
|
(() => { throw new TemplateLiteralFiniteError(`Unknown expression type`); })());
|
|
39
43
|
}
|
|
44
|
+
/** Returns true if this TemplateLiteral resolves to a finite set of values */
|
|
45
|
+
export function IsTemplateLiteralFinite(schema) {
|
|
46
|
+
const expression = TemplateLiteralParseExact(schema.pattern);
|
|
47
|
+
return IsTemplateLiteralExpressionFinite(expression);
|
|
48
|
+
}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
|
+
import { TIsTemplateLiteralFinite } from './finite.mjs';
|
|
2
|
+
import type { Assert } from '../helpers/index.mjs';
|
|
3
|
+
import type { TBoolean } from '../boolean/index.mjs';
|
|
1
4
|
import type { TTemplateLiteral, TTemplateLiteralKind } from './index.mjs';
|
|
2
5
|
import type { TLiteral, TLiteralValue } from '../literal/index.mjs';
|
|
3
|
-
import type { Expression } from './
|
|
6
|
+
import type { Expression } from './parse.mjs';
|
|
4
7
|
import type { TUnion } from '../union/index.mjs';
|
|
8
|
+
export declare class TemplateLiteralGenerateError extends Error {
|
|
9
|
+
}
|
|
5
10
|
type StringReduceUnary<L extends string, R extends string[]> = R extends [infer A extends string, ...infer B extends string[]] ? [`${L}${A}`, ...StringReduceUnary<L, B>] : [];
|
|
6
11
|
type StringReduceBinary<L extends string[], R extends string[]> = L extends [infer A extends string, ...infer B extends string[]] ? [...StringReduceUnary<A, R>, ...StringReduceBinary<B, R>] : [];
|
|
7
12
|
type StringReduceMany<T extends string[][]> = T extends [infer L extends string[], infer R extends string[], ...infer Rest extends string[][]] ? StringReduceMany<[StringReduceBinary<L, R>, ...Rest]> : T;
|
|
8
|
-
type StringReduce<T extends string[][], O = StringReduceMany<T>> = 0 extends keyof O ? O[0] : [];
|
|
9
|
-
type
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export type
|
|
14
|
-
|
|
13
|
+
type StringReduce<T extends string[][], O = StringReduceMany<T>> = 0 extends keyof O ? Assert<O[0], string[]> : [];
|
|
14
|
+
type FromTemplateLiteralUnionKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TLiteral, ...infer R extends TLiteral[]] ? [L['const'], ...FromTemplateLiteralUnionKinds<R>] : [];
|
|
15
|
+
type FromTemplateLiteralKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? (L extends TLiteral<infer S extends TLiteralValue> ? [[S], ...FromTemplateLiteralKinds<R>] : L extends TUnion<infer S extends TTemplateLiteralKind[]> ? [FromTemplateLiteralUnionKinds<S>, ...FromTemplateLiteralKinds<R>] : L extends TBoolean ? [['true', 'false'], ...FromTemplateLiteralKinds<R>] : [
|
|
16
|
+
]) : [];
|
|
17
|
+
export declare function TemplateLiteralExpressionGenerate(expression: Expression): IterableIterator<string>;
|
|
18
|
+
export type TTemplateLiteralGenerate<T extends TTemplateLiteral, F = TIsTemplateLiteralFinite<T>> = F extends true ? (T extends TTemplateLiteral<infer S extends TTemplateLiteralKind[]> ? FromTemplateLiteralKinds<S> extends infer R extends string[][] ? StringReduce<R> : [] : []) : [];
|
|
19
|
+
/** Generates a tuple of strings from the given TemplateLiteral. Returns an empty tuple if infinite. */
|
|
20
|
+
export declare function TemplateLiteralGenerate<T extends TTemplateLiteral>(schema: T): TTemplateLiteralGenerate<T>;
|
|
15
21
|
export {};
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import { IsTemplateLiteralExpressionFinite } from './finite.mjs';
|
|
2
|
+
import { TemplateLiteralParseExact } from './parse.mjs';
|
|
1
3
|
// ------------------------------------------------------------------
|
|
2
|
-
//
|
|
4
|
+
// TemplateLiteralGenerateError
|
|
5
|
+
// ------------------------------------------------------------------
|
|
6
|
+
export class TemplateLiteralGenerateError extends Error {
|
|
7
|
+
}
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
// TemplateLiteralExpressionGenerate
|
|
3
10
|
// ------------------------------------------------------------------
|
|
4
11
|
// prettier-ignore
|
|
5
12
|
function* GenerateReduce(buffer) {
|
|
@@ -13,23 +20,18 @@ function* GenerateReduce(buffer) {
|
|
|
13
20
|
}
|
|
14
21
|
// prettier-ignore
|
|
15
22
|
function* GenerateAnd(expression) {
|
|
16
|
-
return yield* GenerateReduce(expression.expr.map((expr) => [...
|
|
23
|
+
return yield* GenerateReduce(expression.expr.map((expr) => [...TemplateLiteralExpressionGenerate(expr)]));
|
|
17
24
|
}
|
|
18
25
|
// prettier-ignore
|
|
19
26
|
function* GenerateOr(expression) {
|
|
20
27
|
for (const expr of expression.expr)
|
|
21
|
-
yield*
|
|
28
|
+
yield* TemplateLiteralExpressionGenerate(expr);
|
|
22
29
|
}
|
|
23
30
|
// prettier-ignore
|
|
24
31
|
function* GenerateConst(expression) {
|
|
25
32
|
return yield expression.const;
|
|
26
33
|
}
|
|
27
|
-
|
|
28
|
-
// TemplateLiteralGenerateError
|
|
29
|
-
// ------------------------------------------------------------------
|
|
30
|
-
export class TemplateLiteralGenerateError extends Error {
|
|
31
|
-
}
|
|
32
|
-
export function* TemplateLiteralGenerate(expression) {
|
|
34
|
+
export function* TemplateLiteralExpressionGenerate(expression) {
|
|
33
35
|
return expression.type === 'and'
|
|
34
36
|
? yield* GenerateAnd(expression)
|
|
35
37
|
: expression.type === 'or'
|
|
@@ -40,3 +42,11 @@ export function* TemplateLiteralGenerate(expression) {
|
|
|
40
42
|
throw new TemplateLiteralGenerateError('Unknown expression');
|
|
41
43
|
})();
|
|
42
44
|
}
|
|
45
|
+
/** Generates a tuple of strings from the given TemplateLiteral. Returns an empty tuple if infinite. */
|
|
46
|
+
export function TemplateLiteralGenerate(schema) {
|
|
47
|
+
const expression = TemplateLiteralParseExact(schema.pattern);
|
|
48
|
+
// prettier-ignore
|
|
49
|
+
return (IsTemplateLiteralExpressionFinite(expression)
|
|
50
|
+
? [...TemplateLiteralExpressionGenerate(expression)]
|
|
51
|
+
: []);
|
|
52
|
+
}
|
|
@@ -127,16 +127,21 @@ function And(pattern) {
|
|
|
127
127
|
(expressions.length === 1) ? expressions[0] :
|
|
128
128
|
{ type: 'and', expr: expressions });
|
|
129
129
|
}
|
|
130
|
+
// ------------------------------------------------------------------
|
|
131
|
+
// TemplateLiteralParse
|
|
132
|
+
// ------------------------------------------------------------------
|
|
130
133
|
/** Parses a pattern and returns an expression tree */
|
|
131
|
-
// prettier-ignore
|
|
132
134
|
export function TemplateLiteralParse(pattern) {
|
|
135
|
+
// prettier-ignore
|
|
133
136
|
return (IsGroup(pattern) ? TemplateLiteralParse(InGroup(pattern)) :
|
|
134
137
|
IsPrecedenceOr(pattern) ? Or(pattern) :
|
|
135
138
|
IsPrecedenceAnd(pattern) ? And(pattern) :
|
|
136
139
|
{ type: 'const', const: pattern });
|
|
137
140
|
}
|
|
141
|
+
// ------------------------------------------------------------------
|
|
142
|
+
// TemplateLiteralParseExact
|
|
143
|
+
// ------------------------------------------------------------------
|
|
138
144
|
/** Parses a pattern and strips forward and trailing ^ and $ */
|
|
139
|
-
// prettier-ignore
|
|
140
145
|
export function TemplateLiteralParseExact(pattern) {
|
|
141
146
|
return TemplateLiteralParse(pattern.slice(1, pattern.length - 1));
|
|
142
147
|
}
|
|
@@ -14,7 +14,7 @@ type FromUnion<T extends string> = UnionEvaluated<FromUnionLiteral<T>>;
|
|
|
14
14
|
type FromTerminal<T extends string> = T extends 'boolean' ? TBoolean : T extends 'bigint' ? TBigInt : T extends 'number' ? TNumber : T extends 'string' ? TString : FromUnion<T>;
|
|
15
15
|
type FromString<T extends string> = T extends `{${infer L}}${infer R}` ? [FromTerminal<L>, ...FromString<R>] : T extends `${infer L}$${infer R}` ? [TLiteral<L>, ...FromString<R>] : T extends `${infer L}` ? [TLiteral<L>] : [
|
|
16
16
|
];
|
|
17
|
-
export type
|
|
17
|
+
export type TTemplateLiteralSyntax<T extends string> = (TTemplateLiteral<Assert<FromString<T>, TTemplateLiteralKind[]>>);
|
|
18
18
|
/** Parses TemplateLiteralSyntax and returns a tuple of TemplateLiteralKinds */
|
|
19
19
|
export declare function TemplateLiteralSyntax(syntax: string): TTemplateLiteralKind[];
|
|
20
20
|
export {};
|
|
@@ -9,7 +9,7 @@ import type { TString } from '../string/index.mjs';
|
|
|
9
9
|
import type { TBoolean } from '../boolean/index.mjs';
|
|
10
10
|
import type { TNever } from '../never/index.mjs';
|
|
11
11
|
import type { Static } from '../static/index.mjs';
|
|
12
|
-
import {
|
|
12
|
+
import { TTemplateLiteralSyntax } from './syntax.mjs';
|
|
13
13
|
import { EmptyString } from '../helpers/index.mjs';
|
|
14
14
|
import { Kind } from '../symbols/index.mjs';
|
|
15
15
|
type TemplateLiteralStaticKind<T, Acc extends string> = T extends TUnion<infer U> ? {
|
|
@@ -24,7 +24,7 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
|
|
|
24
24
|
pattern: string;
|
|
25
25
|
}
|
|
26
26
|
/** `[Json]` Creates a TemplateLiteral type from template dsl string */
|
|
27
|
-
export declare function TemplateLiteral<T extends string>(syntax: T, options?: SchemaOptions):
|
|
27
|
+
export declare function TemplateLiteral<T extends string>(syntax: T, options?: SchemaOptions): TTemplateLiteralSyntax<T>;
|
|
28
28
|
/** `[Json]` Creates a TemplateLiteral type */
|
|
29
29
|
export declare function TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
|
|
30
30
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TemplateLiteralPattern } from './pattern.mjs';
|
|
2
1
|
import { TemplateLiteralSyntax } from './syntax.mjs';
|
|
2
|
+
import { TemplateLiteralPattern } from './pattern.mjs';
|
|
3
3
|
import { IsString } from '../guard/value.mjs';
|
|
4
4
|
import { Kind } from '../symbols/index.mjs';
|
|
5
5
|
/** `[Json]` Creates a TemplateLiteral type */
|
|
@@ -3,5 +3,5 @@ import { type TUnion } from '../union/index.mjs';
|
|
|
3
3
|
import { type TLiteral } from '../literal/index.mjs';
|
|
4
4
|
import { type TString } from '../string/index.mjs';
|
|
5
5
|
import { type TNever } from '../never/index.mjs';
|
|
6
|
-
/**
|
|
7
|
-
export declare function TemplateLiteralToUnion(
|
|
6
|
+
/** Returns a Union from the given TemplateLiteral */
|
|
7
|
+
export declare function TemplateLiteralToUnion(schema: TTemplateLiteral): TNever | TString | TUnion<TLiteral[]>;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import { Union } from '../union/index.mjs';
|
|
2
2
|
import { Literal } from '../literal/index.mjs';
|
|
3
|
-
import { String } from '../string/index.mjs';
|
|
4
3
|
import { TemplateLiteralGenerate } from './generate.mjs';
|
|
5
|
-
import { TemplateLiteralParseExact } from './parser.mjs';
|
|
6
|
-
import { IsTemplateLiteralFinite } from './finite.mjs';
|
|
7
4
|
// ------------------------------------------------------------------
|
|
8
5
|
// TemplateLiteralToUnion
|
|
9
6
|
// ------------------------------------------------------------------
|
|
10
|
-
/**
|
|
11
|
-
export function TemplateLiteralToUnion(
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const literals = [...TemplateLiteralGenerate(expression)].map((value) => Literal(value));
|
|
16
|
-
return Union(literals);
|
|
7
|
+
/** Returns a Union from the given TemplateLiteral */
|
|
8
|
+
export function TemplateLiteralToUnion(schema) {
|
|
9
|
+
const R = TemplateLiteralGenerate(schema);
|
|
10
|
+
const L = R.map((S) => Literal(S));
|
|
11
|
+
return Union(L);
|
|
17
12
|
}
|
|
@@ -34,7 +34,7 @@ import { type TRequired, type TRequiredFromMappedResult } from '../required/inde
|
|
|
34
34
|
import { type TRest } from '../rest/index.mjs';
|
|
35
35
|
import { type TSchema, type SchemaOptions } from '../schema/index.mjs';
|
|
36
36
|
import { type TString, type StringOptions } from '../string/index.mjs';
|
|
37
|
-
import { type TTemplateLiteral, type TTemplateLiteralKind, type
|
|
37
|
+
import { type TTemplateLiteral, type TTemplateLiteralKind, type TTemplateLiteralSyntax } from '../template-literal/index.mjs';
|
|
38
38
|
import { TransformDecodeBuilder } from '../transform/index.mjs';
|
|
39
39
|
import { type TTuple } from '../tuple/index.mjs';
|
|
40
40
|
import { Union } from '../union/index.mjs';
|
|
@@ -157,7 +157,7 @@ export declare class JsonTypeBuilder {
|
|
|
157
157
|
/** `[Json]` Creates a String type */
|
|
158
158
|
String(options?: StringOptions): TString;
|
|
159
159
|
/** `[Json]` Creates a TemplateLiteral type from template dsl string */
|
|
160
|
-
TemplateLiteral<T extends string>(syntax: T, options?: SchemaOptions):
|
|
160
|
+
TemplateLiteral<T extends string>(syntax: T, options?: SchemaOptions): TTemplateLiteralSyntax<T>;
|
|
161
161
|
/** `[Json]` Creates a TemplateLiteral type */
|
|
162
162
|
TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
|
|
163
163
|
/** `[Json]` Creates a Transform type */
|
|
@@ -94,7 +94,7 @@ function Default(schema, references, value) {
|
|
|
94
94
|
// ------------------------------------------------------------------
|
|
95
95
|
// Cast
|
|
96
96
|
// ------------------------------------------------------------------
|
|
97
|
-
function
|
|
97
|
+
function FromArray(schema, references, value) {
|
|
98
98
|
if (Check(schema, references, value))
|
|
99
99
|
return Clone(value);
|
|
100
100
|
const created = IsArray(value) ? Clone(value) : Create(schema, references);
|
|
@@ -108,7 +108,7 @@ function TArray(schema, references, value) {
|
|
|
108
108
|
throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
|
|
109
109
|
return unique;
|
|
110
110
|
}
|
|
111
|
-
function
|
|
111
|
+
function FromConstructor(schema, references, value) {
|
|
112
112
|
if (Check(schema, references, value))
|
|
113
113
|
return Create(schema, references);
|
|
114
114
|
const required = new Set(schema.returns.required || []);
|
|
@@ -120,15 +120,15 @@ function TConstructor(schema, references, value) {
|
|
|
120
120
|
}
|
|
121
121
|
return result;
|
|
122
122
|
}
|
|
123
|
-
function
|
|
123
|
+
function FromIntersect(schema, references, value) {
|
|
124
124
|
const created = Create(schema, references);
|
|
125
125
|
const mapped = IsPlainObject(created) && IsPlainObject(value) ? { ...created, ...value } : value;
|
|
126
126
|
return Check(schema, references, mapped) ? mapped : Create(schema, references);
|
|
127
127
|
}
|
|
128
|
-
function
|
|
128
|
+
function FromNever(schema, references, value) {
|
|
129
129
|
throw new ValueCastNeverTypeError(schema);
|
|
130
130
|
}
|
|
131
|
-
function
|
|
131
|
+
function FromObject(schema, references, value) {
|
|
132
132
|
if (Check(schema, references, value))
|
|
133
133
|
return value;
|
|
134
134
|
if (value === null || typeof value !== 'object')
|
|
@@ -151,7 +151,7 @@ function TObject(schema, references, value) {
|
|
|
151
151
|
}
|
|
152
152
|
return result;
|
|
153
153
|
}
|
|
154
|
-
function
|
|
154
|
+
function FromRecord(schema, references, value) {
|
|
155
155
|
if (Check(schema, references, value))
|
|
156
156
|
return Clone(value);
|
|
157
157
|
if (value === null || typeof value !== 'object' || Array.isArray(value) || value instanceof Date)
|
|
@@ -164,13 +164,13 @@ function TRecord(schema, references, value) {
|
|
|
164
164
|
}
|
|
165
165
|
return result;
|
|
166
166
|
}
|
|
167
|
-
function
|
|
167
|
+
function FromRef(schema, references, value) {
|
|
168
168
|
return Visit(Deref(schema, references), references, value);
|
|
169
169
|
}
|
|
170
|
-
function
|
|
170
|
+
function FromThis(schema, references, value) {
|
|
171
171
|
return Visit(Deref(schema, references), references, value);
|
|
172
172
|
}
|
|
173
|
-
function
|
|
173
|
+
function FromTuple(schema, references, value) {
|
|
174
174
|
if (Check(schema, references, value))
|
|
175
175
|
return Clone(value);
|
|
176
176
|
if (!IsArray(value))
|
|
@@ -179,7 +179,7 @@ function TTuple(schema, references, value) {
|
|
|
179
179
|
return [];
|
|
180
180
|
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
181
181
|
}
|
|
182
|
-
function
|
|
182
|
+
function FromUnion(schema, references, value) {
|
|
183
183
|
return Check(schema, references, value) ? Clone(value) : CastUnion(schema, references, value);
|
|
184
184
|
}
|
|
185
185
|
function Visit(schema, references, value) {
|
|
@@ -190,25 +190,25 @@ function Visit(schema, references, value) {
|
|
|
190
190
|
// Structural
|
|
191
191
|
// --------------------------------------------------------------
|
|
192
192
|
case 'Array':
|
|
193
|
-
return
|
|
193
|
+
return FromArray(schema_, references_, value);
|
|
194
194
|
case 'Constructor':
|
|
195
|
-
return
|
|
195
|
+
return FromConstructor(schema_, references_, value);
|
|
196
196
|
case 'Intersect':
|
|
197
|
-
return
|
|
197
|
+
return FromIntersect(schema_, references_, value);
|
|
198
198
|
case 'Never':
|
|
199
|
-
return
|
|
199
|
+
return FromNever(schema_, references_, value);
|
|
200
200
|
case 'Object':
|
|
201
|
-
return
|
|
201
|
+
return FromObject(schema_, references_, value);
|
|
202
202
|
case 'Record':
|
|
203
|
-
return
|
|
203
|
+
return FromRecord(schema_, references_, value);
|
|
204
204
|
case 'Ref':
|
|
205
|
-
return
|
|
205
|
+
return FromRef(schema_, references_, value);
|
|
206
206
|
case 'This':
|
|
207
|
-
return
|
|
207
|
+
return FromThis(schema_, references_, value);
|
|
208
208
|
case 'Tuple':
|
|
209
|
-
return
|
|
209
|
+
return FromTuple(schema_, references_, value);
|
|
210
210
|
case 'Union':
|
|
211
|
-
return
|
|
211
|
+
return FromUnion(schema_, references_, value);
|
|
212
212
|
// --------------------------------------------------------------
|
|
213
213
|
// DefaultClone
|
|
214
214
|
// --------------------------------------------------------------
|