@@ -156,7 +156,6 @@ const isDateType = (schema: TAnySchema): boolean => {
156156interface Instruction {
157157 array : number
158158 optional : number
159- hasString : boolean
160159 properties : string [ ]
161160 /**
162161 * If unsafe character is found, how should the encoder handle it?
@@ -173,12 +172,15 @@ interface Instruction {
173172 definitions : Record < string , TAnySchema >
174173}
175174
175+ // equivalent to /["\n\r\t\b\f\v]/
176+ const findEscapeSequence = / [ " \b \t \n \v \f \r \/ ] /
177+
176178const SANITIZE = {
177179 auto : ( property : string ) =>
178- `re .test(${ property } )?JSON.stringify(${ property } ):\`"$\{ ${ property } }"\` ` ,
180+ `${ findEscapeSequence } .test(${ property } )?JSON.stringify(${ property } ).slice(1,-1): ${ property } ` ,
179181 manual : ( property : string ) => `${ property } ` ,
180182 throw : ( property : string ) =>
181- `re .test(${ property } )?(()=>{throw new Error("Property '${ property } ' contains invalid characters")})():${ property } `
183+ `${ findEscapeSequence } .test(${ property } )?(()=>{throw new Error("Property '${ property } ' contains invalid characters")})():${ property } `
182184} satisfies Record < Instruction [ 'sanitize' ] , ( v : string ) => string >
183185
184186const joinStringArray = ( p : string ) =>
@@ -260,8 +262,6 @@ const accelerate = (
260262
261263 switch ( schema . type ) {
262264 case 'string' :
263- if ( ! schema . const && ! schema . trusted ) instruction . hasString = true
264-
265265 // string operation would be repeated multiple time
266266 // it's fine to optimize it to the most optimized way
267267 if (
@@ -280,13 +280,13 @@ const accelerate = (
280280 sanitize = ( v : string ) =>
281281 `\`"$\{${ SANITIZE [ 'manual' ] ( v ) } }"\``
282282
283- v = `\${${ nullableCondition } ?${ schema . const !== undefined ? `'${ JSON . stringify ( schema . const ) } '` : schema . default !== undefined ? `'${ JSON . stringify ( schema . default ) } '` : `'null'` } :${ sanitize ( property ) } }`
283+ v = `\${${ nullableCondition } ?${ schema . const !== undefined ? `'${ JSON . stringify ( schema . const ) } '` : schema . default !== undefined ? `'${ JSON . stringify ( schema . default ) } '` : `'null'` } :\`"\${ ${ sanitize ( property ) } }"\` }`
284284 } else {
285285 if ( schema . const !== undefined )
286286 v = JSON . stringify ( schema . const )
287287 else if ( schema . trusted )
288288 v = `"\${${ SANITIZE [ 'manual' ] ( property ) } }"`
289- else v = `\${${ sanitize ( property ) } }`
289+ else v = `" \${${ sanitize ( property ) } }" `
290290 }
291291 } else {
292292 // In this case quote is handle outside to improve performance
@@ -356,7 +356,8 @@ const accelerate = (
356356 const name = joinProperty ( property , key )
357357 const hasShortName =
358358 schema . properties [ key ] . type === 'object' &&
359- ! name . startsWith ( 'ar' )
359+ ! name . startsWith ( 'ar' ) &&
360+ Object . keys ( schema . properties ) . length > 5
360361
361362 const i = instruction . properties . length
362363 if ( hasShortName ) instruction . properties . push ( name )
@@ -487,9 +488,6 @@ const accelerate = (
487488
488489 let setup = ''
489490
490- if ( instruction . hasString )
491- setup += `const re=/[\\b\\f\\n\\r\\t\\\\\\\\/"]/\n`
492-
493491 if ( instruction . optional ) {
494492 setup += 'let '
495493
@@ -528,7 +526,6 @@ export const createAccelerator = <T extends TAnySchema>(
528526 array : 0 ,
529527 optional : 0 ,
530528 properties : [ ] ,
531- hasString : false ,
532529 sanitize,
533530 definitions
534531 } )
0 commit comments