@@ -21,7 +21,7 @@ const sanitize = (key: string, sanitize = 0, schema: TAnySchema) => {
2121 if ( schema . type !== 'string' || schema . const || schema . trusted ) return key
2222
2323 let hof = ''
24- for ( let i = sanitize - 1 ; i >= 0 ; i -- ) hof += `sanitize[ ${ i } ] (`
24+ for ( let i = sanitize - 1 ; i >= 0 ; i -- ) hof += `d.h ${ i } (`
2525 return hof + key + ')' . repeat ( sanitize )
2626}
2727
@@ -66,7 +66,7 @@ export interface Instruction {
6666 optionalsInArray : string [ ] [ ]
6767 parentIsOptional : boolean
6868 array : number
69- unions : string [ ] [ ]
69+ unions : TypeCheck < any > [ ] [ ]
7070 unionKeys : Record < string , 1 >
7171 sanitize : MaybeArray < ( v : string ) => string > | undefined
7272 /**
@@ -201,13 +201,6 @@ export function deepClone<T>(source: T, weak = new WeakMap<object, any>()): T {
201201 return source
202202}
203203
204- // Rename typebox function
205- const renameFunction = ( v : string , name : string ) => {
206- if ( ! v . startsWith ( 'return' ) ) v = v . replace ( / c h e c k _ T 0 / g, `_${ name } ` )
207-
208- return v . replace ( 'return function check(' , `function ${ name } (` )
209- }
210-
211204const handleUnion = (
212205 schemas : TAnySchema [ ] ,
213206 property : string ,
@@ -236,7 +229,7 @@ const handleUnion = (
236229 instruction . unionKeys [ property ] = 1
237230
238231 const ui = instruction . unions . length
239- const typeChecks = ( instruction . unions [ ui ] = < string [ ] > [ ] )
232+ const typeChecks = ( instruction . unions [ ui ] = < TypeCheck < any > [ ] > [ ] )
240233
241234 let v = `(()=>{\n`
242235
@@ -274,14 +267,16 @@ const handleUnion = (
274267 else type . items = unwrapRef ( type . items )
275268 }
276269
277- const name = `un_${ ui } _${ i } `
278- typeChecks . push ( renameFunction ( TypeCompiler . Code ( type ) , name ) )
279-
280- v += `if(${ name } (${ property } )){return ${ mirror ( type , property , {
281- ...instruction ,
282- recursion : instruction . recursion + 1 ,
283- parentIsOptional : true
284- } ) } }\n`
270+ typeChecks . push ( TypeCompiler . Compile ( type ) )
271+ v += `if(d.unions[${ ui } ][${ i } ].Check(${ property } )){return ${ mirror (
272+ type ,
273+ property ,
274+ {
275+ ...instruction ,
276+ recursion : instruction . recursion + 1 ,
277+ parentIsOptional : true
278+ }
279+ ) } }\n`
285280 }
286281
287282 // unknown type, return as-is (this is a default intended behavior)
@@ -505,16 +500,15 @@ const mirror = (
505500 return `${ v } return x`
506501}
507502
508- export const createMirrorCode = (
509- schema : TAnySchema ,
503+ export const createMirror = < T extends TAnySchema > (
504+ schema : T ,
510505 {
511506 TypeCompiler,
512507 modules,
513508 definitions,
514509 sanitize,
515510 recursionLimit = 8 ,
516- removeUnknownUnionType = false ,
517- name = 'mirror'
511+ removeUnknownUnionType = false
518512 } : Partial <
519513 Pick <
520514 Instruction ,
@@ -524,16 +518,14 @@ export const createMirrorCode = (
524518 | 'modules'
525519 | 'recursionLimit'
526520 | 'removeUnknownUnionType'
527- > & {
528- name ?: string
529- }
521+ >
530522 > = { }
531- ) : string | [ string , ( ( v : string ) => string ) [ ] ] => {
523+ ) : ( ( v : T [ 'static' ] ) => T [ 'static' ] ) => {
532524 const unions = < Instruction [ 'unions' ] > [ ]
533525
534526 if ( typeof sanitize === 'function' ) sanitize = [ sanitize ]
535527
536- let f = mirror ( schema , 'v' , {
528+ const f = mirror ( schema , 'v' , {
537529 optionals : [ ] ,
538530 optionalsInArray : [ ] ,
539531 array : 0 ,
@@ -550,59 +542,21 @@ export const createMirrorCode = (
550542 removeUnknownUnionType
551543 } )
552544
553- if ( unions . length ) {
554- let header = ''
545+ if ( ! unions . length && ! sanitize ?. length ) return Function ( 'v' , f ) as any
555546
556- for ( let i = 0 ; i < unions . length ; i ++ ) {
557- for ( let j = 0 ; j < unions [ i ] . length ; j ++ )
558- header += unions [ i ] [ j ] + '\n'
559- }
560-
561- f = header + f
562-
563- unions . length = 0 // clear unions to avoid memory leak
547+ let hof : Record < string , Function > | undefined
548+ if ( sanitize ?. length ) {
549+ hof = { }
550+ for ( let i = 0 ; i < sanitize . length ; i ++ ) hof [ `h${ i } ` ] = sanitize [ i ]
564551 }
565552
566- f = `function ${ name } (v){${ f } }`
567-
568- if ( ! sanitize ?. length ) return f
569-
570- return [ `function ${ name } (sanitize){return ${ f } }` , sanitize ] as const
571- }
572-
573- export const createMirror = < T extends TAnySchema > (
574- schema : T ,
575- {
576- TypeCompiler,
577- modules,
578- definitions,
579- sanitize,
580- recursionLimit = 8 ,
581- removeUnknownUnionType = false
582- } : Partial <
583- Pick <
584- Instruction ,
585- | 'TypeCompiler'
586- | 'definitions'
587- | 'sanitize'
588- | 'modules'
589- | 'recursionLimit'
590- | 'removeUnknownUnionType'
591- >
592- > = { }
593- ) : ( ( v : T [ 'static' ] ) => T [ 'static' ] ) => {
594- const code = createMirrorCode ( schema , {
595- TypeCompiler,
596- modules,
597- definitions,
598- sanitize,
599- recursionLimit,
600- removeUnknownUnionType
601- } )
602-
603- if ( typeof code === 'string' ) return Function ( `return ${ code } ` ) ( ) as any
604-
605- return Function ( `return ${ code [ 0 ] } ` ) ( ) ( code [ 1 ] )
553+ return Function (
554+ 'd' ,
555+ `return function mirror(v){${ f } }`
556+ ) ( {
557+ unions,
558+ ...hof
559+ } ) as any
606560}
607561
608562export default createMirror
0 commit comments