File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -202,7 +202,7 @@ function escape(char: string) {
202
202
}
203
203
204
204
function stringifyPrimitive ( thing : any ) {
205
- if ( typeof thing === 'string' ) return JSON . stringify ( thing ) . replace ( unsafe , escape ) ;
205
+ if ( typeof thing === 'string' ) return stringifyString ( thing ) ;
206
206
if ( thing === void 0 ) return 'void 0' ;
207
207
if ( thing === 0 && 1 / thing < 0 ) return '-0' ;
208
208
const str = String ( thing ) ;
@@ -220,4 +220,27 @@ function safeKey(key: string) {
220
220
221
221
function safeProp ( key : string ) {
222
222
return / ^ [ _ $ a - z A - Z ] [ _ $ a - z A - Z 0 - 9 ] * $ / . test ( key ) ? `.${ key } ` : `[${ JSON . stringify ( key ) } ]` ;
223
+ }
224
+
225
+ function stringifyString ( str : string ) {
226
+ let result = '"' ;
227
+
228
+ for ( let i = 0 ; i < str . length ; i += 1 ) {
229
+ const char = str [ i ] ;
230
+ const code = char . charCodeAt ( 0 ) ;
231
+
232
+ if ( char === '"' ) {
233
+ result += '\\"' ;
234
+ } else if ( char in escaped ) {
235
+ result += escaped [ char ] ;
236
+ } else if ( ( code >= 0xD800 && code <= 0xDBFF ) && i < str . length - 1 ) {
237
+ // escape lone surrogates
238
+ result += `\\\\u${ code . toString ( 16 ) . toUpperCase ( ) } ` ;
239
+ } else {
240
+ result += char ;
241
+ }
242
+ }
243
+
244
+ result += '"' ;
245
+ return result ;
223
246
}
You can’t perform that action at this time.
0 commit comments