-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorigin.js
117 lines (106 loc) · 3.09 KB
/
origin.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
((exports) => {
const DEFAULT_SYMBOL = {
open: '{',
close: '}'
}
let symbol = {
open: DEFAULT_SYMBOL.open,
close: DEFAULT_SYMBOL.close
}
/**
* merge(word: any, type: string): string
* @param word
* @param type
* @returns string
*/
exports.merge = (word = '', type) => {
if (isUndefined(type)) {
return word
}
let koreanStartChar = 0xAC00 // '가'
let koreanLastChar = 0XD7A3 // '힣'
let jongjangCycle = 28
let targetLetter = word.charCodeAt(word.length - 1) - koreanStartChar
if (targetLetter < 0 || targetLetter > (koreanLastChar - koreanStartChar)) {
return word + getPostposition(type, false)
}
if (targetLetter % jongjangCycle === 0) {
return word + getPostposition(type, false)
} else {
return word + getPostposition(type, true)
}
}
/**
* parse(sentence: any): string
* @param sentence
* @returns string
*/
exports.parse = (sentence) => {
const parseRegExp = new RegExp(`(.)${symbol.open}([^${symbol.close}.]*)${symbol.close}`, 'gm')
let result = sentence
let matches
while (matches = parseRegExp.exec(result)) {
let target = matches[0]
let word = matches[1]
let type = matches[2]
let mergedWord = exports.merge(word, type)
result = result.replace(target, mergedWord)
}
return result
}
exports.getSymbol = () => symbol
exports.setSymbol = (openSymbol, closeSymbol) => {
exports.setOpenSymbol(openSymbol)
exports.setCloseSymbol(closeSymbol)
}
exports.setOpenSymbol = (openSymbol = DEFAULT_SYMBOL.open) => {
symbol.open = `\\${openSymbol}`
}
exports.setCloseSymbol = (closeSymbol = DEFAULT_SYMBOL.close) => {
symbol.close = `\\${closeSymbol}`
}
/**
* attach(word: any, type: string): string
* @deprecated - changed to merge(). Use merge() because it will be removed on v3.0.0
*/
exports.attach = exports.merge
const getPostposition = (type, hasJongjang) => {
let koreanYiCode = 0xC774 // 이
switch (type) {
case '을':
case '를':
return hasJongjang ? '을' : '를'
case '이':
case '가':
return hasJongjang ? '이' : '가'
case '은':
case '는':
return hasJongjang ? '은' : '는'
case '과':
case '와':
return hasJongjang ? '과' : '와'
case '아':
case '야':
return hasJongjang ? '아' : '야'
case '이어':
case '여':
return hasJongjang ? '이어' : '여'
case '이었':
case '였':
return hasJongjang ? '이었' : '였'
case '으로':
case '로':
return hasJongjang ? '으로' : '로'
case '은커녕':
case '는커녕':
return hasJongjang ? '은커녕' : '는커녕'
default:
if(type.charCodeAt(0) === koreanYiCode) {
return hasJongjang ? type : type.substring(1)
} else {
return hasJongjang ? '이' + type : type
}
}
}
const isUndefined = variable => typeof variable === 'undefined'
})(typeof exports === 'undefined' ? window[ 'krPostposition' ] = {} : exports);