-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmt.ordinal.js
90 lines (89 loc) · 2.99 KB
/
fmt.ordinal.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
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +--------------------------------------------------------+
// | Copyright : Song Hyo-Jin <shj at xenosi.de> |
// +--------------------------------------------------------+
// | Number to Ordinal Cardinal Korean Converter |
// +--------------------------------------------------------+
// | License : MIT |
// +--------------------------------------------------------+
//
// $Id: fmt.ordinal.js, 2015. 4. 14. crucify Exp $
(function(w, undefined) {
let digits = ['', '만', '억', '조', '경', '해', '자', '양', '구', '간', '정', '재', '극', '항하사', '아승기', '나유타', '불가사의', '무량대수', '겁', '업'],
num = ['', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구'],
cardinal = ['', '십', '백', '천'],
ordinal = [
['', '하나', '둘', '셋', '넷', '다섯', '여섯', '일곱', '여덟', '아홉'],
['', '한', '두', '세', '네', '다섯', '여섯', '일곱', '여덟', '아홉'],
],
largeordinal = ['', '열', '스물', '서른', '마흔', '쉰', '예순', '일흔', '여든', '아흔'],
fmt = {
toCardinal : function(n, pronunFirstOne) {
n += '';
if(n.length > 80) return '무한';
const r = [];
let pronunDigits = true;
for(let i = 0; i < n.length - 1; i ++) {
const d = n.length - i - 1;
if(n[i] > '1' || (i > 0 && d % 4 == 0) || (pronunFirstOne && i == 0)) {
const tnum = num[n[i] * 1];
if(tnum) {
r.push(tnum);
pronunDigits = true;
}
}
if(pronunDigits && d % 4 == 0) {
r.push(digits[d / 4]);
pronunDigits = false;
}
if(n[i] != '0') {
r.push(cardinal[d % 4]);
}
}
r.push(num[n[n.length - 1] * 1]);
return r.join('');
},
$cardinal: function() {
var t = $(this).text();
this.title = t;
$(this).text(
fmt.toCardinal(t, $(this).data('pronun-first-one'))
);
},
toOrdinal : function(n, type, unit) {
n += '';
if(n.length > 80) return '무한';
if(!type) type = 1; /* 1: 한, 2: 첫 */
if(!unit) unit = '';
if(n == '1' && type == 2 && unit == '') return '처음';
var s, r = [];
if(unit != '') {
var u = unit.charCodeAt(0) - 44032, cho = Math.floor(u / 588), jong = u % 588 % 28;
s = ((cho == 1 || cho == 4 || cho == 8 || cho == 10 || cho == 13) && jong == 0) ? 0 : 1;
} else {
s = 0;
type = 1;
}
if(n.length > 2) {
r.push(fmt.toCardinal(n.substring(0, n.length - 2) + '00'));
}
n = '0' + n;
n = n.substring(n.length - 2);
r.push(largeordinal[n[0] * 1]);
r.push(
type == 2 && n[1] == '1' ? '첫' : ordinal[s][n[1] * 1]
);
r.push(unit);
return r.join('');
},
$ordinal: function() {
var t = $(this).text();
this.title = t;
$(this).text(
fmt.toOrdinal(t, $(this).data('type'), $(this).data('unit'))
);
}
};
if(w.define) define(function() { return fmt; });
else w.FmtOrdinal = fmt;
})(window);