decode_codepoint.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, node/no-unsupported-features/es-builtins
  39. (_a = String.fromCodePoint) !== null && _a !== void 0 ? _a : function (codePoint) {
  40. let output = "";
  41. if (codePoint > 0xffff) {
  42. codePoint -= 0x10000;
  43. output += String.fromCharCode(((codePoint >>> 10) & 0x3ff) | 0xd800);
  44. codePoint = 0xdc00 | (codePoint & 0x3ff);
  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 >= 0xd800 && codePoint <= 0xdfff) || codePoint > 0x10ffff) {
  57. return 0xfffd;
  58. }
  59. return (_a = decodeMap.get(codePoint)) !== null && _a !== void 0 ? _a : codePoint;
  60. }
  61. /**
  62. * Replace the code point if relevant, then convert it to a string.
  63. *
  64. * @deprecated Use `fromCodePoint(replaceCodePoint(codePoint))` instead.
  65. * @param codePoint The code point to decode.
  66. * @returns The decoded code point.
  67. */
  68. export default function decodeCodePoint(codePoint) {
  69. return fromCodePoint(replaceCodePoint(codePoint));
  70. }
  71. //# sourceMappingURL=decode_codepoint.js.map