Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 26, 2024
1 parent b2960fe commit b274aa6
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions src/value/parse/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,65 +41,72 @@ import { Clone } from '../clone/index'
// ------------------------------------------------------------------
import { IsArray } from '../guard/index'

// ------------------------------------------------------------------
// Error
// ------------------------------------------------------------------
export class ParseError extends TypeBoxError {
constructor(message: string) {
super(message)
}
}

// ------------------------------------------------------------------
// ParseRegistry
// ------------------------------------------------------------------
export type TParseOperation = 'Clone' | 'Clean' | 'Default' | 'Convert' | 'Assert' | 'Decode' | ({} & string)

export type TParseCallback = (schema: TSchema, references: TSchema[], value: unknown) => unknown
export type TParseFunction = (schema: TSchema, references: TSchema[], value: unknown) => unknown

// prettier-ignore
export namespace ParseRegistry {
const map = new Map<string, TParseCallback>([
['Clone', (_schema: TSchema, _references: TSchema[], value: unknown) => Clone(value)],
['Clean', (schema: TSchema, references: TSchema[], value: unknown) => Clean(schema, references, value)],
['Default', (schema: TSchema, references: TSchema[], value: unknown) => Default(schema, references, value)],
['Convert', (schema: TSchema, references: TSchema[], value: unknown) => Convert(schema, references, value)],
['Assert', (schema: TSchema, references: TSchema[], value: unknown) => { Assert(schema, references, value); return value }],
['Decode', (schema: TSchema, references: TSchema[], value: unknown) => (HasTransform(schema, references) ? TransformDecode(schema, references, value) : value)],
['Encode', (schema: TSchema, references: TSchema[], value: unknown) => (HasTransform(schema, references) ? TransformEncode(schema, references, value) : value)],
const registry = new Map<string, TParseFunction>([
['Clone', (_schema, _references, value: unknown) => Clone(value)],
['Clean', (schema, references, value: unknown) => Clean(schema, references, value)],
['Default', (schema, references, value: unknown) => Default(schema, references, value)],
['Convert', (schema, references, value: unknown) => Convert(schema, references, value)],
['Assert', (schema, references, value: unknown) => { Assert(schema, references, value); return value }],
['Decode', (schema, references, value: unknown) => (HasTransform(schema, references) ? TransformDecode(schema, references, value) : value)],
['Encode', (schema, references, value: unknown) => (HasTransform(schema, references) ? TransformEncode(schema, references, value) : value)],
])
// Deletes an entry from the registry
export function Delete(key: string): void {
map.delete(key)
registry.delete(key)
}
// Sets an entry in the registry
export function Set(key: string, callback: TParseCallback): void {
map.set(key, callback)
export function Set(key: string, callback: TParseFunction): void {
registry.set(key, callback)
}
// Gets an entry in the registry
export function Get(key: string): TParseCallback | undefined {
return map.get(key)
export function Get(key: string): TParseFunction | undefined {
return registry.get(key)
}
}

// ------------------------------------------------------------------
// ParseDefault: Default Sequence
// ------------------------------------------------------------------
// prettier-ignore
export const ParseDefault: TParseOperation[] = [
'Clone',
'Clean',
'Default',
'Convert',
'Assert',
export const ParseDefault = [
'Clone',
'Clean',
'Default',
'Convert',
'Assert',
'Decode'
]
] as const

// ------------------------------------------------------------------
// ParseValue
// ------------------------------------------------------------------
function ParseValue<Type extends TSchema, Result extends StaticDecode<Type> = StaticDecode<Type>>(keys: TParseOperation[], type: Type, references: TSchema[], value: unknown): Result {
return keys.reduce((value, key) => {
const reducer = ParseRegistry.Get(key)
if (reducer === undefined) throw new TypeBoxError(`Parse: Unable to resolve reduce key '${key}'`)
if (reducer === undefined) throw new TypeBoxError(`Parse: Unable to find parse operation '${key}'`)
return reducer(type, references, value)
}, value) as Result
}

// ------------------------------------------------------------------
// Parse
// ------------------------------------------------------------------

/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
export function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
Expand All @@ -115,7 +122,7 @@ export function Parse(...args: any[]): unknown {
args.length === 4 ? [args[0], args[1], args[2], args[3]] :
args.length === 3 ? IsArray(args[0]) ? [args[0], args[1], [], args[2]] : [ParseDefault, args[0], args[1], args[2]] :
args.length === 2 ? [ParseDefault, args[0], [], args[1]] :
(() => { throw new TypeBoxError('Parse: Invalid Arguments') }) ()
(() => { throw new TypeBoxError('Parse: Invalid Arguments') })()
)
return ParseValue(reducers, schema, references, value)
}

0 comments on commit b274aa6

Please sign in to comment.