@@ -31,47 +31,45 @@ export interface EscapeOptions {
3131 * for the complete original algorithm.
3232 */
3333export function escapeString ( rawValue : any , options : EscapeOptions = { } ) {
34- const {
35- delimiter = '"' ,
36- escapeChar = '\\' ,
37- escapeNewlines = true
38- } = options ;
34+ const { delimiter = '"' , escapeChar = '\\' , escapeNewlines = true } = options ;
3935
4036 const stringValue = rawValue . toString ( ) ;
4137
42- return [ ...stringValue ] . map ( ( c ) => {
43- if ( c === '\b' ) {
44- return escapeChar + 'b' ;
45- } else if ( c === '\t' ) {
46- return escapeChar + 't' ;
47- } else if ( c === '\n' ) {
48- if ( escapeNewlines ) {
49- return escapeChar + 'n' ;
50- } else {
51- return c ; // Don't just continue, or this is caught by < \u0020
52- }
53- } else if ( c === '\f' ) {
54- return escapeChar + 'f' ;
55- } else if ( c === '\r' ) {
56- if ( escapeNewlines ) {
57- return escapeChar + 'r' ;
58- } else {
59- return c ; // Don't just continue, or this is caught by < \u0020
60- }
61- } else if ( c === escapeChar ) {
62- return escapeChar + escapeChar ;
63- } else if ( c === delimiter ) {
64- return escapeChar + delimiter ;
65- } else if ( c < '\u0020' || c > '\u007E' ) {
66- // Delegate the trickier non-ASCII cases to the normal algorithm. Some of these
67- // are escaped as \uXXXX, whilst others are represented literally. Since we're
68- // using this primarily for header values that are generally (though not 100%
69- // strictly?) ASCII-only, this should almost never happen.
70- return JSON . stringify ( c ) . slice ( 1 , - 1 ) ;
71- } else {
72- return c ;
73- }
74- } ) . join ( '' ) ;
38+ return [ ...stringValue ]
39+ . map ( c => {
40+ if ( c === '\b' ) {
41+ return `${ escapeChar } b` ;
42+ } else if ( c === '\t' ) {
43+ return `${ escapeChar } t` ;
44+ } else if ( c === '\n' ) {
45+ if ( escapeNewlines ) {
46+ return `${ escapeChar } n` ;
47+ }
48+ return c ; // Don't just continue, or this is caught by < \u0020
49+
50+ } else if ( c === '\f' ) {
51+ return `${ escapeChar } f` ;
52+ } else if ( c === '\r' ) {
53+ if ( escapeNewlines ) {
54+ return `${ escapeChar } r` ;
55+ }
56+ return c ; // Don't just continue, or this is caught by < \u0020
57+
58+ } else if ( c === escapeChar ) {
59+ return escapeChar + escapeChar ;
60+ } else if ( c === delimiter ) {
61+ return escapeChar + delimiter ;
62+ } else if ( c < '\u0020' || c > '\u007E' ) {
63+ // Delegate the trickier non-ASCII cases to the normal algorithm. Some of these
64+ // are escaped as \uXXXX, whilst others are represented literally. Since we're
65+ // using this primarily for header values that are generally (though not 100%
66+ // strictly?) ASCII-only, this should almost never happen.
67+ return JSON . stringify ( c ) . slice ( 1 , - 1 ) ;
68+ }
69+ return c ;
70+
71+ } )
72+ . join ( '' ) ;
7573}
7674
7775/**
@@ -81,8 +79,7 @@ export function escapeString(rawValue: any, options: EscapeOptions = {}) {
8179 *
8280 * If value is not a string, it will be stringified with .toString() first.
8381 */
84- export const escapeForSingleQuotes = ( value : any ) =>
85- escapeString ( value , { delimiter : "'" } ) ;
82+ export const escapeForSingleQuotes = ( value : any ) => escapeString ( value , { delimiter : "'" } ) ;
8683
8784/**
8885 * Make a string value safe to insert literally into a snippet within double quotes,
@@ -91,5 +88,4 @@ export const escapeForSingleQuotes = (value: any) =>
9188 *
9289 * If value is not a string, it will be stringified with .toString() first.
9390 */
94- export const escapeForDoubleQuotes = ( value : any ) =>
95- escapeString ( value , { delimiter : '"' } ) ;
91+ export const escapeForDoubleQuotes = ( value : any ) => escapeString ( value , { delimiter : '"' } ) ;
0 commit comments