bahttext 2.0.4 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-en.md +14 -3
- package/README.md +14 -3
- package/index.html +5 -5
- package/package.json +5 -1
- package/src/index.js +26 -61
- package/src/index.test.js +37 -158
package/README-en.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
Language: [ไทย](https://github.com/jojoee/bahttext/blob/master/README.md), [English](https://github.com/jojoee/bahttext/blob/master/README-en.md)
|
|
14
14
|
|
|
15
|
-
Change number to Thai pronunciation string, test against [
|
|
15
|
+
Change number to Thai pronunciation string, test against [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en), [Demo page](https://jojoee.github.io/bahttext/)
|
|
16
16
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
@@ -46,11 +46,22 @@ bahttext(63147.89)
|
|
|
46
46
|
- [x] 0 Dependencies
|
|
47
47
|
- [x] Unit test
|
|
48
48
|
- [ ] Browser compatibility test
|
|
49
|
-
- [
|
|
49
|
+
- [x] Remove duplication between test code and test-case-csv file
|
|
50
50
|
- [x] [Demo page](https://jojoee.github.io/bahttext/)
|
|
51
|
-
- [
|
|
51
|
+
- [x] Support negative number
|
|
52
52
|
- [x] Fix semantic-release
|
|
53
53
|
|
|
54
|
+
## CMD
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
brew install curl
|
|
58
|
+
brew install jq
|
|
59
|
+
npm install -g
|
|
60
|
+
curl -L -o ./misc/testcases.csv https://docs.google.com/spreadsheets/d/e/2PACX-1vTb8PIKzgo07rn9UpcjqE0YrdMAmf4fyDbL2plUieLCyrn_5O3vDvece7UfkaArWQLUSsaw92jVpY_z/pub?gid=0&single=true&output=csv
|
|
61
|
+
csvtojson ./misc/testcases.csv | jq > ./misc/testcases.json
|
|
62
|
+
```
|
|
63
|
+
|
|
54
64
|
## Reference
|
|
65
|
+
- [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en)
|
|
55
66
|
- [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
|
|
56
67
|
- Inspired by [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
ภาษา: [ไทย](https://github.com/jojoee/bahttext/blob/master/README.md), [English](https://github.com/jojoee/bahttext/blob/master/README-en.md)
|
|
13
13
|
|
|
14
|
-
เปลี่ยนตัวเลข เป็นคำอ่านภาษาไทย, โมดูลตัวนี้ได้ทำการทดสอบกับ [
|
|
14
|
+
เปลี่ยนตัวเลข เป็นคำอ่านภาษาไทย, โมดูลตัวนี้ได้ทำการทดสอบกับ [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en), [Demo page](https://jojoee.github.io/bahttext/) เรียบร้อยแล้ว
|
|
15
15
|
|
|
16
16
|
## ติดตั้ง
|
|
17
17
|
|
|
@@ -45,11 +45,22 @@ bahttext(63147.89)
|
|
|
45
45
|
- [x] 0 Dependencies
|
|
46
46
|
- [x] ทำการทดสอบแบบ unit test
|
|
47
47
|
- [ ] ทดสอบบนเบราว์เซอร์ ในแต่ละ เวอร์ชั่น
|
|
48
|
-
- [
|
|
48
|
+
- [x] ลดการซ้ำซ้อนของการเขียน ระหว่าง code และ ไฟล์ csv ในการทำการทดสอบ
|
|
49
49
|
- [x] [หน้าตัวอย่างการใช้งาน](https://jojoee.github.io/bahttext/)
|
|
50
|
-
- [
|
|
50
|
+
- [x] สนับสนุนการใช้งานกับตัวเลขติดลบ
|
|
51
51
|
- [x] แก้ไข semantic-release
|
|
52
52
|
|
|
53
|
+
## CMD
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
brew install curl
|
|
57
|
+
brew install jq
|
|
58
|
+
npm install -g
|
|
59
|
+
curl -L -o ./misc/testcases.csv https://docs.google.com/spreadsheets/d/e/2PACX-1vTb8PIKzgo07rn9UpcjqE0YrdMAmf4fyDbL2plUieLCyrn_5O3vDvece7UfkaArWQLUSsaw92jVpY_z/pub?gid=0&single=true&output=csv
|
|
60
|
+
csvtojson ./misc/testcases.csv | jq > ./misc/testcases.json
|
|
61
|
+
```
|
|
62
|
+
|
|
53
63
|
## อ้างอิง
|
|
64
|
+
- [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en)
|
|
54
65
|
- [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
|
|
55
66
|
- แรงบัลดาลใจจาก [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)
|
package/index.html
CHANGED
|
@@ -39,7 +39,7 @@ td {
|
|
|
39
39
|
<th>category</th>
|
|
40
40
|
<th>case</th>
|
|
41
41
|
<th>number</th>
|
|
42
|
-
<th>
|
|
42
|
+
<th>text</th>
|
|
43
43
|
</tr>
|
|
44
44
|
</thead>
|
|
45
45
|
<tfoot>
|
|
@@ -47,7 +47,7 @@ td {
|
|
|
47
47
|
<th>category</th>
|
|
48
48
|
<th>case</th>
|
|
49
49
|
<th>number</th>
|
|
50
|
-
<th>
|
|
50
|
+
<th>text</th>
|
|
51
51
|
</tr>
|
|
52
52
|
</tfoot>
|
|
53
53
|
</table>
|
|
@@ -88,7 +88,7 @@ $(document).ready(function() {
|
|
|
88
88
|
|
|
89
89
|
// random
|
|
90
90
|
$random.click(function(e) {
|
|
91
|
-
const num = r.real(
|
|
91
|
+
const num = r.real(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
|
|
92
92
|
$number.val(num)
|
|
93
93
|
updateResult()
|
|
94
94
|
})
|
|
@@ -99,7 +99,7 @@ $(document).ready(function() {
|
|
|
99
99
|
searching: false,
|
|
100
100
|
ordering: false,
|
|
101
101
|
ajax: {
|
|
102
|
-
url: 'misc/
|
|
102
|
+
url: 'misc/testcases.json',
|
|
103
103
|
dataSrc: ''
|
|
104
104
|
},
|
|
105
105
|
columns: [
|
|
@@ -116,7 +116,7 @@ $(document).ready(function() {
|
|
|
116
116
|
width: '22%'
|
|
117
117
|
},
|
|
118
118
|
{
|
|
119
|
-
data: '
|
|
119
|
+
data: 'text'
|
|
120
120
|
}
|
|
121
121
|
]
|
|
122
122
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bahttext",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Change number to Thai pronunciation string",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"typings": "src/index.js",
|
|
@@ -45,10 +45,14 @@
|
|
|
45
45
|
"cz-conventional-changelog": "3.3.0",
|
|
46
46
|
"ghooks": "2.0.4",
|
|
47
47
|
"jest": "^26.6.1",
|
|
48
|
+
"jest-expect-message": "^1.0.2",
|
|
48
49
|
"semantic-release": "^17.2.1",
|
|
49
50
|
"standard": "^15.0.0"
|
|
50
51
|
},
|
|
51
52
|
"jest": {
|
|
53
|
+
"setupFilesAfterEnv": [
|
|
54
|
+
"jest-expect-message"
|
|
55
|
+
],
|
|
52
56
|
"verbose": true,
|
|
53
57
|
"testMatch": [
|
|
54
58
|
"**/*.test.js"
|
package/src/index.js
CHANGED
|
@@ -1,35 +1,6 @@
|
|
|
1
|
-
/*
|
|
2
|
-
step:
|
|
3
|
-
1. validate
|
|
4
|
-
2. sanitize
|
|
5
|
-
3. split
|
|
6
|
-
4. proceed
|
|
7
|
-
5. grammar
|
|
8
|
-
6. combine
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
1
|
const defaultResult = 'ศูนย์บาทถ้วน'
|
|
12
|
-
const singleUnitStrs = [
|
|
13
|
-
|
|
14
|
-
'หนึ่ง',
|
|
15
|
-
'สอง',
|
|
16
|
-
'สาม',
|
|
17
|
-
'สี่',
|
|
18
|
-
'ห้า',
|
|
19
|
-
'หก',
|
|
20
|
-
'เจ็ด',
|
|
21
|
-
'แปด',
|
|
22
|
-
'เก้า'
|
|
23
|
-
]
|
|
24
|
-
const placeNameStrs = [
|
|
25
|
-
'',
|
|
26
|
-
'สิบ',
|
|
27
|
-
'ร้อย',
|
|
28
|
-
'พัน',
|
|
29
|
-
'หมื่น',
|
|
30
|
-
'แสน',
|
|
31
|
-
'ล้าน'
|
|
32
|
-
]
|
|
2
|
+
const singleUnitStrs = ['', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า']
|
|
3
|
+
const placeNameStrs = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']
|
|
33
4
|
|
|
34
5
|
/**
|
|
35
6
|
* @param {number[]} nums
|
|
@@ -59,22 +30,16 @@ function num2Word (nums) {
|
|
|
59
30
|
}
|
|
60
31
|
|
|
61
32
|
/**
|
|
33
|
+
* @todo improve performance
|
|
62
34
|
* @param {string} str
|
|
63
35
|
* @returns {string}
|
|
64
36
|
*/
|
|
65
37
|
function grammarFix (str) {
|
|
66
38
|
let result = str
|
|
67
39
|
|
|
68
|
-
// "สิบ"
|
|
69
40
|
result = result.replace(/หนึ่งสิบ/g, 'สิบ')
|
|
70
|
-
// "ยี่สิบ"
|
|
71
41
|
result = result.replace(/สองสิบ/g, 'ยี่สิบ')
|
|
72
|
-
|
|
73
|
-
const neungLen = 5
|
|
74
|
-
if (result.length > neungLen &&
|
|
75
|
-
result.length - result.lastIndexOf('หนึ่ง') === neungLen) {
|
|
76
|
-
result = result.substr(0, result.length - neungLen) + 'เอ็ด'
|
|
77
|
-
}
|
|
42
|
+
result = result.replace(/สิบหนึ่ง/g, 'สิบเอ็ด')
|
|
78
43
|
|
|
79
44
|
return result
|
|
80
45
|
}
|
|
@@ -110,42 +75,42 @@ function combine (baht, satang) {
|
|
|
110
75
|
* @returns {string}
|
|
111
76
|
*/
|
|
112
77
|
function bahttext (num) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
//
|
|
116
|
-
if (
|
|
117
|
-
//
|
|
118
|
-
if (num
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
78
|
+
// no null
|
|
79
|
+
if (!num) return defaultResult
|
|
80
|
+
// no boolean
|
|
81
|
+
if (typeof num === 'boolean') return defaultResult
|
|
82
|
+
// must be number only
|
|
83
|
+
if (isNaN(Number(num))) return defaultResult
|
|
84
|
+
// not less than Number.MIN_SAFE_INTEGER
|
|
85
|
+
if (num < Number.MIN_SAFE_INTEGER) return defaultResult
|
|
86
|
+
// no more than Number.MAX_SAFE_INTEGER
|
|
87
|
+
if (num > Number.MAX_SAFE_INTEGER) return defaultResult
|
|
88
|
+
|
|
89
|
+
// set
|
|
90
|
+
const positiveNum = Math.abs(num)
|
|
91
|
+
|
|
92
|
+
// split baht and satang e.g. 432.214567 >> 432, 21
|
|
93
|
+
const bahtStr = Math.floor(positiveNum).toString()
|
|
127
94
|
/** @type {string} */
|
|
128
|
-
const satangStr =
|
|
95
|
+
const satangStr = (positiveNum % 1 * 100).toFixed(2).split('.')[0]
|
|
129
96
|
|
|
130
|
-
// 3. split: convert number array
|
|
131
|
-
// @todo optimize it
|
|
132
97
|
/** @type {number[]} */
|
|
133
98
|
const bahtArr = Array.from(bahtStr).map(Number)
|
|
134
99
|
/** @type {number[]} */
|
|
135
100
|
const satangArr = Array.from(satangStr).map(Number)
|
|
136
101
|
|
|
137
|
-
//
|
|
102
|
+
// proceed
|
|
138
103
|
let baht = num2Word(bahtArr)
|
|
139
104
|
let satang = num2Word(satangArr)
|
|
140
105
|
|
|
141
|
-
//
|
|
106
|
+
// grammar
|
|
142
107
|
baht = grammarFix(baht)
|
|
143
108
|
satang = grammarFix(satang)
|
|
144
109
|
|
|
145
|
-
//
|
|
146
|
-
result = combine(baht, satang)
|
|
110
|
+
// combine
|
|
111
|
+
const result = combine(baht, satang)
|
|
147
112
|
|
|
148
|
-
return result
|
|
113
|
+
return num >= 0 ? result : 'ลบ' + result
|
|
149
114
|
}
|
|
150
115
|
|
|
151
116
|
if (typeof module !== 'undefined' &&
|
package/src/index.test.js
CHANGED
|
@@ -1,167 +1,46 @@
|
|
|
1
1
|
const { bahttext } = require('./index')
|
|
2
2
|
const defaultResult = 'ศูนย์บาทถ้วน'
|
|
3
|
+
const testCases = require('../misc/testcases.json')
|
|
3
4
|
jest.autoMockOff()
|
|
4
5
|
|
|
5
|
-
describe('
|
|
6
|
-
test('
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
expect(bahttext(
|
|
12
|
-
expect(bahttext(
|
|
13
|
-
expect(bahttext(3)).toBe(
|
|
14
|
-
expect(bahttext(
|
|
15
|
-
expect(bahttext(
|
|
16
|
-
expect(bahttext(
|
|
17
|
-
expect(bahttext(
|
|
18
|
-
expect(bahttext(
|
|
19
|
-
expect(bahttext(
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
test('5', () => {
|
|
36
|
-
expect(bahttext(44444)).toBe('สี่หมื่นสี่พันสี่ร้อยสี่สิบสี่บาทถ้วน')
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
test('6', () => {
|
|
40
|
-
expect(bahttext(555555)).toBe('ห้าแสนห้าหมื่นห้าพันห้าร้อยห้าสิบห้าบาทถ้วน')
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
test('7', () => {
|
|
44
|
-
expect(bahttext(6666666)).toBe('หกล้านหกแสนหกหมื่นหกพันหกร้อยหกสิบหกบาทถ้วน')
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test('8', () => {
|
|
48
|
-
expect(bahttext(77777777)).toBe('เจ็ดสิบเจ็ดล้านเจ็ดแสนเจ็ดหมื่นเจ็ดพันเจ็ดร้อยเจ็ดสิบเจ็ดบาทถ้วน')
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
test('9', () => {
|
|
52
|
-
expect(bahttext(888888888)).toBe('แปดร้อยแปดสิบแปดล้านแปดแสนแปดหมื่นแปดพันแปดร้อยแปดสิบแปดบาทถ้วน')
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('10', () => {
|
|
56
|
-
expect(bahttext(9999999999)).toBe('เก้าพันเก้าร้อยเก้าสิบเก้าล้านเก้าแสนเก้าหมื่นเก้าพันเก้าร้อยเก้าสิบเก้าบาทถ้วน')
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
test('11', () => {
|
|
60
|
-
expect(bahttext(12345678901)).toBe('หนึ่งหมื่นสองพันสามร้อยสี่สิบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยเอ็ดบาทถ้วน')
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
test('12', () => {
|
|
64
|
-
expect(bahttext(123456789012)).toBe('หนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบหกล้านเจ็ดแสนแปดหมื่นเก้าพันสิบสองบาทถ้วน')
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('13', () => {
|
|
68
|
-
expect(bahttext(1234567890133)).toBe('หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดล้านแปดแสนเก้าหมื่นหนึ่งร้อยสามสิบสามบาทถ้วน')
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
test('14', () => {
|
|
72
|
-
expect(bahttext(12345678901234)).toBe('สิบสองล้านสามแสนสี่หมื่นห้าพันหกร้อยเจ็ดสิบแปดล้านเก้าแสนหนึ่งพันสองร้อยสามสิบสี่บาทถ้วน')
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('15', () => {
|
|
76
|
-
expect(bahttext(123456789012345)).toBe('หนึ่งร้อยยี่สิบสามล้านสี่แสนห้าหมื่นหกพันเจ็ดร้อยแปดสิบเก้าล้านหนึ่งหมื่นสองพันสามร้อยสี่สิบห้าบาทถ้วน')
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
test('16 digits, not more than Number.MAX_SAFE_INTEGER', () => {
|
|
80
|
-
// Number.MAX_SAFE_INTEGER
|
|
81
|
-
// 9007199254740991
|
|
82
|
-
expect(bahttext(1234567890123450)).toBe('หนึ่งพันสองร้อยสามสิบสี่ล้านห้าแสนหกหมื่นเจ็ดพันแปดร้อยเก้าสิบล้านหนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบบาทถ้วน')
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
test('16 digits, more than Number.MAX_SAFE_INTEGER', () => {
|
|
86
|
-
// eslint-disable-next-line
|
|
87
|
-
expect(bahttext(9007199254750991)).toBe(defaultResult)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
test('more than 16', () => {
|
|
91
|
-
expect(bahttext(12345678901234500)).toBe(defaultResult)
|
|
92
|
-
expect(bahttext(123456789012345000)).toBe(defaultResult)
|
|
93
|
-
expect(bahttext(1234567890123450000)).toBe(defaultResult)
|
|
94
|
-
expect(bahttext(123456789012345000000)).toBe(defaultResult)
|
|
95
|
-
})
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
describe('grammar', () => {
|
|
99
|
-
test('end with 1 (more than 10)', () => {
|
|
100
|
-
expect(bahttext(11)).toBe('สิบเอ็ดบาทถ้วน')
|
|
101
|
-
expect(bahttext(201)).toBe('สองร้อยเอ็ดบาทถ้วน')
|
|
102
|
-
|
|
103
|
-
expect(bahttext(3061.21)).toBe('สามพันหกสิบเอ็ดบาทยี่สิบเอ็ดสตางค์')
|
|
104
|
-
expect(bahttext(456011.71)).toBe('สี่แสนห้าหมื่นหกพันสิบเอ็ดบาทเจ็ดสิบเอ็ดสตางค์')
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
test('end with 1x', () => {
|
|
108
|
-
expect(bahttext(14)).toBe('สิบสี่บาทถ้วน')
|
|
109
|
-
expect(bahttext(40019)).toBe('สี่หมื่นสิบเก้าบาทถ้วน')
|
|
110
|
-
|
|
111
|
-
expect(bahttext(317.10)).toBe('สามร้อยสิบเจ็ดบาทสิบสตางค์')
|
|
112
|
-
expect(bahttext(40019.17)).toBe('สี่หมื่นสิบเก้าบาทสิบเจ็ดสตางค์')
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
test('end with 2x', () => {
|
|
116
|
-
expect(bahttext(22)).toBe('ยี่สิบสองบาทถ้วน')
|
|
117
|
-
expect(bahttext(5723)).toBe('ห้าพันเจ็ดร้อยยี่สิบสามบาทถ้วน')
|
|
118
|
-
|
|
119
|
-
expect(bahttext(57.23)).toBe('ห้าสิบเจ็ดบาทยี่สิบสามสตางค์')
|
|
120
|
-
expect(bahttext(422.26)).toBe('สี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์')
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
test('multi occurrence of 1x', () => {
|
|
124
|
-
expect(bahttext(10056518)).toBe('สิบล้านห้าหมื่นหกพันห้าร้อยสิบแปดบาทถ้วน')
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
test('multi occurrence of 2x', () => {
|
|
128
|
-
expect(bahttext(20056528)).toBe('ยี่สิบล้านห้าหมื่นหกพันห้าร้อยยี่สิบแปดบาทถ้วน')
|
|
129
|
-
})
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
describe('decimals', () => {
|
|
133
|
-
test('general', () => {
|
|
134
|
-
expect(bahttext(1.01)).toBe('หนึ่งบาทหนึ่งสตางค์')
|
|
135
|
-
expect(bahttext(1.02)).toBe('หนึ่งบาทสองสตางค์')
|
|
136
|
-
expect(bahttext(1.03)).toBe('หนึ่งบาทสามสตางค์')
|
|
137
|
-
expect(bahttext(1.04)).toBe('หนึ่งบาทสี่สตางค์')
|
|
138
|
-
expect(bahttext(1.05)).toBe('หนึ่งบาทห้าสตางค์')
|
|
139
|
-
expect(bahttext(1.06)).toBe('หนึ่งบาทหกสตางค์')
|
|
140
|
-
expect(bahttext(1.07)).toBe('หนึ่งบาทเจ็ดสตางค์')
|
|
141
|
-
expect(bahttext(1.08)).toBe('หนึ่งบาทแปดสตางค์')
|
|
142
|
-
expect(bahttext(1.09)).toBe('หนึ่งบาทเก้าสตางค์')
|
|
143
|
-
|
|
144
|
-
expect(bahttext(32.23)).toBe('สามสิบสองบาทยี่สิบสามสตางค์')
|
|
145
|
-
expect(bahttext(474.45)).toBe('สี่ร้อยเจ็ดสิบสี่บาทสี่สิบห้าสตางค์')
|
|
146
|
-
expect(bahttext(5789.67)).toBe('ห้าพันเจ็ดร้อยแปดสิบเก้าบาทหกสิบเจ็ดสตางค์')
|
|
147
|
-
expect(bahttext(63147.89)).toBe('หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์')
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
test('more than 2 decimals, using 2 decimals', () => {
|
|
151
|
-
expect(bahttext(25.1230)).toBe('ยี่สิบห้าบาทสิบสองสตางค์')
|
|
152
|
-
expect(bahttext(345.2345)).toBe('สามร้อยสี่สิบห้าบาทยี่สิบสามสตางค์')
|
|
153
|
-
expect(bahttext(5678.4567)).toBe('ห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์')
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
test('not more than 1', () => {
|
|
157
|
-
expect(bahttext(0.25)).toBe('ยี่สิบห้าสตางค์')
|
|
158
|
-
expect(bahttext(0.36)).toBe('สามสิบหกสตางค์')
|
|
159
|
-
expect(bahttext(0.69)).toBe('หกสิบเก้าสตางค์')
|
|
6
|
+
describe('misc', () => {
|
|
7
|
+
test('invalid number', () => {
|
|
8
|
+
const zeroText = 'ศูนย์บาทถ้วน'
|
|
9
|
+
|
|
10
|
+
expect(bahttext(null)).toBe(zeroText)
|
|
11
|
+
expect(bahttext(true)).toBe(zeroText)
|
|
12
|
+
expect(bahttext(false)).toBe(zeroText)
|
|
13
|
+
expect(bahttext([])).toBe(zeroText)
|
|
14
|
+
expect(bahttext([1, 2, 3])).toBe(zeroText)
|
|
15
|
+
expect(bahttext({})).toBe(zeroText)
|
|
16
|
+
expect(bahttext('')).toBe(zeroText)
|
|
17
|
+
expect(bahttext()).toBe(zeroText)
|
|
18
|
+
expect(bahttext(undefined)).toBe(zeroText)
|
|
19
|
+
expect(bahttext('this-is-not-number')).toBe(zeroText)
|
|
20
|
+
expect(bahttext('it-must-be-number-only')).toBe(zeroText)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('less than Number.MIN_SAFE_INTEGER', () => {
|
|
24
|
+
const items = [1, 20, 9451, 5656549]
|
|
25
|
+
for (let i = 0; i < items.length; i++) {
|
|
26
|
+
expect(bahttext(Number.MIN_SAFE_INTEGER - items[i])).toBe(defaultResult)
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('more than Number.MAX_SAFE_INTEGER', () => {
|
|
31
|
+
const items = [1, 20, 9451, 5656549]
|
|
32
|
+
for (let i = 0; i < items.length; i++) {
|
|
33
|
+
expect(bahttext(Number.MAX_SAFE_INTEGER + items[i])).toBe(defaultResult)
|
|
34
|
+
}
|
|
160
35
|
})
|
|
161
36
|
})
|
|
162
37
|
|
|
163
|
-
describe('
|
|
164
|
-
test('
|
|
165
|
-
|
|
38
|
+
describe('number', () => {
|
|
39
|
+
test('imported from Google Sheet', () => {
|
|
40
|
+
for (let i = 0; i < testCases.length; i++) {
|
|
41
|
+
const customMessage = JSON.stringify(testCases[i])
|
|
42
|
+
const number = Number(testCases[i].number)
|
|
43
|
+
expect(bahttext(number), customMessage).toBe(testCases[i].text)
|
|
44
|
+
}
|
|
166
45
|
})
|
|
167
46
|
})
|