Skip to content

Commit

Permalink
Revision 0.32.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 28, 2023
1 parent 9852ba6 commit 0bc5197
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export { BigInt, type TBigInt, BigIntOptions } from './type/bigint/index'
export { Boolean, type TBoolean } from './type/boolean/index'
export { Capitalize, type TCapitalize } from './type/intrinsic/index'
export { Composite, type TComposite } from './type/composite/index'
export { Const, type TConst } from './type/const/index'
export { Constructor, type TConstructor } from './type/constructor/index'
export { ConstructorParameters, type TConstructorParameters } from './type/constructor-parameters/index'
export { Date, type TDate, type DateOptions } from './type/date/index'
Expand Down
5 changes: 5 additions & 0 deletions src/type/builder/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { JsonTypeBuilder } from './json'
import { AsyncIterator, type TAsyncIterator } from '../async-iterator/index'
import { Awaited, type TAwaited } from '../awaited/index'
import { BigInt, type TBigInt, type BigIntOptions } from '../bigint/index'
import { Const, type TConst } from '../const/index'
import { Constructor, type TConstructor } from '../constructor/index'
import { ConstructorParameters, type TConstructorParameters } from '../constructor-parameters/index'
import { Date, type TDate, type DateOptions } from '../date/index'
Expand Down Expand Up @@ -60,6 +61,10 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
public BigInt(options: BigIntOptions = {}): TBigInt {
return BigInt(options)
}
/** `[JavaScript]` Creates a readonly const type from the given value. */
public Const</* const (not supported in 4.0) */ T>(value: T, options: SchemaOptions = {}): TConst<T> {
return Const(value, options)
}
/** `[JavaScript]` Extracts the ConstructorParameters from the given Constructor type */
public ConstructorParameters<T extends TConstructor<TSchema[], TSchema>>(schema: T, options: SchemaOptions = {}): TConstructorParameters<T> {
return ConstructorParameters(schema, options)
Expand Down
1 change: 1 addition & 0 deletions src/type/builder/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { Awaited } from '../awaited/index'
export { BigInt } from '../bigint/index'
export { Boolean } from '../boolean/index'
export { Composite } from '../composite/index'
export { Const } from '../const/index'
export { Constructor } from '../constructor/index'
export { ConstructorParameters } from '../constructor-parameters/index'
export { Date } from '../date/index'
Expand Down
128 changes: 128 additions & 0 deletions src/type/const/const.ts
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>
}
29 changes: 29 additions & 0 deletions src/type/const/index.ts
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'
16 changes: 16 additions & 0 deletions src/type/guard/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

/** Returns true if this value is an async iterator */
export function IsAsyncIterator(value: unknown): value is AsyncIterableIterator<unknown> {
return IsObject(value) && Symbol.asyncIterator in value
}
/** Returns true if this value is an array */
export function IsArray(value: unknown): value is unknown[] {
return Array.isArray(value)
Expand All @@ -42,6 +46,14 @@ export function IsBoolean(value: unknown): value is boolean {
export function IsDate(value: unknown): value is Date {
return value instanceof globalThis.Date
}
/** Returns true if this value is a function */
export function IsFunction(value: unknown): value is Function {
return typeof value === 'function'
}
/** Returns true if this value is an iterator */
export function IsIterator(value: unknown): value is IterableIterator<unknown> {
return IsObject(value) && Symbol.iterator in value
}
/** Returns true if this value is null */
export function IsNull(value: unknown): value is null {
return value === null
Expand All @@ -58,6 +70,10 @@ export function IsObject(value: unknown): value is Record<PropertyKey, unknown>
export function IsString(value: unknown): value is string {
return typeof value === 'string'
}
/** Returns true if this value is symbol */
export function IsSymbol(value: unknown): value is symbol {
return typeof value === 'symbol'
}
/** Returns true if this value is a Uint8Array */
export function IsUint8Array(value: unknown): value is Uint8Array {
return value instanceof globalThis.Uint8Array
Expand Down
1 change: 1 addition & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export * from './boolean/index'
export * from './builder/index'
export * from './clone/index'
export * from './composite/index'
export * from './const/index'
export * from './constructor/index'
export * from './constructor-parameters/index'
export * from './date/index'
Expand Down
8 changes: 4 additions & 4 deletions src/type/template-literal/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ THE SOFTWARE.

import type { TTemplateLiteral, TTemplateLiteralKind } from './index'
import type { TLiteral, TLiteralValue } from '../literal/index'
import type { Expression, And, Or, Const } from './parser'
import type { Expression, ExpressionAnd, ExpressionOr, ExpressionConst } from './parser'
import type { TUnion } from '../union/index'

// ------------------------------------------------------------------
Expand Down Expand Up @@ -88,15 +88,15 @@ function* GenerateReduce(buffer: string[][]): IterableIterator<string> {
}
}
// prettier-ignore
function* GenerateAnd(expression: And): IterableIterator<string> {
function* GenerateAnd(expression: ExpressionAnd): IterableIterator<string> {
return yield* GenerateReduce(expression.expr.map((expr) => [...TemplateLiteralGenerate(expr)]))
}
// prettier-ignore
function* GenerateOr(expression: Or): IterableIterator<string> {
function* GenerateOr(expression: ExpressionOr): IterableIterator<string> {
for (const expr of expression.expr) yield* TemplateLiteralGenerate(expr)
}
// prettier-ignore
function* GenerateConst(expression: Const): IterableIterator<string> {
function* GenerateConst(expression: ExpressionConst): IterableIterator<string> {
return yield expression.const
}
// ------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/type/template-literal/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export class TemplateLiteralParserError extends Error {}
// TemplateLiteralParse
// ------------------------------------------------------------------
// prettier-ignore
export type Expression = And | Or | Const
export type Const = { type: 'const'; const: string }
export type And = { type: 'and'; expr: Expression[] }
export type Or = { type: 'or'; expr: Expression[] }
export type Expression = ExpressionAnd | ExpressionOr | ExpressionConst
export type ExpressionConst = { type: 'const'; const: string }
export type ExpressionAnd = { type: 'and'; expr: Expression[] }
export type ExpressionOr = { type: 'or'; expr: Expression[] }
// prettier-ignore
function IsNonEscaped(pattern: string, index: number, char: string) {
return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92
Expand Down
8 changes: 5 additions & 3 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
for general composition (i think) but not for key generation.
- Investigate Transform Issue: https://github.com/sinclairzx81/typebox/issues/554
- Clean up Composite (Should technically support Intersect and Union)
- Exclude, Extract and Extends could use a clean up
- Look for invalid import references
- The Symbols Change May be a bit too breaking. Move Kind, Hint, and others to the top (again)
- [x] Exclude, Extract and Extends could use a clean up
- [x] Look for invalid import references
- [x] The Symbols Change May be a bit too breaking. Move Kind, Hint, and others to the top (again)
- [ ] Implement static tests for Type.Const()
- [ ] Implement experimental Type.Mapped(). This will only be usable on the Workbench

0 comments on commit 0bc5197

Please sign in to comment.