Skip to content

Commit b4f42c8

Browse files
authored
Version 1.1.32 (#1586)
* Guard TakeLeft Function * ChangeLog * Version
1 parent 6de45fd commit b4f42c8

21 files changed

Lines changed: 141 additions & 150 deletions

File tree

changelog/1.1.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Version Updates
6+
- [Revision 1.1.32](https://github.com/sinclairzx81/typebox/pull/1586)
7+
- TakeLeft Guard Function
68
- [Revision 1.1.31](https://github.com/sinclairzx81/typebox/pull/1585)
79
- Flag Awaited, Promise, Iterator and AsyncIterator for Deprecation
810
- [Revision 1.1.30](https://github.com/sinclairzx81/typebox/pull/1584)

src/guard/guard.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,25 @@ export function IsMinLength(value: string, length: number): boolean {
167167
// --------------------------------------------------------------------------
168168
// Array
169169
// --------------------------------------------------------------------------
170+
/** Returns true if all elements from offset satisfy the callback, short-circuiting on the first failure */
170171
export function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean {
171172
for (let index = offset; index < value.length; index++) {
172173
if (!callback(value[index], index)) return false
173174
}
174175
return true
175176
}
177+
/** Returns true if all elements from offset satisfy the callback, visiting every element regardless of failure */
176178
export function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean {
177179
let result = true
178180
for (let index = offset; index < value.length; index++) {
179181
if (!callback(value[index], index)) result = false
180182
}
181183
return result
182184
}
185+
/** Takes the left-most element from an array and dispatches to the true arm, or the false arm if empty */
186+
export function TakeLeft<T, True extends (left: T, right: T[]) => unknown, False extends () => unknown>(array: T[], true_: True, false_: False): ReturnType<True> | ReturnType<False> {
187+
return (IsEqual(array.length, 0) ? false_() : true_(array[0], array.slice(1))) as never
188+
}
183189
// --------------------------------------------------------------------------
184190
// Object
185191
// --------------------------------------------------------------------------

src/type/engine/call/resolve-arguments.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31+
import { Guard } from '../../../guard/index.ts'
3132
import { Memory } from '../../../system/memory/index.ts'
32-
import { type TSchema, IsSchema } from '../../types/schema.ts'
33+
import { type TSchema } from '../../types/schema.ts'
3334
import { type TParameter } from '../../types/parameter.ts'
3435
import { type TProperties } from '../../types/properties.ts'
3536
import { type TState, type TInstantiateType, InstantiateType } from '../instantiate.ts'
@@ -76,11 +77,9 @@ function BindArguments<Context extends TProperties, State extends TState, Parame
7677
TBindArguments<Context, State, ParameterLeft, ParameterRight, Arguments> {
7778
const instantiatedExtends = InstantiateType(context, state, parameterLeft.extends)
7879
const instantiatedEquals = InstantiateType(context, state, parameterLeft.equals)
79-
const [left, ...right] = arguments_
80-
return (
81-
IsSchema(left)
82-
? BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, left), state, parameterRight, right)
83-
: BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, instantiatedEquals), state, parameterRight, [])
80+
return Guard.TakeLeft(arguments_, (left, right) =>
81+
BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, left), state, parameterRight, right),
82+
() => BindParameters(BindArgument(context, state, parameterLeft['name'], instantiatedExtends, instantiatedEquals), state, parameterRight, [])
8483
) as never
8584
}
8685
// ------------------------------------------------------------------
@@ -94,11 +93,9 @@ type TBindParameters<Context extends TProperties, State extends TState, Paramete
9493
function BindParameters<Context extends TProperties, State extends TState, Parameters extends TParameter[], Arguments extends TSchema[]>
9594
(context: Context, state: State, parameters: [...Parameters], arguments_: [...Arguments]):
9695
TBindParameters<Context, State, Parameters, Arguments> {
97-
const [left, ...right] = parameters
98-
return (
99-
IsSchema(left)
100-
? BindArguments(context, state, left, right, arguments_)
101-
: context
96+
return Guard.TakeLeft(parameters, (left, right) =>
97+
BindArguments(context, state, left, right, arguments_),
98+
() => context
10299
) as never
103100
}
104101
// ------------------------------------------------------------------

src/type/engine/cyclic/check.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { type TSchema, IsSchema } from '../../types/schema.ts'
31+
import { Guard } from '../../../guard/index.ts'
32+
import { type TSchema } from '../../types/schema.ts'
3233
import { type TArray, IsArray } from '../../types/array.ts'
3334
import { type TAsyncIterator, IsAsyncIterator } from '../../types/async-iterator.ts'
3435
import { type TConstructor, IsConstructor } from '../../types/constructor.ts'
@@ -81,21 +82,20 @@ function FromProperties<Stack extends string[], Context extends TProperties, Pro
8182
type TFromTypes<Stack extends string[], Context extends TProperties, Types extends TSchema[]> =
8283
Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
8384
? TFromType<Stack, Context, Left> extends true
84-
? true
85-
: TFromTypes<Stack, Context, Right>
85+
? true
86+
: TFromTypes<Stack, Context, Right>
8687
: false
8788

8889
function FromTypes<Stack extends string[], Context extends TProperties, Types extends TSchema[]>
8990
(stack: [...Stack], context: Context, types: [...Types]):
9091
TFromTypes<Stack, Context, Types> {
91-
const [left, ...right] = types
92-
return (
93-
IsSchema(left)
94-
? FromType(stack, context, left)
95-
? true
96-
: FromTypes(stack, context, right)
97-
: false
92+
return Guard.TakeLeft(types, (left, right) =>
93+
FromType(stack, context, left)
94+
? true
95+
: FromTypes(stack, context, right),
96+
() => false
9897
) as never
98+
9999
}
100100
// ------------------------------------------------------------------
101101
// Type

src/type/engine/evaluate/distribute.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ THE SOFTWARE.
3030
// deno-fmt-ignore-file
3131

3232
import { Guard } from '../../../guard/index.ts'
33-
import { type TSchema, IsSchema } from '../../types/schema.ts'
33+
import { type TSchema } from '../../types/schema.ts'
3434
import { type TUnion, IsUnion } from '../../types/union.ts'
3535
import { type TObject, IsObject } from '../../types/object.ts'
3636
import { type TTuple, IsTuple } from '../../types/tuple.ts'
@@ -105,14 +105,11 @@ type TDistributeType<Type extends TSchema, Distribution extends TSchema[], Resul
105105
: Result
106106
)
107107
function DistributeType<Type extends TSchema, Distribution extends TSchema[]>(type: Type, types: [...Distribution], result: TSchema[] = []): TDistributeType<Type, Distribution> {
108-
const [left, ...right] = types
109-
return (
110-
!Guard.IsUndefined(left) // TSchema[]
111-
? DistributeType(type, right, [...result, DistributeOperation(type, left)])
112-
: result.length === 0
113-
? [type]
114-
: result
115-
) as never
108+
return Guard.TakeLeft(types, (left, right) =>
109+
DistributeType(type, right, [...result, DistributeOperation(type, left)]),
110+
() => Guard.IsEqual(result.length, 0)
111+
? [type]
112+
: result) as never
116113
}
117114
// -----------------------------------------------------------------------------------------
118115
// DistributeUnion
@@ -123,12 +120,9 @@ type TDistributeUnion<Types extends TSchema[], Distribution extends TSchema[], R
123120
: Result
124121
)
125122
function DistributeUnion<Types extends TSchema[], Distribution extends TSchema[]>(types: [...Types], distribution: [...Distribution], result: TSchema[] = []): TDistributeUnion<Types, Distribution> {
126-
const [left, ...right] = types
127-
return (
128-
IsSchema(left)
129-
? DistributeUnion(right, distribution, [...result, ...Distribute([left], distribution)])
130-
: result
131-
) as never
123+
return Guard.TakeLeft(types, (left, right) =>
124+
DistributeUnion(right, distribution, [...result, ...Distribute([left], distribution)]),
125+
() => result) as never
132126
}
133127
// -----------------------------------------------------------------------------------------
134128
// Distribute
@@ -141,12 +135,9 @@ export type TDistribute<Types extends TSchema[], Result extends TSchema[] = []>
141135
: Result
142136
)
143137
export function Distribute<Types extends TSchema[]>(types: [...Types], result: TSchema[] = []): TDistribute<Types> {
144-
const [left, ...right] = types
145-
return (
146-
IsSchema(left)
147-
? IsUnion(left)
148-
? Distribute(right, DistributeUnion(left.anyOf, result))
149-
: Distribute(right, DistributeType(left, result))
150-
: result
151-
) as never
138+
return Guard.TakeLeft(types, (left, right) =>
139+
IsUnion(left)
140+
? Distribute(right, DistributeUnion(left.anyOf, result))
141+
: Distribute(right, DistributeType(left, result)),
142+
() => result) as never
152143
}

