typebox 1.1.30 → 1.1.32
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/guard/guard.d.mts +4 -0
- package/build/guard/guard.mjs +6 -0
- package/build/type/action/awaited.d.mts +5 -1
- package/build/type/action/awaited.mjs +5 -1
- package/build/type/engine/call/resolve-arguments.mjs +3 -9
- package/build/type/engine/cyclic/check.mjs +4 -7
- package/build/type/engine/evaluate/distribute.mjs +7 -17
- package/build/type/engine/instantiate.mjs +3 -7
- package/build/type/engine/object/from-union.mjs +3 -10
- package/build/type/engine/template-literal/decode.d.mts +1 -1
- package/build/type/engine/template-literal/decode.mjs +4 -10
- package/build/type/engine/template-literal/encode.mjs +3 -9
- package/build/type/engine/template-literal/is-finite.mjs +3 -7
- package/build/type/extends/extends-right.mjs +3 -2
- package/build/type/extends/inference.mjs +1 -1
- package/build/type/extends/parameters.mjs +3 -2
- package/build/type/extends/result.d.mts +0 -3
- package/build/type/extends/result.mjs +0 -3
- package/build/type/extends/tuple.mjs +3 -3
- package/build/type/extends/union.mjs +3 -2
- package/build/type/types/async-iterator.d.mts +5 -1
- package/build/type/types/async-iterator.mjs +5 -1
- package/build/type/types/iterator.d.mts +5 -1
- package/build/type/types/iterator.mjs +5 -1
- package/build/type/types/promise.d.mts +5 -1
- package/build/type/types/promise.mjs +5 -1
- package/package.json +30 -30
- package/readme.md +0 -7
package/build/guard/guard.d.mts
CHANGED
|
@@ -43,8 +43,12 @@ export declare function GraphemeCount(value: string): number;
|
|
|
43
43
|
export declare function IsMaxLength(value: string, length: number): boolean;
|
|
44
44
|
/** Returns true if the string has at least the given number of graphemes */
|
|
45
45
|
export declare function IsMinLength(value: string, length: number): boolean;
|
|
46
|
+
/** Returns true if all elements from offset satisfy the callback, short-circuiting on the first failure */
|
|
46
47
|
export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
48
|
+
/** Returns true if all elements from offset satisfy the callback, visiting every element regardless of failure */
|
|
47
49
|
export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
50
|
+
/** Takes the left-most element from an array and dispatches to the true arm, or the false arm if empty */
|
|
51
|
+
export declare function TakeLeft<T, True extends (left: T, right: T[]) => unknown, False extends () => unknown>(array: T[], true_: True, false_: False): ReturnType<True> | ReturnType<False>;
|
|
48
52
|
/** Returns true if this value has this property key */
|
|
49
53
|
export declare function HasPropertyKey<Key extends PropertyKey>(value: object, key: Key): value is {
|
|
50
54
|
[_ in Key]: unknown;
|
package/build/guard/guard.mjs
CHANGED
|
@@ -145,6 +145,7 @@ export function IsMinLength(value, length) {
|
|
|
145
145
|
// --------------------------------------------------------------------------
|
|
146
146
|
// Array
|
|
147
147
|
// --------------------------------------------------------------------------
|
|
148
|
+
/** Returns true if all elements from offset satisfy the callback, short-circuiting on the first failure */
|
|
148
149
|
export function Every(value, offset, callback) {
|
|
149
150
|
for (let index = offset; index < value.length; index++) {
|
|
150
151
|
if (!callback(value[index], index))
|
|
@@ -152,6 +153,7 @@ export function Every(value, offset, callback) {
|
|
|
152
153
|
}
|
|
153
154
|
return true;
|
|
154
155
|
}
|
|
156
|
+
/** Returns true if all elements from offset satisfy the callback, visiting every element regardless of failure */
|
|
155
157
|
export function EveryAll(value, offset, callback) {
|
|
156
158
|
let result = true;
|
|
157
159
|
for (let index = offset; index < value.length; index++) {
|
|
@@ -160,6 +162,10 @@ export function EveryAll(value, offset, callback) {
|
|
|
160
162
|
}
|
|
161
163
|
return result;
|
|
162
164
|
}
|
|
165
|
+
/** Takes the left-most element from an array and dispatches to the true arm, or the false arm if empty */
|
|
166
|
+
export function TakeLeft(array, true_, false_) {
|
|
167
|
+
return (IsEqual(array.length, 0) ? false_() : true_(array[0], array.slice(1)));
|
|
168
|
+
}
|
|
163
169
|
// --------------------------------------------------------------------------
|
|
164
170
|
// Object
|
|
165
171
|
// --------------------------------------------------------------------------
|
|
@@ -7,5 +7,9 @@ export type TAwaitedDeferred<Type extends TSchema> = (TDeferred<'Awaited', [Type
|
|
|
7
7
|
export declare function AwaitedDeferred<Type extends TSchema>(type: Type, options?: TSchemaOptions): TAwaitedDeferred<Type>;
|
|
8
8
|
/** Applies an Awaited action to a type. */
|
|
9
9
|
export type TAwaited<Type extends TSchema> = (TAwaitedAction<Type>);
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Applies an Awaited action to a type.
|
|
12
|
+
*
|
|
13
|
+
* @deprecated This action is being removed in the next version of TypeBox.
|
|
14
|
+
*/
|
|
11
15
|
export declare function Awaited<Type extends TSchema>(type: Type, options?: TSchemaOptions): TAwaited<Type>;
|
|
@@ -5,7 +5,11 @@ import { AwaitedAction } from '../engine/awaited/instantiate.mjs';
|
|
|
5
5
|
export function AwaitedDeferred(type, options = {}) {
|
|
6
6
|
return Deferred('Awaited', [type], options);
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Applies an Awaited action to a type.
|
|
10
|
+
*
|
|
11
|
+
* @deprecated This action is being removed in the next version of TypeBox.
|
|
12
|
+
*/
|
|
9
13
|
export function Awaited(type, options = {}) {
|
|
10
14
|
return AwaitedAction(type, options);
|
|
11
15
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../../guard/index.mjs';
|
|
2
3
|
import { Memory } from '../../../system/memory/index.mjs';
|
|
3
|
-
import { IsSchema } from '../../types/schema.mjs';
|
|
4
4
|
import { InstantiateType } from '../instantiate.mjs';
|
|
5
5
|
import { Extends, ExtendsResult } from '../../extends/index.mjs';
|
|
6
6
|
import { IsInfer } from '../../types/infer.mjs';
|
|
@@ -23,16 +23,10 @@ function BindArgument(context, state, name, extends_, type) {
|
|
|
23
23
|
function BindArguments(context, state, parameterLeft, parameterRight, arguments_) {
|
|
24
24
|
const instantiatedExtends = InstantiateType(context, state, parameterLeft.extends);
|
|
25
25
|
const instantiatedEquals = InstantiateType(context, state, parameterLeft.equals);
|
|
26
|
-
|
|
27
|
-
return (IsSchema(left)
|
|
28
|
-
? BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, left), state, parameterRight, right)
|
|
29
|
-
: BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, instantiatedEquals), state, parameterRight, []));
|
|
26
|
+
return Guard.TakeLeft(arguments_, (left, right) => BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, left), state, parameterRight, right), () => BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, instantiatedEquals), state, parameterRight, []));
|
|
30
27
|
}
|
|
31
28
|
function BindParameters(context, state, parameters, arguments_) {
|
|
32
|
-
|
|
33
|
-
return (IsSchema(left)
|
|
34
|
-
? BindArguments(context, state, left, right, arguments_)
|
|
35
|
-
: context);
|
|
29
|
+
return Guard.TakeLeft(parameters, (left, right) => BindArguments(context, state, left, right, arguments_), () => context);
|
|
36
30
|
}
|
|
37
31
|
export function ResolveArgumentsContext(context, state, parameters, arguments_) {
|
|
38
32
|
return BindParameters(context, state, parameters, arguments_);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
2
|
+
import { Guard } from '../../../guard/index.mjs';
|
|
3
3
|
import { IsArray } from '../../types/array.mjs';
|
|
4
4
|
import { IsAsyncIterator } from '../../types/async-iterator.mjs';
|
|
5
5
|
import { IsConstructor } from '../../types/constructor.mjs';
|
|
@@ -24,12 +24,9 @@ function FromProperties(stack, context, properties) {
|
|
|
24
24
|
return FromTypes(stack, context, types);
|
|
25
25
|
}
|
|
26
26
|
function FromTypes(stack, context, types) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
? true
|
|
31
|
-
: FromTypes(stack, context, right)
|
|
32
|
-
: false);
|
|
27
|
+
return Guard.TakeLeft(types, (left, right) => FromType(stack, context, left)
|
|
28
|
+
? true
|
|
29
|
+
: FromTypes(stack, context, right), () => false);
|
|
33
30
|
}
|
|
34
31
|
function FromType(stack, context, type) {
|
|
35
32
|
return (IsRef(type) ? FromRef(stack, context, type.$ref) :
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
// deno-fmt-ignore-file
|
|
3
3
|
import { Guard } from '../../../guard/index.mjs';
|
|
4
|
-
import { IsSchema } from '../../types/schema.mjs';
|
|
5
4
|
import { IsUnion } from '../../types/union.mjs';
|
|
6
5
|
import { IsObject } from '../../types/object.mjs';
|
|
7
6
|
import { IsTuple } from '../../types/tuple.mjs';
|
|
@@ -32,24 +31,15 @@ function DistributeOperation(left, right) {
|
|
|
32
31
|
return result;
|
|
33
32
|
}
|
|
34
33
|
function DistributeType(type, types, result = []) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
: result.length === 0
|
|
39
|
-
? [type]
|
|
40
|
-
: result);
|
|
34
|
+
return Guard.TakeLeft(types, (left, right) => DistributeType(type, right, [...result, DistributeOperation(type, left)]), () => Guard.IsEqual(result.length, 0)
|
|
35
|
+
? [type]
|
|
36
|
+
: result);
|
|
41
37
|
}
|
|
42
38
|
function DistributeUnion(types, distribution, result = []) {
|
|
43
|
-
|
|
44
|
-
return (IsSchema(left)
|
|
45
|
-
? DistributeUnion(right, distribution, [...result, ...Distribute([left], distribution)])
|
|
46
|
-
: result);
|
|
39
|
+
return Guard.TakeLeft(types, (left, right) => DistributeUnion(right, distribution, [...result, ...Distribute([left], distribution)]), () => result);
|
|
47
40
|
}
|
|
48
41
|
export function Distribute(types, result = []) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
? Distribute(right, DistributeUnion(left.anyOf, result))
|
|
53
|
-
: Distribute(right, DistributeType(left, result))
|
|
54
|
-
: result);
|
|
42
|
+
return Guard.TakeLeft(types, (left, right) => IsUnion(left)
|
|
43
|
+
? Distribute(right, DistributeUnion(left.anyOf, result))
|
|
44
|
+
: Distribute(right, DistributeType(left, result)), () => result);
|
|
55
45
|
}
|
|
@@ -11,7 +11,6 @@ import { IsReadonly, ReadonlyAdd, ReadonlyRemove } from '../types/_readonly.mjs'
|
|
|
11
11
|
// Types
|
|
12
12
|
// ------------------------------------------------------------------
|
|
13
13
|
import { IsBase } from '../types/base.mjs';
|
|
14
|
-
import { IsSchema } from '../types/schema.mjs';
|
|
15
14
|
import { Array, IsArray, ArrayOptions } from '../types/array.mjs';
|
|
16
15
|
import { AsyncIterator, IsAsyncIterator, AsyncIteratorOptions } from '../types/async-iterator.mjs';
|
|
17
16
|
import { Constructor, IsConstructor, ConstructorOptions } from '../types/constructor.mjs';
|
|
@@ -66,12 +65,9 @@ import { UncapitalizeInstantiate } from './intrinsics/instantiate.mjs';
|
|
|
66
65
|
import { UppercaseInstantiate } from './intrinsics/instantiate.mjs';
|
|
67
66
|
import { RestSpread } from './rest/index.mjs';
|
|
68
67
|
export function CanInstantiate(types) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
? false
|
|
73
|
-
: CanInstantiate(right)
|
|
74
|
-
: true);
|
|
68
|
+
return Guard.TakeLeft(types, (left, right) => IsRef(left)
|
|
69
|
+
? false
|
|
70
|
+
: CanInstantiate(right), () => true);
|
|
75
71
|
}
|
|
76
72
|
function ModifierActions(type, readonly, optional) {
|
|
77
73
|
return (IsReadonlyRemoveAction(type) ? ModifierActions(type.type, 'remove', optional) :
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import { Unreachable } from '../../../system/unreachable/index.mjs';
|
|
3
2
|
import { Guard } from '../../../guard/index.mjs';
|
|
4
|
-
import {
|
|
3
|
+
import { Unreachable } from '../../../system/unreachable/index.mjs';
|
|
5
4
|
import { EvaluateUnion } from '../evaluate/evaluate.mjs';
|
|
6
5
|
import { FromType } from './from-type.mjs';
|
|
7
6
|
function CollapseUnionProperties(left, right) {
|
|
@@ -12,15 +11,9 @@ function CollapseUnionProperties(left, right) {
|
|
|
12
11
|
return result;
|
|
13
12
|
}
|
|
14
13
|
function ReduceVariants(types, result) {
|
|
15
|
-
|
|
16
|
-
return (IsSchema(left)
|
|
17
|
-
? ReduceVariants(right, CollapseUnionProperties(result, FromType(left)))
|
|
18
|
-
: result);
|
|
14
|
+
return Guard.TakeLeft(types, (left, right) => ReduceVariants(right, CollapseUnionProperties(result, FromType(left))), () => result);
|
|
19
15
|
}
|
|
20
16
|
export function FromUnion(types) {
|
|
21
|
-
|
|
22
|
-
return (IsSchema(left)
|
|
23
|
-
? ReduceVariants(right, FromType(left))
|
|
24
|
-
: Unreachable());
|
|
17
|
+
return Guard.TakeLeft(types, (left, right) => ReduceVariants(right, FromType(left)), () => Unreachable());
|
|
25
18
|
}
|
|
26
19
|
// deno-coverage-ignore-stop
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TUnreachable } from '../../../system/unreachable/index.mjs';
|
|
2
|
-
import { type TLiteral, type TLiteralValue } from '../../types/literal.mjs';
|
|
3
2
|
import { type TSchema } from '../../types/schema.mjs';
|
|
3
|
+
import { type TLiteral, type TLiteralValue } from '../../types/literal.mjs';
|
|
4
4
|
import { type TString } from '../../types/string.mjs';
|
|
5
5
|
import { type TTemplateLiteral } from '../../types/template-literal.mjs';
|
|
6
6
|
import { type TUnion } from '../../types/union.mjs';
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import { Unreachable } from '../../../system/unreachable/index.mjs';
|
|
3
2
|
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
+
import { Unreachable } from '../../../system/unreachable/index.mjs';
|
|
4
4
|
import { Literal, IsLiteral } from '../../types/literal.mjs';
|
|
5
|
-
import { IsSchema } from '../../types/schema.mjs';
|
|
6
5
|
import { String } from '../../types/string.mjs';
|
|
7
6
|
import { IsTemplateLiteral } from '../../types/template-literal.mjs';
|
|
8
7
|
import { Union, IsUnion } from '../../types/union.mjs';
|
|
@@ -10,17 +9,13 @@ import { ParsePatternIntoTypes } from '../patterns/pattern.mjs';
|
|
|
10
9
|
import { IsTemplateLiteralFinite } from './is-finite.mjs';
|
|
11
10
|
import { TemplateLiteralCreate } from './create.mjs';
|
|
12
11
|
function FromLiteralPush(variants, value, result = []) {
|
|
13
|
-
|
|
14
|
-
return (Guard.IsString(left) ? FromLiteralPush(right, value, [...result, `${left}${value}`]) : result);
|
|
12
|
+
return Guard.TakeLeft(variants, (left, right) => FromLiteralPush(right, value, [...result, `${left}${value}`]), () => result);
|
|
15
13
|
}
|
|
16
14
|
function FromLiteral(variants, value) {
|
|
17
15
|
return (Guard.IsEqual(variants.length, 0) ? [`${value}`] : FromLiteralPush(variants, value));
|
|
18
16
|
}
|
|
19
17
|
function FromUnion(variants, types, result = []) {
|
|
20
|
-
|
|
21
|
-
return (IsSchema(left)
|
|
22
|
-
? FromUnion(variants, right, [...result, ...FromType(variants, left)])
|
|
23
|
-
: result);
|
|
18
|
+
return Guard.TakeLeft(types, (left, right) => FromUnion(variants, right, [...result, ...FromType(variants, left)]), () => result);
|
|
24
19
|
}
|
|
25
20
|
// ------------------------------------------------------------------
|
|
26
21
|
// deno-coverage-ignore-start - symmetric unreachable | internal
|
|
@@ -38,8 +33,7 @@ function FromType(variants, type) {
|
|
|
38
33
|
return result;
|
|
39
34
|
}
|
|
40
35
|
function DecodeFromSpan(variants, types) {
|
|
41
|
-
|
|
42
|
-
return (IsSchema(left) ? DecodeFromSpan(FromType(variants, left), right) : variants);
|
|
36
|
+
return Guard.TakeLeft(types, (left, right) => DecodeFromSpan(FromType(variants, left), right), () => variants);
|
|
43
37
|
}
|
|
44
38
|
function VariantsToLiterals(variants) {
|
|
45
39
|
return variants.map(variant => Literal(variant));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
2
|
+
import { Guard } from '../../../guard/index.mjs';
|
|
3
3
|
import { IsEnum } from '../../types/enum.mjs';
|
|
4
4
|
import { Literal, IsLiteral } from '../../types/literal.mjs';
|
|
5
5
|
import { Union, IsUnion } from '../../types/union.mjs';
|
|
@@ -50,10 +50,7 @@ function EncodeEnum(types, right, pattern) {
|
|
|
50
50
|
return EncodeUnion(variants, right, pattern);
|
|
51
51
|
}
|
|
52
52
|
function EncodeUnion(types, right, pattern, result = []) {
|
|
53
|
-
|
|
54
|
-
return (IsSchema(head)
|
|
55
|
-
? EncodeUnion(tail, right, pattern, [...result, EncodeType(head, [], '')])
|
|
56
|
-
: EncodeTypes(right, `${pattern}(${JoinString(result)})`));
|
|
53
|
+
return Guard.TakeLeft(types, (head, tail) => EncodeUnion(tail, right, pattern, [...result, EncodeType(head, [], '')]), () => EncodeTypes(right, `${pattern}(${JoinString(result)})`));
|
|
57
54
|
}
|
|
58
55
|
function EncodeType(type, right, pattern) {
|
|
59
56
|
return (IsEnum(type) ? EncodeEnum(type.enum, right, pattern) :
|
|
@@ -69,10 +66,7 @@ function EncodeType(type, right, pattern) {
|
|
|
69
66
|
NeverPattern);
|
|
70
67
|
}
|
|
71
68
|
function EncodeTypes(types, pattern) {
|
|
72
|
-
|
|
73
|
-
return (IsSchema(left)
|
|
74
|
-
? EncodeType(left, right, pattern)
|
|
75
|
-
: pattern);
|
|
69
|
+
return Guard.TakeLeft(types, (left, right) => EncodeType(left, right, pattern), () => pattern);
|
|
76
70
|
}
|
|
77
71
|
function EncodePattern(types) {
|
|
78
72
|
const encoded = EncodeTypes(types, '');
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
-
import { IsSchema } from '../../types/schema.mjs';
|
|
4
3
|
import { IsLiteral } from '../../types/literal.mjs';
|
|
5
4
|
import { IsUnion } from '../../types/union.mjs';
|
|
6
5
|
function FromLiteral(_value) {
|
|
7
6
|
return true;
|
|
8
7
|
}
|
|
9
8
|
function FromTypesReduce(types) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
? FromTypesReduce(right)
|
|
14
|
-
: false
|
|
15
|
-
: true);
|
|
9
|
+
return Guard.TakeLeft(types, (left, right) => FromType(left)
|
|
10
|
+
? FromTypesReduce(right)
|
|
11
|
+
: false, () => true);
|
|
16
12
|
}
|
|
17
13
|
function FromTypes(types) {
|
|
18
14
|
const result = Guard.IsEqual(types.length, 0) ? false : FromTypesReduce(types);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../guard/index.mjs';
|
|
2
3
|
import { Memory } from '../../system/memory/index.mjs';
|
|
3
4
|
import { IsAny } from '../types/any.mjs';
|
|
4
5
|
import { IsEnum } from '../types/enum.mjs';
|
|
@@ -22,14 +23,14 @@ function ExtendsRightEnum(inferred, left, right) {
|
|
|
22
23
|
return ExtendsLeft(inferred, left, union);
|
|
23
24
|
}
|
|
24
25
|
function ExtendsRightIntersect(inferred, left, right) {
|
|
25
|
-
return
|
|
26
|
+
return Guard.TakeLeft(right, (head, tail) => Result.Match(ExtendsLeft(inferred, left, head), inferred => ExtendsRightIntersect(inferred, left, tail), () => Result.ExtendsFalse()), () => Result.ExtendsTrue(inferred));
|
|
26
27
|
}
|
|
27
28
|
function ExtendsRightTemplateLiteral(inferred, left, right) {
|
|
28
29
|
const decoded = TemplateLiteralDecode(right);
|
|
29
30
|
return ExtendsLeft(inferred, left, decoded);
|
|
30
31
|
}
|
|
31
32
|
function ExtendsRightUnion(inferred, left, right) {
|
|
32
|
-
return
|
|
33
|
+
return Guard.TakeLeft(right, (head, tail) => Result.Match(ExtendsLeft(inferred, left, head), inferred => Result.ExtendsTrue(inferred), () => ExtendsRightUnion(inferred, left, tail)), () => Result.ExtendsFalse());
|
|
33
34
|
}
|
|
34
35
|
export function ExtendsRight(inferred, left, right) {
|
|
35
36
|
return (IsAny(right) ? ExtendsRightAny(inferred, left) :
|
|
@@ -46,7 +46,7 @@ export function TryInferable(type) {
|
|
|
46
46
|
undefined);
|
|
47
47
|
}
|
|
48
48
|
function TryInferResults(rest, right, result = []) {
|
|
49
|
-
return
|
|
49
|
+
return Guard.TakeLeft(rest, (head, tail) => Result.Match(ExtendsLeft({}, head, right), () => TryInferResults(tail, right, [...result, head]), () => undefined), () => result);
|
|
50
50
|
}
|
|
51
51
|
export function InferTupleResult(inferred, name, left, right) {
|
|
52
52
|
const results = TryInferResults(left, right);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../guard/index.mjs';
|
|
2
3
|
import { IsInfer } from '../types/infer.mjs';
|
|
3
4
|
import { IsOptional } from '../types/_optional.mjs';
|
|
4
5
|
import { ExtendsLeft } from './extends-left.mjs';
|
|
@@ -15,12 +16,12 @@ function ParameterCompare(inferred, left, leftRest, right, rightRest) {
|
|
|
15
16
|
);
|
|
16
17
|
}
|
|
17
18
|
function ParameterRight(inferred, left, leftRest, rightRest) {
|
|
18
|
-
return
|
|
19
|
+
return Guard.TakeLeft(rightRest, (head, tail) => ParameterCompare(inferred, left, leftRest, head, tail), () => IsOptional(left) // 'right-did-not-have-enough-elements'
|
|
19
20
|
? Result.ExtendsTrue(inferred) // 'ok: left was optional'
|
|
20
21
|
: Result.ExtendsFalse()); // 'fail: left was required'
|
|
21
22
|
}
|
|
22
23
|
function ParametersLeft(inferred, left, rightRest) {
|
|
23
|
-
return
|
|
24
|
+
return Guard.TakeLeft(left, (head, tail) => ParameterRight(inferred, head, tail, rightRest), () => Result.ExtendsTrue(inferred)); // 'ok: no-more-elements-in-left'
|
|
24
25
|
}
|
|
25
26
|
export function ExtendsParameters(inferred, left, right) {
|
|
26
27
|
return ParametersLeft(inferred, left, right);
|
|
@@ -22,6 +22,3 @@ export declare function IsExtendsTrueLike(value: unknown): value is TExtendsTrue
|
|
|
22
22
|
export type MatchTrueLike = (inferred: TProperties) => unknown;
|
|
23
23
|
export type MatchFalse = () => unknown;
|
|
24
24
|
export declare function Match(result: TResult, true_: MatchTrueLike, false_: MatchFalse): unknown;
|
|
25
|
-
export type TakeLeftTrue<T> = (left: T, right: T[]) => unknown;
|
|
26
|
-
export type TakeLeftFalse = () => unknown;
|
|
27
|
-
export declare function TakeLeft<T>(array: T[], true_: TakeLeftTrue<T>, false_: TakeLeftFalse): unknown;
|
|
@@ -34,6 +34,3 @@ export function IsExtendsTrueLike(value) {
|
|
|
34
34
|
export function Match(result, true_, false_) {
|
|
35
35
|
return IsExtendsTrueLike(result) ? true_(result.inferred) : false_();
|
|
36
36
|
}
|
|
37
|
-
export function TakeLeft(array, true_, false_) {
|
|
38
|
-
return Guard.IsEqual(array.length, 0) ? false_() : true_(array[0], array.slice(1));
|
|
39
|
-
}
|
|
@@ -31,10 +31,10 @@ function ElementsLeft(inferred, reversed, leftRest, right, rightRest) {
|
|
|
31
31
|
// Rest Inferrable Right Means we delegate to TInferTupleResult to Generate a Result
|
|
32
32
|
IsInferable(inferable)
|
|
33
33
|
? InferTupleResult(inferred, inferable['name'], ApplyReverse(leftRest, reversed), inferable['type'])
|
|
34
|
-
:
|
|
34
|
+
: Guard.TakeLeft(leftRest, (head, tail) => ElementsCompare(inferred, reversed, head, tail, right, rightRest), () => Result.ExtendsFalse()));
|
|
35
35
|
}
|
|
36
36
|
function ElementsRight(inferred, reversed, leftRest, rightRest) {
|
|
37
|
-
return
|
|
37
|
+
return Guard.TakeLeft(rightRest, (head, tail) => ElementsLeft(inferred, reversed, leftRest, head, tail), () => Guard.IsEqual(leftRest.length, 0)
|
|
38
38
|
? Result.ExtendsTrue(inferred) // 'Ok: right-empty-and-left-empty'
|
|
39
39
|
: Result.ExtendsFalse()); // 'Fail: right-empty-and-left-not-empty'
|
|
40
40
|
}
|
|
@@ -50,7 +50,7 @@ function ExtendsTupleToArray(inferred, left, right) {
|
|
|
50
50
|
const inferrable = TryInferable(right);
|
|
51
51
|
return (IsInferable(inferrable)
|
|
52
52
|
? InferUnionResult(inferred, inferrable['name'], left, inferrable['type'])
|
|
53
|
-
:
|
|
53
|
+
: Guard.TakeLeft(left, (head, tail) => Result.Match(ExtendsLeft(inferred, head, right), inferred => ExtendsTupleToArray(inferred, tail, right), () => Result.ExtendsFalse()), () => Result.ExtendsTrue(inferred)));
|
|
54
54
|
}
|
|
55
55
|
export function ExtendsTuple(inferred, left, right) {
|
|
56
56
|
const instantiatedLeft = InstantiateElements(inferred, { callstack: [] }, left);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../guard/index.mjs';
|
|
2
3
|
import { IsUnion } from '../types/union.mjs';
|
|
3
4
|
import { ExtendsLeft } from './extends-left.mjs';
|
|
4
5
|
import * as Result from './result.mjs';
|
|
@@ -7,10 +8,10 @@ import * as Result from './result.mjs';
|
|
|
7
8
|
// ----------------------------------------------------------------------------
|
|
8
9
|
import { IsInferable, TryInferable, InferUnionResult } from './inference.mjs';
|
|
9
10
|
function ExtendsUnionSome(inferred, type, unionTypes) {
|
|
10
|
-
return
|
|
11
|
+
return Guard.TakeLeft(unionTypes, (head, tail) => Result.Match(ExtendsLeft(inferred, type, head), inferred => Result.ExtendsTrue(inferred), () => ExtendsUnionSome(inferred, type, tail)), () => Result.ExtendsFalse());
|
|
11
12
|
}
|
|
12
13
|
function ExtendsUnionLeft(inferred, left, right) {
|
|
13
|
-
return
|
|
14
|
+
return Guard.TakeLeft(left, (head, tail) => Result.Match(ExtendsUnionSome(inferred, head, right), inferred => ExtendsUnionLeft(inferred, tail, right), () => Result.ExtendsFalse()), () => Result.ExtendsTrue(inferred));
|
|
14
15
|
}
|
|
15
16
|
export function ExtendsUnion(inferred, left, right) {
|
|
16
17
|
const inferrable = TryInferable(right);
|
|
@@ -8,7 +8,11 @@ export interface TAsyncIterator<Type extends TSchema = TSchema> extends TSchema
|
|
|
8
8
|
type: 'asyncIterator';
|
|
9
9
|
iteratorItems: Type;
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Creates a AsyncIterator type.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
|
|
15
|
+
*/
|
|
12
16
|
export declare function AsyncIterator<Type extends TSchema>(iteratorItems: Type, options?: TSchemaOptions): TAsyncIterator<Type>;
|
|
13
17
|
/** Returns true if the given value is a TAsyncIterator */
|
|
14
18
|
export declare function IsAsyncIterator(value: unknown): value is TAsyncIterator;
|
|
@@ -4,7 +4,11 @@ import { IsKind } from './schema.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
// Factory
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Creates a AsyncIterator type.
|
|
9
|
+
*
|
|
10
|
+
* @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
|
|
11
|
+
*/
|
|
8
12
|
export function AsyncIterator(iteratorItems, options) {
|
|
9
13
|
return Memory.Create({ '~kind': 'AsyncIterator' }, { type: 'asyncIterator', iteratorItems }, options);
|
|
10
14
|
}
|
|
@@ -8,7 +8,11 @@ export interface TIterator<Type extends TSchema = TSchema> extends TSchema {
|
|
|
8
8
|
type: 'iterator';
|
|
9
9
|
iteratorItems: Type;
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Iterator type.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
|
|
15
|
+
*/
|
|
12
16
|
export declare function Iterator<Type extends TSchema>(iteratorItems: Type, options?: TSchemaOptions): TIterator<Type>;
|
|
13
17
|
/** Returns true if the given value is TIterator. */
|
|
14
18
|
export declare function IsIterator(value: unknown): value is TIterator;
|
|
@@ -4,7 +4,11 @@ import { IsKind } from './schema.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
// Factory
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Iterator type.
|
|
9
|
+
*
|
|
10
|
+
* @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
|
|
11
|
+
*/
|
|
8
12
|
export function Iterator(iteratorItems, options) {
|
|
9
13
|
return Memory.Create({ '~kind': 'Iterator' }, { type: 'iterator', iteratorItems }, options);
|
|
10
14
|
}
|
|
@@ -8,7 +8,11 @@ export interface TPromise<Type extends TSchema = TSchema> extends TSchema {
|
|
|
8
8
|
type: 'promise';
|
|
9
9
|
item: Type;
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Promise type.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
|
|
15
|
+
*/
|
|
12
16
|
export declare function _Promise_<Type extends TSchema>(item: Type, options?: TSchemaOptions): TPromise<Type>;
|
|
13
17
|
export { _Promise_ as Promise };
|
|
14
18
|
/** Returns true if the given type is TPromise. */
|
|
@@ -4,7 +4,11 @@ import { IsKind } from './schema.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
// Factory
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Promise type.
|
|
9
|
+
*
|
|
10
|
+
* @deprecated This type is being removed in the next version of TypeBox. A fallback will be provided under examples.
|
|
11
|
+
*/
|
|
8
12
|
export function _Promise_(item, options) {
|
|
9
13
|
return Memory.Create({ ['~kind']: 'Promise' }, { type: 'promise', item }, options);
|
|
10
14
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typebox",
|
|
3
3
|
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.32",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
7
|
"jsonschema"
|
|
@@ -16,37 +16,37 @@
|
|
|
16
16
|
"types": "./build/index.d.mts",
|
|
17
17
|
"module": "./build/index.mjs",
|
|
18
18
|
"exports": {
|
|
19
|
-
"./value": {
|
|
20
|
-
"import": "./build/value/index.mjs",
|
|
21
|
-
"default": "./build/value/index.mjs"
|
|
22
|
-
},
|
|
23
19
|
"./guard": {
|
|
24
20
|
"import": "./build/guard/index.mjs",
|
|
25
21
|
"default": "./build/guard/index.mjs"
|
|
26
22
|
},
|
|
27
|
-
"./
|
|
28
|
-
"import": "./build/
|
|
29
|
-
"default": "./build/
|
|
23
|
+
"./error": {
|
|
24
|
+
"import": "./build/error/index.mjs",
|
|
25
|
+
"default": "./build/error/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./compile": {
|
|
28
|
+
"import": "./build/compile/index.mjs",
|
|
29
|
+
"default": "./build/compile/index.mjs"
|
|
30
30
|
},
|
|
31
31
|
"./system": {
|
|
32
32
|
"import": "./build/system/index.mjs",
|
|
33
33
|
"default": "./build/system/index.mjs"
|
|
34
34
|
},
|
|
35
|
+
"./format": {
|
|
36
|
+
"import": "./build/format/index.mjs",
|
|
37
|
+
"default": "./build/format/index.mjs"
|
|
38
|
+
},
|
|
35
39
|
"./type": {
|
|
36
40
|
"import": "./build/type/index.mjs",
|
|
37
41
|
"default": "./build/type/index.mjs"
|
|
38
42
|
},
|
|
39
|
-
"./
|
|
40
|
-
"import": "./build/
|
|
41
|
-
"default": "./build/
|
|
42
|
-
},
|
|
43
|
-
"./error": {
|
|
44
|
-
"import": "./build/error/index.mjs",
|
|
45
|
-
"default": "./build/error/index.mjs"
|
|
43
|
+
"./schema": {
|
|
44
|
+
"import": "./build/schema/index.mjs",
|
|
45
|
+
"default": "./build/schema/index.mjs"
|
|
46
46
|
},
|
|
47
|
-
"./
|
|
48
|
-
"import": "./build/
|
|
49
|
-
"default": "./build/
|
|
47
|
+
"./value": {
|
|
48
|
+
"import": "./build/value/index.mjs",
|
|
49
|
+
"default": "./build/value/index.mjs"
|
|
50
50
|
},
|
|
51
51
|
".": {
|
|
52
52
|
"import": "./build/index.mjs",
|
|
@@ -55,29 +55,29 @@
|
|
|
55
55
|
},
|
|
56
56
|
"typesVersions": {
|
|
57
57
|
"*": {
|
|
58
|
-
"value": [
|
|
59
|
-
"./build/value/index.d.mts"
|
|
60
|
-
],
|
|
61
58
|
"guard": [
|
|
62
59
|
"./build/guard/index.d.mts"
|
|
63
60
|
],
|
|
64
|
-
"
|
|
65
|
-
"./build/
|
|
61
|
+
"error": [
|
|
62
|
+
"./build/error/index.d.mts"
|
|
63
|
+
],
|
|
64
|
+
"compile": [
|
|
65
|
+
"./build/compile/index.d.mts"
|
|
66
66
|
],
|
|
67
67
|
"system": [
|
|
68
68
|
"./build/system/index.d.mts"
|
|
69
69
|
],
|
|
70
|
+
"format": [
|
|
71
|
+
"./build/format/index.d.mts"
|
|
72
|
+
],
|
|
70
73
|
"type": [
|
|
71
74
|
"./build/type/index.d.mts"
|
|
72
75
|
],
|
|
73
|
-
"
|
|
74
|
-
"./build/
|
|
75
|
-
],
|
|
76
|
-
"error": [
|
|
77
|
-
"./build/error/index.d.mts"
|
|
76
|
+
"schema": [
|
|
77
|
+
"./build/schema/index.d.mts"
|
|
78
78
|
],
|
|
79
|
-
"
|
|
80
|
-
"./build/
|
|
79
|
+
"value": [
|
|
80
|
+
"./build/value/index.d.mts"
|
|
81
81
|
],
|
|
82
82
|
".": [
|
|
83
83
|
"./build/index.d.mts"
|
package/readme.md
CHANGED
|
@@ -80,7 +80,6 @@ The following creates a User type and infers with Static.
|
|
|
80
80
|
```typescript
|
|
81
81
|
import Type from 'typebox'
|
|
82
82
|
|
|
83
|
-
|
|
84
83
|
// Type
|
|
85
84
|
|
|
86
85
|
const User = Type.Object({ // const User = {
|
|
@@ -100,7 +99,6 @@ const User = Type.Object({ // const User = {
|
|
|
100
99
|
// ]
|
|
101
100
|
// }
|
|
102
101
|
|
|
103
|
-
|
|
104
102
|
// Static
|
|
105
103
|
|
|
106
104
|
type User = Type.Static<typeof User> // type User = {
|
|
@@ -124,7 +122,6 @@ The following uses the Script function to parse TypeScript interfaces into JSON
|
|
|
124
122
|
```typescript
|
|
125
123
|
import Type from 'typebox'
|
|
126
124
|
|
|
127
|
-
|
|
128
125
|
// Script
|
|
129
126
|
|
|
130
127
|
const { User, UserUpdate } = Type.Script(`
|
|
@@ -145,7 +142,6 @@ const { User, UserUpdate } = Type.Script(`
|
|
|
145
142
|
|
|
146
143
|
`)
|
|
147
144
|
|
|
148
|
-
|
|
149
145
|
// Reflect
|
|
150
146
|
|
|
151
147
|
console.log(User) // {
|
|
@@ -172,7 +168,6 @@ console.log(UserUpdate) // {
|
|
|
172
168
|
// required: ['id']
|
|
173
169
|
// }
|
|
174
170
|
|
|
175
|
-
|
|
176
171
|
// Static
|
|
177
172
|
|
|
178
173
|
type User = Type.Static<typeof User> // type User = {
|
|
@@ -205,7 +200,6 @@ The following uses the Schema submodule to compile a TypeBox type.
|
|
|
205
200
|
```typescript
|
|
206
201
|
import Schema from 'typebox/schema'
|
|
207
202
|
|
|
208
|
-
|
|
209
203
|
// Compile
|
|
210
204
|
|
|
211
205
|
const User = Schema.Compile(Type.Object({ // const User = Validator<Type.TObject<{
|
|
@@ -214,7 +208,6 @@ const User = Schema.Compile(Type.Object({ // const User = Validator<Type.
|
|
|
214
208
|
email: Type.String({ format: 'email' }) // email: Type.TString;
|
|
215
209
|
})) // }>, { ... }>
|
|
216
210
|
|
|
217
|
-
|
|
218
211
|
// Parse
|
|
219
212
|
|
|
220
213
|
const user = User.Parse({ // const user: {
|