-
Notifications
You must be signed in to change notification settings - Fork 2
/
hangul-jamo.js
127 lines (103 loc) · 3.47 KB
/
hangul-jamo.js
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
/*!
* Hangul Jamo Library v0.1
* https://github.com/teampopong/hangul-jamo-js
*
* Copyright 2013 Team POPONG
* Released under the MIT license
* https://github.com/teampopong/hangul-jamo-js/blob/master/LICENSE
*/
(function () {
var global = this; // either window or global
var // 초성
FIRST_CONSONANTS = [
'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ',
'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'
],
// 중성
VOWELS = [
'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅗㅏ', 'ㅗㅐ',
'ㅗㅣ', 'ㅛ', 'ㅜ', 'ㅜㅓ', 'ㅜㅔ', 'ㅜㅣ', 'ㅠ', 'ㅡ', 'ㅡㅣ', 'ㅣ'
],
// 종성
LAST_CONSONANTS = [
'', 'ㄱ', 'ㄲ', 'ㄱㅅ', 'ㄴ', 'ㄴㅈ', 'ㄴㅎ', 'ㄷ', 'ㄹ', 'ㄹㄱ',
'ㄹㅁ', 'ㄹㅂ', 'ㄹㅅ', 'ㄹㅌ', 'ㄹㅍ', 'ㄹㅎ', 'ㅁ', 'ㅂ', 'ㅂㅅ',
'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'
],
// 독립적인 자음
SINGLE_CONSONANTS = [
'ㄱ', 'ㄲ', 'ㄱㅅ', 'ㄴ', 'ㄴㅈ', 'ㄴㅎ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㄹㄱ',
'ㄹㅁ', 'ㄹㅂ', 'ㄹㅅ', 'ㄹㅌ', 'ㄹㅍ', 'ㄹㅎ', 'ㅁ', 'ㅂ', 'ㅃ',
'ㅂㅅ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'
];
var HANGUL = global.HANGUL = global.HANGUL || {};
HANGUL.toJamos = function (str) {
var jamos = [];
for (var i = 0, len = str.length; i < len; i++) {
var _char = str[i];
jamos.push(this.toChosungs(_char));
jamos.push(this.toJoongsungs(_char));
jamos.push(this.toJongsungs(_char));
}
return jamos.join('');
};
HANGUL.toChosungs = function (str) {
var consonants = [];
for (var i = 0, len = str.length; i < len; i++) {
var code = str.charCodeAt(i),
offset,
consonant;
if (0x3131 <= code && code < 0x314f) {
offset = parseInt(code - 0x3131);
consonant = SINGLE_CONSONANTS[offset];
} else if (0xac00 <= code && code < 0xd7a4) {
offset = parseInt((code - 0xac00) / 28 / 21);
consonant = FIRST_CONSONANTS[offset];
} else {
consonant = str.charAt(i);
}
consonants.push(consonant);
}
return consonants.join('');
};
HANGUL.toJoongsungs = function (str) {
var vowels = [];
for (var i = 0, len = str.length; i < len; i++) {
var code = str.charCodeAt(i),
offset,
vowel;
if (0xac00 <= code && code < 0xd7a4) {
offset = parseInt((code - 0xac00) / 28) % 21;
vowel = VOWELS[offset];
} else {
vowel = '';
}
vowels.push(vowel);
}
return vowels.join('');
};
HANGUL.toJongsungs = function (str) {
var consonants = [];
for (var i = 0, len = str.length; i < len; i++) {
var code = str.charCodeAt(i),
offset,
consonant;
if (0xac00 <= code && code < 0xd7a4) {
offset = (code - 0xac00) % 28;
consonant = LAST_CONSONANTS[offset];
} else {
consonant = '';
}
consonants.push(consonant);
}
return consonants.join('');
};
HANGUL.indexOf = function (haystack, needle) {
var jamoHaystack = HANGUL.toJamos(haystack),
jamoNeedle = HANGUL.toJamos(needle);
return jamoHaystack.indexOf(jamoNeedle);
};
HANGUL.startsWith = function (haystack, needle) {
return HANGUL.indexOf(haystack, needle) === 0;
};
}());