Skip to content

Commit b936794

Browse files
authored
Merge pull request #13 from PingHuskar/master
clean up code
2 parents aa4e8ce + 7ca4125 commit b936794

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const bahtxtConst = {
44
placeNameStrs: ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']
55
}
66

7+
const GrammarFixs = [
8+
{ pat: //g, replace: 'สิบ' },
9+
{ pat: //g, replace: 'ยี่สิบ' },
10+
{ pat: //g, replace: 'สิบเอ็ด' }
11+
]
12+
713
/**
814
* @private
915
* @param {number[]} nums
@@ -21,10 +27,10 @@ function bahtxtNum2Word (nums) {
2127
const remainingNumbs = nums.slice(overflowIndex)
2228
return bahtxtNum2Word(overflowNums) + 'ล้าน' + bahtxtNum2Word(remainingNumbs)
2329
} else {
24-
for (let i = 0; i < len; i++) {
25-
const digit = nums[i]
30+
for (const num in nums) {
31+
const digit = nums[num]
2632
if (digit > 0) {
27-
result += bahtxtConst.singleUnitStrs[digit] + bahtxtConst.placeNameStrs[len - i - 1]
33+
result += bahtxtConst.singleUnitStrs[digit] + bahtxtConst.placeNameStrs[len - num - 1]
2834
}
2935
}
3036
}
@@ -39,9 +45,10 @@ function bahtxtNum2Word (nums) {
3945
* @returns {string}
4046
*/
4147
function bahtxtGrammarFix (str) {
42-
return str.replace(//g, 'สิบ')
43-
.replace(//g, 'ยี่สิบ')
44-
.replace(//g, 'สิบเอ็ด')
48+
for (const GrammarFix of GrammarFixs) {
49+
str = str.replace(GrammarFix.pat, GrammarFix.replace)
50+
}
51+
return str
4552
}
4653

4754
/**

0 commit comments

Comments
 (0)