index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. require("string.fromcodepoint");
  7. /**
  8. * \\ - matches the backslash which indicates the beginning of an escape sequence
  9. * (
  10. * u\{([0-9A-Fa-f]+)\} - first alternative; matches the variable-length hexadecimal escape sequence (\u{ABCD0})
  11. * |
  12. * u([0-9A-Fa-f]{4}) - second alternative; matches the 4-digit hexadecimal escape sequence (\uABCD)
  13. * |
  14. * x([0-9A-Fa-f]{2}) - third alternative; matches the 2-digit hexadecimal escape sequence (\xA5)
  15. * |
  16. * ([1-7][0-7]{0,2}|[0-7]{2,3}) - fourth alternative; matches the up-to-3-digit octal escape sequence (\5 or \512)
  17. * |
  18. * (['"tbrnfv0\\]) - fifth alternative; matches the special escape characters (\t, \n and so on)
  19. * |
  20. * \U([0-9A-Fa-f]+) - sixth alternative; matches the 8-digit hexadecimal escape sequence used by python (\U0001F3B5)
  21. * )
  22. */
  23. var jsEscapeRegex = /\\(u\{([0-9A-Fa-f]+)\}|u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([1-7][0-7]{0,2}|[0-7]{2,3})|(['"tbrnfv0\\]))|\\U([0-9A-Fa-f]{8})/g;
  24. var usualEscapeSequences = {
  25. '0': '\0',
  26. 'b': '\b',
  27. 'f': '\f',
  28. 'n': '\n',
  29. 'r': '\r',
  30. 't': '\t',
  31. 'v': '\v',
  32. '\'': '\'',
  33. '"': '"',
  34. '\\': '\\'
  35. };
  36. var fromHex = function fromHex(str) {
  37. return String.fromCodePoint(parseInt(str, 16));
  38. };
  39. var fromOct = function fromOct(str) {
  40. return String.fromCodePoint(parseInt(str, 8));
  41. };
  42. var _default = function _default(string) {
  43. return string.replace(jsEscapeRegex, function (_, __, varHex, longHex, shortHex, octal, specialCharacter, python) {
  44. if (varHex !== undefined) {
  45. return fromHex(varHex);
  46. } else if (longHex !== undefined) {
  47. return fromHex(longHex);
  48. } else if (shortHex !== undefined) {
  49. return fromHex(shortHex);
  50. } else if (octal !== undefined) {
  51. return fromOct(octal);
  52. } else if (python !== undefined) {
  53. return fromHex(python);
  54. } else {
  55. return usualEscapeSequences[specialCharacter];
  56. }
  57. });
  58. };
  59. exports.default = _default;
  60. module.exports = exports.default;