packages/devtools-reps/src/reps/rep-utils.js
function escapeString(str, escapeWhitespace) {
  return `"${str.replace(escapeRegexp, (match, offset) => {
    const c = match.charCodeAt(0);
    if (c in escapeMap) {
      if (!escapeWhitespace && (c === 9 || c === 0xa || c === 0xd)) {
        return match[0];
      }
      return escapeMap[c];
    }
    if (c >= 0xd800 && c <= 0xdfff) {
      // Find the full code point containing the surrogate, with a
      // special case for a trailing surrogate at the start of the
      // string.
      if (c >= 0xdc00 && offset > 0) {
        --offset;
      }
      const codePoint = str.codePointAt(offset);
      if (codePoint >= 0xd800 && codePoint <= 0xdfff) {
        // Unpaired surrogate.
        return `\\u${codePoint.toString(16)}`;
      } else if (codePoint >= 0xf0000 && codePoint <= 0x10fffd) {
        // Private use area.  Because we visit each pair of a such a
        // character, return the empty string for one half and the
        // real result for the other, to avoid duplication.
        if (c <= 0xdbff) {
          return `\\u{${codePoint.toString(16)}}`;
        }
        return "";
      }
      // Other surrogate characters are passed through.
      return match;
    }
    return `\\u${`0000${c.toString(16)}`.substr(-4)}`;
  })}"`;
}
function escapeString(str, escapeWhitespace) {
  return `"${str.replace(escapeRegexp, (match, offset) => {
    const c = match.charCodeAt(0);
    if (c in escapeMap) {
      if (!escapeWhitespace && (c === 9 || c === 0xa || c === 0xd)) {
        return match[0];
      }
      return escapeMap[c];
    }
    if (c >= 0xd800 && c <= 0xdfff) {
      // Find the full code point containing the surrogate, with a
      // special case for a trailing surrogate at the start of the
      // string.
      if (c >= 0xdc00 && offset > 0) {
        --offset;
      }
      const codePoint = str.codePointAt(offset);
      if (codePoint >= 0xd800 && codePoint <= 0xdfff) {
        // Unpaired surrogate.
        return `\\u${codePoint.toString(16)}`;
      } else if (codePoint >= 0xf0000 && codePoint <= 0x10fffd) {
        // Private use area.  Because we visit each pair of a such a
        // character, return the empty string for one half and the
        // real result for the other, to avoid duplication.
        if (c <= 0xdbff) {
          return `\\u{${codePoint.toString(16)}}`;
        }
        return "";
      }
      // Other surrogate characters are passed through.
      return match;
    }
    return `\\u${`0000${c.toString(16)}`.substr(-4)}`;
  })}"`;
}
function escapeString ( str , escapeWhitespace ) { return ` ${ str . replace ( escapeRegexp , ( match , offset ) => { const c = match . charCodeAt ( 0 ) ; if ( c in escapeMap ) { if ( ! escapeWhitespace && ( c === 9 || c === 0xa || c === 0xd ) ) { return match [ 0 ] ; } return escapeMap [ c ] ; } if ( c >= 0xd800 && c <= 0xdfff ) { // Find the full code point containing the surrogate, with a // special case for a trailing surrogate at the start of the // string. if ( c >= 0xdc00 && offset > 0 ) { -- offset ; } const codePoint = str . codePointAt ( offset ) ; if ( codePoint >= 0xd800 && codePoint <= 0xdfff ) { // Unpaired surrogate. return ` \\ ${ codePoint . toString ( 16 ) } ` ; } else if ( codePoint >= 0xf0000 && codePoint <= 0x10fffd ) { // Private use area.  Because we visit each pair of a such a // character, return the empty string for one half and the // real result for the other, to avoid duplication. if ( c <= 0xdbff ) { return ` \\ ${ codePoint . toString ( 16 ) } ` ; } return "" ; } // Other surrogate characters are passed through. return match ; } return ` \\ ${ ` ${ c . toString ( 16 ) } ` . substr ( - 4 ) } ` ; } ) } ` ; }
Escape a string so that the result is viewable and valid JS.
Control characters, other invisibles, invalid characters,
backslash, and double quotes are escaped.  The resulting string is
surrounded by double quotes.

@param {String} str
the input
@param {Boolean} escapeWhitespace
if true, TAB, CR, and NL characters will be escaped
@return {String} the escaped string
Escape a string so that the result is viewable and valid JS . Control characters other invisibles invalid characters backslash and double quotes are escaped . The resulting string is surrounded by double quotes .