-
Notifications
You must be signed in to change notification settings - Fork 637
Expand file tree
/
Copy pathcreateMatcher.js
More file actions
51 lines (45 loc) · 1.38 KB
/
createMatcher.js
File metadata and controls
51 lines (45 loc) · 1.38 KB
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
import escapeRegex from './escapeRegex'
function ch2pattern(ch) {
const offset = 44032 /* '가'의 코드 */
// 한국어 음절 (Korean syllables)
if (/[가-힣]/.test(ch)) {
const chCode = ch.charCodeAt(0) - offset
// 종성이 있으면 문자 그대로를 찾는다.
if (chCode % 28 > 0) {
return ch
}
const begin = Math.floor(chCode / 28) * 28 + offset
const end = begin + 27
return `[\\u${begin.toString(16)}-\\u${end.toString(16)}]`
}
// 한글 자음 (Korean consonants)
if (/[ㄱ-ㅎ]/.test(ch)) {
const con2syl = {
ㄱ: '가'.charCodeAt(0),
ㄲ: '까'.charCodeAt(0),
ㄴ: '나'.charCodeAt(0),
ㄷ: '다'.charCodeAt(0),
ㄸ: '따'.charCodeAt(0),
ㄹ: '라'.charCodeAt(0),
ㅁ: '마'.charCodeAt(0),
ㅂ: '바'.charCodeAt(0),
ㅃ: '빠'.charCodeAt(0),
ㅅ: '사'.charCodeAt(0),
}
const begin =
con2syl[ch] ||
(ch.charCodeAt(0) - 12613) /* 'ㅅ'의 코드 */ * 588 + con2syl['ㅅ']
const end = begin + 587
return `[${ch}\\u${begin.toString(16)}-\\u${end.toString(16)}]`
}
// 그 외엔 그대로 내보냄 (other than korean)
return escapeRegex(ch)
}
export default function createMatcher(input) {
const pattern = input
.split('')
.map(ch2pattern)
.map(pattern => '(' + pattern + ')')
.join('')
return new RegExp(pattern + '.*', 'i')
}