bahttext 2.3.0 → 2.3.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
@@ -1,6 +1,7 @@
1
1
  # bahttext
2
2
 
3
3
  [![Version - npm](https://img.shields.io/npm/v/bahttext.svg)](https://www.npmjs.com/package/bahttext)
4
+ [![Download - npm](https://img.shields.io/npm/dt/bahttext.svg)](https://www.npmjs.com/package/bahttext)
4
5
  [![License - npm](https://img.shields.io/npm/l/bahttext.svg)](http://opensource.org/licenses/MIT)
5
6
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release) [![Greenkeeper badge](https://badges.greenkeeper.io/jojoee/bahttext.svg)](https://greenkeeper.io/)
6
7
  [![Codecov](https://img.shields.io/codecov/c/github/jojoee/bahttext.svg)](https://codecov.io/github/jojoee/bahttext)
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # bahttext
2
2
 
3
3
  [![Version - npm](https://img.shields.io/npm/v/bahttext.svg)](https://www.npmjs.com/package/bahttext)
4
+ [![Download - npm](https://img.shields.io/npm/dt/bahttext.svg)](https://www.npmjs.com/package/bahttext)
4
5
  [![License - npm](https://img.shields.io/npm/l/bahttext.svg)](http://opensource.org/licenses/MIT)
5
6
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release) [![Greenkeeper badge](https://badges.greenkeeper.io/jojoee/bahttext.svg)](https://greenkeeper.io/)
6
7
  [![Codecov](https://img.shields.io/codecov/c/github/jojoee/bahttext.svg)](https://codecov.io/github/jojoee/bahttext)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bahttext",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "Change number to Thai pronunciation string",
5
5
  "main": "src/index.js",
6
6
  "typings": "src/index.d.ts",
@@ -35,15 +35,15 @@
35
35
  },
36
36
  "homepage": "https://github.com/jojoee/bahttext#readme",
37
37
  "devDependencies": {
38
- "@stryker-mutator/core": "^6.3.1",
39
- "@stryker-mutator/jest-runner": "^6.3.1",
38
+ "@stryker-mutator/core": "^8.7.1",
39
+ "@stryker-mutator/jest-runner": "^8.7.1",
40
40
  "@to-da-moon/thai-baht-lib": "^0.0.12",
41
41
  "baht": "^0.7.1",
42
- "jest": "^26.6.3",
43
- "jest-expect-message": "^1.0.2",
44
- "semantic-release": "^19.0.5",
45
- "standard": "^15.0.1",
46
- "thai-baht-text": "^1.0.8",
42
+ "jest": "^29.7.0",
43
+ "jest-expect-message": "^1.1.3",
44
+ "semantic-release": "^24.2.2",
45
+ "standard": "^17.1.2",
46
+ "thai-baht-text": "^2.0.5",
47
47
  "thai-baht-text-ts": "^1.1.0",
48
48
  "typescript": "*"
49
49
  },
package/src/index.js CHANGED
@@ -4,28 +4,56 @@ 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
10
16
  * @returns {string}
11
17
  */
12
18
  function bahtxtNum2Word (nums) {
13
- let result = ''
14
- const len = nums.length
15
- const maxLen = 7
19
+ /**
20
+ * Converts a numeric string (or legacy digit array) into Thai words without unit suffix.
21
+ * The new implementation iterates over the input string directly, eliminating
22
+ * the intermediate `Array.from(...).map(Number)` allocations previously used.
23
+ *
24
+ * @param {string|number[]} nums – string of digits ("123") or array \[1,2,3].
25
+ * @returns {string}
26
+ */
27
+ let numStr = ''
28
+
29
+ if (typeof nums === 'string') {
30
+ numStr = nums
31
+ } else if (Array.isArray(nums)) {
32
+ numStr = nums.join('')
33
+ } else {
34
+ return ''
35
+ }
36
+
37
+ // Trim leading zeros but leave at least one digit so "0" stays "0"
38
+ numStr = numStr.replace(/^0+/, '') || '0'
39
+
40
+ const len = numStr.length
41
+ const maxLen = 7 // handle up to 6-digit group + 1-digit look-ahead for "ล้าน" logic
16
42
 
17
43
  if (len > maxLen) {
18
- // more than million
19
44
  const overflowIndex = len - maxLen + 1
20
- const overflowNums = nums.slice(0, overflowIndex)
21
- const remainingNumbs = nums.slice(overflowIndex)
22
- return bahtxtNum2Word(overflowNums) + 'ล้าน' + bahtxtNum2Word(remainingNumbs)
23
- } else {
24
- for (let i = 0; i < len; i++) {
25
- const digit = nums[i]
26
- if (digit > 0) {
27
- result += bahtxtConst.singleUnitStrs[digit] + bahtxtConst.placeNameStrs[len - i - 1]
28
- }
45
+ const overflowStr = numStr.slice(0, overflowIndex)
46
+ const remainingStr = numStr.slice(overflowIndex)
47
+ return bahtxtNum2Word(overflowStr) + 'ล้าน' + bahtxtNum2Word(remainingStr)
48
+ }
49
+
50
+ let result = ''
51
+ for (let i = 0; i < len; i++) {
52
+ const digit = numStr.charCodeAt(i) - 48 // faster than parseInt(numStr[i], 10)
53
+ if (digit > 0) {
54
+ result +=
55
+ bahtxtConst.singleUnitStrs[digit] +
56
+ bahtxtConst.placeNameStrs[len - i - 1]
29
57
  }
30
58
  }
31
59
 
@@ -39,9 +67,10 @@ function bahtxtNum2Word (nums) {
39
67
  * @returns {string}
40
68
  */
41
69
  function bahtxtGrammarFix (str) {
42
- return str.replace(/หนึ่งสิบ/g, 'สิบ')
43
- .replace(/สองสิบ/g, 'ยี่สิบ')
44
- .replace(/สิบหนึ่ง/g, 'สิบเอ็ด')
70
+ for (const GrammarFix of GrammarFixs) {
71
+ str = str.replace(GrammarFix.pat, GrammarFix.replace)
72
+ }
73
+ return str
45
74
  }
46
75
 
47
76
  /**
@@ -73,31 +102,27 @@ function bahtxtCombine (baht, satang) {
73
102
  * @returns {string}
74
103
  */
75
104
  function bahttext (num) {
76
- if (!num || // no null
105
+ if (num === null || num === undefined || // explicit null/undefined check, allow 0
77
106
  typeof num === 'boolean' || // no boolean
78
- isNaN(Number(num)) || // must be number only
79
- num < Number.MIN_SAFE_INTEGER || // not less than Number.MIN_SAFE_INTEGER
80
- num > Number.MAX_SAFE_INTEGER // no more than Number.MAX_SAFE_INTEGER
107
+ isNaN(Number(num)) || // must be numeric coercible
108
+ num < Number.MIN_SAFE_INTEGER || // outside JS safe integer range
109
+ num > Number.MAX_SAFE_INTEGER
81
110
  ) {
82
111
  return bahtxtConst.defaultResult
83
112
  }
84
113
 
85
- // set
86
- const positiveNum = Math.abs(num)
114
+ // normalise sign & prepare parts
115
+ const positiveNum = Math.abs(Number(num))
87
116
 
88
- // split baht and satang e.g. 432.214567 >> 432, 21
89
- const bahtStr = Math.floor(positiveNum).toString()
90
- /** @type {string} */
91
- const satangStr = (positiveNum % 1 * 100).toFixed(0)
117
+ const bahtPart = Math.floor(positiveNum)
92
118
 
93
- /** @type {number[]} */
94
- const bahtArr = Array.from(bahtStr).map(Number)
95
- /** @type {number[]} */
96
- const satangArr = Array.from(satangStr).map(Number)
119
+ const bahtStr = String(bahtPart)
120
+ // Keep original rounding behaviour ("toFixed(0)") to avoid output drift
121
+ const satangStr = (positiveNum % 1 * 100).toFixed(0)
97
122
 
98
- // proceed
99
- let baht = bahtxtNum2Word(bahtArr)
100
- let satang = bahtxtNum2Word(satangArr)
123
+ // Convert directly without creating intermediate digit arrays
124
+ let baht = bahtxtNum2Word(bahtStr)
125
+ let satang = bahtxtNum2Word(satangStr)
101
126
 
102
127
  // grammar
103
128
  baht = bahtxtGrammarFix(baht)