Skip to content

Commit

Permalink
Optimization Work
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 25, 2024
1 parent 130c520 commit 3fca39b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 65 deletions.
53 changes: 16 additions & 37 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

const T = Type.Object({
const K = Type.TemplateLiteral('p${0|1}${0|1}${0|1}${0|1}${0|1}${0|1}')
const T = Type.Mapped(K, _ => Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})

type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------

const C = TypeCompiler.Compile(T)

console.log(C.Code())

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------

console.log(C.Check(V))
z: Type.Number()
}))

const S = Date.now()
for(let i = 0; i < 1000; i++) {
const K = Type.TemplateLiteral('p${0|1}${0|1}${0|1}${0|1}${0|1}${0|1}')
const T = Type.Mapped(K, _ => Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
}))
}
console.log(Date.now() - S)
11 changes: 8 additions & 3 deletions src/type/clone/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ function RegExpType(value: RegExp) {
return new RegExp(value.source, value.flags)
}
function ObjectType(value: Record<keyof any, unknown>) {
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {})
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key as any]) }), {})
return { ...clonedProperties, ...clonedSymbols }
const result = {} as Record<PropertyKey, unknown>
for(const key of Object.getOwnPropertyNames(value)) {
result[key] = Visit(value[key])
}
for(const key of Object.getOwnPropertySymbols(value)) {
result[key] = Visit(value[key])
}
return result
}
// prettier-ignore
function Visit(value: unknown): any {
Expand Down
18 changes: 11 additions & 7 deletions src/type/composite/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ type TCompositeKeys<T extends TSchema[], Acc extends PropertyKey[] = []> = (
// prettier-ignore
function CompositeKeys<T extends TSchema[]>(T: [...T]): TCompositeKeys<T> {
return SetDistinct(T.reduce((Acc, L) => {
return ([...Acc, ...KeyOfPropertyKeys(L)]) as never
}, [])) as never
Acc.push(...KeyOfPropertyKeys(L))
return Acc
}, [] as PropertyKey[])) as never
}
// ------------------------------------------------------------------
// FilterNever
Expand Down Expand Up @@ -81,8 +82,9 @@ type TCompositeProperty<T extends TSchema[], K extends PropertyKey, Acc extends
// prettier-ignore
function CompositeProperty<T extends TSchema[], K extends PropertyKey>(T: [...T], K: K): TCompositeProperty<T, K> {
return FilterNever(T.reduce((Acc, L) => {
return [...Acc, ...IndexFromPropertyKeys(L, [K])] as never
}, [])) as never
Acc.push(...IndexFromPropertyKeys(L, [K]))
return Acc
}, [] as TSchema[])) as never
}
// ------------------------------------------------------------------
// CompositeProperties
Expand All @@ -95,9 +97,11 @@ type TCompositeProperties<T extends TSchema[], K extends PropertyKey[], Acc = {}
)
// prettier-ignore
function CompositeProperties<T extends TSchema[], K extends PropertyKey[] = []>(T: [...T], K: [...K]): TCompositeProperties<T, K> {
return K.reduce((Acc, L) => {
return { ...Acc, [L]: IntersectEvaluated(CompositeProperty(T, L)) }
}, {}) as never
const Acc = {} as never
for(const L of K) {
Acc[L] = IntersectEvaluated(CompositeProperty(T, L))
}
return Acc
}
// ------------------------------------------------------------------
// Composite
Expand Down
8 changes: 5 additions & 3 deletions src/type/indexed/indexed-from-mapped-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ function FromProperties<
T extends TSchema,
P extends TProperties
>(T: T, P: P, options: SchemaOptions): TFromProperties<T, P> {
return globalThis.Object.getOwnPropertyNames(P).reduce((Acc, K2) => {
return {...Acc, [K2]: Index(T, IndexPropertyKeys(P[K2]), options) }
}, {}) as TFromProperties<T, P>
const Acc = {} as Record<PropertyKey, TSchema>
for(const K2 of Object.getOwnPropertyNames(P)) {
Acc[K2] = Index(T, IndexPropertyKeys(P[K2]), options)
}
return Acc as never
}
// ------------------------------------------------------------------
// FromMappedResult
Expand Down
6 changes: 1 addition & 5 deletions src/type/intersect/intersect-evaluated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ import { IntersectCreate } from './intersect-create'
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
// prettier-ignore
import {
IsOptional,
IsTransform,
} from '../guard/type'
import { IsOptional, IsTransform } from '../guard/type'

// ------------------------------------------------------------------
// IsIntersectOptional
Expand Down
20 changes: 10 additions & 10 deletions src/type/mapped/mapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ type TMappedKeyToUnknownMappedResultProperties<P extends PropertyKey[], Acc exte
)
// prettier-ignore
function MappedKeyToUnknownMappedResultProperties<P extends PropertyKey[]>(P: [...P]): TMappedKeyToUnknownMappedResultProperties<P> {
return P.reduce((Acc, L) => {
return { ...Acc, [L]: Literal(L as TLiteralValue) }
}, {} as TProperties) as TMappedKeyToUnknownMappedResultProperties<P>
const Acc = {} as Record<PropertyKey, TSchema>
for(const L of P) Acc[L] = Literal(L as TLiteralValue)
return Acc as never
}
// ------------------------------------------------------------------
// MappedKeyToMappedResultProperties
Expand Down Expand Up @@ -189,9 +189,9 @@ type FromProperties<K extends PropertyKey, T extends TProperties, R extends TPro
}>> = R
// prettier-ignore
function FromProperties<K extends PropertyKey, T extends TProperties>(K: K, T: T): FromProperties<K, T> {
return globalThis.Object.getOwnPropertyNames(T).reduce((Acc, K2) => {
return { ...Acc, [K2]: FromSchemaType(K, T[K2]) }
}, {}) as FromProperties<K, T>
const Acc = {} as Record<PropertyKey, TSchema>
for(const K2 of globalThis.Object.getOwnPropertyNames(T)) Acc[K2] = FromSchemaType(K, T[K2])
return Acc as never
}
// ------------------------------------------------------------------
// FromMappedPropertyKey
Expand Down Expand Up @@ -250,10 +250,10 @@ export type TMappedFunctionReturnType<K extends PropertyKey[], T extends TSchema
: Acc
)
// prettier-ignore
export function MappedFunctionReturnType<K extends PropertyKey[], T extends TSchema>(K: [...K], T: T, Acc: TProperties = {}): TMappedFunctionReturnType<K, T> {
return K.reduce((Acc, L) => {
return { ...Acc, [L]: FromSchemaType(L, T) }
}, {} as TProperties) as TMappedFunctionReturnType<K, T>
export function MappedFunctionReturnType<K extends PropertyKey[], T extends TSchema>(K: [...K], T: T): TMappedFunctionReturnType<K, T> {
const Acc = {} as Record<PropertyKey, TSchema>
for(const L of K) Acc[L] = FromSchemaType(L, T)
return Acc as never
}
// ------------------------------------------------------------------
// TMappedFunction
Expand Down

0 comments on commit 3fca39b

Please sign in to comment.