智慧水务管理系统 - 精河县供水工程综合管理平台

decode-codepoint.ts 2.1KB

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