-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9852ba6
commit 0bc5197
Showing
10 changed files
with
194 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/*-------------------------------------------------------------------------- | ||
@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. | ||
---------------------------------------------------------------------------*/ | ||
|
||
import type { AssertProperties, AssertRest, Evaluate } from '../helpers/index' | ||
import type { TSchema, SchemaOptions } from '../schema/index' | ||
import type { TProperties } from '../object/index' | ||
import { IsArray, IsNumber, IsBigInt, IsUint8Array, IsDate, IsIterator, IsObject, IsAsyncIterator, IsFunction, IsUndefined, IsNull, IsSymbol, IsBoolean, IsString } from '../guard/value' | ||
import { AsyncIterator, type TAsyncIterator } from '../async-iterator/index' | ||
import { BigInt, type TBigInt } from '../bigint/index' | ||
import { Date, type TDate } from '../date/index' | ||
import { Function, type TFunction } from '../function/index' | ||
import { Literal, type TLiteral } from '../literal/index' | ||
import { Iterator, type TIterator } from '../iterator/index' | ||
import { Never, type TNever } from '../never/index' | ||
import { Null, type TNull } from '../null/index' | ||
import { Object, type TObject } from '../object/index' | ||
import { Symbol, type TSymbol } from '../symbol/index' | ||
import { Tuple, type TTuple } from '../tuple/index' | ||
import { Readonly, type TReadonly } from '../readonly/index' | ||
import { Undefined, type TUndefined } from '../undefined/index' | ||
import { Uint8Array, type TUint8Array } from '../uint8array/index' | ||
import { Unknown, type TUnknown } from '../unknown/index' | ||
import { TypeClone } from '../clone/index' | ||
|
||
// ------------------------------------------------------------------ | ||
// FromArray | ||
// ------------------------------------------------------------------ | ||
// prettier-ignore | ||
export type FromArray<T extends readonly unknown[]> = | ||
T extends readonly [infer L, ...infer R] | ||
? [FromValue<L>, ...FromArray<R>] | ||
: T | ||
// prettier-ignore | ||
function FromArray<T extends readonly unknown[]>(T: [...T]): FromArray<T> { | ||
const [L, ...R] = T | ||
return ( | ||
T.length > 0 | ||
? [FromValue(L), ...FromArray(R)] | ||
: [] | ||
) as FromArray<T> | ||
} | ||
// ------------------------------------------------------------------ | ||
// FromProperties | ||
// ------------------------------------------------------------------ | ||
// prettier-ignore | ||
export type FromProperties<T extends Record<PropertyKey, unknown>> = { | ||
-readonly [K in keyof T]: TReadonly<FromValue<T[K]>> | ||
} | ||
// prettier-ignore | ||
function FromProperties<T extends Record<PropertyKey, unknown>>(value: T): FromProperties<T> { | ||
return globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => { | ||
return { ...acc, [key]: Readonly(FromValue(value[key])) } | ||
}, {} as TProperties) as unknown as FromProperties<T> | ||
} | ||
// ------------------------------------------------------------------ | ||
// FromValue | ||
// ------------------------------------------------------------------ | ||
// prettier-ignore | ||
export type FromValue<T> = | ||
T extends readonly unknown[] ? TTuple<AssertRest<FromArray<T>>> : | ||
T extends Uint8Array ? TUint8Array : | ||
T extends Date ? TDate : | ||
T extends Record<PropertyKey, unknown> ? TObject<Evaluate<AssertProperties<FromProperties<T>>>> : | ||
T extends AsyncIterableIterator<unknown> ? TAsyncIterator<TUnknown> : | ||
T extends IterableIterator<unknown> ? TIterator<TUnknown> : | ||
T extends Function ? TFunction<[], TUnknown> : | ||
T extends undefined ? TUndefined : | ||
T extends null ? TNull : | ||
T extends symbol ? TSymbol : | ||
T extends number ? TLiteral<T> : | ||
T extends boolean ? TLiteral<T> : | ||
T extends string ? TLiteral<T> : | ||
T extends bigint ? TBigInt : | ||
TNever | ||
// prettier-ignore | ||
function FromValue<T>(value: T): FromValue<T> { | ||
return ( | ||
IsArray(value) ? Tuple(FromArray(value) as TSchema[]) : | ||
IsUint8Array(value) ? Uint8Array() : | ||
IsDate(value) ? Date() : | ||
IsObject(value) ? Object(FromProperties(value) as TProperties) : | ||
IsAsyncIterator(value) ? AsyncIterator(Unknown()) : | ||
IsIterator(value) ? Iterator(Unknown()) : | ||
IsFunction(value) ? Function([], Unknown()) : | ||
IsUndefined(value) ? Undefined() : | ||
IsNull(value) ? Null() : | ||
IsSymbol(value) ? Symbol() : | ||
IsBigInt(value) ? BigInt() : | ||
IsNumber(value) ? Literal(value) : | ||
IsBoolean(value) ? Literal(value) : | ||
IsString(value) ? Literal(value) : | ||
Never() | ||
) as unknown as FromValue<T> | ||
} | ||
// ------------------------------------------------------------------ | ||
// TConst | ||
// ------------------------------------------------------------------ | ||
export type TConst<T> = FromValue<T> | ||
|
||
/** `[JavaScript]` Creates a readonly const type from the given value. */ | ||
export function Const</* const (not supported in 4.0) */ T>(T: T, options: SchemaOptions = {}): TConst<T> { | ||
return TypeClone.CloneType(FromValue(T), options) as TConst<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*-------------------------------------------------------------------------- | ||
@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 * from './const' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters