UnicodeHangulKorean.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Hangul script, the Korean writing system
  11. *
  12. * Hangul script has three encoded models in Unicode:
  13. *
  14. * A) Conjoining Jamo (covers modern and historic elements)
  15. * * U+1100..U+11FF ; Hangul Jamo
  16. * * U+A960..U+A97F ; Hangul Jamo Extended-A
  17. * * U+D7B0..U+D7FF ; Hangul Jamo Extended-B
  18. *
  19. * B) Conjoined Syllables (only covers modern Korean language)
  20. * * U+AC00..U+D7AF ; Hangul Syllables
  21. *
  22. * C) Compatibility Jamo (one code-point for each "shape")
  23. * * U+3130..U+318F ; Hangul Compatibility Jamo
  24. *
  25. * This modules helps you convert characters from one model to another.
  26. * Primary functionalities are:
  27. *
  28. * 1) Convert from any encodings to Conjoining Jamo characters (A),
  29. * e.g. for prefix matching
  30. *
  31. * 2) Convert from any encodings to Syllable characters, when possible (B),
  32. * e.g. to reach the normal Unicode form (NFC)
  33. */
  34. 'use strict';
  35. var HANGUL_COMPATIBILITY_OR_SYLLABLE_REGEX = /[\u3130-\u318F\uAC00-\uD7AF]/;
  36. /**
  37. * Returns true if the input includes any Hangul Compatibility Jamo or
  38. * Hangul Conjoined Syllable.
  39. *
  40. * @param {string} str
  41. */
  42. function hasCompatibilityOrSyllable(str) {
  43. return HANGUL_COMPATIBILITY_OR_SYLLABLE_REGEX.test(str);
  44. }
  45. /* Compatibility Jamo -> Conjoining Jamo
  46. *
  47. * Maps a compatibility character to the Conjoining Jamo character,
  48. * positioned at (compatibilityCodePoint - 0x3131).
  49. *
  50. * Generated by:
  51. * $ grep '^31[3-8].;' UnicodeData.txt |\
  52. * awk -F';' '{print $6}' | awk '{print " 0x"$2","}'
  53. */
  54. var CMAP = [0x1100, 0x1101, 0x11AA, 0x1102, 0x11AC, 0x11AD, 0x1103, 0x1104, 0x1105, 0x11B0, 0x11B1, 0x11B2, 0x11B3, 0x11B4, 0x11B5, 0x111A, 0x1106, 0x1107, 0x1108, 0x1121, 0x1109, 0x110A, 0x110B, 0x110C, 0x110D, 0x110E, 0x110F, 0x1110, 0x1111, 0x1112, 0x1161, 0x1162, 0x1163, 0x1164, 0x1165, 0x1166, 0x1167, 0x1168, 0x1169, 0x116A, 0x116B, 0x116C, 0x116D, 0x116E, 0x116F, 0x1170, 0x1171, 0x1172, 0x1173, 0x1174, 0x1175, 0x1160, 0x1114, 0x1115, 0x11C7, 0x11C8, 0x11CC, 0x11CE, 0x11D3, 0x11D7, 0x11D9, 0x111C, 0x11DD, 0x11DF, 0x111D, 0x111E, 0x1120, 0x1122, 0x1123, 0x1127, 0x1129, 0x112B, 0x112C, 0x112D, 0x112E, 0x112F, 0x1132, 0x1136, 0x1140, 0x1147, 0x114C, 0x11F1, 0x11F2, 0x1157, 0x1158, 0x1159, 0x1184, 0x1185, 0x1188, 0x1191, 0x1192, 0x1194, 0x119E, 0x11A1];
  55. var CBASE = 0x3131;
  56. var CCOUNT = CMAP.length;
  57. var CTOP = CBASE + CCOUNT;
  58. /**
  59. * Maps one Hangul Compatibility Jamo code-point to the equivalent Hangul
  60. * Conjoining Jamo characters, as defined in UnicodeData.txt.
  61. *
  62. * @param {number} codePoint One Unicode code-point
  63. * @output {string}
  64. */
  65. function fromCompatibility(codePoint) {
  66. return String.fromCharCode(CMAP[codePoint - CBASE]);
  67. }
  68. /**
  69. * Conjoined Syllable -> Conjoining Jamo
  70. *
  71. * Based on the "Hangul Syllable Decomposition" algorithm provided in
  72. * 3.12 Conjoining Jamo Behavior, The Unicode Standard, Version 6.3.0.
  73. * <http://www.unicode.org/versions/Unicode6.2.0/ch03.pdf>
  74. */
  75. var LBASE = 0x1100;
  76. var VBASE = 0x1161;
  77. var TBASE = 0x11A7;
  78. var SBASE = 0xAC00;
  79. var LCOUNT = 19;
  80. var VCOUNT = 21;
  81. var TCOUNT = 28;
  82. var NCOUNT = VCOUNT * TCOUNT;
  83. var SCOUNT = LCOUNT * NCOUNT;
  84. var STOP = SBASE + SCOUNT;
  85. /**
  86. * Maps one Hangul Syllable code-point to the equivalent Hangul
  87. * Conjoining Jamo characters, as defined in UnicodeData.txt.
  88. *
  89. * @param {number} codePoint One Unicode character
  90. * @output {string}
  91. */
  92. function decomposeSyllable(codePoint) {
  93. var sylSIndex = codePoint - SBASE;
  94. var sylTIndex = sylSIndex % TCOUNT;
  95. return String.fromCharCode(LBASE + sylSIndex / NCOUNT) + String.fromCharCode(VBASE + sylSIndex % NCOUNT / TCOUNT) + (sylTIndex > 0 ? String.fromCharCode(TBASE + sylTIndex) : '');
  96. }
  97. /* To Conjoining Jamo */
  98. /**
  99. * Return Unicode characters as they are, except for Hangul characters, which
  100. * will be converted to the Conjoining Jamo form.
  101. *
  102. * @param {string} string
  103. * @output {string}
  104. */
  105. function toConjoiningJamo(string) {
  106. if (!hasCompatibilityOrSyllable(string)) {
  107. return string;
  108. }
  109. var result = [];
  110. for (var i = 0; i < string.length; i++) {
  111. var charStr = string.charAt(i);
  112. var codeUnit = charStr.charCodeAt(0);
  113. result.push(CBASE <= codeUnit && codeUnit < CTOP ? fromCompatibility(codeUnit) : SBASE <= codeUnit && codeUnit < STOP ? decomposeSyllable(codeUnit) : charStr);
  114. }
  115. return result.join('');
  116. }
  117. var UnicodeHangulKorean = {
  118. toConjoiningJamo: toConjoiningJamo
  119. };
  120. module.exports = UnicodeHangulKorean;