@@ -156,7 +156,6 @@ const isDateType = (schema: TAnySchema): boolean => {
156
156
interface Instruction {
157
157
array : number
158
158
optional : number
159
- hasString : boolean
160
159
properties : string [ ]
161
160
/**
162
161
* If unsafe character is found, how should the encoder handle it?
@@ -173,12 +172,15 @@ interface Instruction {
173
172
definitions : Record < string , TAnySchema >
174
173
}
175
174
175
+ // equivalent to /["\n\r\t\b\f\v]/
176
+ const findEscapeSequence = / [ " \b \t \n \v \f \r \/ ] /
177
+
176
178
const SANITIZE = {
177
179
auto : ( property : string ) =>
178
- `re .test(${ property } )?JSON.stringify(${ property } ):\`"$\{ ${ property } }"\` ` ,
180
+ `${ findEscapeSequence } .test(${ property } )?JSON.stringify(${ property } ).slice(1,-1): ${ property } ` ,
179
181
manual : ( property : string ) => `${ property } ` ,
180
182
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 } `
182
184
} satisfies Record < Instruction [ 'sanitize' ] , ( v : string ) => string >
183
185
184
186
const joinStringArray = ( p : string ) =>
@@ -260,8 +262,6 @@ const accelerate = (
260
262
261
263
switch ( schema . type ) {
262
264
case 'string' :
263
- if ( ! schema . const && ! schema . trusted ) instruction . hasString = true
264
-
265
265
// string operation would be repeated multiple time
266
266
// it's fine to optimize it to the most optimized way
267
267
if (
@@ -280,13 +280,13 @@ const accelerate = (
280
280
sanitize = ( v : string ) =>
281
281
`\`"$\{${ SANITIZE [ 'manual' ] ( v ) } }"\``
282
282
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 ) } }"\` }`
284
284
} else {
285
285
if ( schema . const !== undefined )
286
286
v = JSON . stringify ( schema . const )
287
287
else if ( schema . trusted )
288
288
v = `"\${${ SANITIZE [ 'manual' ] ( property ) } }"`
289
- else v = `\${${ sanitize ( property ) } }`
289
+ else v = `" \${${ sanitize ( property ) } }" `
290
290
}
291
291
} else {
292
292
// In this case quote is handle outside to improve performance
@@ -356,7 +356,8 @@ const accelerate = (
356
356
const name = joinProperty ( property , key )
357
357
const hasShortName =
358
358
schema . properties [ key ] . type === 'object' &&
359
- ! name . startsWith ( 'ar' )
359
+ ! name . startsWith ( 'ar' ) &&
360
+ Object . keys ( schema . properties ) . length > 5
360
361
361
362
const i = instruction . properties . length
362
363
if ( hasShortName ) instruction . properties . push ( name )
@@ -487,9 +488,6 @@ const accelerate = (
487
488
488
489
let setup = ''
489
490
490
- if ( instruction . hasString )
491
- setup += `const re=/[\\b\\f\\n\\r\\t\\\\\\\\/"]/\n`
492
-
493
491
if ( instruction . optional ) {
494
492
setup += 'let '
495
493
@@ -528,7 +526,6 @@ export const createAccelerator = <T extends TAnySchema>(
528
526
array : 0 ,
529
527
optional : 0 ,
530
528
properties : [ ] ,
531
- hasString : false ,
532
529
sanitize,
533
530
definitions
534
531
} )
0 commit comments