Skip to content

Commit

Permalink
Optimize Intersect Encode and Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 4, 2024
1 parent e21b874 commit 85b5d8c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/type/keyof/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './keyof-from-mapped-result'
export * from './keyof-property-entries'
export * from './keyof-property-keys'
export * from './keyof'
42 changes: 42 additions & 0 deletions src/type/keyof/keyof-property-entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*--------------------------------------------------------------------------
@sinclair/typebox/type
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
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 { IndexFromPropertyKeys } from '../indexed/indexed'
import { KeyOfPropertyKeys } from './keyof-property-keys'
import { TSchema } from '../schema/index'

/**
* `[Utility]` Resolves an array of keys and schemas from the given schema. This method is faster
* than obtaining the keys and resolving each individually via indexing. This method was written
* accellerate Intersect and Union encoding.
*/
export function KeyOfPropertyEntries(schema: TSchema): [key: string, schema: TSchema][] {
const keys = KeyOfPropertyKeys(schema) as string[]
const schemas = IndexFromPropertyKeys(schema, keys)
return keys.map((_, index) => [keys[index], schemas[index]])
}
9 changes: 5 additions & 4 deletions src/value/transform/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ THE SOFTWARE.
import { Kind, TransformKind } from '../../type/symbols/index'
import { TypeBoxError } from '../../type/error/index'
import { ValueError } from '../../errors/index'
import { KeyOfPropertyKeys } from '../../type/keyof/index'
import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index'
import { Index } from '../../type/indexed/index'
import { Deref } from '../deref/index'
import { Check } from '../check/index'
Expand Down Expand Up @@ -98,10 +98,11 @@ function FromArray(schema: TArray, references: TSchema[], path: string, value: a
// prettier-ignore
function FromIntersect(schema: TIntersect, references: TSchema[], path: string, value: any) {
if (!IsStandardObject(value) || IsValueType(value)) return Default(schema, path, value)
const knownKeys = KeyOfPropertyKeys(schema) as string[]
const knownEntries = KeyOfPropertyEntries(schema)
const knownKeys = knownEntries.map(entry => entry[0])
const knownProperties = { ...value } as Record<PropertyKey, unknown>
for(const key of knownKeys) if(key in knownProperties) {
knownProperties[key] = Visit(Index(schema, [key]), references, `${path}/${key}`, knownProperties[key])
for(const [knownKey, knownSchema] of knownEntries) if(knownKey in knownProperties) {
knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey])
}
if (!IsTransform(schema.unevaluatedProperties)) {
return Default(schema, path, knownProperties)
Expand Down
9 changes: 5 additions & 4 deletions src/value/transform/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ THE SOFTWARE.
import { Kind, TransformKind } from '../../type/symbols/index'
import { TypeBoxError } from '../../type/error/index'
import { ValueError } from '../../errors/index'
import { KeyOfPropertyKeys } from '../../type/keyof/index'
import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index'
import { Index } from '../../type/indexed/index'
import { Deref } from '../deref/index'
import { Check } from '../check/index'
Expand Down Expand Up @@ -99,10 +99,11 @@ function FromArray(schema: TArray, references: TSchema[], path: string, value: a
function FromIntersect(schema: TIntersect, references: TSchema[], path: string, value: any) {
const defaulted = Default(schema, path, value)
if (!IsStandardObject(value) || IsValueType(value)) return defaulted
const knownKeys = KeyOfPropertyKeys(schema) as string[]
const knownEntries = KeyOfPropertyEntries(schema)
const knownKeys = knownEntries.map(entry => entry[0])
const knownProperties = { ...defaulted } as Record<PropertyKey, unknown>
for(const key of knownKeys) if(key in knownProperties) {
knownProperties[key] = Visit(Index(schema, [key]), references, `${path}/${key}`, knownProperties[key])
for(const [knownKey, knownSchema] of knownEntries) if(knownKey in knownProperties) {
knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey])
}
if (!IsTransform(schema.unevaluatedProperties)) {
return Default(schema, path, knownProperties)
Expand Down
6 changes: 3 additions & 3 deletions src/value/value/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TransformDecode, TransformEncode, TransformDecodeCheckError, TransformEncodeCheckError } from '../transform/index'
import { HasTransform, TransformDecode, TransformEncode, TransformDecodeCheckError, TransformEncodeCheckError } from '../transform/index'
import { Mutate as MutateValue, type Mutable } from '../mutate/index'
import { Hash as HashValue } from '../hash/index'
import { Equal as EqualValue } from '../equal/index'
Expand Down Expand Up @@ -95,7 +95,7 @@ export function Decode<T extends TSchema, R = StaticDecode<T>>(schema: T, value:
export function Decode(...args: any[]) {
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]]
if (!Check(schema, references, value)) throw new TransformDecodeCheckError(schema, value, Errors(schema, references, value).First()!)
return TransformDecode(schema, references, value)
return HasTransform(schema, references) ? TransformDecode(schema, references, value) : value
}
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
export function Default(schema: TSchema, references: TSchema[], value: unknown): unknown
Expand All @@ -112,7 +112,7 @@ export function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, value:
/** Encodes a value or throws if error */
export function Encode(...args: any[]) {
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]]
const encoded = TransformEncode(schema, references, value)
const encoded = HasTransform(schema, references) ? TransformEncode(schema, references, value) : value
if (!Check(schema, references, encoded)) throw new TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First()!)
return encoded
}
Expand Down

0 comments on commit 85b5d8c

Please sign in to comment.