-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
93 lines (78 loc) · 2.6 KB
/
index.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
class DigitToWord {
constructor(value, locale) {
this.value = parseInt(value);
this.locale = locale || require('./locales/fa');
}
toWords() {
return (this._splitTrilateral(this._splitDigits(this.value)))
.map(i => {
return this._printTrilateral(i);
})
.reduce((carry, v, k) => {
if (v.length === 0) {
return carry;
}
if (k < 1) {
carry.push(v);
return carry;
}
const position = k >= 10 ? this.locale.POSITIONS[10] : this.locale.POSITIONS[k];
carry.push(`${v} ${position}`);
return carry;
}, [])
.reverse()
.join(this.locale.SEPARATORS.trilateral);
}
_splitTrilateral(value) {
const out = [];
let part = '';
let i = value.length - 1;
let temp = 1;
for (i; i >= 0; i-- , temp++) {
part = value[i] + part;
if (temp % 3 === 0) {
out.push(part);
part = '';
temp = 0;
}
}
if (part.length > 0) {
out.push(part);
}
return out;
}
_splitDigits(value) {
return value.toString().split('');
}
_printTrilateral(digits) {
let parts = [];
if (digits.length === 3) {
parts.push(parseInt(digits[0]) === 0 ? '' : this.locale.THIRD[parseInt(digits[0] * 100)]);
parts.push(this._printTrilateral(this._joinDigits(digits[1], digits[0])));
} else if (digits.length === 2) {
if (parseInt(digits[0]) === 0) {
parts.push(this._printTrilateral(digits[1]));
} else {
if (parseInt(digits) < 20) {
parts.push(this.locale.SECOND[this._joinDigits(digits[0], digits[1])]);
} else {
parts.push(this.locale.SECOND[parseInt(digits[0] * 10)]);
parts.push(this._printTrilateral(digits[1]));
}
}
} else {
parts.push(parseInt(digits[0]) === 0 ? '' : this.locale.FIRST[digits[0]]);
}
parts = parts.filter(i => {
return i.length > 0;
})
return parts.length ? parts.join(this.locale.SEPARATORS.words) : '';
}
_joinDigits() {
return Array.from(arguments).join('');
}
static fromDigits(digits, locale) {
return (new DigitToWord(digits, locale)).toWords();
}
}
module.exports = DigitToWord;