decode-codepoint.js 2.2 KB

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