decode_codepoint.js 2.3 KB

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