-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjosa.js
52 lines (41 loc) · 1.38 KB
/
josa.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
;String.prototype.josa = (function() {
'use strict';
var table = [
{ reg: /^이?([면다지네고])/, map : [ '이$1', '$1' ] },
{ reg: /^으?로/, map : [ '으로', '로' ], callback : function(jongSeong) {
return jongSeong === 0 || jongSeong === 8 ? 1 : 0;
} },
{ reg: /^이|가/, map : [ '이', '가' ] },
{ reg: /^과|와/, map : [ '과', '와' ] },
{ reg: /^을|를/, map : [ '을', '를' ] },
{ reg: /^은|는/, map : [ '은', '는' ] }
];
function escapeRegExp(string){
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
return function (str) {
if (arguments.length === 0 || arguments.length === 2) {
var open = escapeRegExp(arguments[0] || '{');
var close = escapeRegExp(arguments[1] || '}');
var regExp = new RegExp('(.)' + open + '(.*?)' + close, 'g');
return this.replace(regExp, function(_, str, josa) {
var after = str.josa(josa);
if (str + josa !== after) {
return after;
}
return _;
});
}
var code = this.charCodeAt(this.length - 1);
if (code < 0xAC00 || 0xD743 < code) { return this + str; }
var jongSeong = (code - 0xAC00) % 28;
for (var i = 0, item; !!(item = table[i]); i++) {
if (!item.reg.test(str)) { continue; }
return this + str.replace(
item.reg,
item.map[ typeof item.callback === 'function' ? item.callback(jongSeong) : (jongSeong ? 0 : 1) ]
);
}
return this + str;
};
})();