code: function escape(string) {
    return string.replace(/[^]/g, char => {
      const code = char.charCodeAt();
  
      if (code < 256) return char;
  
      const escape = code.toString(16);
      const long = escape.length > 2;
      return `\\${long ? 'u' : 'x'}${('0000' + escape).slice(long ? -4 : -2)}`;
    });
  }
docs: https://gist.github.com/mathiasbynens/1243213