1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule UnicodeCJK
* @typechecks
*/
/**
* Unicode algorithms for CJK (Chinese, Japanese, Korean) writing systems.
*
* Utilities for Hanzi/Kanji/Hanja logographs and Kanas (Katakana and Hiragana)
* syllables.
*
* For Korean Hangul see module `UnicodeHangulKorean`.
*/
'use strict';
/**
* Latin
*
* NOTE: The code assumes these sets include only BMP characters.
*/
const R_LATIN_ASCII = 'a-zA-Z';
const R_LATIN_FULLWIDTH = '\uFF21-\uFF3A\uFF41-\uFF5A';
const R_LATIN = R_LATIN_ASCII + R_LATIN_FULLWIDTH;
/**
* Hiragana & Katakana
*
* NOTE: Some ranges include non-BMP characters. We do not support those ranges
* for now.
*/
const R_HIRAGANA = '\u3040-\u309F';
const R_KATAKANA = '\u30A0-\u30FF';
const R_KATAKANA_PHONETIC = '\u31F0-\u31FF';
const R_KATAKANA_HALFWIDTH = '\uFF65-\uFF9F';
// var R_KANA_SUPPLEMENT = '\U0001B000-\U0001B0FF';
const R_KATAKANA_ALL = R_KATAKANA + R_KATAKANA_PHONETIC + R_KATAKANA_HALFWIDTH;
const R_KANA = R_HIRAGANA + R_KATAKANA_ALL;
const I_HIRAGANA = [0x3040, 0x309F];
const I_KATAKANA = [0x30A0, 0x30FF];
const I_HIRAGANA_TO_KATAKANA = I_KATAKANA[0] - I_HIRAGANA[0];
/**
* Hanzi/Kanji/Hanja
*
* NOTE: Some ranges include non-BMP characters. We do not support those ranges
* for now.
*/
const R_IDEO_MAIN = '\u4E00-\u9FCF';
const R_IDEO_EXT_A = '\u3400-\u4DBF';
// var R_IDEO_EXT_B = '\U00020000-\U0002A6DF';
// var R_IDEO_EXT_C = '\U0002A700-\U0002B73F';
// var R_IDEO_EXT_D = '\U0002B740-\U0002B81F';
const R_IDEO = R_IDEO_MAIN + R_IDEO_EXT_A;
/**
* Hangul
*/
// var R_HANGUL_JAMO = '\u1100-\u11FF';
// var R_HANGUL_JAMO_EXT_A = '\uA960-\uA97F';
// var R_HANGUL_JAMO_EXT_B = '\uD7B0-\uD7FF';
// var R_HANGUL_COMPATIBILITY = '\u3130-\u318F';
// var R_HANGUL_COMP_HALFWIDTH = '\uFFA0-\uFFDF';
const R_HANGUL_SYLLABLES = '\uAC00-\uD7AF';
/**
* Globals
*/
const R_IDEO_OR_SYLL = R_IDEO + R_KANA + R_HANGUL_SYLLABLES;
let REGEX_IDEO = null;
let REGEX_KANA = null;
let REGEX_IDEO_OR_SYLL = null;
let REGEX_IS_KANA_WITH_TRAILING_LATIN = null;
/**
* Whether the string includes any Katakana or Hiragana characters.
*
* @param {string} str
* @return {boolean}
*/
function hasKana(str) {
REGEX_KANA = REGEX_KANA || new RegExp('[' + R_KANA + ']');
return REGEX_KANA.test(str);
}
/**
* Whether the string includes any CJK Ideograph characters.
*
* @param {string} str
* @return {boolean}
*/
function hasIdeograph(str) {
REGEX_IDEO = REGEX_IDEO || new RegExp('[' + R_IDEO + ']');
return REGEX_IDEO.test(str);
}
/**
* Whether the string includes any CJK Ideograph or Syllable characters.
*
* @param {string} str
* @return {boolean}
*/
function hasIdeoOrSyll(str) {
REGEX_IDEO_OR_SYLL = REGEX_IDEO_OR_SYLL || new RegExp('[' + R_IDEO_OR_SYLL + ']');
return REGEX_IDEO_OR_SYLL.test(str);
}
/**
* @param {string} chr
* @output {string}
*/
function charCodeToKatakana(chr) {
const charCode = chr.charCodeAt(0);
return String.fromCharCode(charCode < I_HIRAGANA[0] || charCode > I_HIRAGANA[1] ? charCode : charCode + I_HIRAGANA_TO_KATAKANA);
}
/**
* Replace any Hiragana character with the matching Katakana
*
* @param {string} str
* @output {string}
*/
function hiraganaToKatakana(str) {
if (!hasKana(str)) {
return str;
}
return str.split('').map(charCodeToKatakana).join('');
}
/**
* Whether the string is exactly a sequence of Kana characters followed by one
* Latin character.
*
* @param {string} str
* @output {string}
*/
function isKanaWithTrailingLatin(str) {
REGEX_IS_KANA_WITH_TRAILING_LATIN = REGEX_IS_KANA_WITH_TRAILING_LATIN || new RegExp('^' + '[' + R_KANA + ']+' + '[' + R_LATIN + ']' + '$');
return REGEX_IS_KANA_WITH_TRAILING_LATIN.test(str);
}
/**
* Drops the trailing Latin character from a string that is exactly a sequence
* of Kana characters followed by one Latin character.
*
* @param {string} str
* @output {string}
*/
function kanaRemoveTrailingLatin(str) {
if (isKanaWithTrailingLatin(str)) {
return str.substr(0, str.length - 1);
}
return str;
}
const UnicodeCJK = {
hasKana: hasKana,
hasIdeograph: hasIdeograph,
hasIdeoOrSyll: hasIdeoOrSyll,
hiraganaToKatakana: hiraganaToKatakana,
isKanaWithTrailingLatin: isKanaWithTrailingLatin,
kanaRemoveTrailingLatin: kanaRemoveTrailingLatin
};
module.exports = UnicodeCJK;
|