Skip to content

Commit

Permalink
Esm
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 28, 2023
1 parent b05d47e commit 9a44521
Show file tree
Hide file tree
Showing 61 changed files with 1,384 additions and 1,251 deletions.
7 changes: 3 additions & 4 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { String } from '@sinclair/typebox'



import { String } from '@sinclair/typebox/type'

console.log(String)
console.log('hello')
2 changes: 1 addition & 1 deletion hammer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function test(filter = '') {
// Build
// -------------------------------------------------------------------------------
export async function build(target = 'target/build') {
await test()
// await test()
await folder(target).delete()
await shell(`tsc -p ./src/tsconfig.json --outDir ${target}`)
await folder(target).add('package.json')
Expand Down
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ export default Type
// ----------------------------------------------------------------
// Runtime
// ----------------------------------------------------------------
export { CloneType } from './type/clone/index'
export { TypeRegistry, FormatRegistry } from './type/registry/index'
export { TypeGuard, ValueGuard } from './type/guard/index'
export { Strict } from './type/strict/index'
export { Symbols } from './type/symbols/index'
export { CloneType, CloneRest } from './type/clone/type'

export * as TypeRegistry from './type/registry/type'
export * as FormatRegistry from './type/registry/format'
export * as ValueGuard from './type/guard/value'
export * as TypeGuard from './type/guard/type'

// ----------------------------------------------------------------
// Types
// ----------------------------------------------------------------
export { Strict } from './type/strict/index'
export { Symbols } from './type/symbols/index'

export { type TAny, Any } from './type/any/index'
export { type TArray, Array } from './type/array/index'
export { type TAsyncIterator, AsyncIterator } from './type/async-iterator/index'
Expand Down Expand Up @@ -71,7 +76,7 @@ export { type TNever, Never } from './type/never/index'
export { type TNot, Not } from './type/not/index'
export { type TNull, Null } from './type/null/index'
export { type TNumber, Number } from './type/number/index'
export { type TObject, ObjectType as Object } from './type/object/index'
export { type TObject, Object as Object } from './type/object/index'
export { type TOmit, Omit } from './type/omit/index'
export { type TOptional, Optional } from './type/optional/index'
export { type TParameters, Parameters } from './type/parameters/index'
Expand Down
2 changes: 1 addition & 1 deletion src/type/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ THE SOFTWARE.

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import { CloneType } from '../clone/index'
import { Symbols } from '../symbols/index'
import { CloneType } from '../clone/type'

export interface ArrayOptions extends SchemaOptions {
/** The minimum number of items in this array */
Expand Down
2 changes: 1 addition & 1 deletion src/type/async-iterator/async-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ THE SOFTWARE.

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import { CloneType } from '../clone/index'
import { Symbols } from '../symbols/index'
import { CloneType } from '../clone/type'

export interface TAsyncIterator<T extends TSchema = TSchema> extends TSchema {
[Symbols.Kind]: 'AsyncIterator'
Expand Down
10 changes: 5 additions & 5 deletions src/type/awaited/awaited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import type { TSchema, SchemaOptions } from '../schema/index'
import { type TIntersect, Intersect } from '../intersect/index'
import { type TUnion, Union } from '../union/index'
import { type TPromise } from '../promise/index'
import { CloneType } from '../clone/index'
import { TypeGuard } from '../guard/index'
import { CloneType } from '../clone/type'
import { TIntersect as IsIntersectType, TUnion as IsUnionType, TPromise as IsPromiseType } from '../guard/type'

// ----------------------------------------------------------------
// Unwrap
Expand Down Expand Up @@ -88,9 +88,9 @@ export type AwaitedResolve<T extends TSchema> =
// prettier-ignore
export function Resolve<T extends TSchema>(T: T): AwaitedResolve<T> {
return (
TypeGuard.TIntersect(T) ? AwaitedIntersectRest(T.allOf) :
TypeGuard.TUnion(T) ? AwaitedUnionRest(T.anyOf) :
TypeGuard.TPromise(T) ? AwaitedPromise(T.item) :
IsIntersectType(T) ? AwaitedIntersectRest(T.allOf) :
IsUnionType(T) ? AwaitedUnionRest(T.anyOf) :
IsPromiseType(T) ? AwaitedPromise(T.item) :
T
) as AwaitedResolve<T>
}
Expand Down
4 changes: 2 additions & 2 deletions src/type/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { Never } from '../never/index'
import { Not } from '../not/index'
import { Null } from '../null/index'
import { Number } from '../number/index'
import { ObjectType } from '../object/index'
import { Object } from '../object/index'
import { Omit } from '../omit/index'
import { Optional } from '../optional/index'
import { Parameters } from '../parameters/index'
Expand Down Expand Up @@ -111,7 +111,7 @@ export class TypeBuilder {
public Not = Not
public Null = Null
public Number = Number
public Object = ObjectType
public Object = Object
public Omit = Omit
public Optional = Optional
public Parameters = Parameters
Expand Down
4 changes: 2 additions & 2 deletions src/type/clone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './type'
export * from './value'
export * as TypeClone from './type'
export * as ValueClone from './value'
4 changes: 2 additions & 2 deletions src/type/clone/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import { ValueClone } from './value'
import { Clone } from './value'

/** Clones a Rest */
export function CloneRest<T extends TSchema[]>(schemas: T): T {
return schemas.map((schema) => CloneType(schema)) as T
}
/** Clones a Type */
export function CloneType<T extends TSchema>(schema: T, options: SchemaOptions = {}): T {
return { ...ValueClone.Clone(schema), ...options }
return { ...Clone(schema), ...options }
}
60 changes: 29 additions & 31 deletions src/type/clone/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,33 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { ValueGuard } from '../guard/index'

export namespace ValueClone {
function ArrayType(value: unknown[]) {
return (value as any).map((value: unknown) => Visit(value as any))
}
function DateType(value: Date) {
return new Date(value.getTime())
}
function Uint8ArrayType(value: Uint8Array) {
return new Uint8Array(value)
}
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 }
}
// prettier-ignore
function Visit(value: unknown): any {
return (
ValueGuard.IsArray(value) ? ArrayType(value) :
ValueGuard.IsDate(value) ? DateType(value) :
ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
ValueGuard.IsObject(value) ? ObjectType(value) :
value
)
}
/** Clones a value */
export function Clone<T>(value: T): T {
return Visit(value)
}
import * as ValueGuard from '../guard/value'

function ArrayType(value: unknown[]) {
return (value as any).map((value: unknown) => Visit(value as any))
}
function DateType(value: Date) {
return new Date(value.getTime())
}
function Uint8ArrayType(value: Uint8Array) {
return new Uint8Array(value)
}
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 }
}
// prettier-ignore
function Visit(value: unknown): any {
return (
ValueGuard.IsArray(value) ? ArrayType(value) :
ValueGuard.IsDate(value) ? DateType(value) :
ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
ValueGuard.IsObject(value) ? ObjectType(value) :
value
)
}
/** Clones a value */
export function Clone<T>(value: T): T {
return Visit(value)
}
6 changes: 3 additions & 3 deletions src/type/composite/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ THE SOFTWARE.

import type { TSchema } from '../schema/index'
import type { UnionToTuple, Assert, Evaluate } from '../helpers/index'
import { type TObject, type TProperties, type ObjectOptions, ObjectType } from '../object/index'
import { type TObject, type TProperties, type ObjectOptions, Object } from '../object/index'
import { type TIntersect, Intersect } from '../intersect/index'
import { KeyOfStringResolve } from '../keyof/index'
import { IndexedTypeResolve, Index } from '../indexed/index'
import { CloneType } from '../clone/index'
import { CloneType } from '../clone/type'

// ------------------------------------------------------------------
// Resolve
Expand All @@ -59,7 +59,7 @@ export function CompositeResolve<T extends TObject[]>(T: [...T]): CompositeResol
const intersect: TSchema = Intersect(T, {})
const keys = KeyOfStringResolve(intersect) as string[]
const properties = keys.reduce((acc, key) => ({ ...acc, [key]: Index(intersect, [key]) }), {} as TProperties)
return ObjectType(properties) as CompositeResolve<T>
return Object(properties) as CompositeResolve<T>
}
// ------------------------------------------------------------------
// Type
Expand Down
3 changes: 2 additions & 1 deletion src/type/constructor-parameters/constructor-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type { TSchema, SchemaOptions } from '../schema/index'
import type { Ensure } from '../helpers/index'
import type { TConstructor } from '../constructor/index'
import { type TTuple, Tuple } from '../tuple/index'
import { CloneRest } from '../clone/type'

// prettier-ignore
export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> = (
Expand All @@ -38,5 +39,5 @@ export type TConstructorParameters<T extends TConstructor<TSchema[], TSchema>> =

/** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
export function ConstructorParameters<T extends TConstructor<TSchema[], TSchema>>(schema: T, options: SchemaOptions = {}): TConstructorParameters<T> {
return Tuple([...schema.parameters], { ...options })
return Tuple(CloneRest(schema.parameters), { ...options })
}
4 changes: 2 additions & 2 deletions src/type/constructor/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ THE SOFTWARE.

import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import { CloneType, CloneRest } from '../clone/index'
import type { Ensure } from '../helpers/index'
import { Symbols } from '../symbols/index'
import { Ensure } from '../helpers/index'
import { CloneType, CloneRest } from '../clone/type'

// ------------------------------------------------------------------
// TConstructor
Expand Down
6 changes: 2 additions & 4 deletions src/type/enum/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ THE SOFTWARE.
import type { TSchema, SchemaOptions } from '../schema/index'
import { type TLiteral, Literal } from '../literal/index'
import { Symbols } from '../symbols/index'
import { ValueGuard } from '../guard/index'
import { Union } from '../union/index'

import { IsUndefined } from '../guard/value'
// ------------------------------------------------------------------
// TEnum
// ------------------------------------------------------------------
Expand All @@ -44,10 +43,9 @@ export interface TEnum<T extends Record<string, string | number> = Record<string
static: T[keyof T]
anyOf: TLiteral<T[keyof T]>[]
}

/** `[Json]` Creates a Enum type */
export function Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item: T, options: SchemaOptions = {}): TEnum<T> {
if (ValueGuard.IsUndefined(item)) throw new Error('Enum undefined or empty')
if (IsUndefined(item)) throw new Error('Enum undefined or empty')
const values1 = globalThis.Object.getOwnPropertyNames(item)
.filter((key) => isNaN(key as any))
.map((key) => item[key]) as T[keyof T][]
Expand Down
13 changes: 6 additions & 7 deletions src/type/exclude/exclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import type { TSchema, SchemaOptions } from '../schema/index'
import type { UnionToIntersect, UnionLast, UnionToTuple, AssertRest, AssertType, Assert } from '../helpers/index'
import type { UnionToTuple, AssertRest, AssertType, Assert } from '../helpers/index'
import { type TTemplateLiteral, TemplateLiteralToUnion } from '../template-literal/index'
import { type TUnion, Union } from '../union/index'
import { type TNever, Never } from '../never/index'
import { type TLiteral } from '../literal/index'
import { type Static } from '../static/index'
import { ExtendsCheck, ExtendsResult } from '../extends/index'
import { TypeGuard } from '../guard/index'
import { CloneType } from '../clone/index'
import { UnionResolve } from '../union/index'

import { CloneType } from '../clone/type'
import { TTemplateLiteral as IsTemplateLiteralType, TUnion as IsUnionType } from '../guard/type'
// ------------------------------------------------------------------
// Resolve
// ------------------------------------------------------------------
Expand All @@ -61,9 +60,9 @@ export type ExcludeResolve<T extends TSchema, U extends TSchema> =
// prettier-ignore
export function ExcludeResolve<L extends TSchema, R extends TSchema>(L: L, R: R): ExcludeResolve<L, R> {
return (
TypeGuard.TTemplateLiteral(L) ? ExcludeResolve(TemplateLiteralToUnion.Resolve(L), R) :
TypeGuard.TTemplateLiteral(R) ? ExcludeResolve(L, TemplateLiteralToUnion.Resolve(R)) :
TypeGuard.TUnion(L) ? (() => {
IsTemplateLiteralType(L) ? ExcludeResolve(TemplateLiteralToUnion.Resolve(L), R) :
IsTemplateLiteralType(R) ? ExcludeResolve(L, TemplateLiteralToUnion.Resolve(R)) :
IsUnionType(L) ? (() => {
const narrowed = L.anyOf.filter((inner) => ExtendsCheck(inner, R) === ExtendsResult.False)
return (narrowed.length === 1 ? narrowed[0] : Union(narrowed))
})() :
Expand Down
2 changes: 1 addition & 1 deletion src/type/extends/extends-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ import { type TUnknown, Unknown } from '../unknown/index'
import { type TVoid } from '../void/index'

import { TemplateLiteralToUnion } from '../template-literal/index'
import { TypeGuard, ValueGuard } from '../guard/index'
import { PatternNumberExact, PatternStringExact } from '../patterns/index'
import { Symbols } from '../symbols/index'
import { TypeGuard, ValueGuard } from '../guard/index'

export class ExtendsResolverError extends Error {}

Expand Down
2 changes: 1 addition & 1 deletion src/type/extends/extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import { type TUnion, Union } from '../union/index'
import { ExtendsCheck, ExtendsResult } from './extends-check'
import { CloneType } from '../clone/index'
import { UnionToTuple } from '../helpers/index'
import { CloneType } from '../clone/type'

// prettier-ignore
export type ExtendsResolve<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (
Expand Down
10 changes: 5 additions & 5 deletions src/type/extract/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import { type TLiteral } from '../literal/index'
import { type TUnion, Union } from '../union/index'
import { type Static } from '../static/index'
import { Never } from '../never/index'
import { TypeGuard } from '../guard/index'
import { CloneType } from '../clone/index'
import { UnionResolve } from '../union/index'
import { ExtendsCheck, ExtendsResult } from '../extends/index'
import { CloneType } from '../clone/type'
import { TTemplateLiteral as IsTemplateLiteralType, TUnion as IsUnionType } from '../guard/type'

// ------------------------------------------------------------------
// From
Expand All @@ -63,9 +63,9 @@ export type ExtractResolve<T extends TSchema, U extends TSchema> = (
// prettier-ignore
export function ExtractResolve<L extends TSchema, R extends TSchema>(L: L, R: R): ExtractResolve<L, R> {
return (
TypeGuard.TTemplateLiteral(L) ? ExtractResolve(TemplateLiteralToUnion.Resolve(L), R) :
TypeGuard.TTemplateLiteral(R) ? ExtractResolve(L, TemplateLiteralToUnion.Resolve(R) as any) :
TypeGuard.TUnion(L) ? (() => {
IsTemplateLiteralType(L) ? ExtractResolve(TemplateLiteralToUnion.Resolve(L), R) :
IsTemplateLiteralType(R) ? ExtractResolve(L, TemplateLiteralToUnion.Resolve(R) as any) :
IsUnionType(L) ? (() => {
const narrowed = L.anyOf.filter((inner) => ExtendsCheck(inner, R) !== ExtendsResult.False)
return (narrowed.length === 1 ? narrowed[0] : Union(narrowed))
})() :
Expand Down
2 changes: 1 addition & 1 deletion src/type/function/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ THE SOFTWARE.
import type { TSchema, SchemaOptions } from '../schema/index'
import type { Static } from '../static/index'
import type { Ensure } from '../helpers/index'
import { CloneType, CloneRest } from '../clone/index'
import { CloneType, CloneRest } from '../clone/type'
import { Symbols } from '../symbols/index'

export type TFunctionReturnTypeResolve<T extends TSchema, P extends unknown[]> = Static<T, P>
Expand Down
32 changes: 30 additions & 2 deletions src/type/guard/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
export * from './type'
export * from './value'
/*--------------------------------------------------------------------------
@sinclair/typebox
The MIT License (MIT)
Copyright (c) 2017-2023 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.
---------------------------------------------------------------------------*/

export * as TypeGuard from './type'
export * as ValueGuard from './value'
Loading

0 comments on commit 9a44521

Please sign in to comment.