src/type/engine/instantiate.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ export type TCanInstantiate<Types extends TSchema[]> =
118118
: TCanInstantiate<Right>
119119
: true
120120
export function CanInstantiate<Types extends TSchema[]>(types: [...Types]): TCanInstantiate<Types> {
121-
const [left, ...right] = types
122-
return (
123-
IsSchema(left)
124-
? IsRef(left)
125-
? false
126-
: CanInstantiate(right)
127-
: true
121+
return Guard.TakeLeft(types, (left, right) =>
122+
IsRef(left)
123+
? false
124+
: CanInstantiate(right),
125+
() => true
128126
) as never
129127
}
130128
// ------------------------------------------------------------------

src/type/engine/object/from-union.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { type TUnreachable, Unreachable } from '../../../system/unreachable/index.ts'
3231
import { Guard } from '../../../guard/index.ts'
33-
import { type TSchema, IsSchema } from '../../types/schema.ts'
32+
import { type TUnreachable, Unreachable } from '../../../system/unreachable/index.ts'
33+
import { type TSchema } from '../../types/schema.ts'
3434
import { type TProperties } from '../../types/properties.ts'
3535
import { type TEvaluateUnion, EvaluateUnion } from '../evaluate/evaluate.ts'
3636
import { type TFromType, FromType } from './from-type.ts'
@@ -45,8 +45,8 @@ type TCollapseUnionProperties<Left extends TProperties, Right extends TPropertie
4545
}
4646
> = Result
4747
function CollapseUnionProperties<Left extends TProperties, Right extends TProperties>
48-
(left: Left, right: Right):
49-
TCollapseUnionProperties<Left, Right> {
48+
(left: Left, right: Right):
49+
TCollapseUnionProperties<Left, Right> {
5050
const sharedKeys = Guard.Keys(left).filter((key) => key in right)
5151
const result = sharedKeys.reduce((result, key) => {
5252
return { ...result, [key]: EvaluateUnion([left[key], right[key]]) }
@@ -63,13 +63,10 @@ type TReduceVariants<Types extends TSchema[], Result extends TProperties> = (
6363
)
6464
function ReduceVariants<Types extends TSchema[], Result extends TProperties>
6565
(types: [...Types], result: Result):
66-
TReduceVariants<Types, Result> {
67-
const [left, ...right] = types
68-
return (
69-
IsSchema(left)
70-
? ReduceVariants(right, CollapseUnionProperties(result, FromType(left)))
71-
: result
72-
) as never
66+
TReduceVariants<Types, Result> {
67+
return Guard.TakeLeft(types, (left, right) =>
68+
ReduceVariants(right, CollapseUnionProperties(result, FromType(left))),
69+
() => result) as never
7370
}
7471
// ------------------------------------------------------------------
7572
// FromUnion
@@ -86,13 +83,10 @@ export type TFromUnion<Types extends TSchema[]> = (
8683
: TUnreachable
8784
)
8885
export function FromUnion<Types extends TSchema[]>
89-
(types: [...Types]):
90-
TFromUnion<Types> {
91-
const [left, ...right] = types
92-
return (
93-
IsSchema(left)
94-
? ReduceVariants(right, FromType(left))
95-
: Unreachable()
96-
) as never
86+
(types: [...Types]):
87+
TFromUnion<Types> {
88+
return Guard.TakeLeft(types, (left, right) =>
89+
ReduceVariants(right, FromType(left)),
90+
() => Unreachable()) as never
9791
}
9892
// deno-coverage-ignore-stop

src/type/engine/template-literal/decode.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { TUnreachable, Unreachable } from '../../../system/unreachable/index.ts'
3231
import { Guard } from '../../../guard/index.ts'
32+
import { TUnreachable, Unreachable } from '../../../system/unreachable/index.ts'
3333

34+
import { type TSchema } from '../../types/schema.ts'
3435
import { type TLiteral, type TLiteralValue, Literal, IsLiteral } from '../../types/literal.ts'
35-
import { type TSchema, IsSchema } from '../../types/schema.ts'
3636
import { type TString, String } from '../../types/string.ts'
3737
import { type TTemplateLiteral, IsTemplateLiteral } from '../../types/template-literal.ts'
3838
import { type TUnion, Union, IsUnion } from '../../types/union.ts'
@@ -44,14 +44,15 @@ import { TemplateLiteralCreate } from './create.ts'
4444
// ------------------------------------------------------------------
4545
// FromLiteral
4646
// ------------------------------------------------------------------
47-
type TFromLiteralPush<Variants extends string[], Value extends TLiteralValue, Result extends string[] = []> =
47+
type TFromLiteralPush<Variants extends string[], Value extends TLiteralValue, Result extends string[] = []> =
4848
Variants extends [infer Left extends string, ...infer Right extends string[]]
49-
? TFromLiteralPush<Right, Value, [...Result, `${Left}${Value}`]>
50-
: Result
49+
? TFromLiteralPush<Right, Value, [...Result, `${Left}${Value}`]>
50+
: Result
5151

5252
function FromLiteralPush<Variants extends string[], Value extends TLiteralValue>(variants: [...Variants], value: Value, result: string[] = []): TFromLiteralPush<Variants, Value> {
53-
const [left, ...right] = variants
54-
return (Guard.IsString(left) ? FromLiteralPush(right, value, [...result, `${left}${value}`]): result) as never
53+
return Guard.TakeLeft(variants, (left, right) =>
54+
FromLiteralPush(right, value, [...result, `${left}${value}`]),
55+
() => result) as never
5556
}
5657
type TFromLiteral<Variants extends string[], Value extends TLiteralValue> =
5758
Variants extends [] ? [`${Value}`] : TFromLiteralPush<Variants, Value>
@@ -64,14 +65,12 @@ function FromLiteral<Variants extends string[], Value extends TLiteralValue>(var
6465
// ------------------------------------------------------------------
6566
type TFromUnion<Variants extends string[], Types extends TSchema[], Result extends string[] = []> =
6667
Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
67-
? TFromUnion<Variants, Right, [...Result, ...TFromType<Variants, Left>]>
68-
: Result
68+
? TFromUnion<Variants, Right, [...Result, ...TFromType<Variants, Left>]>
69+
: Result
6970
function FromUnion<Variants extends string[], Types extends TSchema[]>(variants: [...Variants], types: [...Types], result: string[] = []): TFromUnion<Variants, Types> {
70-
const [left, ...right] = types
71-
return (
72-
IsSchema(left)
73-
? FromUnion(variants, right, [...result, ...FromType(variants, left)])
74-
: result
71+
return Guard.TakeLeft(types, (left, right) =>
72+
FromUnion(variants, right, [...result, ...FromType(variants, left)]),
73+
() => result
7574
) as never
7675
}
7776
// ------------------------------------------------------------------
@@ -106,23 +105,22 @@ function FromType<Variants extends string[], Type extends TSchema>(variants: [..
106105
// ------------------------------------------------------------------
107106
// FromSpan
108107
// ------------------------------------------------------------------
109-
type TDecodeFromSpan<Variants extends string[], Types extends TSchema[]> =
108+
type TDecodeFromSpan<Variants extends string[], Types extends TSchema[]> =
110109
Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]]
111-
? TDecodeFromSpan<TFromType<Variants, Left>, Right>
112-
: Variants
110+
? TDecodeFromSpan<TFromType<Variants, Left>, Right>
111+
: Variants
113112
function DecodeFromSpan<Variants extends string[], Types extends TSchema[]>(variants: [...Variants], types: [...Types]): TDecodeFromSpan<Variants, Types> {
114-
const [left, ...right] = types
115-
return (
116-
IsSchema(left) ? DecodeFromSpan(FromType(variants, left) as string[], right) : variants
117-
) as never
113+
return Guard.TakeLeft(types, (left, right) =>
114+
DecodeFromSpan(FromType(variants, left) as string[], right),
115+
() => variants) as never
118116
}
119117
// ------------------------------------------------------------------
120118
// VariantsToLiterals
121119
// ------------------------------------------------------------------
122-
type TVariantsToLiterals<Variants extends string[], Result extends TSchema[] = []> =
120+
type TVariantsToLiterals<Variants extends string[], Result extends TSchema[] = []> =
123121
Variants extends [infer Left extends string, ...infer Right extends string[]]
124-
? TVariantsToLiterals<Right, [...Result, TLiteral<Left>]>
125-
: Result
122+
? TVariantsToLiterals<Right, [...Result, TLiteral<Left>]>
123+
: Result
126124
function VariantsToLiterals<Variants extends string[]>(variants: [...Variants]): TVariantsToLiterals<Variants> {
127125
return variants.map(variant => Literal(variant)) as never
128126
}
@@ -167,7 +165,7 @@ function DecodeTypes<Types extends TSchema[]>(types: [...Types]): TDecodeTypes<T
167165
Guard.IsEqual(types.length, 1) && IsLiteral(types[0]) ? types[0] :
168166
DecodeTypesAsUnion(types)
169167
) as never
170-
}
168+
}
171169
// deno-coverage-ignore-stop
172170
// ------------------------------------------------------------------
173171
// TemplateLiteralDecodeUnsafe
@@ -177,10 +175,10 @@ export type TTemplateLiteralDecodeUnsafe<Pattern extends string,
177175
Types extends TSchema[] = TParsePatternIntoTypes<Pattern>,
178176
Result extends TSchema = (
179177
Types extends [] // Failed to Parse | IsTemplateLiteralPattern
180-
? TString
181-
: TIsTemplateLiteralFinite<Types> extends true
182-
? TDecodeTypes<Types>
183-
: TTemplateLiteral<Pattern>
178+
? TString
179+
: TIsTemplateLiteralFinite<Types> extends true
180+
? TDecodeTypes<Types>
181+
: TTemplateLiteral<Pattern>
184182
)
185183
> = Result
186184
/**
@@ -193,7 +191,7 @@ export function TemplateLiteralDecodeUnsafe<Pattern extends string>(pattern: Pat
193191
const types = ParsePatternIntoTypes(pattern)
194192
const result = Guard.IsEqual(types.length, 0) // Failed to Parse | IsTemplateLiteralPattern
195193
? String() // ... Pattern cannot be typed, so discard
196-
: IsTemplateLiteralFinite(types)
194+
: IsTemplateLiteralFinite(types)
197195
? DecodeTypes(types)
198196
: TemplateLiteralCreate(pattern)
199197
return result as never

0 commit comments

Comments
 (0)