UnicodeCJK.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @typechecks
  8. */
  9. /**
  10. * Unicode algorithms for CJK (Chinese, Japanese, Korean) writing systems.
  11. *
  12. * Utilities for Hanzi/Kanji/Hanja logographs and Kanas (Katakana and Hiragana)
  13. * syllables.
  14. *
  15. * For Korean Hangul see module `UnicodeHangulKorean`.
  16. */
  17. 'use strict';
  18. /**
  19. * Latin
  20. *
  21. * NOTE: The code assumes these sets include only BMP characters.
  22. */
  23. var R_LATIN_ASCII = 'a-zA-Z';
  24. var R_LATIN_FULLWIDTH = '\uFF21-\uFF3A\uFF41-\uFF5A';
  25. var R_LATIN = R_LATIN_ASCII + R_LATIN_FULLWIDTH;
  26. /**
  27. * Hiragana & Katakana
  28. *
  29. * NOTE: Some ranges include non-BMP characters. We do not support those ranges
  30. * for now.
  31. */
  32. var R_HIRAGANA = '\u3040-\u309F';
  33. var R_KATAKANA = '\u30A0-\u30FF';
  34. var R_KATAKANA_PHONETIC = '\u31F0-\u31FF';
  35. var R_KATAKANA_HALFWIDTH = '\uFF65-\uFF9F';
  36. // var R_KANA_SUPPLEMENT = '\U0001B000-\U0001B0FF';
  37. var R_KATAKANA_ALL = R_KATAKANA + R_KATAKANA_PHONETIC + R_KATAKANA_HALFWIDTH;
  38. var R_KANA = R_HIRAGANA + R_KATAKANA_ALL;
  39. var I_HIRAGANA = [0x3040, 0x309F];
  40. var I_KATAKANA = [0x30A0, 0x30FF];
  41. var I_HIRAGANA_TO_KATAKANA = I_KATAKANA[0] - I_HIRAGANA[0];
  42. /**
  43. * Hanzi/Kanji/Hanja
  44. *
  45. * NOTE: Some ranges include non-BMP characters. We do not support those ranges
  46. * for now.
  47. */
  48. var R_IDEO_MAIN = '\u4E00-\u9FCF';
  49. var R_IDEO_EXT_A = '\u3400-\u4DBF';
  50. // var R_IDEO_EXT_B = '\U00020000-\U0002A6DF';
  51. // var R_IDEO_EXT_C = '\U0002A700-\U0002B73F';
  52. // var R_IDEO_EXT_D = '\U0002B740-\U0002B81F';
  53. var R_IDEO = R_IDEO_MAIN + R_IDEO_EXT_A;
  54. /**
  55. * Hangul
  56. */
  57. // var R_HANGUL_JAMO = '\u1100-\u11FF';
  58. // var R_HANGUL_JAMO_EXT_A = '\uA960-\uA97F';
  59. // var R_HANGUL_JAMO_EXT_B = '\uD7B0-\uD7FF';
  60. // var R_HANGUL_COMPATIBILITY = '\u3130-\u318F';
  61. // var R_HANGUL_COMP_HALFWIDTH = '\uFFA0-\uFFDF';
  62. var R_HANGUL_SYLLABLES = '\uAC00-\uD7AF';
  63. /**
  64. * Globals
  65. */
  66. var R_IDEO_OR_SYLL = R_IDEO + R_KANA + R_HANGUL_SYLLABLES;
  67. var REGEX_IDEO = null;
  68. var REGEX_KANA = null;
  69. var REGEX_IDEO_OR_SYLL = null;
  70. var REGEX_IS_KANA_WITH_TRAILING_LATIN = null;
  71. /**
  72. * Whether the string includes any Katakana or Hiragana characters.
  73. *
  74. * @param {string} str
  75. * @return {boolean}
  76. */
  77. function hasKana(str) {
  78. REGEX_KANA = REGEX_KANA || new RegExp('[' + R_KANA + ']');
  79. return REGEX_KANA.test(str);
  80. }
  81. /**
  82. * Whether the string includes any CJK Ideograph characters.
  83. *
  84. * @param {string} str
  85. * @return {boolean}
  86. */
  87. function hasIdeograph(str) {
  88. REGEX_IDEO = REGEX_IDEO || new RegExp('[' + R_IDEO + ']');
  89. return REGEX_IDEO.test(str);
  90. }
  91. /**
  92. * Whether the string includes any CJK Ideograph or Syllable characters.
  93. *
  94. * @param {string} str
  95. * @return {boolean}
  96. */
  97. function hasIdeoOrSyll(str) {
  98. REGEX_IDEO_OR_SYLL = REGEX_IDEO_OR_SYLL || new RegExp('[' + R_IDEO_OR_SYLL + ']');
  99. return REGEX_IDEO_OR_SYLL.test(str);
  100. }
  101. /**
  102. * @param {string} chr
  103. * @output {string}
  104. */
  105. function charCodeToKatakana(chr) {
  106. var charCode = chr.charCodeAt(0);
  107. return String.fromCharCode(charCode < I_HIRAGANA[0] || charCode > I_HIRAGANA[1] ? charCode : charCode + I_HIRAGANA_TO_KATAKANA);
  108. }
  109. /**
  110. * Replace any Hiragana character with the matching Katakana
  111. *
  112. * @param {string} str
  113. * @output {string}
  114. */
  115. function hiraganaToKatakana(str) {
  116. if (!hasKana(str)) {
  117. return str;
  118. }
  119. return str.split('').map(charCodeToKatakana).join('');
  120. }
  121. /**
  122. * Whether the string is exactly a sequence of Kana characters followed by one
  123. * Latin character.
  124. *
  125. * @param {string} str
  126. * @output {string}
  127. */
  128. function isKanaWithTrailingLatin(str) {
  129. REGEX_IS_KANA_WITH_TRAILING_LATIN = REGEX_IS_KANA_WITH_TRAILING_LATIN || new RegExp('^' + '[' + R_KANA + ']+' + '[' + R_LATIN + ']' + '$');
  130. return REGEX_IS_KANA_WITH_TRAILING_LATIN.test(str);
  131. }
  132. /**
  133. * Drops the trailing Latin character from a string that is exactly a sequence
  134. * of Kana characters followed by one Latin character.
  135. *
  136. * @param {string} str
  137. * @output {string}
  138. */
  139. function kanaRemoveTrailingLatin(str) {
  140. if (isKanaWithTrailingLatin(str)) {
  141. return str.substr(0, str.length - 1);
  142. }
  143. return str;
  144. }
  145. var UnicodeCJK = {
  146. hasKana: hasKana,
  147. hasIdeograph: hasIdeograph,
  148. hasIdeoOrSyll: hasIdeoOrSyll,
  149. hiraganaToKatakana: hiraganaToKatakana,
  150. isKanaWithTrailingLatin: isKanaWithTrailingLatin,
  151. kanaRemoveTrailingLatin: kanaRemoveTrailingLatin
  152. };
  153. module.exports = UnicodeCJK;