@@ -4,6 +4,12 @@ const bahtxtConst = {
4
4
placeNameStrs : [ '' , 'สิบ' , 'ร้อย' , 'พัน' , 'หมื่น' , 'แสน' , 'ล้าน' ]
5
5
}
6
6
7
+ const GrammarFixs = [
8
+ { pat : / ห น ึ ่ ง ส ิ บ / g, replace : 'สิบ' } ,
9
+ { pat : / ส อ ง ส ิ บ / g, replace : 'ยี่สิบ' } ,
10
+ { pat : / ส ิ บ ห น ึ ่ ง / g, replace : 'สิบเอ็ด' }
11
+ ]
12
+
7
13
/**
8
14
* @private
9
15
* @param {number[] } nums
@@ -21,10 +27,10 @@ function bahtxtNum2Word (nums) {
21
27
const remainingNumbs = nums . slice ( overflowIndex )
22
28
return bahtxtNum2Word ( overflowNums ) + 'ล้าน' + bahtxtNum2Word ( remainingNumbs )
23
29
} 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 ]
26
32
if ( digit > 0 ) {
27
- result += bahtxtConst . singleUnitStrs [ digit ] + bahtxtConst . placeNameStrs [ len - i - 1 ]
33
+ result += bahtxtConst . singleUnitStrs [ digit ] + bahtxtConst . placeNameStrs [ len - num - 1 ]
28
34
}
29
35
}
30
36
}
@@ -39,9 +45,10 @@ function bahtxtNum2Word (nums) {
39
45
* @returns {string }
40
46
*/
41
47
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
45
52
}
46
53
47
54
/**
0 commit comments