Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Jul 28, 2023
1 parent 2e397e4 commit 4e21fe3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
10 changes: 5 additions & 5 deletions changelog/0.30.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ Revision 0.30.0 carries out several internal refactorings to reduce package and
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'128.2 kb'' 58.0 kb''2.21 x'
typebox/errors'110.7 kb'' 49.6 kb''2.23 x'
typebox/system' 75.5 kb'' 31.2 kb''2.42 x'
typebox/value'179.9 kb'' 78.8 kb''2.28 x'
typebox' 74.4 kb'' 30.7 kb''2.42 x'
typebox/compiler'128.3 kb'' 58.0 kb''2.21 x'
typebox/errors'110.5 kb'' 49.5 kb''2.23 x'
typebox/system' 75.4 kb'' 31.2 kb''2.42 x'
typebox/value'179.7 kb'' 78.7 kb''2.28 x'
typebox' 74.3 kb'' 30.7 kb''2.42 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```

Expand Down
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.30.0-dev-3",
"version": "0.30.0-dev-4",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1586,11 +1586,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'128.2 kb'' 58.2 kb''2.20 x'
typebox/errors'110.4 kb'' 49.5 kb''2.23 x'
typebox/system' 75.2 kb'' 31.1 kb''2.42 x'
typebox/value'179.6 kb'' 78.7 kb''2.28 x'
typebox' 74.1 kb'' 30.6 kb''2.42 x'
typebox/compiler'128.3 kb'' 58.0 kb''2.21 x'
typebox/errors'110.5 kb'' 49.5 kb''2.23 x'
typebox/system' 75.4 kb'' 31.2 kb''2.42 x'
typebox/value'179.7 kb'' 78.7 kb''2.28 x'
typebox' 74.3 kb'' 30.7 kb''2.42 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```
Expand Down
84 changes: 42 additions & 42 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ export namespace TypeClone {
return value
}
/** Clones a type. */
export function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T {
export function Clone<T extends TSchema>(schema: T, options: SchemaOptions = {}): T {
return { ...Visit(schema), ...options }
}
}
Expand All @@ -2020,7 +2020,7 @@ export namespace TypeClone {
export namespace IndexedAccessor {
function OptionalUnwrap(schema: TSchema[]): TSchema[] {
return schema.map((schema) => {
const { [Optional]: _, ...clone } = TypeClone.Clone(schema, {})
const { [Optional]: _, ...clone } = TypeClone.Clone(schema)
return clone
})
}
Expand Down Expand Up @@ -2103,7 +2103,7 @@ export namespace ObjectMap {
return schema
}
export function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T {
return { ...Visit(TypeClone.Clone(schema, {}), callback), ...options } as unknown as T
return { ...Visit(TypeClone.Clone(schema), callback), ...options } as unknown as T
}
}
// --------------------------------------------------------------------------
Expand Down Expand Up @@ -2489,11 +2489,11 @@ export class StandardTypeBuilder extends TypeBuilder {
}
/** `[Standard]` Creates a Readonly property */
public Readonly<T extends TSchema>(schema: T): TReadonly<T> {
return { ...TypeClone.Clone(schema, {}), [Readonly]: 'Readonly' }
return { ...TypeClone.Clone(schema), [Readonly]: 'Readonly' }
}
/** `[Standard]` Creates an Optional property */
public Optional<T extends TSchema>(schema: T): TOptional<T> {
return { ...TypeClone.Clone(schema, {}), [Optional]: 'Optional' }
return { ...TypeClone.Clone(schema), [Optional]: 'Optional' }
}
// ------------------------------------------------------------------------
// Types
Expand All @@ -2504,7 +2504,7 @@ export class StandardTypeBuilder extends TypeBuilder {
}
/** `[Standard]` Creates an Array type */
public Array<T extends TSchema>(items: T, options: ArrayOptions = {}): TArray<T> {
return this.Create({ ...options, [Kind]: 'Array', type: 'array', items: TypeClone.Clone(items, {}) })
return this.Create({ ...options, [Kind]: 'Array', type: 'array', items: TypeClone.Clone(items) })
}
/** `[Standard]` Creates a Boolean type */
public Boolean(options: SchemaOptions = {}): TBoolean {
Expand Down Expand Up @@ -2577,11 +2577,11 @@ export class StandardTypeBuilder extends TypeBuilder {
return TypeClone.Clone(schema.items, options)
} else if (TypeGuard.TTuple(schema) && TypeGuard.TNumber(unresolved)) {
const items = ValueGuard.IsUndefined(schema.items) ? [] : schema.items
const cloned = items.map((schema) => TypeClone.Clone(schema, {}))
const cloned = items.map((schema) => TypeClone.Clone(schema))
return this.Union(cloned, options)
} else {
const keys = KeyArrayResolver.Resolve(unresolved)
const clone = TypeClone.Clone(schema, {})
const clone = TypeClone.Clone(schema)
return IndexedAccessor.Resolve(clone, keys, options)
}
}
Expand All @@ -2599,8 +2599,8 @@ export class StandardTypeBuilder extends TypeBuilder {
if (allOf.length === 0) return Type.Never()
if (allOf.length === 1) return TypeClone.Clone(allOf[0], options)
const objects = allOf.every((schema) => TypeGuard.TObject(schema))
const cloned = allOf.map((schema) => TypeClone.Clone(schema, {}))
const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties) ? { unevaluatedProperties: TypeClone.Clone(options.unevaluatedProperties, {}) } : {}
const cloned = allOf.map((schema) => TypeClone.Clone(schema))
const clonedUnevaluatedProperties = TypeGuard.TSchema(options.unevaluatedProperties) ? { unevaluatedProperties: TypeClone.Clone(options.unevaluatedProperties) } : {}
if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
return this.Create({ ...options, ...clonedUnevaluatedProperties, [Kind]: 'Intersect', type: 'object', allOf: cloned })
} else {
Expand Down Expand Up @@ -2652,8 +2652,8 @@ export class StandardTypeBuilder extends TypeBuilder {
const propertyKeys = Object.getOwnPropertyNames(properties)
const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]))
const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name))
const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties, {}) } : {}
const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(properties[key], {}) }), {} as TProperties)
const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties) } : {}
const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(properties[key]) }), {} as TProperties)
if (requiredKeys.length > 0) {
return this.Create({ ...options, ...clonedAdditionalProperties, [Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys })
} else {
Expand All @@ -2673,15 +2673,15 @@ export class StandardTypeBuilder extends TypeBuilder {
public Omit(schema: TSchema, unresolved: any, options: SchemaOptions = {}): any {
const keys = KeyArrayResolver.Resolve(unresolved)
// prettier-ignore
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
if (schema.required) {
schema.required = schema.required.filter((key: string) => !keys.includes(key as any))
if (schema.required.length === 0) delete schema.required
return ObjectMap.Map(TypeClone.Clone(schema), (object) => {
if (ValueGuard.IsArray(object.required)) {
object.required = object.required.filter((key: string) => !keys.includes(key as any))
if (object.required.length === 0) delete object.required
}
for (const key of Object.getOwnPropertyNames(schema.properties)) {
if (keys.includes(key as any)) delete schema.properties[key]
for (const key of Object.getOwnPropertyNames(object.properties)) {
if (keys.includes(key as any)) delete object.properties[key]
}
return this.Create(schema)
return this.Create(object)
}, options)
}
/** `[Standard]` Creates a mapped type where all properties are Optional */
Expand All @@ -2707,15 +2707,15 @@ export class StandardTypeBuilder extends TypeBuilder {
public Pick(schema: TSchema, unresolved: any, options: SchemaOptions = {}): any {
const keys = KeyArrayResolver.Resolve(unresolved)
// prettier-ignore
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
if (schema.required) {
schema.required = schema.required.filter((key: any) => keys.includes(key))
if (schema.required.length === 0) delete schema.required
return ObjectMap.Map(TypeClone.Clone(schema), (object) => {
if (ValueGuard.IsArray(object.required)) {
object.required = object.required.filter((key: any) => keys.includes(key))
if (object.required.length === 0) delete object.required
}
for (const key of Object.getOwnPropertyNames(schema.properties)) {
if (!keys.includes(key as any)) delete schema.properties[key]
for (const key of Object.getOwnPropertyNames(object.properties)) {
if (!keys.includes(key as any)) delete object.properties[key]
}
return this.Create(schema)
return this.Create(object)
}, options)
}
/** `[Standard]` Creates a Record type */
Expand All @@ -2734,23 +2734,23 @@ export class StandardTypeBuilder extends TypeBuilder {
const expression = TemplateLiteralParser.ParseExact(key.pattern)
// prettier-ignore
return TemplateLiteralFinite.Check(expression)
? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema, {}) }), {} as TProperties), options))
: this.Create<any>({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) }})
? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema) }), {} as TProperties), options))
: this.Create<any>({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema) }})
} else if (TypeGuard.TUnion(key)) {
const union = UnionResolver.Resolve(key)
if (TypeGuard.TUnionLiteral(union)) {
const properties = union.anyOf.reduce((acc: any, literal: any) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {} as TProperties)
const properties = union.anyOf.reduce((acc: any, literal: any) => ({ ...acc, [literal.const]: TypeClone.Clone(schema) }), {} as TProperties)
return this.Object(properties, { ...options, [Hint]: 'Record' })
} else throw Error('StandardTypeBuilder: Record key of type union contains non-literal types')
} else if (TypeGuard.TLiteral(key)) {
if (ValueGuard.IsString(key.const) || ValueGuard.IsNumber(key.const)) {
return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options)
return this.Object({ [key.const]: TypeClone.Clone(schema) }, options)
} else throw Error('StandardTypeBuilder: Record key of type literal is not of type string or number')
} else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
return this.Create<any>({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [PatternNumberExact]: TypeClone.Clone(schema, {}) } })
return this.Create<any>({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [PatternNumberExact]: TypeClone.Clone(schema) } })
} else if (TypeGuard.TString(key)) {
const pattern = ValueGuard.IsUndefined(key.pattern) ? PatternStringExact : key.pattern
return this.Create<any>({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } })
return this.Create<any>({ ...options, [Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema) } })
} else {
throw Error(`StandardTypeBuilder: Record key is an invalid type`)
}
Expand Down Expand Up @@ -2786,9 +2786,9 @@ export class StandardTypeBuilder extends TypeBuilder {
public Rest<T extends TSchema>(schema: T): TRest<T> {
if (TypeGuard.TTuple(schema)) {
if (ValueGuard.IsUndefined(schema.items)) return [] as TSchema[] as TRest<T>
return schema.items.map((schema) => TypeClone.Clone(schema, {})) as TRest<T>
return schema.items.map((schema) => TypeClone.Clone(schema)) as TRest<T>
} else {
return [TypeClone.Clone(schema, {})] as TRest<T>
return [TypeClone.Clone(schema)] as TRest<T>
}
}
/** `[Standard]` Creates a String type */
Expand All @@ -2810,7 +2810,7 @@ export class StandardTypeBuilder extends TypeBuilder {
/** `[Standard]` Creates a Tuple type */
public Tuple<T extends TSchema[]>(items: [...T], options: SchemaOptions = {}): TTuple<T> {
const [additionalItems, minItems, maxItems] = [false, items.length, items.length]
const clonedItems = items.map((item) => TypeClone.Clone(item, {}))
const clonedItems = items.map((item) => TypeClone.Clone(item))
// prettier-ignore
const schema = (items.length > 0 ?
{ ...options, [Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } :
Expand All @@ -2832,7 +2832,7 @@ export class StandardTypeBuilder extends TypeBuilder {
const anyOf = union
if (anyOf.length === 0) return this.Never(options)
if (anyOf.length === 1) return this.Create(TypeClone.Clone(anyOf[0], options))
const clonedAnyOf = anyOf.map((schema) => TypeClone.Clone(schema, {}))
const clonedAnyOf = anyOf.map((schema) => TypeClone.Clone(schema))
return this.Create({ ...options, [Kind]: 'Union', anyOf: clonedAnyOf })
}
}
Expand All @@ -2851,7 +2851,7 @@ export class StandardTypeBuilder extends TypeBuilder {
export class ExtendedTypeBuilder extends StandardTypeBuilder {
/** `[Extended]` Creates a AsyncIterator type */
public AsyncIterator<T extends TSchema>(items: T, options: SchemaOptions = {}): TAsyncIterator<T> {
return this.Create({ ...options, [Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Clone(items, {}) })
return this.Create({ ...options, [Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Clone(items) })
}
/** `[Extended]` Creates a BigInt type */
public BigInt(options: NumericOptions<bigint> = {}): TBigInt {
Expand All @@ -2863,8 +2863,8 @@ export class ExtendedTypeBuilder extends StandardTypeBuilder {
}
/** `[Extended]` Creates a Constructor type */
public Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U> {
const clonedReturns = TypeClone.Clone(returns, {})
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}))
const clonedReturns = TypeClone.Clone(returns)
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter))
return this.Create({ ...options, [Kind]: 'Constructor', type: 'constructor', parameters: clonedParameters, returns: clonedReturns })
}
/** `[Extended]` Creates a Date type */
Expand All @@ -2874,7 +2874,7 @@ export class ExtendedTypeBuilder extends StandardTypeBuilder {
/** `[Extended]` Creates a Function type */
public Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U> {
const clonedReturns = TypeClone.Clone(returns, {})
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}))
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter))
return this.Create({ ...options, [Kind]: 'Function', type: 'function', parameters: clonedParameters, returns: clonedReturns })
}
/** `[Extended]` Extracts the InstanceType from the given Constructor */
Expand All @@ -2883,15 +2883,15 @@ export class ExtendedTypeBuilder extends StandardTypeBuilder {
}
/** `[Extended]` Creates an Iterator type */
public Iterator<T extends TSchema>(items: T, options: SchemaOptions = {}): TIterator<T> {
return this.Create({ ...options, [Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Clone(items, {}) })
return this.Create({ ...options, [Kind]: 'Iterator', type: 'Iterator', items: TypeClone.Clone(items) })
}
/** `[Extended]` Extracts the Parameters from the given Function type */
public Parameters<T extends TFunction<any[], any>>(schema: T, options: SchemaOptions = {}): TParameters<T> {
return this.Tuple(schema.parameters, { ...options })
}
/** `[Extended]` Creates a Promise type */
public Promise<T extends TSchema>(item: T, options: SchemaOptions = {}): TPromise<T> {
return this.Create({ ...options, [Kind]: 'Promise', type: 'Promise', item: TypeClone.Clone(item, {}) })
return this.Create({ ...options, [Kind]: 'Promise', type: 'Promise', item: TypeClone.Clone(item) })
}
/** `[Extended]` Creates a String pattern type from Regular Expression */
public RegExp(pattern: string, options?: SchemaOptions): TString
Expand Down

0 comments on commit 4e21fe3

Please sign in to comment.