unescape.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // https://github.com/iamakulov/unescape-js/blob/master/src/index.js
  2. /**
  3. * \\ - matches the backslash which indicates the beginning of an escape sequence
  4. * (
  5. * u\{([0-9A-Fa-f]+)\} - first alternative; matches the variable-length hexadecimal escape sequence (\u{ABCD0})
  6. * |
  7. * u([0-9A-Fa-f]{4}) - second alternative; matches the 4-digit hexadecimal escape sequence (\uABCD)
  8. * |
  9. * x([0-9A-Fa-f]{2}) - third alternative; matches the 2-digit hexadecimal escape sequence (\xA5)
  10. * |
  11. * ([1-7][0-7]{0,2}|[0-7]{2,3}) - fourth alternative; matches the up-to-3-digit octal escape sequence (\5 or \512)
  12. * |
  13. * (['"tbrnfv0\\]) - fifth alternative; matches the special escape characters (\t, \n and so on)
  14. * |
  15. * \U([0-9A-Fa-f]+) - sixth alternative; matches the 8-digit hexadecimal escape sequence used by python (\U0001F3B5)
  16. * )
  17. */
  18. const 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
  19. const usualEscapeSequences: Record<string, string> = {
  20. '0': '\0',
  21. 'b': '\b',
  22. 'f': '\f',
  23. 'n': '\n',
  24. 'r': '\r',
  25. 't': '\t',
  26. 'v': '\v',
  27. '\'': '\'',
  28. '"': '"',
  29. '\\': '\\',
  30. }
  31. const fromHex = (str: string) => String.fromCodePoint(parseInt(str, 16))
  32. const fromOct = (str: string) => String.fromCodePoint(parseInt(str, 8))
  33. const unescape = (str: string) => {
  34. return str.replace(jsEscapeRegex, (_, __, varHex, longHex, shortHex, octal, specialCharacter, python) => {
  35. if (varHex !== undefined)
  36. return fromHex(varHex)
  37. else if (longHex !== undefined)
  38. return fromHex(longHex)
  39. else if (shortHex !== undefined)
  40. return fromHex(shortHex)
  41. else if (octal !== undefined)
  42. return fromOct(octal)
  43. else if (python !== undefined)
  44. return fromHex(python)
  45. else
  46. return usualEscapeSequences[specialCharacter]
  47. })
  48. }
  49. export default unescape