UnicodeCJK.js.flow 4.4 KB

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