decode-codepoint.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Adapted from https://github.com/mathiasbynens/he/blob/36afe179392226cf1b6ccdb16ebbb7a5a844d93a/src/he.js#L106-L134
  2. var _a;
  3. const decodeMap = new Map([
  4. [0, 65533],
  5. // C1 Unicode control character reference replacements
  6. [128, 8364],
  7. [130, 8218],
  8. [131, 402],
  9. [132, 8222],
  10. [133, 8230],
  11. [134, 8224],
  12. [135, 8225],
  13. [136, 710],
  14. [137, 8240],
  15. [138, 352],
  16. [139, 8249],
  17. [140, 338],
  18. [142, 381],
  19. [145, 8216],
  20. [146, 8217],
  21. [147, 8220],
  22. [148, 8221],
  23. [149, 8226],
  24. [150, 8211],
  25. [151, 8212],
  26. [152, 732],
  27. [153, 8482],
  28. [154, 353],
  29. [155, 8250],
  30. [156, 339],
  31. [158, 382],
  32. [159, 376],
  33. ]);
  34. /**
  35. * Polyfill for `String.fromCodePoint`. It is used to create a string from a Unicode code point.
  36. */
  37. export const fromCodePoint =
  38. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, n/no-unsupported-features/es-builtins
  39. (_a = String.fromCodePoint) !== null && _a !== void 0 ? _a : function (codePoint) {
  40. let output = "";
  41. if (codePoint > 65535) {
  42. codePoint -= 65536;
  43. output += String.fromCharCode(((codePoint >>> 10) & 1023) | 55296);
  44. codePoint = 56320 | (codePoint & 1023);
  45. }
  46. output += String.fromCharCode(codePoint);
  47. return output;
  48. };
  49. /**
  50. * Replace the given code point with a replacement character if it is a
  51. * surrogate or is outside the valid range. Otherwise return the code
  52. * point unchanged.
  53. */
  54. export function replaceCodePoint(codePoint) {
  55. var _a;
  56. if ((codePoint >= 55296 && codePoint <= 57343) ||
  57. codePoint > 1114111) {
  58. return 65533;
  59. }
  60. return (_a = decodeMap.get(codePoint)) !== null && _a !== void 0 ? _a : codePoint;
  61. }
  62. /**
  63. * Replace the code point if relevant, then convert it to a string.
  64. *
  65. * @deprecated Use `fromCodePoint(replaceCodePoint(codePoint))` instead.
  66. * @param codePoint The code point to decode.
  67. * @returns The decoded code point.
  68. */
  69. export function decodeCodePoint(codePoint) {
  70. return fromCodePoint(replaceCodePoint(codePoint));
  71. }
  72. //# sourceMappingURL=decode-codepoint.js.map