bahttext 2.0.3 → 2.1.1

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 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 [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c), [Demo page](https://jojoee.github.io/bahttext/)
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
 
@@ -36,21 +36,62 @@ import { bahttext } from "bahttext"
36
36
  ## Example usage
37
37
 
38
38
  ```javascript
39
- // หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์
40
- bahttext(63147.89)
39
+ bahttext(8.00) // แปดบาทถ้วน
40
+ bahttext(5678.00) // ห้าพันหกร้อยเจ็ดสิบแปดบาทถ้วน
41
+ bahttext(63147.89) // หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์
42
+ bahttext(51000001.00) // ห้าสิบเอ็ดล้านหนึ่งบาทถ้วน
43
+ bahttext(317.10) // สามร้อยสิบเจ็ดบาทสิบสตางค์
44
+ bahttext(422.26) // สี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์
45
+ bahttext(11.11) // สิบเอ็ดบาทสิบเอ็ดสตางค์
46
+ bahttext(191415.11) // หนึ่งแสนเก้าหมื่นหนึ่งพันสี่ร้อยสิบห้าบาทสิบเอ็ดสตางค์
47
+ bahttext(1.01) // หนึ่งบาทหนึ่งสตางค์
48
+ bahttext(5678.46) // ห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์
49
+ bahttext(0.67) // หกสิบเจ็ดสตางค์
50
+ bahttext(-3.00) // ลบสามบาทถ้วน
51
+ bahttext(-232.00) // ลบสองร้อยสามสิบสองบาทถ้วน
52
+ bahttext(-44444.00) // ลบสี่หมื่นสี่พันสี่ร้อยสี่สิบสี่บาทถ้วน
53
+ bahttext(-5678934.00) // ลบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยสามสิบสี่บาทถ้วน
54
+ bahttext(-201.00) // ลบสองร้อยหนึ่งบาทถ้วน
55
+ bahttext(-317.10) // ลบสามร้อยสิบเจ็ดบาทสิบสตางค์
56
+ bahttext(-5723.00) // ลบห้าพันเจ็ดร้อยยี่สิบสามบาทถ้วน
57
+ bahttext(-11.00) // ลบสิบเอ็ดบาทถ้วน
58
+ bahttext(-45621.21) // ลบสี่หมื่นห้าพันหกร้อยยี่สิบเอ็ดบาทยี่สิบเอ็ดสตางค์
59
+ bahttext(-191415.11) // ลบหนึ่งแสนเก้าหมื่นหนึ่งพันสี่ร้อยสิบห้าบาทสิบเอ็ดสตางค์
60
+ bahttext(-282622.22) // ลบสองแสนแปดหมื่นสองพันหกร้อยยี่สิบสองบาทยี่สิบสองสตางค์
61
+ bahttext(-1.04) // ลบหนึ่งบาทสี่สตางค์
62
+ bahttext(-574.45) // ลบห้าร้อยเจ็ดสิบสี่บาทสี่สิบห้าสตางค์
63
+ bahttext(-345.23) // ลบสามร้อยสี่สิบห้าบาทยี่สิบสามสตางค์
64
+ bahttext(-0.20) // ลบยี่สิบสตางค์
41
65
  ```
42
66
 
43
67
  ## Note
44
68
  - [x] Compatible with all browsers
45
- - [x] Node.js version support: 6-12
69
+ - [x] Node.js version support: 6+
46
70
  - [x] 0 Dependencies
47
71
  - [x] Unit test
48
- - [ ] Browser compatibility test
49
- - [ ] Remove duplication between test code and test-case-csv file
72
+ - [x] Remove duplication between test code and test-case-csv file
50
73
  - [x] [Demo page](https://jojoee.github.io/bahttext/)
51
- - [ ] Support negative number
74
+ - [x] Support negative number
52
75
  - [x] Fix semantic-release
53
76
 
77
+ ## Other languages
78
+ - Python: [jojoee/pybaht](https://github.com/jojoee/pybaht)
79
+
80
+ ## CMD
81
+
82
+ ```
83
+ brew install curl
84
+ brew install jq
85
+ npm install -g csvtojson
86
+ curl -L -o ./misc/testcases.csv https://docs.google.com/spreadsheets/d/e/2PACX-1vTb8PIKzgo07rn9UpcjqE0YrdMAmf4fyDbL2plUieLCyrn_5O3vDvece7UfkaArWQLUSsaw92jVpY_z/pub?gid=0&single=true&output=csv
87
+ csvtojson ./misc/testcases.csv | jq > ./misc/testcases.json
88
+
89
+ # to update dependency version
90
+ npm update --save
91
+ npm audit fix --force
92
+ ```
93
+
54
94
  ## Reference
95
+ - [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en)
55
96
  - [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
56
97
  - 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
- เปลี่ยนตัวเลข เป็นคำอ่านภาษาไทย, โมดูลตัวนี้ได้ทำการทดสอบกับ [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c), [Demo page](https://jojoee.github.io/bahttext/) เรียบร้อยแล้ว
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
 
@@ -35,21 +35,62 @@ import { bahttext } from "bahttext"
35
35
  ## ตัวอย่างการใช้งาน
36
36
 
37
37
  ```javascript
38
- // หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์
39
- bahttext(63147.89)
38
+ bahttext(8.00) // แปดบาทถ้วน
39
+ bahttext(5678.00) // ห้าพันหกร้อยเจ็ดสิบแปดบาทถ้วน
40
+ bahttext(63147.89) // หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์
41
+ bahttext(51000001.00) // ห้าสิบเอ็ดล้านหนึ่งบาทถ้วน
42
+ bahttext(317.10) // สามร้อยสิบเจ็ดบาทสิบสตางค์
43
+ bahttext(422.26) // สี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์
44
+ bahttext(11.11) // สิบเอ็ดบาทสิบเอ็ดสตางค์
45
+ bahttext(191415.11) // หนึ่งแสนเก้าหมื่นหนึ่งพันสี่ร้อยสิบห้าบาทสิบเอ็ดสตางค์
46
+ bahttext(1.01) // หนึ่งบาทหนึ่งสตางค์
47
+ bahttext(5678.46) // ห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์
48
+ bahttext(0.67) // หกสิบเจ็ดสตางค์
49
+ bahttext(-3.00) // ลบสามบาทถ้วน
50
+ bahttext(-232.00) // ลบสองร้อยสามสิบสองบาทถ้วน
51
+ bahttext(-44444.00) // ลบสี่หมื่นสี่พันสี่ร้อยสี่สิบสี่บาทถ้วน
52
+ bahttext(-5678934.00) // ลบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยสามสิบสี่บาทถ้วน
53
+ bahttext(-201.00) // ลบสองร้อยหนึ่งบาทถ้วน
54
+ bahttext(-317.10) // ลบสามร้อยสิบเจ็ดบาทสิบสตางค์
55
+ bahttext(-5723.00) // ลบห้าพันเจ็ดร้อยยี่สิบสามบาทถ้วน
56
+ bahttext(-11.00) // ลบสิบเอ็ดบาทถ้วน
57
+ bahttext(-45621.21) // ลบสี่หมื่นห้าพันหกร้อยยี่สิบเอ็ดบาทยี่สิบเอ็ดสตางค์
58
+ bahttext(-191415.11) // ลบหนึ่งแสนเก้าหมื่นหนึ่งพันสี่ร้อยสิบห้าบาทสิบเอ็ดสตางค์
59
+ bahttext(-282622.22) // ลบสองแสนแปดหมื่นสองพันหกร้อยยี่สิบสองบาทยี่สิบสองสตางค์
60
+ bahttext(-1.04) // ลบหนึ่งบาทสี่สตางค์
61
+ bahttext(-574.45) // ลบห้าร้อยเจ็ดสิบสี่บาทสี่สิบห้าสตางค์
62
+ bahttext(-345.23) // ลบสามร้อยสี่สิบห้าบาทยี่สิบสามสตางค์
63
+ bahttext(-0.20) // ลบยี่สิบสตางค์
40
64
  ```
41
65
 
42
66
  ## ฟีเจอร์
43
67
  - [x] สามารถใช้งานได้ทุก เบราว์เซอร์
44
- - [x] สามารถใช้งานได้ตั้งแต่ Node.js version 6-12
68
+ - [x] สามารถใช้งานได้ตั้งแต่ Node.js version 6+
45
69
  - [x] 0 Dependencies
46
70
  - [x] ทำการทดสอบแบบ unit test
47
- - [ ] ทดสอบบนเบราว์เซอร์ ในแต่ละ เวอร์ชั่น
48
- - [ ] ลดการซ้ำซ้อนของการเขียน ระหว่าง code และ ไฟล์ csv ในการทำการทดสอบ
71
+ - [x] ลดการซ้ำซ้อนของการเขียน ระหว่าง code และ ไฟล์ csv ในการทำการทดสอบ
49
72
  - [x] [หน้าตัวอย่างการใช้งาน](https://jojoee.github.io/bahttext/)
50
- - [ ] สนับสนุนการใช้งานกับตัวเลขติดลบ
73
+ - [x] สนับสนุนการใช้งานกับตัวเลขติดลบ
51
74
  - [x] แก้ไข semantic-release
52
75
 
76
+ ## ภาษาอื่นๆ
77
+ - Python: [jojoee/pybaht](https://github.com/jojoee/pybaht)
78
+
79
+ ## CMD
80
+
81
+ ```
82
+ brew install curl
83
+ brew install jq
84
+ npm install -g csvtojson
85
+ curl -L -o ./misc/testcases.csv https://docs.google.com/spreadsheets/d/e/2PACX-1vTb8PIKzgo07rn9UpcjqE0YrdMAmf4fyDbL2plUieLCyrn_5O3vDvece7UfkaArWQLUSsaw92jVpY_z/pub?gid=0&single=true&output=csv
86
+ csvtojson ./misc/testcases.csv | jq > ./misc/testcases.json
87
+
88
+ # to update dependency version
89
+ npm update --save
90
+ npm audit fix --force
91
+ ```
92
+
53
93
  ## อ้างอิง
94
+ - [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en)
54
95
  - [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
55
96
  - แรงบัลดาลใจจาก [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>thai</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>thai</th>
50
+ <th>text</th>
51
51
  </tr>
52
52
  </tfoot>
53
53
  </table>
@@ -74,12 +74,15 @@ function updateResult() {
74
74
  $result.html(html)
75
75
  }
76
76
 
77
- function init() {
77
+ function randomNumber() {
78
+ const num = r.real(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
79
+ $number.val(num)
78
80
  updateResult()
79
81
  }
80
82
 
81
83
  $(document).ready(function() {
82
- init()
84
+ // init
85
+ randomNumber()
83
86
 
84
87
  // number
85
88
  $number.on('input', function(e) {
@@ -88,9 +91,7 @@ $(document).ready(function() {
88
91
 
89
92
  // random
90
93
  $random.click(function(e) {
91
- const num = r.real(0, Number.MAX_SAFE_INTEGER)
92
- $number.val(num)
93
- updateResult()
94
+ randomNumber()
94
95
  })
95
96
 
96
97
  // example
@@ -99,7 +100,7 @@ $(document).ready(function() {
99
100
  searching: false,
100
101
  ordering: false,
101
102
  ajax: {
102
- url: 'misc/test.json',
103
+ url: 'misc/testcases.json',
103
104
  dataSrc: ''
104
105
  },
105
106
  columns: [
@@ -116,7 +117,7 @@ $(document).ready(function() {
116
117
  width: '22%'
117
118
  },
118
119
  {
119
- data: 'thai'
120
+ data: 'text'
120
121
  }
121
122
  ]
122
123
  })
package/package.json CHANGED
@@ -1,26 +1,18 @@
1
1
  {
2
2
  "name": "bahttext",
3
- "version": "2.0.3",
3
+ "version": "2.1.1",
4
4
  "description": "Change number to Thai pronunciation string",
5
5
  "main": "src/index.js",
6
6
  "typings": "src/index.js",
7
7
  "scripts": {
8
8
  "jest": "jest",
9
9
  "jest.watch": "jest --watch",
10
- "commit": "git-cz",
11
10
  "coverage.check": "jest --coverage",
12
11
  "semantic-release": "semantic-release",
13
12
  "standard": "standard",
13
+ "standard.fix": "standard --fix",
14
14
  "validate": "npm run standard && npm run coverage.check"
15
15
  },
16
- "config": {
17
- "ghooks": {
18
- "pre-commit": "npm run validate"
19
- },
20
- "commitizen": {
21
- "path": "node_modules/cz-conventional-changelog"
22
- }
23
- },
24
16
  "repository": {
25
17
  "type": "git",
26
18
  "url": "https://github.com/jojoee/bahttext.git"
@@ -39,16 +31,16 @@
39
31
  "url": "https://github.com/jojoee/bahttext/issues"
40
32
  },
41
33
  "homepage": "https://github.com/jojoee/bahttext#readme",
42
- "dependencies": {},
43
34
  "devDependencies": {
44
- "commitizen": "4.2.2",
45
- "cz-conventional-changelog": "3.3.0",
46
- "ghooks": "2.0.4",
47
- "jest": "^26.6.1",
48
- "semantic-release": "^17.2.1",
49
- "standard": "^15.0.0"
35
+ "jest": "^26.6.3",
36
+ "jest-expect-message": "^1.0.2",
37
+ "semantic-release": "^19.0.5",
38
+ "standard": "^15.0.1"
50
39
  },
51
40
  "jest": {
41
+ "setupFilesAfterEnv": [
42
+ "jest-expect-message"
43
+ ],
52
44
  "verbose": true,
53
45
  "testMatch": [
54
46
  "**/*.test.js"
package/src/index.js CHANGED
@@ -1,41 +1,14 @@
1
- /*
2
- step:
3
- 1. validate
4
- 2. sanitize
5
- 3. split
6
- 4. proceed
7
- 5. grammar
8
- 6. combine
9
- */
10
-
11
- 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
- ]
1
+ const bahtxtConst = {
2
+ defaultResult: 'ศูนย์บาทถ้วน',
3
+ singleUnitStrs: ['', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า'],
4
+ placeNameStrs: ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']
5
+ }
33
6
 
34
7
  /**
35
8
  * @param {number[]} nums
36
9
  * @returns {string}
37
10
  */
38
- function num2Word (nums) {
11
+ function bahtxtNum2Word (nums) {
39
12
  let result = ''
40
13
  const len = nums.length
41
14
  const maxLen = 7
@@ -45,12 +18,12 @@ function num2Word (nums) {
45
18
  const overflowIndex = len - maxLen + 1
46
19
  const overflowNums = nums.slice(0, overflowIndex)
47
20
  const remainingNumbs = nums.slice(overflowIndex)
48
- return num2Word(overflowNums) + 'ล้าน' + num2Word(remainingNumbs)
21
+ return bahtxtNum2Word(overflowNums) + 'ล้าน' + bahtxtNum2Word(remainingNumbs)
49
22
  } else {
50
23
  for (let i = 0; i < len; i++) {
51
24
  const digit = nums[i]
52
25
  if (digit > 0) {
53
- result += singleUnitStrs[digit] + placeNameStrs[len - i - 1]
26
+ result += bahtxtConst.singleUnitStrs[digit] + bahtxtConst.placeNameStrs[len - i - 1]
54
27
  }
55
28
  }
56
29
  }
@@ -59,39 +32,33 @@ function num2Word (nums) {
59
32
  }
60
33
 
61
34
  /**
35
+ * @todo improve performance
62
36
  * @param {string} str
63
37
  * @returns {string}
64
38
  */
65
- function grammarFix (str) {
39
+ function bahtxtGrammarFix (str) {
66
40
  let result = str
67
41
 
68
- // "สิบ"
69
42
  result = result.replace(/หนึ่งสิบ/g, 'สิบ')
70
- // "ยี่สิบ"
71
43
  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
- }
44
+ result = result.replace(/สิบหนึ่ง/g, 'สิบเอ็ด')
78
45
 
79
46
  return result
80
47
  }
81
48
 
82
49
  /**
83
- * Combine baht and satang
50
+ * bahtxtCombine baht and satang
84
51
  * and also adding unit
85
52
  *
86
53
  * @param {string} baht
87
54
  * @param {string} satang
88
55
  * @returns {string}
89
56
  */
90
- function combine (baht, satang) {
57
+ function bahtxtCombine (baht, satang) {
91
58
  let result = ''
92
59
 
93
60
  if (baht === '' && satang === '') {
94
- result = defaultResult
61
+ result = bahtxtConst.defaultResult
95
62
  } else if (baht !== '' && satang === '') {
96
63
  result = baht + 'บาท' + 'ถ้วน'
97
64
  } else if (baht === '' && satang !== '') {
@@ -110,48 +77,53 @@ function combine (baht, satang) {
110
77
  * @returns {string}
111
78
  */
112
79
  function bahttext (num) {
113
- let result = defaultResult
114
-
115
- // 1. validate: invalid number
116
- if (isNaN(num)) return result
117
- // 1. validate: more than
118
- if (num >= Number.MAX_SAFE_INTEGER) return result
119
-
120
- // 2. sanitize: ????
121
-
122
- // 3. split: baht and satang
123
- // e.g. 432.21 >> 432, 21
124
- // @todo optimize
80
+ // no null
81
+ if (!num) return bahtxtConst.defaultResult
82
+ // no boolean
83
+ if (typeof num === 'boolean') return bahtxtConst.defaultResult
84
+ // must be number only
85
+ if (isNaN(Number(num))) return bahtxtConst.defaultResult
86
+ // not less than Number.MIN_SAFE_INTEGER
87
+ if (num < Number.MIN_SAFE_INTEGER) return bahtxtConst.defaultResult
88
+ // no more than Number.MAX_SAFE_INTEGER
89
+ if (num > Number.MAX_SAFE_INTEGER) return bahtxtConst.defaultResult
90
+
91
+ // set
92
+ const positiveNum = Math.abs(num)
93
+
94
+ // split baht and satang e.g. 432.214567 >> 432, 21
95
+ const bahtStr = Math.floor(positiveNum).toString()
125
96
  /** @type {string} */
126
- const bahtStr = Math.floor(num).toString()
127
- /** @type {string} */
128
- const satangStr = Math.round(num % 1 * 100).toString()
97
+ const satangStr = (positiveNum % 1 * 100).toFixed(2).split('.')[0]
129
98
 
130
- // 3. split: convert number array
131
- // @todo optimize it
132
99
  /** @type {number[]} */
133
100
  const bahtArr = Array.from(bahtStr).map(Number)
134
101
  /** @type {number[]} */
135
102
  const satangArr = Array.from(satangStr).map(Number)
136
103
 
137
- // 4. proceed
138
- let baht = num2Word(bahtArr)
139
- let satang = num2Word(satangArr)
104
+ // proceed
105
+ let baht = bahtxtNum2Word(bahtArr)
106
+ let satang = bahtxtNum2Word(satangArr)
140
107
 
141
- // 5. grammar
142
- baht = grammarFix(baht)
143
- satang = grammarFix(satang)
108
+ // grammar
109
+ baht = bahtxtGrammarFix(baht)
110
+ satang = bahtxtGrammarFix(satang)
144
111
 
145
- // 6. combine
146
- result = combine(baht, satang)
112
+ // combine
113
+ const result = bahtxtCombine(baht, satang)
147
114
 
148
- return result
115
+ return num >= 0 ? result : 'ลบ' + result
149
116
  }
150
117
 
151
118
  if (typeof module !== 'undefined' &&
152
119
  module.exports != null) {
153
120
  module.exports = {
154
- bahttext
121
+ bahttext,
122
+
123
+ // export for testing only
124
+ bahtxtNum2Word,
125
+ bahtxtGrammarFix,
126
+ bahtxtCombine
155
127
  }
156
128
  exports.default = {
157
129
  bahttext
package/src/index.test.js CHANGED
@@ -1,167 +1,93 @@
1
- const { bahttext } = require('./index')
1
+ const {
2
+ bahttext,
3
+ bahtxtGrammarFix: grammarFix,
4
+ bahtxtCombine: combine
5
+ } = require('./index')
2
6
  const defaultResult = 'ศูนย์บาทถ้วน'
7
+ const testCases = require('../misc/testcases.json')
3
8
  jest.autoMockOff()
4
9
 
5
- describe('digit', () => {
6
- test('1, zero', () => {
7
- expect(bahttext(0)).toBe('ศูนย์บาทถ้วน')
8
- })
9
-
10
- test('1 digit', () => {
11
- expect(bahttext(1)).toBe('หนึ่งบาทถ้วน')
12
- expect(bahttext(2)).toBe('สองบาทถ้วน')
13
- expect(bahttext(3)).toBe('สามบาทถ้วน')
14
- expect(bahttext(4)).toBe('สี่บาทถ้วน')
15
- expect(bahttext(5)).toBe('ห้าบาทถ้วน')
16
- expect(bahttext(6)).toBe('หกบาทถ้วน')
17
- expect(bahttext(7)).toBe('เจ็ดบาทถ้วน')
18
- expect(bahttext(8)).toBe('แปดบาทถ้วน')
19
- expect(bahttext(9)).toBe('เก้าบาทถ้วน')
20
- })
21
-
22
- test('2', () => {
23
- expect(bahttext(37)).toBe('สามสิบเจ็ดบาทถ้วน')
24
- expect(bahttext(48)).toBe('สี่สิบแปดบาทถ้วน')
25
- })
26
-
27
- test('3', () => {
28
- expect(bahttext(232)).toBe('สองร้อยสามสิบสองบาทถ้วน')
29
- })
30
-
31
- test('4', () => {
32
- expect(bahttext(3333)).toBe('สามพันสามร้อยสามสิบสามบาทถ้วน')
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)
10
+ // TODO: to be implemented
11
+ describe('num2Word', () => {
12
+ test('test', () => {
13
+ expect(true).toBeTruthy()
95
14
  })
96
15
  })
97
16
 
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('สี่แสนห้าหมื่นหกพันสิบเอ็ดบาทเจ็ดสิบเอ็ดสตางค์')
17
+ describe('grammarFix', () => {
18
+ test('replace "หนึ่งสิบ" with "สิบ"', () => {
19
+ expect(grammarFix('หนึ่งสิบหก')).toBe('สิบหก')
20
+ expect(grammarFix('หนึ่งสิบสี่')).toBe('สิบสี่')
21
+ expect(grammarFix('ห้าร้อยหนึ่งสิบสาม')).toBe('ห้าร้อยสิบสาม')
22
+ expect(grammarFix('สี่หมื่นหนึ่งสิบสาม')).toBe('สี่หมื่นสิบสาม')
105
23
  })
106
24
 
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('สี่หมื่นสิบเก้าบาทสิบเจ็ดสตางค์')
25
+ test('replace "สองสิบ" with "ยี่สิบ"', () => {
26
+ expect(grammarFix('ห้าร้อยสองสิบหนึ่ง')).toBe('ห้าร้อยยี่สิบเอ็ด')
27
+ expect(grammarFix('สองสิบสี่')).toBe('ยี่สิบสี่')
28
+ expect(grammarFix('เก้าแสนหกหมื่นสามพันสามร้อยสองสิบสี่')).toBe('เก้าแสนหกหมื่นสามพันสามร้อยยี่สิบสี่')
29
+ expect(grammarFix('เจ็ดหมื่นห้าพันสี่ร้อยสองสิบหนึ่ง')).toBe('เจ็ดหมื่นห้าพันสี่ร้อยยี่สิบเอ็ด')
113
30
  })
114
31
 
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 1x', () => {
128
- expect(bahttext(20056528)).toBe('ยี่สิบล้านห้าหมื่นหกพันห้าร้อยยี่สิบแปดบาทถ้วน')
32
+ test('replace "สิบหนึ่ง" with "สิบเอ็ด"', () => {
33
+ expect(grammarFix('หนึ่งสิบหนึ่ง')).toBe('สิบเอ็ด')
129
34
  })
130
35
  })
131
36
 
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('หนึ่งบาทเก้าสตางค์')
37
+ describe('combine', () => {
38
+ test('both baht and satang are empty', () => {
39
+ expect(combine('', '')).toBe('ศูนย์บาทถ้วน')
40
+ })
143
41
 
144
- expect(bahttext(32.23)).toBe('สามสิบสองบาทยี่สิบสามสตางค์')
145
- expect(bahttext(474.45)).toBe('สี่ร้อยเจ็ดสิบสี่บาทสี่สิบห้าสตางค์')
146
- expect(bahttext(5789.67)).toBe('ห้าพันเจ็ดร้อยแปดสิบเก้าบาทหกสิบเจ็ดสตางค์')
147
- expect(bahttext(63147.89)).toBe('หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์')
42
+ test('both baht and satang are not empty', () => {
43
+ expect(combine('หนึ่งร้อยยี่สิบสาม', 'ห้าสิบหก')).toBe('หนึ่งร้อยยี่สิบสามบาทห้าสิบหกสตางค์')
148
44
  })
149
45
 
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('ห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์')
46
+ test('has only baht', () => {
47
+ expect(combine('แปดแสนเจ็ดหมื่นสี่พันห้าร้อยหกสิบสาม', '')).toBe('แปดแสนเจ็ดหมื่นสี่พันห้าร้อยหกสิบสามบาทถ้วน')
154
48
  })
155
49
 
156
- test('not more than 1', () => {
157
- expect(bahttext(0.25)).toBe('ยี่สิบห้าสตางค์')
158
- expect(bahttext(0.36)).toBe('สามสิบหกสตางค์')
159
- expect(bahttext(0.69)).toBe('หกสิบเก้าสตางค์')
50
+ test('has only satang', () => {
51
+ expect(combine('', 'ลบสามสิบหก')).toBe('ลบสามสิบหกสตางค์')
160
52
  })
161
53
  })
162
54
 
163
- describe('misc', () => {
55
+ describe('bahttext', () => {
164
56
  test('invalid number', () => {
165
- expect(bahttext('invalid number')).toBe('ศูนย์บาทถ้วน')
57
+ const zeroText = 'ศูนย์บาทถ้วน'
58
+
59
+ expect(bahttext(null)).toBe(zeroText)
60
+ expect(bahttext(true)).toBe(zeroText)
61
+ expect(bahttext(false)).toBe(zeroText)
62
+ expect(bahttext([])).toBe(zeroText)
63
+ expect(bahttext([1, 2, 3])).toBe(zeroText)
64
+ expect(bahttext({})).toBe(zeroText)
65
+ expect(bahttext('')).toBe(zeroText)
66
+ expect(bahttext()).toBe(zeroText)
67
+ expect(bahttext(undefined)).toBe(zeroText)
68
+ expect(bahttext('this-is-not-number')).toBe(zeroText)
69
+ expect(bahttext('it-must-be-number-only')).toBe(zeroText)
70
+ })
71
+
72
+ test('less than Number.MIN_SAFE_INTEGER', () => {
73
+ const items = [1, 20, 9451, 5656549]
74
+ for (let i = 0; i < items.length; i++) {
75
+ expect(bahttext(Number.MIN_SAFE_INTEGER - items[i])).toBe(defaultResult)
76
+ }
77
+ })
78
+
79
+ test('more than Number.MAX_SAFE_INTEGER', () => {
80
+ const items = [1, 20, 9451, 5656549]
81
+ for (let i = 0; i < items.length; i++) {
82
+ expect(bahttext(Number.MAX_SAFE_INTEGER + items[i])).toBe(defaultResult)
83
+ }
84
+ })
85
+
86
+ test('numbers are imported from Google Sheet', () => {
87
+ for (let i = 0; i < testCases.length; i++) {
88
+ const customMessage = JSON.stringify(testCases[i])
89
+ const number = Number(testCases[i].number)
90
+ expect(bahttext(number), customMessage).toBe(testCases[i].text)
91
+ }
166
92
  })
167
93
  })