Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

eastasianwidth.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. var eaw = {};
  2. if ('undefined' == typeof module) {
  3. window.eastasianwidth = eaw;
  4. } else {
  5. module.exports = eaw;
  6. }
  7. eaw.eastAsianWidth = function(character) {
  8. var x = character.charCodeAt(0);
  9. var y = (character.length == 2) ? character.charCodeAt(1) : 0;
  10. var codePoint = x;
  11. if ((0xD800 <= x && x <= 0xDBFF) && (0xDC00 <= y && y <= 0xDFFF)) {
  12. x &= 0x3FF;
  13. y &= 0x3FF;
  14. codePoint = (x << 10) | y;
  15. codePoint += 0x10000;
  16. }
  17. if ((0x3000 == codePoint) ||
  18. (0xFF01 <= codePoint && codePoint <= 0xFF60) ||
  19. (0xFFE0 <= codePoint && codePoint <= 0xFFE6)) {
  20. return 'F';
  21. }
  22. if ((0x20A9 == codePoint) ||
  23. (0xFF61 <= codePoint && codePoint <= 0xFFBE) ||
  24. (0xFFC2 <= codePoint && codePoint <= 0xFFC7) ||
  25. (0xFFCA <= codePoint && codePoint <= 0xFFCF) ||
  26. (0xFFD2 <= codePoint && codePoint <= 0xFFD7) ||
  27. (0xFFDA <= codePoint && codePoint <= 0xFFDC) ||
  28. (0xFFE8 <= codePoint && codePoint <= 0xFFEE)) {
  29. return 'H';
  30. }
  31. if ((0x1100 <= codePoint && codePoint <= 0x115F) ||
  32. (0x11A3 <= codePoint && codePoint <= 0x11A7) ||
  33. (0x11FA <= codePoint && codePoint <= 0x11FF) ||
  34. (0x2329 <= codePoint && codePoint <= 0x232A) ||
  35. (0x2E80 <= codePoint && codePoint <= 0x2E99) ||
  36. (0x2E9B <= codePoint && codePoint <= 0x2EF3) ||
  37. (0x2F00 <= codePoint && codePoint <= 0x2FD5) ||
  38. (0x2FF0 <= codePoint && codePoint <= 0x2FFB) ||
  39. (0x3001 <= codePoint && codePoint <= 0x303E) ||
  40. (0x3041 <= codePoint && codePoint <= 0x3096) ||
  41. (0x3099 <= codePoint && codePoint <= 0x30FF) ||
  42. (0x3105 <= codePoint && codePoint <= 0x312D) ||
  43. (0x3131 <= codePoint && codePoint <= 0x318E) ||
  44. (0x3190 <= codePoint && codePoint <= 0x31BA) ||
  45. (0x31C0 <= codePoint && codePoint <= 0x31E3) ||
  46. (0x31F0 <= codePoint && codePoint <= 0x321E) ||
  47. (0x3220 <= codePoint && codePoint <= 0x3247) ||
  48. (0x3250 <= codePoint && codePoint <= 0x32FE) ||
  49. (0x3300 <= codePoint && codePoint <= 0x4DBF) ||
  50. (0x4E00 <= codePoint && codePoint <= 0xA48C) ||
  51. (0xA490 <= codePoint && codePoint <= 0xA4C6) ||
  52. (0xA960 <= codePoint && codePoint <= 0xA97C) ||
  53. (0xAC00 <= codePoint && codePoint <= 0xD7A3) ||
  54. (0xD7B0 <= codePoint && codePoint <= 0xD7C6) ||
  55. (0xD7CB <= codePoint && codePoint <= 0xD7FB) ||
  56. (0xF900 <= codePoint && codePoint <= 0xFAFF) ||
  57. (0xFE10 <= codePoint && codePoint <= 0xFE19) ||
  58. (0xFE30 <= codePoint && codePoint <= 0xFE52) ||
  59. (0xFE54 <= codePoint && codePoint <= 0xFE66) ||
  60. (0xFE68 <= codePoint && codePoint <= 0xFE6B) ||
  61. (0x1B000 <= codePoint && codePoint <= 0x1B001) ||
  62. (0x1F200 <= codePoint && codePoint <= 0x1F202) ||
  63. (0x1F210 <= codePoint && codePoint <= 0x1F23A) ||
  64. (0x1F240 <= codePoint && codePoint <= 0x1F248) ||
  65. (0x1F250 <= codePoint && codePoint <= 0x1F251) ||
  66. (0x20000 <= codePoint && codePoint <= 0x2F73F) ||
  67. (0x2B740 <= codePoint && codePoint <= 0x2FFFD) ||
  68. (0x30000 <= codePoint && codePoint <= 0x3FFFD)) {
  69. return 'W';
  70. }
  71. if ((0x0020 <= codePoint && codePoint <= 0x007E) ||
  72. (0x00A2 <= codePoint && codePoint <= 0x00A3) ||
  73. (0x00A5 <= codePoint && codePoint <= 0x00A6) ||
  74. (0x00AC == codePoint) ||
  75. (0x00AF == codePoint) ||
  76. (0x27E6 <= codePoint && codePoint <= 0x27ED) ||
  77. (0x2985 <= codePoint && codePoint <= 0x2986)) {
  78. return 'Na';
  79. }
  80. if ((0x00A1 == codePoint) ||
  81. (0x00A4 == codePoint) ||
  82. (0x00A7 <= codePoint && codePoint <= 0x00A8) ||
  83. (0x00AA == codePoint) ||
  84. (0x00AD <= codePoint && codePoint <= 0x00AE) ||
  85. (0x00B0 <= codePoint && codePoint <= 0x00B4) ||
  86. (0x00B6 <= codePoint && codePoint <= 0x00BA) ||
  87. (0x00BC <= codePoint && codePoint <= 0x00BF) ||
  88. (0x00C6 == codePoint) ||
  89. (0x00D0 == codePoint) ||
  90. (0x00D7 <= codePoint && codePoint <= 0x00D8) ||
  91. (0x00DE <= codePoint && codePoint <= 0x00E1) ||
  92. (0x00E6 == codePoint) ||
  93. (0x00E8 <= codePoint && codePoint <= 0x00EA) ||
  94. (0x00EC <= codePoint && codePoint <= 0x00ED) ||
  95. (0x00F0 == codePoint) ||
  96. (0x00F2 <= codePoint && codePoint <= 0x00F3) ||
  97. (0x00F7 <= codePoint && codePoint <= 0x00FA) ||
  98. (0x00FC == codePoint) ||
  99. (0x00FE == codePoint) ||
  100. (0x0101 == codePoint) ||
  101. (0x0111 == codePoint) ||
  102. (0x0113 == codePoint) ||
  103. (0x011B == codePoint) ||
  104. (0x0126 <= codePoint && codePoint <= 0x0127) ||
  105. (0x012B == codePoint) ||
  106. (0x0131 <= codePoint && codePoint <= 0x0133) ||
  107. (0x0138 == codePoint) ||
  108. (0x013F <= codePoint && codePoint <= 0x0142) ||
  109. (0x0144 == codePoint) ||
  110. (0x0148 <= codePoint && codePoint <= 0x014B) ||
  111. (0x014D == codePoint) ||
  112. (0x0152 <= codePoint && codePoint <= 0x0153) ||
  113. (0x0166 <= codePoint && codePoint <= 0x0167) ||
  114. (0x016B == codePoint) ||
  115. (0x01CE == codePoint) ||
  116. (0x01D0 == codePoint) ||
  117. (0x01D2 == codePoint) ||
  118. (0x01D4 == codePoint) ||
  119. (0x01D6 == codePoint) ||
  120. (0x01D8 == codePoint) ||
  121. (0x01DA == codePoint) ||
  122. (0x01DC == codePoint) ||
  123. (0x0251 == codePoint) ||
  124. (0x0261 == codePoint) ||
  125. (0x02C4 == codePoint) ||
  126. (0x02C7 == codePoint) ||
  127. (0x02C9 <= codePoint && codePoint <= 0x02CB) ||
  128. (0x02CD == codePoint) ||
  129. (0x02D0 == codePoint) ||
  130. (0x02D8 <= codePoint && codePoint <= 0x02DB) ||
  131. (0x02DD == codePoint) ||
  132. (0x02DF == codePoint) ||
  133. (0x0300 <= codePoint && codePoint <= 0x036F) ||
  134. (0x0391 <= codePoint && codePoint <= 0x03A1) ||
  135. (0x03A3 <= codePoint && codePoint <= 0x03A9) ||
  136. (0x03B1 <= codePoint && codePoint <= 0x03C1) ||
  137. (0x03C3 <= codePoint && codePoint <= 0x03C9) ||
  138. (0x0401 == codePoint) ||
  139. (0x0410 <= codePoint && codePoint <= 0x044F) ||
  140. (0x0451 == codePoint) ||
  141. (0x2010 == codePoint) ||
  142. (0x2013 <= codePoint && codePoint <= 0x2016) ||
  143. (0x2018 <= codePoint && codePoint <= 0x2019) ||
  144. (0x201C <= codePoint && codePoint <= 0x201D) ||
  145. (0x2020 <= codePoint && codePoint <= 0x2022) ||
  146. (0x2024 <= codePoint && codePoint <= 0x2027) ||
  147. (0x2030 == codePoint) ||
  148. (0x2032 <= codePoint && codePoint <= 0x2033) ||
  149. (0x2035 == codePoint) ||
  150. (0x203B == codePoint) ||
  151. (0x203E == codePoint) ||
  152. (0x2074 == codePoint) ||
  153. (0x207F == codePoint) ||
  154. (0x2081 <= codePoint && codePoint <= 0x2084) ||
  155. (0x20AC == codePoint) ||
  156. (0x2103 == codePoint) ||
  157. (0x2105 == codePoint) ||
  158. (0x2109 == codePoint) ||
  159. (0x2113 == codePoint) ||
  160. (0x2116 == codePoint) ||
  161. (0x2121 <= codePoint && codePoint <= 0x2122) ||
  162. (0x2126 == codePoint) ||
  163. (0x212B == codePoint) ||
  164. (0x2153 <= codePoint && codePoint <= 0x2154) ||
  165. (0x215B <= codePoint && codePoint <= 0x215E) ||
  166. (0x2160 <= codePoint && codePoint <= 0x216B) ||
  167. (0x2170 <= codePoint && codePoint <= 0x2179) ||
  168. (0x2189 == codePoint) ||
  169. (0x2190 <= codePoint && codePoint <= 0x2199) ||
  170. (0x21B8 <= codePoint && codePoint <= 0x21B9) ||
  171. (0x21D2 == codePoint) ||
  172. (0x21D4 == codePoint) ||
  173. (0x21E7 == codePoint) ||
  174. (0x2200 == codePoint) ||
  175. (0x2202 <= codePoint && codePoint <= 0x2203) ||
  176. (0x2207 <= codePoint && codePoint <= 0x2208) ||
  177. (0x220B == codePoint) ||
  178. (0x220F == codePoint) ||
  179. (0x2211 == codePoint) ||
  180. (0x2215 == codePoint) ||
  181. (0x221A == codePoint) ||
  182. (0x221D <= codePoint && codePoint <= 0x2220) ||
  183. (0x2223 == codePoint) ||
  184. (0x2225 == codePoint) ||
  185. (0x2227 <= codePoint && codePoint <= 0x222C) ||
  186. (0x222E == codePoint) ||
  187. (0x2234 <= codePoint && codePoint <= 0x2237) ||
  188. (0x223C <= codePoint && codePoint <= 0x223D) ||
  189. (0x2248 == codePoint) ||
  190. (0x224C == codePoint) ||
  191. (0x2252 == codePoint) ||
  192. (0x2260 <= codePoint && codePoint <= 0x2261) ||
  193. (0x2264 <= codePoint && codePoint <= 0x2267) ||
  194. (0x226A <= codePoint && codePoint <= 0x226B) ||
  195. (0x226E <= codePoint && codePoint <= 0x226F) ||
  196. (0x2282 <= codePoint && codePoint <= 0x2283) ||
  197. (0x2286 <= codePoint && codePoint <= 0x2287) ||
  198. (0x2295 == codePoint) ||
  199. (0x2299 == codePoint) ||
  200. (0x22A5 == codePoint) ||
  201. (0x22BF == codePoint) ||
  202. (0x2312 == codePoint) ||
  203. (0x2460 <= codePoint && codePoint <= 0x24E9) ||
  204. (0x24EB <= codePoint && codePoint <= 0x254B) ||
  205. (0x2550 <= codePoint && codePoint <= 0x2573) ||
  206. (0x2580 <= codePoint && codePoint <= 0x258F) ||
  207. (0x2592 <= codePoint && codePoint <= 0x2595) ||
  208. (0x25A0 <= codePoint && codePoint <= 0x25A1) ||
  209. (0x25A3 <= codePoint && codePoint <= 0x25A9) ||
  210. (0x25B2 <= codePoint && codePoint <= 0x25B3) ||
  211. (0x25B6 <= codePoint && codePoint <= 0x25B7) ||
  212. (0x25BC <= codePoint && codePoint <= 0x25BD) ||
  213. (0x25C0 <= codePoint && codePoint <= 0x25C1) ||
  214. (0x25C6 <= codePoint && codePoint <= 0x25C8) ||
  215. (0x25CB == codePoint) ||
  216. (0x25CE <= codePoint && codePoint <= 0x25D1) ||
  217. (0x25E2 <= codePoint && codePoint <= 0x25E5) ||
  218. (0x25EF == codePoint) ||
  219. (0x2605 <= codePoint && codePoint <= 0x2606) ||
  220. (0x2609 == codePoint) ||
  221. (0x260E <= codePoint && codePoint <= 0x260F) ||
  222. (0x2614 <= codePoint && codePoint <= 0x2615) ||
  223. (0x261C == codePoint) ||
  224. (0x261E == codePoint) ||
  225. (0x2640 == codePoint) ||
  226. (0x2642 == codePoint) ||
  227. (0x2660 <= codePoint && codePoint <= 0x2661) ||
  228. (0x2663 <= codePoint && codePoint <= 0x2665) ||
  229. (0x2667 <= codePoint && codePoint <= 0x266A) ||
  230. (0x266C <= codePoint && codePoint <= 0x266D) ||
  231. (0x266F == codePoint) ||
  232. (0x269E <= codePoint && codePoint <= 0x269F) ||
  233. (0x26BE <= codePoint && codePoint <= 0x26BF) ||
  234. (0x26C4 <= codePoint && codePoint <= 0x26CD) ||
  235. (0x26CF <= codePoint && codePoint <= 0x26E1) ||
  236. (0x26E3 == codePoint) ||
  237. (0x26E8 <= codePoint && codePoint <= 0x26FF) ||
  238. (0x273D == codePoint) ||
  239. (0x2757 == codePoint) ||
  240. (0x2776 <= codePoint && codePoint <= 0x277F) ||
  241. (0x2B55 <= codePoint && codePoint <= 0x2B59) ||
  242. (0x3248 <= codePoint && codePoint <= 0x324F) ||
  243. (0xE000 <= codePoint && codePoint <= 0xF8FF) ||
  244. (0xFE00 <= codePoint && codePoint <= 0xFE0F) ||
  245. (0xFFFD == codePoint) ||
  246. (0x1F100 <= codePoint && codePoint <= 0x1F10A) ||
  247. (0x1F110 <= codePoint && codePoint <= 0x1F12D) ||
  248. (0x1F130 <= codePoint && codePoint <= 0x1F169) ||
  249. (0x1F170 <= codePoint && codePoint <= 0x1F19A) ||
  250. (0xE0100 <= codePoint && codePoint <= 0xE01EF) ||
  251. (0xF0000 <= codePoint && codePoint <= 0xFFFFD) ||
  252. (0x100000 <= codePoint && codePoint <= 0x10FFFD)) {
  253. return 'A';
  254. }
  255. return 'N';
  256. };
  257. eaw.characterLength = function(character) {
  258. var code = this.eastAsianWidth(character);
  259. if (code == 'F' || code == 'W' || code == 'A') {
  260. return 2;
  261. } else {
  262. return 1;
  263. }
  264. };
  265. // Split a string considering surrogate-pairs.
  266. function stringToArray(string) {
  267. return string.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) || [];
  268. }
  269. eaw.length = function(string) {
  270. var characters = stringToArray(string);
  271. var len = 0;
  272. for (var i = 0; i < characters.length; i++) {
  273. len = len + this.characterLength(characters[i]);
  274. }
  275. return len;
  276. };
  277. eaw.slice = function(text, start, end) {
  278. textLen = eaw.length(text)
  279. start = start ? start : 0;
  280. end = end ? end : 1;
  281. if (start < 0) {
  282. start = textLen + start;
  283. }
  284. if (end < 0) {
  285. end = textLen + end;
  286. }
  287. var result = '';
  288. var eawLen = 0;
  289. var chars = stringToArray(text);
  290. for (var i = 0; i < chars.length; i++) {
  291. var char = chars[i];
  292. var charLen = eaw.length(char);
  293. if (eawLen >= start - (charLen == 2 ? 1 : 0)) {
  294. if (eawLen + charLen <= end) {
  295. result += char;
  296. } else {
  297. break;
  298. }
  299. }
  300. eawLen += charLen;
  301. }
  302. return result;
  303. };