bahttext 2.0.3
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/LICENSE +21 -0
- package/README-en.md +56 -0
- package/README.md +55 -0
- package/bower.json +21 -0
- package/index.html +126 -0
- package/package.json +69 -0
- package/src/index.js +159 -0
- package/src/index.test.js +167 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Nathachai Thongniran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README-en.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# bahttext
|
|
2
|
+
|
|
3
|
+
[](https://codecov.io/github/jojoee/bahttext)
|
|
4
|
+
[](https://www.npmjs.com/package/bahttext)
|
|
5
|
+
[](http://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://github.com/semantic-release/semantic-release) [](https://greenkeeper.io/)
|
|
7
|
+
|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
Language: [ไทย](https://github.com/jojoee/bahttext/blob/master/README.md), [English](https://github.com/jojoee/bahttext/blob/master/README-en.md)
|
|
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/)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
// CommonJS
|
|
21
|
+
npm install bahttext
|
|
22
|
+
const { bahttext } = require('bahttext')
|
|
23
|
+
|
|
24
|
+
// Bower
|
|
25
|
+
bower install bahttext
|
|
26
|
+
<script src="bower_components/bahttext/src/index.js"></script>
|
|
27
|
+
|
|
28
|
+
// githack
|
|
29
|
+
<script src="https://raw.githack.com/jojoee/bahttext/master/src/index.js"></script>
|
|
30
|
+
|
|
31
|
+
// ES6
|
|
32
|
+
npm install bahttext
|
|
33
|
+
import { bahttext } from "bahttext"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Example usage
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
// หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์
|
|
40
|
+
bahttext(63147.89)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Note
|
|
44
|
+
- [x] Compatible with all browsers
|
|
45
|
+
- [x] Node.js version support: 6-12
|
|
46
|
+
- [x] 0 Dependencies
|
|
47
|
+
- [x] Unit test
|
|
48
|
+
- [ ] Browser compatibility test
|
|
49
|
+
- [ ] Remove duplication between test code and test-case-csv file
|
|
50
|
+
- [x] [Demo page](https://jojoee.github.io/bahttext/)
|
|
51
|
+
- [ ] Support negative number
|
|
52
|
+
- [x] Fix semantic-release
|
|
53
|
+
|
|
54
|
+
## Reference
|
|
55
|
+
- [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
|
|
56
|
+
- Inspired by [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# bahttext
|
|
2
|
+
|
|
3
|
+
[](https://codecov.io/github/jojoee/bahttext)
|
|
4
|
+
[](https://www.npmjs.com/package/bahttext)
|
|
5
|
+
[](http://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://github.com/semantic-release/semantic-release) [](https://greenkeeper.io/)
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
ภาษา: [ไทย](https://github.com/jojoee/bahttext/blob/master/README.md), [English](https://github.com/jojoee/bahttext/blob/master/README-en.md)
|
|
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/) เรียบร้อยแล้ว
|
|
15
|
+
|
|
16
|
+
## ติดตั้ง
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
// ติดตั้งด้วย CommonJS
|
|
20
|
+
npm install bahttext
|
|
21
|
+
const { bahttext } = require('bahttext')
|
|
22
|
+
|
|
23
|
+
// ติดตั้งด้วย Bower
|
|
24
|
+
bower install bahttext
|
|
25
|
+
<script src="bower_components/bahttext/src/index.js"></script>
|
|
26
|
+
|
|
27
|
+
// ติดตั้งด้วย githack
|
|
28
|
+
<script src="https://raw.githack.com/jojoee/bahttext/master/src/index.js"></script>
|
|
29
|
+
|
|
30
|
+
// ติดตั้งด้วย ES6
|
|
31
|
+
npm install bahttext
|
|
32
|
+
import { bahttext } from "bahttext"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## ตัวอย่างการใช้งาน
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
// หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์
|
|
39
|
+
bahttext(63147.89)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## ฟีเจอร์
|
|
43
|
+
- [x] สามารถใช้งานได้ทุก เบราว์เซอร์
|
|
44
|
+
- [x] สามารถใช้งานได้ตั้งแต่ Node.js version 6-12
|
|
45
|
+
- [x] 0 Dependencies
|
|
46
|
+
- [x] ทำการทดสอบแบบ unit test
|
|
47
|
+
- [ ] ทดสอบบนเบราว์เซอร์ ในแต่ละ เวอร์ชั่น
|
|
48
|
+
- [ ] ลดการซ้ำซ้อนของการเขียน ระหว่าง code และ ไฟล์ csv ในการทำการทดสอบ
|
|
49
|
+
- [x] [หน้าตัวอย่างการใช้งาน](https://jojoee.github.io/bahttext/)
|
|
50
|
+
- [ ] สนับสนุนการใช้งานกับตัวเลขติดลบ
|
|
51
|
+
- [x] แก้ไข semantic-release
|
|
52
|
+
|
|
53
|
+
## อ้างอิง
|
|
54
|
+
- [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
|
|
55
|
+
- แรงบัลดาลใจจาก [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)
|
package/bower.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bahttext",
|
|
3
|
+
"homepage": "https://github.com/jojoee/bahttext",
|
|
4
|
+
"authors": [
|
|
5
|
+
"Nathachai Thongniran <inid3a@gmail.com>"
|
|
6
|
+
],
|
|
7
|
+
"description": "Change number to Thai pronunciation string",
|
|
8
|
+
"main": "src/index.js",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"baht",
|
|
11
|
+
"thai"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"ignore": [
|
|
15
|
+
"**/.*",
|
|
16
|
+
"node_modules",
|
|
17
|
+
"bower_components",
|
|
18
|
+
"test",
|
|
19
|
+
"tests"
|
|
20
|
+
]
|
|
21
|
+
}
|
package/index.html
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
7
|
+
<title>bahttext.js</title>
|
|
8
|
+
<link href='//fonts.googleapis.com/css?family=Raleway:400,300,600' rel='stylesheet' type='text/css'>
|
|
9
|
+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
|
|
10
|
+
<link rel="stylesheet" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
|
|
11
|
+
<style>
|
|
12
|
+
table {
|
|
13
|
+
table-layout: fixed
|
|
14
|
+
}
|
|
15
|
+
td {
|
|
16
|
+
overflow: hidden;
|
|
17
|
+
white-space: nowrap;
|
|
18
|
+
text-overflow: ellipsis;
|
|
19
|
+
}
|
|
20
|
+
</style>
|
|
21
|
+
</head>
|
|
22
|
+
<body>
|
|
23
|
+
<div class="container">
|
|
24
|
+
<div class="row">
|
|
25
|
+
<h2>bahttext.js</h2>
|
|
26
|
+
<hr>
|
|
27
|
+
|
|
28
|
+
<input type="text" id="number" class="u-full-width" placeholder="number">
|
|
29
|
+
<div id="result" class="u-full-width"></div><br>
|
|
30
|
+
<button id="random">
|
|
31
|
+
Random
|
|
32
|
+
</button>
|
|
33
|
+
<hr>
|
|
34
|
+
|
|
35
|
+
<h4>Example</h4>
|
|
36
|
+
<table id="example" class="display" cellspacing="0" width="100%">
|
|
37
|
+
<thead>
|
|
38
|
+
<tr>
|
|
39
|
+
<th>category</th>
|
|
40
|
+
<th>case</th>
|
|
41
|
+
<th>number</th>
|
|
42
|
+
<th>thai</th>
|
|
43
|
+
</tr>
|
|
44
|
+
</thead>
|
|
45
|
+
<tfoot>
|
|
46
|
+
<tr>
|
|
47
|
+
<th>category</th>
|
|
48
|
+
<th>case</th>
|
|
49
|
+
<th>number</th>
|
|
50
|
+
<th>thai</th>
|
|
51
|
+
</tr>
|
|
52
|
+
</tfoot>
|
|
53
|
+
</table>
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
|
|
57
|
+
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
|
|
58
|
+
<script src="//cdnjs.cloudflare.com/ajax/libs/random-js/1.0.8/random.min.js"></script>
|
|
59
|
+
<script src="./src/index.js"></script>
|
|
60
|
+
<script>
|
|
61
|
+
// random
|
|
62
|
+
const r = new Random(Random.engines.mt19937().autoSeed())
|
|
63
|
+
|
|
64
|
+
const $number = $('#number')
|
|
65
|
+
const $result = $('#result')
|
|
66
|
+
const $random = $('#random')
|
|
67
|
+
const $exmaple = $('#example')
|
|
68
|
+
|
|
69
|
+
function updateResult() {
|
|
70
|
+
const val = $number.val()
|
|
71
|
+
const num = parseFloat(val)
|
|
72
|
+
const html = bahttext(num)
|
|
73
|
+
|
|
74
|
+
$result.html(html)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function init() {
|
|
78
|
+
updateResult()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
$(document).ready(function() {
|
|
82
|
+
init()
|
|
83
|
+
|
|
84
|
+
// number
|
|
85
|
+
$number.on('input', function(e) {
|
|
86
|
+
updateResult()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
// random
|
|
90
|
+
$random.click(function(e) {
|
|
91
|
+
const num = r.real(0, Number.MAX_SAFE_INTEGER)
|
|
92
|
+
$number.val(num)
|
|
93
|
+
updateResult()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// example
|
|
97
|
+
$exmaple.DataTable({
|
|
98
|
+
paging: false,
|
|
99
|
+
searching: false,
|
|
100
|
+
ordering: false,
|
|
101
|
+
ajax: {
|
|
102
|
+
url: 'misc/test.json',
|
|
103
|
+
dataSrc: ''
|
|
104
|
+
},
|
|
105
|
+
columns: [
|
|
106
|
+
{
|
|
107
|
+
data: 'category',
|
|
108
|
+
width: '10%'
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
data: 'case',
|
|
112
|
+
width: '16%'
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
data: 'number',
|
|
116
|
+
width: '22%'
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
data: 'thai'
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
</script>
|
|
125
|
+
</body>
|
|
126
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bahttext",
|
|
3
|
+
"version": "2.0.3",
|
|
4
|
+
"description": "Change number to Thai pronunciation string",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"typings": "src/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"jest": "jest",
|
|
9
|
+
"jest.watch": "jest --watch",
|
|
10
|
+
"commit": "git-cz",
|
|
11
|
+
"coverage.check": "jest --coverage",
|
|
12
|
+
"semantic-release": "semantic-release",
|
|
13
|
+
"standard": "standard",
|
|
14
|
+
"validate": "npm run standard && npm run coverage.check"
|
|
15
|
+
},
|
|
16
|
+
"config": {
|
|
17
|
+
"ghooks": {
|
|
18
|
+
"pre-commit": "npm run validate"
|
|
19
|
+
},
|
|
20
|
+
"commitizen": {
|
|
21
|
+
"path": "node_modules/cz-conventional-changelog"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/jojoee/bahttext.git"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"thai",
|
|
30
|
+
"baht",
|
|
31
|
+
"text",
|
|
32
|
+
"number",
|
|
33
|
+
"convert",
|
|
34
|
+
"thai baht"
|
|
35
|
+
],
|
|
36
|
+
"author": "Nathachai Thongniran <inid3a@gmail.com>",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/jojoee/bahttext/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/jojoee/bahttext#readme",
|
|
42
|
+
"dependencies": {},
|
|
43
|
+
"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"
|
|
50
|
+
},
|
|
51
|
+
"jest": {
|
|
52
|
+
"verbose": true,
|
|
53
|
+
"testMatch": [
|
|
54
|
+
"**/*.test.js"
|
|
55
|
+
],
|
|
56
|
+
"coverageThreshold": {
|
|
57
|
+
"global": {
|
|
58
|
+
"branches": 80,
|
|
59
|
+
"functions": 80,
|
|
60
|
+
"lines": 80,
|
|
61
|
+
"statements": 80
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"standard": {
|
|
66
|
+
"verbose": true,
|
|
67
|
+
"env": "jest"
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
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
|
+
]
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {number[]} nums
|
|
36
|
+
* @returns {string}
|
|
37
|
+
*/
|
|
38
|
+
function num2Word (nums) {
|
|
39
|
+
let result = ''
|
|
40
|
+
const len = nums.length
|
|
41
|
+
const maxLen = 7
|
|
42
|
+
|
|
43
|
+
if (len > maxLen) {
|
|
44
|
+
// more than million
|
|
45
|
+
const overflowIndex = len - maxLen + 1
|
|
46
|
+
const overflowNums = nums.slice(0, overflowIndex)
|
|
47
|
+
const remainingNumbs = nums.slice(overflowIndex)
|
|
48
|
+
return num2Word(overflowNums) + 'ล้าน' + num2Word(remainingNumbs)
|
|
49
|
+
} else {
|
|
50
|
+
for (let i = 0; i < len; i++) {
|
|
51
|
+
const digit = nums[i]
|
|
52
|
+
if (digit > 0) {
|
|
53
|
+
result += singleUnitStrs[digit] + placeNameStrs[len - i - 1]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return result
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {string} str
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
function grammarFix (str) {
|
|
66
|
+
let result = str
|
|
67
|
+
|
|
68
|
+
// "สิบ"
|
|
69
|
+
result = result.replace(/หนึ่งสิบ/g, 'สิบ')
|
|
70
|
+
// "ยี่สิบ"
|
|
71
|
+
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
|
+
}
|
|
78
|
+
|
|
79
|
+
return result
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Combine baht and satang
|
|
84
|
+
* and also adding unit
|
|
85
|
+
*
|
|
86
|
+
* @param {string} baht
|
|
87
|
+
* @param {string} satang
|
|
88
|
+
* @returns {string}
|
|
89
|
+
*/
|
|
90
|
+
function combine (baht, satang) {
|
|
91
|
+
let result = ''
|
|
92
|
+
|
|
93
|
+
if (baht === '' && satang === '') {
|
|
94
|
+
result = defaultResult
|
|
95
|
+
} else if (baht !== '' && satang === '') {
|
|
96
|
+
result = baht + 'บาท' + 'ถ้วน'
|
|
97
|
+
} else if (baht === '' && satang !== '') {
|
|
98
|
+
result = satang + 'สตางค์'
|
|
99
|
+
} else {
|
|
100
|
+
result = baht + 'บาท' + satang + 'สตางค์'
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return result
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Change number to Thai pronunciation string
|
|
108
|
+
*
|
|
109
|
+
* @param {number} num
|
|
110
|
+
* @returns {string}
|
|
111
|
+
*/
|
|
112
|
+
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
|
|
125
|
+
/** @type {string} */
|
|
126
|
+
const bahtStr = Math.floor(num).toString()
|
|
127
|
+
/** @type {string} */
|
|
128
|
+
const satangStr = Math.round(num % 1 * 100).toString()
|
|
129
|
+
|
|
130
|
+
// 3. split: convert number array
|
|
131
|
+
// @todo optimize it
|
|
132
|
+
/** @type {number[]} */
|
|
133
|
+
const bahtArr = Array.from(bahtStr).map(Number)
|
|
134
|
+
/** @type {number[]} */
|
|
135
|
+
const satangArr = Array.from(satangStr).map(Number)
|
|
136
|
+
|
|
137
|
+
// 4. proceed
|
|
138
|
+
let baht = num2Word(bahtArr)
|
|
139
|
+
let satang = num2Word(satangArr)
|
|
140
|
+
|
|
141
|
+
// 5. grammar
|
|
142
|
+
baht = grammarFix(baht)
|
|
143
|
+
satang = grammarFix(satang)
|
|
144
|
+
|
|
145
|
+
// 6. combine
|
|
146
|
+
result = combine(baht, satang)
|
|
147
|
+
|
|
148
|
+
return result
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (typeof module !== 'undefined' &&
|
|
152
|
+
module.exports != null) {
|
|
153
|
+
module.exports = {
|
|
154
|
+
bahttext
|
|
155
|
+
}
|
|
156
|
+
exports.default = {
|
|
157
|
+
bahttext
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const { bahttext } = require('./index')
|
|
2
|
+
const defaultResult = 'ศูนย์บาทถ้วน'
|
|
3
|
+
jest.autoMockOff()
|
|
4
|
+
|
|
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)
|
|
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 1x', () => {
|
|
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('หกสิบเก้าสตางค์')
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
describe('misc', () => {
|
|
164
|
+
test('invalid number', () => {
|
|
165
|
+
expect(bahttext('invalid number')).toBe('ศูนย์บาทถ้วน')
|
|
166
|
+
})
|
|
167
|
+
})
|