@@ -21,67 +21,15 @@ module.exports = stringify
21
21
22
22
// eslint-disable-next-line no-control-regex
23
23
const strEscapeSequencesRegExp = / [ \u0000 - \u001f \u0022 \u005c \ud800 - \udfff ] | [ \ud800 - \udbff ] (? ! [ \udc00 - \udfff ] ) | (?: [ ^ \ud800 - \udbff ] | ^ ) [ \udc00 - \udfff ] /
24
- const strEscapeSequencesReplacer = new RegExp ( strEscapeSequencesRegExp , 'g' )
25
-
26
- // Escaped special characters. Use empty strings to fill up unused entries.
27
- const meta = [
28
- '\\u0000' , '\\u0001' , '\\u0002' , '\\u0003' , '\\u0004' ,
29
- '\\u0005' , '\\u0006' , '\\u0007' , '\\b' , '\\t' ,
30
- '\\n' , '\\u000b' , '\\f' , '\\r' , '\\u000e' ,
31
- '\\u000f' , '\\u0010' , '\\u0011' , '\\u0012' , '\\u0013' ,
32
- '\\u0014' , '\\u0015' , '\\u0016' , '\\u0017' , '\\u0018' ,
33
- '\\u0019' , '\\u001a' , '\\u001b' , '\\u001c' , '\\u001d' ,
34
- '\\u001e' , '\\u001f' , '' , '' , '\\"' ,
35
- '' , '' , '' , '' , '' , '' , '' , '' , '' , '' ,
36
- '' , '' , '' , '' , '' , '' , '' , '' , '' , '' ,
37
- '' , '' , '' , '' , '' , '' , '' , '' , '' , '' ,
38
- '' , '' , '' , '' , '' , '' , '' , '' , '' , '' ,
39
- '' , '' , '' , '' , '' , '' , '' , '' , '' , '' ,
40
- '' , '' , '' , '' , '' , '' , '' , '\\\\'
41
- ]
42
-
43
- function escapeFn ( str ) {
44
- if ( str . length === 2 ) {
45
- const charCode = str . charCodeAt ( 1 )
46
- return `${ str [ 0 ] } \\u${ charCode . toString ( 16 ) } `
47
- }
48
- const charCode = str . charCodeAt ( 0 )
49
- return meta . length > charCode
50
- ? meta [ charCode ]
51
- : `\\u${ charCode . toString ( 16 ) } `
52
- }
53
24
54
25
// Escape C0 control characters, double quotes, the backslash and every code
55
26
// unit with a numeric value in the inclusive range 0xD800 to 0xDFFF.
56
27
function strEscape ( str ) {
57
28
// Some magic numbers that worked out fine while benchmarking with v8 8.0
58
29
if ( str . length < 5000 && ! strEscapeSequencesRegExp . test ( str ) ) {
59
- return str
60
- }
61
- if ( str . length > 100 ) {
62
- return str . replace ( strEscapeSequencesReplacer , escapeFn )
63
- }
64
- let result = ''
65
- let last = 0
66
- for ( let i = 0 ; i < str . length ; i ++ ) {
67
- const point = str . charCodeAt ( i )
68
- if ( point === 34 || point === 92 || point < 32 ) {
69
- result += `${ str . slice ( last , i ) } ${ meta [ point ] } `
70
- last = i + 1
71
- } else if ( point >= 0xd800 && point <= 0xdfff ) {
72
- if ( point <= 0xdbff && i + 1 < str . length ) {
73
- const nextPoint = str . charCodeAt ( i + 1 )
74
- if ( nextPoint >= 0xdc00 && nextPoint <= 0xdfff ) {
75
- i ++
76
- continue
77
- }
78
- }
79
- result += `${ str . slice ( last , i ) } \\u${ point . toString ( 16 ) } `
80
- last = i + 1
81
- }
30
+ return `"${ str } "`
82
31
}
83
- result += str . slice ( last )
84
- return result
32
+ return JSON . stringify ( str )
85
33
}
86
34
87
35
function insertSort ( array ) {
@@ -237,7 +185,7 @@ function configure (options) {
237
185
238
186
switch ( typeof value ) {
239
187
case 'string' :
240
- return `" ${ strEscape ( value ) } "`
188
+ return strEscape ( value )
241
189
case 'object' : {
242
190
if ( value === null ) {
243
191
return 'null'
@@ -313,7 +261,7 @@ function configure (options) {
313
261
const key = keys [ i ]
314
262
const tmp = stringifyFnReplacer ( key , value , stack , replacer , spacer , indentation )
315
263
if ( tmp !== undefined ) {
316
- res += `${ separator } " ${ strEscape ( key ) } " :${ whitespace } ${ tmp } `
264
+ res += `${ separator } ${ strEscape ( key ) } :${ whitespace } ${ tmp } `
317
265
separator = join
318
266
}
319
267
}
@@ -351,7 +299,7 @@ function configure (options) {
351
299
352
300
switch ( typeof value ) {
353
301
case 'string' :
354
- return `" ${ strEscape ( value ) } "`
302
+ return strEscape ( value )
355
303
case 'object' : {
356
304
if ( value === null ) {
357
305
return 'null'
@@ -407,7 +355,7 @@ function configure (options) {
407
355
for ( const key of replacer ) {
408
356
const tmp = stringifyArrayReplacer ( key , value [ key ] , stack , replacer , spacer , indentation )
409
357
if ( tmp !== undefined ) {
410
- res += `${ separator } " ${ strEscape ( key ) } " :${ whitespace } ${ tmp } `
358
+ res += `${ separator } ${ strEscape ( key ) } :${ whitespace } ${ tmp } `
411
359
separator = join
412
360
}
413
361
}
@@ -436,7 +384,7 @@ function configure (options) {
436
384
function stringifyIndent ( key , value , stack , spacer , indentation ) {
437
385
switch ( typeof value ) {
438
386
case 'string' :
439
- return `" ${ strEscape ( value ) } "`
387
+ return strEscape ( value )
440
388
case 'object' : {
441
389
if ( value === null ) {
442
390
return 'null'
@@ -512,7 +460,7 @@ function configure (options) {
512
460
const key = keys [ i ]
513
461
const tmp = stringifyIndent ( key , value [ key ] , stack , spacer , indentation )
514
462
if ( tmp !== undefined ) {
515
- res += `${ separator } " ${ strEscape ( key ) } " : ${ tmp } `
463
+ res += `${ separator } ${ strEscape ( key ) } : ${ tmp } `
516
464
separator = join
517
465
}
518
466
}
@@ -546,7 +494,7 @@ function configure (options) {
546
494
function stringifySimple ( key , value , stack ) {
547
495
switch ( typeof value ) {
548
496
case 'string' :
549
- return `" ${ strEscape ( value ) } "`
497
+ return strEscape ( value )
550
498
case 'object' : {
551
499
if ( value === null ) {
552
500
return 'null'
@@ -616,7 +564,7 @@ function configure (options) {
616
564
const key = keys [ i ]
617
565
const tmp = stringifySimple ( key , value [ key ] , stack )
618
566
if ( tmp !== undefined ) {
619
- res += `${ separator } " ${ strEscape ( key ) } " :${ tmp } `
567
+ res += `${ separator } ${ strEscape ( key ) } :${ tmp } `
620
568
separator = ','
621
569
}
622
570
}
0 commit comments