escape.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.escape = void 0;
  4. const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/;
  5. const strEscapeSequencesReplacer = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/g;
  6. const meta = [
  7. '\\u0000',
  8. '\\u0001',
  9. '\\u0002',
  10. '\\u0003',
  11. '\\u0004',
  12. '\\u0005',
  13. '\\u0006',
  14. '\\u0007',
  15. '\\b',
  16. '\\t',
  17. '\\n',
  18. '\\u000b',
  19. '\\f',
  20. '\\r',
  21. '\\u000e',
  22. '\\u000f',
  23. '\\u0010',
  24. '\\u0011',
  25. '\\u0012',
  26. '\\u0013',
  27. '\\u0014',
  28. '\\u0015',
  29. '\\u0016',
  30. '\\u0017',
  31. '\\u0018',
  32. '\\u0019',
  33. '\\u001a',
  34. '\\u001b',
  35. '\\u001c',
  36. '\\u001d',
  37. '\\u001e',
  38. '\\u001f',
  39. '',
  40. '',
  41. '\\"',
  42. '',
  43. '',
  44. '',
  45. '',
  46. '',
  47. '',
  48. '',
  49. '',
  50. '',
  51. '',
  52. '',
  53. '',
  54. '',
  55. '',
  56. '',
  57. '',
  58. '',
  59. '',
  60. '',
  61. '',
  62. '',
  63. '',
  64. '',
  65. '',
  66. '',
  67. '',
  68. '',
  69. '',
  70. '',
  71. '',
  72. '',
  73. '',
  74. '',
  75. '',
  76. '',
  77. '',
  78. '',
  79. '',
  80. '',
  81. '',
  82. '',
  83. '',
  84. '',
  85. '',
  86. '',
  87. '',
  88. '',
  89. '',
  90. '',
  91. '',
  92. '',
  93. '',
  94. '',
  95. '',
  96. '',
  97. '',
  98. '',
  99. '\\\\',
  100. ];
  101. const esc_ = (str) => {
  102. if (str.length === 2)
  103. return str[0] + '\\u' + str.charCodeAt(1).toString(16);
  104. const charCode = str.charCodeAt(0);
  105. return meta.length > charCode ? meta[charCode] : '\\u' + charCode.toString(16);
  106. };
  107. const escape = (str) => {
  108. let point, last = 0, result = '';
  109. if (str.length < 5000 && !strEscapeSequencesRegExp.test(str))
  110. return str;
  111. if (str.length > 100)
  112. return str.replace(strEscapeSequencesReplacer, esc_);
  113. for (let i = 0; i < str.length; i++) {
  114. point = str.charCodeAt(i);
  115. if (point === 34 || point === 92 || point < 32) {
  116. result += str.slice(last, i) + meta[point];
  117. last = i + 1;
  118. }
  119. else if (point >= 0xd800 && point <= 0xdfff) {
  120. if (point <= 0xdbff && i + 1 < str.length) {
  121. point = str.charCodeAt(i + 1);
  122. if (point >= 0xdc00 && point <= 0xdfff) {
  123. i++;
  124. continue;
  125. }
  126. }
  127. result += str.slice(last, i) + '\\u' + point.toString(16);
  128. last = i + 1;
  129. }
  130. }
  131. result += str.slice(last);
  132. return result;
  133. };
  134. exports.escape = escape;
  135. //# sourceMappingURL=escape.js.map