Skip to content

Commit

Permalink
Revision 0.32.11 (#738)
Browse files Browse the repository at this point in the history
* Optimize Extract and Exclude

* Version
  • Loading branch information
sinclairzx81 authored Jan 20, 2024
1 parent 0c0da45 commit cc06871
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 65 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.32.10",
"version": "0.32.11",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export { ConstructorParameters, type TConstructorParameters } from './type/const
export { Date, type TDate, type DateOptions } from './type/date/index'
export { Deref, type TDeref } from './type/deref/index'
export { Enum, type TEnum } from './type/enum/index'
export { Exclude, type TExclude, type TExcludeFromMappedResult } from './type/exclude/index'
export { Exclude, type TExclude, type TExcludeFromMappedResult, type TExcludeFromTemplateLiteral } from './type/exclude/index'
export { Extends, ExtendsCheck, ExtendsResult, ExtendsUndefinedCheck, type TExtends, type ExtendsFromMappedResult, type ExtendsFromMappedKey } from './type/extends/index'
export { Extract, type TExtract, type TExtractFromMappedResult } from './type/extract/index'
export { Extract, type TExtract, type TExtractFromMappedResult, type TExtractFromTemplateLiteral } from './type/extract/index'
export { Function, type TFunction } from './type/function/index'
export {
Index,
Expand Down Expand Up @@ -136,13 +136,15 @@ export {
TemplateLiteralGenerate,
TemplateLiteralParse,
TemplateLiteralParseExact,
TemplateLiteralToUnion,
IsTemplateLiteralFinite,
TemplateLiteralExpressionGenerate,
IsTemplateLiteralExpressionFinite,
type TTemplateLiteral,
type TTemplateLiteralSyntax,
type TTemplateLiteralGenerate,
type TTemplateLiteralKind,
type TTemplateLiteralToUnion,
type TIsTemplateLiteralFinite,
} from './type/template-literal/index'
export { Transform, TransformDecodeBuilder, TransformEncodeBuilder, type TTransform, type TransformOptions, type TransformFunction } from './type/transform/index'
Expand Down
14 changes: 7 additions & 7 deletions src/type/exclude/exclude-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import type { TSchema } from '../schema/index'
import type { TProperties } from '../object/index'
import { MappedResult, type TMappedResult } from '../mapped/index'
import { Exclude, type TExclude } from './exclude'
Expand All @@ -45,9 +45,9 @@ type TFromProperties<
function FromProperties<
P extends TProperties,
T extends TSchema
>(P: P, U: T, options: SchemaOptions): TFromProperties<P, T> {
>(P: P, U: T): TFromProperties<P, T> {
return globalThis.Object.getOwnPropertyNames(P).reduce((Acc, K2) => {
return {...Acc, [K2]: Exclude(P[K2], U, options) }
return {...Acc, [K2]: Exclude(P[K2], U) }
}, {}) as TFromProperties<P, T>
}
// ------------------------------------------------------------------
Expand All @@ -64,8 +64,8 @@ type TFromMappedResult<
function FromMappedResult<
R extends TMappedResult,
T extends TSchema
>(R: R, T: T, options: SchemaOptions): TFromMappedResult<R, T> {
return FromProperties(R.properties, T, options) as TFromMappedResult<R, T>
>(R: R, T: T): TFromMappedResult<R, T> {
return FromProperties(R.properties, T) as TFromMappedResult<R, T>
}
// ------------------------------------------------------------------
// ExcludeFromMappedResult
Expand All @@ -83,7 +83,7 @@ export function ExcludeFromMappedResult<
R extends TMappedResult,
T extends TSchema,
P extends TProperties = TFromMappedResult<R, T>
>(R: R, T: T, options: SchemaOptions): TMappedResult<P> {
const P = FromMappedResult(R, T, options) as unknown as P
>(R: R, T: T): TMappedResult<P> {
const P = FromMappedResult(R, T) as unknown as P
return MappedResult(P)
}
39 changes: 39 additions & 0 deletions src/type/exclude/exclude-from-template-literal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema } from '../schema/index'
import { TExclude, Exclude } from './exclude'
import { TemplateLiteralToUnion, type TTemplateLiteral, type TTemplateLiteralToUnion } from '../template-literal/index'

// prettier-ignore
export type TExcludeFromTemplateLiteral<L extends TTemplateLiteral, R extends TSchema> = (
TExclude<TTemplateLiteralToUnion<L>, R>
)
export function ExcludeFromTemplateLiteral<L extends TTemplateLiteral, R extends TSchema>(L: L, R: R): TExcludeFromTemplateLiteral<L, R> {
return Exclude(TemplateLiteralToUnion(L), R) as never
}
37 changes: 16 additions & 21 deletions src/type/exclude/exclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,23 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import type { UnionToTuple, AssertRest, AssertType, Assert } from '../helpers/index'
import type { UnionToTuple, AssertRest, AssertType } from '../helpers/index'
import type { TMappedResult } from '../mapped/index'
import { TemplateLiteralToUnion, type TTemplateLiteral } from '../template-literal/index'
import { type TTemplateLiteral } from '../template-literal/index'
import { Union, type TUnion } from '../union/index'
import { Never, type TNever } from '../never/index'
import { type TLiteral } from '../literal/index'
import { type Static } from '../static/index'
import { type TUnionEvaluated } from '../union/index'
import { ExtendsCheck, ExtendsResult } from '../extends/index'
import { CloneType } from '../clone/type'
import { ExcludeFromMappedResult, type TExcludeFromMappedResult } from './exclude-from-mapped-result'
import { ExcludeFromTemplateLiteral, type TExcludeFromTemplateLiteral } from './exclude-from-template-literal'

// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsMappedResult, IsTemplateLiteral, IsUnion } from '../guard/type'
// ------------------------------------------------------------------
// ExcludeTemplateLiteral
// ------------------------------------------------------------------
// prettier-ignore
type TExcludeTemplateLiteralResult<T extends string> = TUnionEvaluated<AssertRest<UnionToTuple<{ [K in T]: TLiteral<K> }[T]>>>
// prettier-ignore
type TExcludeTemplateLiteral<L extends TTemplateLiteral, R extends TSchema> = (
Exclude<Static<L>, Static<R>> extends infer S ? TExcludeTemplateLiteralResult<Assert<S, string>> : never
)
// ------------------------------------------------------------------
// ExcludeRest
// ------------------------------------------------------------------
// prettier-ignore
Expand All @@ -69,19 +60,23 @@ function ExcludeRest<L extends TSchema[], R extends TSchema>(L: [...L], R: R) {
// ------------------------------------------------------------------
// prettier-ignore
export type TExclude<L extends TSchema, R extends TSchema> = (
L extends TMappedResult<L> ? TExcludeFromMappedResult<L, R> :
L extends TTemplateLiteral ? TExcludeTemplateLiteral<L, R> :
L extends TUnion<infer S> ? TExcludeRest<S, R> :
L extends R ? TNever : L
)
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
export function Exclude<L extends TSchema, R extends TSchema>(L: L, R: R, options: SchemaOptions = {}): TExclude<L, R> {
export function Exclude<L extends TMappedResult, R extends TSchema>(unionType: L, excludedMembers: R, options?: SchemaOptions): TExcludeFromMappedResult<L, R>
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
export function Exclude<L extends TTemplateLiteral, R extends TSchema>(unionType: L, excludedMembers: R, options?: SchemaOptions): TExcludeFromTemplateLiteral<L, R>
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
export function Exclude<L extends TSchema, R extends TSchema>(unionType: L, excludedMembers: R, options?: SchemaOptions): TExclude<L, R>
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
export function Exclude(L: TSchema, R: TSchema, options: SchemaOptions = {}): any {
// overloads
if (IsTemplateLiteral(L)) return CloneType(ExcludeFromTemplateLiteral(L, R), options)
if (IsMappedResult(L)) return CloneType(ExcludeFromMappedResult(L, R), options)
// prettier-ignore
return CloneType((
IsMappedResult(L) ? ExcludeFromMappedResult(L, R, options) :
IsTemplateLiteral(L) ? Exclude(TemplateLiteralToUnion(L), R) :
IsTemplateLiteral(R) ? Exclude(L, TemplateLiteralToUnion(R)) :
IsUnion(L) ? ExcludeRest(L.anyOf, R) :
return CloneType(
IsUnion(L) ? ExcludeRest(L.anyOf, R) :
ExtendsCheck(L, R) !== ExtendsResult.False ? Never() : L
), options) as never
, options)
}
1 change: 1 addition & 0 deletions src/type/exclude/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './exclude-from-mapped-result'
export * from './exclude-from-template-literal'
export * from './exclude'
14 changes: 7 additions & 7 deletions src/type/extract/extract-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import type { TSchema } from '../schema/index'
import type { TProperties } from '../object/index'
import { MappedResult, type TMappedResult } from '../mapped/index'
import { Extract, type TExtract } from './extract'
Expand All @@ -45,9 +45,9 @@ type TFromProperties<
function FromProperties<
P extends TProperties,
T extends TSchema
>(P: P, T: T, options: SchemaOptions): TFromProperties<P, T> {
>(P: P, T: T): TFromProperties<P, T> {
return globalThis.Object.getOwnPropertyNames(P).reduce((Acc, K2) => {
return {...Acc, [K2]: Extract(P[K2], T, options) }
return {...Acc, [K2]: Extract(P[K2], T) }
}, {}) as TFromProperties<P, T>
}
// ------------------------------------------------------------------
Expand All @@ -64,8 +64,8 @@ type TFromMappedResult<
function FromMappedResult<
R extends TMappedResult,
T extends TSchema
>(R: R, T: T, options: SchemaOptions): TFromMappedResult<R, T> {
return FromProperties(R.properties, T, options) as TFromMappedResult<R, T>
>(R: R, T: T): TFromMappedResult<R, T> {
return FromProperties(R.properties, T) as TFromMappedResult<R, T>
}
// ------------------------------------------------------------------
// ExtractFromMappedResult
Expand All @@ -83,7 +83,7 @@ export function ExtractFromMappedResult<
R extends TMappedResult,
T extends TSchema,
P extends TProperties = TFromMappedResult<R, T>
>(R: R, T: T, options: SchemaOptions): TMappedResult<P> {
const P = FromMappedResult(R, T, options) as unknown as P
>(R: R, T: T): TMappedResult<P> {
const P = FromMappedResult(R, T) as unknown as P
return MappedResult(P)
}
39 changes: 39 additions & 0 deletions src/type/extract/extract-from-template-literal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema } from '../schema/index'
import { Extract, type TExtract } from './extract'
import { TemplateLiteralToUnion, type TTemplateLiteral, type TTemplateLiteralToUnion } from '../template-literal/index'

// prettier-ignore
export type TExtractFromTemplateLiteral<L extends TTemplateLiteral, R extends TSchema> = (
TExtract<TTemplateLiteralToUnion<L>, R>
)
export function ExtractFromTemplateLiteral<L extends TTemplateLiteral, R extends TSchema>(L: L, R: R): TExtractFromTemplateLiteral<L, R> {
return Extract(TemplateLiteralToUnion(L), R) as never
}
34 changes: 16 additions & 18 deletions src/type/extract/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,23 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Assert, AssertRest, AssertType, UnionToTuple } from '../helpers/index'
import type { AssertRest, AssertType, UnionToTuple } from '../helpers/index'
import type { TMappedResult } from '../mapped/index'
import { TemplateLiteralToUnion, type TTemplateLiteral } from '../template-literal/index'
import { type TLiteral } from '../literal/index'
import { Union, type TUnion } from '../union/index'
import { type Static } from '../static/index'
import { Never, type TNever } from '../never/index'
import { type TUnionEvaluated } from '../union/index'
import { type TTemplateLiteral } from '../template-literal/index'
import { ExtendsCheck, ExtendsResult } from '../extends/index'
import { CloneType } from '../clone/type'
import { ExtractFromMappedResult, type TExtractFromMappedResult } from './extract-from-mapped-result'
import { ExtractFromTemplateLiteral, type TExtractFromTemplateLiteral } from './extract-from-template-literal'

// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsMappedResult, IsTemplateLiteral, IsUnion } from '../guard/type'
// ------------------------------------------------------------------
// ExtractTemplateLiteral
// ------------------------------------------------------------------
// prettier-ignore
type TExtractTemplateLiteralResult<T extends string> = TUnionEvaluated<AssertRest<UnionToTuple<{ [K in T]: TLiteral<K> }[T]>>>
// prettier-ignore
type TExtractTemplateLiteral<L extends TTemplateLiteral, R extends TSchema> = Extract<Static<L>, Static<R>> extends infer S ? TExtractTemplateLiteralResult<Assert<S, string>> : never

// ------------------------------------------------------------------
// ExtractRest
// ------------------------------------------------------------------
Expand All @@ -67,19 +61,23 @@ function ExtractRest<L extends TSchema[], R extends TSchema>(L: [...L], R: R) {
// ------------------------------------------------------------------
// prettier-ignore
export type TExtract<L extends TSchema, U extends TSchema> = (
L extends TMappedResult ? TExtractFromMappedResult<L, U> :
L extends TTemplateLiteral ? TExtractTemplateLiteral<L, U> :
L extends TUnion<infer S> ? TExtractRest<S, U> :
L extends U ? L : TNever
)
/** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
export function Extract<L extends TSchema, R extends TSchema>(L: L, R: R, options: SchemaOptions = {}): TExtract<L, R> {
export function Extract<L extends TMappedResult, R extends TSchema>(type: L, union: R, options?: SchemaOptions): TExtractFromMappedResult<L, R>
/** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
export function Extract<L extends TTemplateLiteral, R extends TSchema>(type: L, union: R, options?: SchemaOptions): TExtractFromTemplateLiteral<L, R>
/** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
export function Extract<L extends TSchema, R extends TSchema>(type: L, union: R, options?: SchemaOptions): TExtract<L, R>
/** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
export function Extract(L: TSchema, R: TSchema, options: SchemaOptions = {}): any {
// overloads
if (IsTemplateLiteral(L)) return CloneType(ExtractFromTemplateLiteral(L, R), options)
if (IsMappedResult(L)) return CloneType(ExtractFromMappedResult(L, R), options)
// prettier-ignore
return CloneType((
IsMappedResult(L) ? ExtractFromMappedResult(L, R, options) :
IsTemplateLiteral(L) ? Extract(TemplateLiteralToUnion(L), R) :
IsTemplateLiteral(R) ? Extract(L, TemplateLiteralToUnion(R) as any) :
return CloneType(
IsUnion(L) ? ExtractRest(L.anyOf, R) :
ExtendsCheck(L, R) !== ExtendsResult.False ? L : Never()
), options) as never
, options)
}
1 change: 1 addition & 0 deletions src/type/extract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './extract-from-mapped-result'
export * from './extract-from-template-literal'
export * from './extract'
22 changes: 17 additions & 5 deletions src/type/template-literal/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,31 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { Static } from '../static/index'
import type { TTemplateLiteral } from './template-literal'
import { Union, type TUnion } from '../union/index'
import type { UnionToTuple } from '../helpers/index'
import { UnionEvaluated, type TUnionEvaluated } from '../union/index'
import { Literal, type TLiteral } from '../literal/index'
import { type TString } from '../string/index'
import { type TNever } from '../never/index'
import { TemplateLiteralGenerate } from './generate'

// ------------------------------------------------------------------
// TemplateLiteralToUnion
// ------------------------------------------------------------------
// prettier-ignore
export type TTemplateLiteralToUnionLiteralArray<T extends string[], Acc extends TLiteral[] = []> = (
T extends [infer L extends string, ...infer R extends string[]]
? TTemplateLiteralToUnionLiteralArray<R, [...Acc, TLiteral<L>]>
: Acc
)
// prettier-ignore
export type TTemplateLiteralToUnion<
T extends TTemplateLiteral,
U extends string[] = UnionToTuple<Static<T>>
> = TUnionEvaluated<TTemplateLiteralToUnionLiteralArray<U>>

/** Returns a Union from the given TemplateLiteral */
export function TemplateLiteralToUnion(schema: TTemplateLiteral): TNever | TString | TUnion<TLiteral[]> {
export function TemplateLiteralToUnion<T extends TTemplateLiteral>(schema: TTemplateLiteral): TTemplateLiteralToUnion<T> {
const R = TemplateLiteralGenerate(schema) as string[]
const L = R.map((S) => Literal(S))
return Union(L)
return UnionEvaluated(L)
}
Loading

0 comments on commit cc06871

Please sign in to comment.