New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bahttext

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bahttext - npm Package Compare versions

Comparing version 2.0.4 to 2.1.0

6

package.json
{
"name": "bahttext",
"version": "2.0.4",
"version": "2.1.0",
"description": "Change number to Thai pronunciation string",

@@ -48,2 +48,3 @@ "main": "src/index.js",

"jest": "^26.6.1",
"jest-expect-message": "^1.0.2",
"semantic-release": "^17.2.1",

@@ -53,2 +54,5 @@ "standard": "^15.0.0"

"jest": {
"setupFilesAfterEnv": [
"jest-expect-message"
],
"verbose": true,

@@ -55,0 +59,0 @@ "testMatch": [

@@ -15,3 +15,3 @@ # bahttext

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/)
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/)

@@ -50,9 +50,20 @@ ## Installation

- [ ] Browser compatibility test
- [ ] Remove duplication between test code and test-case-csv file
- [x] Remove duplication between test code and test-case-csv file
- [x] [Demo page](https://jojoee.github.io/bahttext/)
- [ ] Support negative number
- [x] Support negative number
- [x] Fix semantic-release
## CMD
```
brew install curl
brew install jq
npm install -g
curl -L -o ./misc/testcases.csv https://docs.google.com/spreadsheets/d/e/2PACX-1vTb8PIKzgo07rn9UpcjqE0YrdMAmf4fyDbL2plUieLCyrn_5O3vDvece7UfkaArWQLUSsaw92jVpY_z/pub?gid=0&single=true&output=csv
csvtojson ./misc/testcases.csv | jq > ./misc/testcases.json
```
## Reference
- [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en)
- [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
- Inspired by [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)

@@ -14,3 +14,3 @@ # bahttext

เปลี่ยนตัวเลข เป็นคำอ่านภาษาไทย, โมดูลตัวนี้ได้ทำการทดสอบกับ [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/) เรียบร้อยแล้ว
เปลี่ยนตัวเลข เป็นคำอ่านภาษาไทย, โมดูลตัวนี้ได้ทำการทดสอบกับ [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en), [Demo page](https://jojoee.github.io/bahttext/) เรียบร้อยแล้ว

@@ -49,9 +49,20 @@ ## ติดตั้ง

- [ ] ทดสอบบนเบราว์เซอร์ ในแต่ละ เวอร์ชั่น
- [ ] ลดการซ้ำซ้อนของการเขียน ระหว่าง code และ ไฟล์ csv ในการทำการทดสอบ
- [x] ลดการซ้ำซ้อนของการเขียน ระหว่าง code และ ไฟล์ csv ในการทำการทดสอบ
- [x] [หน้าตัวอย่างการใช้งาน](https://jojoee.github.io/bahttext/)
- [ ] สนับสนุนการใช้งานกับตัวเลขติดลบ
- [x] สนับสนุนการใช้งานกับตัวเลขติดลบ
- [x] แก้ไข semantic-release
## CMD
```
brew install curl
brew install jq
npm install -g
curl -L -o ./misc/testcases.csv https://docs.google.com/spreadsheets/d/e/2PACX-1vTb8PIKzgo07rn9UpcjqE0YrdMAmf4fyDbL2plUieLCyrn_5O3vDvece7UfkaArWQLUSsaw92jVpY_z/pub?gid=0&single=true&output=csv
csvtojson ./misc/testcases.csv | jq > ./misc/testcases.json
```
## อ้างอิง
- [Google Sheets BAHTTEXT function](https://support.google.com/docs/answer/9982303?hl=en)
- [Microsoft Office's BAHTTEXT function](https://support.office.com/en-us/article/BAHTTEXT-function-5ba4d0b4-abd3-4325-8d22-7a92d59aab9c)
- แรงบัลดาลใจจาก [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)

83

src/index.js

@@ -1,33 +0,4 @@

/*
step:
1. validate
2. sanitize
3. split
4. proceed
5. grammar
6. combine
*/
const defaultResult = 'ศูนย์บาทถ้วน'
const singleUnitStrs = [
'',
'หนึ่ง',
'สอง',
'สาม',
'สี่',
'ห้า',
'หก',
'เจ็ด',
'แปด',
'เก้า'
]
const placeNameStrs = [
'',
'สิบ',
'ร้อย',
'พัน',
'หมื่น',
'แสน',
'ล้าน'
]
const singleUnitStrs = ['', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า']
const placeNameStrs = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']

@@ -62,2 +33,3 @@ /**

/**
* @todo improve performance
* @param {string} str

@@ -69,12 +41,5 @@ * @returns {string}

// "สิบ"
result = result.replace(/หนึ่งสิบ/g, 'สิบ')
// "ยี่สิบ"
result = result.replace(/สองสิบ/g, 'ยี่สิบ')
// "เอ็ด"
const neungLen = 5
if (result.length > neungLen &&
result.length - result.lastIndexOf('หนึ่ง') === neungLen) {
result = result.substr(0, result.length - neungLen) + 'เอ็ด'
}
result = result.replace(/สิบหนึ่ง/g, 'สิบเอ็ด')

@@ -115,21 +80,21 @@ return result

function bahttext (num) {
let result = defaultResult
// no null
if (!num) return defaultResult
// no boolean
if (typeof num === 'boolean') return defaultResult
// must be number only
if (isNaN(Number(num))) return defaultResult
// not less than Number.MIN_SAFE_INTEGER
if (num < Number.MIN_SAFE_INTEGER) return defaultResult
// no more than Number.MAX_SAFE_INTEGER
if (num > Number.MAX_SAFE_INTEGER) return defaultResult
// 1. validate: invalid number
if (isNaN(num)) return result
// 1. validate: more than
if (num >= Number.MAX_SAFE_INTEGER) return result
// set
const positiveNum = Math.abs(num)
// 2. sanitize: ????
// 3. split: baht and satang
// e.g. 432.21 >> 432, 21
// @todo optimize
// split baht and satang e.g. 432.214567 >> 432, 21
const bahtStr = Math.floor(positiveNum).toString()
/** @type {string} */
const bahtStr = Math.floor(num).toString()
/** @type {string} */
const satangStr = Math.round(num % 1 * 100).toString()
const satangStr = (positiveNum % 1 * 100).toFixed(2).split('.')[0]
// 3. split: convert number array
// @todo optimize it
/** @type {number[]} */

@@ -140,14 +105,14 @@ const bahtArr = Array.from(bahtStr).map(Number)

// 4. proceed
// proceed
let baht = num2Word(bahtArr)
let satang = num2Word(satangArr)
// 5. grammar
// grammar
baht = grammarFix(baht)
satang = grammarFix(satang)
// 6. combine
result = combine(baht, satang)
// combine
const result = combine(baht, satang)
return result
return num >= 0 ? result : 'ลบ' + result
}

@@ -154,0 +119,0 @@

const { bahttext } = require('./index')
const defaultResult = 'ศูนย์บาทถ้วน'
const testCases = require('../misc/testcases.json')
jest.autoMockOff()
describe('digit', () => {
test('1, zero', () => {
expect(bahttext(0)).toBe('ศูนย์บาทถ้วน')
})
describe('misc', () => {
test('invalid number', () => {
const zeroText = 'ศูนย์บาทถ้วน'
test('1 digit', () => {
expect(bahttext(1)).toBe('หนึ่งบาทถ้วน')
expect(bahttext(2)).toBe('สองบาทถ้วน')
expect(bahttext(3)).toBe('สามบาทถ้วน')
expect(bahttext(4)).toBe('สี่บาทถ้วน')
expect(bahttext(5)).toBe('ห้าบาทถ้วน')
expect(bahttext(6)).toBe('หกบาทถ้วน')
expect(bahttext(7)).toBe('เจ็ดบาทถ้วน')
expect(bahttext(8)).toBe('แปดบาทถ้วน')
expect(bahttext(9)).toBe('เก้าบาทถ้วน')
expect(bahttext(null)).toBe(zeroText)
expect(bahttext(true)).toBe(zeroText)
expect(bahttext(false)).toBe(zeroText)
expect(bahttext([])).toBe(zeroText)
expect(bahttext([1, 2, 3])).toBe(zeroText)
expect(bahttext({})).toBe(zeroText)
expect(bahttext('')).toBe(zeroText)
expect(bahttext()).toBe(zeroText)
expect(bahttext(undefined)).toBe(zeroText)
expect(bahttext('this-is-not-number')).toBe(zeroText)
expect(bahttext('it-must-be-number-only')).toBe(zeroText)
})
test('2', () => {
expect(bahttext(37)).toBe('สามสิบเจ็ดบาทถ้วน')
expect(bahttext(48)).toBe('สี่สิบแปดบาทถ้วน')
test('less than Number.MIN_SAFE_INTEGER', () => {
const items = [1, 20, 9451, 5656549]
for (let i = 0; i < items.length; i++) {
expect(bahttext(Number.MIN_SAFE_INTEGER - items[i])).toBe(defaultResult)
}
})
test('3', () => {
expect(bahttext(232)).toBe('สองร้อยสามสิบสองบาทถ้วน')
test('more than Number.MAX_SAFE_INTEGER', () => {
const items = [1, 20, 9451, 5656549]
for (let i = 0; i < items.length; i++) {
expect(bahttext(Number.MAX_SAFE_INTEGER + items[i])).toBe(defaultResult)
}
})
test('4', () => {
expect(bahttext(3333)).toBe('สามพันสามร้อยสามสิบสามบาทถ้วน')
})
test('5', () => {
expect(bahttext(44444)).toBe('สี่หมื่นสี่พันสี่ร้อยสี่สิบสี่บาทถ้วน')
})
test('6', () => {
expect(bahttext(555555)).toBe('ห้าแสนห้าหมื่นห้าพันห้าร้อยห้าสิบห้าบาทถ้วน')
})
test('7', () => {
expect(bahttext(6666666)).toBe('หกล้านหกแสนหกหมื่นหกพันหกร้อยหกสิบหกบาทถ้วน')
})
test('8', () => {
expect(bahttext(77777777)).toBe('เจ็ดสิบเจ็ดล้านเจ็ดแสนเจ็ดหมื่นเจ็ดพันเจ็ดร้อยเจ็ดสิบเจ็ดบาทถ้วน')
})
test('9', () => {
expect(bahttext(888888888)).toBe('แปดร้อยแปดสิบแปดล้านแปดแสนแปดหมื่นแปดพันแปดร้อยแปดสิบแปดบาทถ้วน')
})
test('10', () => {
expect(bahttext(9999999999)).toBe('เก้าพันเก้าร้อยเก้าสิบเก้าล้านเก้าแสนเก้าหมื่นเก้าพันเก้าร้อยเก้าสิบเก้าบาทถ้วน')
})
test('11', () => {
expect(bahttext(12345678901)).toBe('หนึ่งหมื่นสองพันสามร้อยสี่สิบห้าล้านหกแสนเจ็ดหมื่นแปดพันเก้าร้อยเอ็ดบาทถ้วน')
})
test('12', () => {
expect(bahttext(123456789012)).toBe('หนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบหกล้านเจ็ดแสนแปดหมื่นเก้าพันสิบสองบาทถ้วน')
})
test('13', () => {
expect(bahttext(1234567890133)).toBe('หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดล้านแปดแสนเก้าหมื่นหนึ่งร้อยสามสิบสามบาทถ้วน')
})
test('14', () => {
expect(bahttext(12345678901234)).toBe('สิบสองล้านสามแสนสี่หมื่นห้าพันหกร้อยเจ็ดสิบแปดล้านเก้าแสนหนึ่งพันสองร้อยสามสิบสี่บาทถ้วน')
})
test('15', () => {
expect(bahttext(123456789012345)).toBe('หนึ่งร้อยยี่สิบสามล้านสี่แสนห้าหมื่นหกพันเจ็ดร้อยแปดสิบเก้าล้านหนึ่งหมื่นสองพันสามร้อยสี่สิบห้าบาทถ้วน')
})
test('16 digits, not more than Number.MAX_SAFE_INTEGER', () => {
// Number.MAX_SAFE_INTEGER
// 9007199254740991
expect(bahttext(1234567890123450)).toBe('หนึ่งพันสองร้อยสามสิบสี่ล้านห้าแสนหกหมื่นเจ็ดพันแปดร้อยเก้าสิบล้านหนึ่งแสนสองหมื่นสามพันสี่ร้อยห้าสิบบาทถ้วน')
})
test('16 digits, more than Number.MAX_SAFE_INTEGER', () => {
// eslint-disable-next-line
expect(bahttext(9007199254750991)).toBe(defaultResult)
})
test('more than 16', () => {
expect(bahttext(12345678901234500)).toBe(defaultResult)
expect(bahttext(123456789012345000)).toBe(defaultResult)
expect(bahttext(1234567890123450000)).toBe(defaultResult)
expect(bahttext(123456789012345000000)).toBe(defaultResult)
})
})
describe('grammar', () => {
test('end with 1 (more than 10)', () => {
expect(bahttext(11)).toBe('สิบเอ็ดบาทถ้วน')
expect(bahttext(201)).toBe('สองร้อยเอ็ดบาทถ้วน')
expect(bahttext(3061.21)).toBe('สามพันหกสิบเอ็ดบาทยี่สิบเอ็ดสตางค์')
expect(bahttext(456011.71)).toBe('สี่แสนห้าหมื่นหกพันสิบเอ็ดบาทเจ็ดสิบเอ็ดสตางค์')
describe('number', () => {
test('imported from Google Sheet', () => {
for (let i = 0; i < testCases.length; i++) {
const customMessage = JSON.stringify(testCases[i])
const number = Number(testCases[i].number)
expect(bahttext(number), customMessage).toBe(testCases[i].text)
}
})
test('end with 1x', () => {
expect(bahttext(14)).toBe('สิบสี่บาทถ้วน')
expect(bahttext(40019)).toBe('สี่หมื่นสิบเก้าบาทถ้วน')
expect(bahttext(317.10)).toBe('สามร้อยสิบเจ็ดบาทสิบสตางค์')
expect(bahttext(40019.17)).toBe('สี่หมื่นสิบเก้าบาทสิบเจ็ดสตางค์')
})
test('end with 2x', () => {
expect(bahttext(22)).toBe('ยี่สิบสองบาทถ้วน')
expect(bahttext(5723)).toBe('ห้าพันเจ็ดร้อยยี่สิบสามบาทถ้วน')
expect(bahttext(57.23)).toBe('ห้าสิบเจ็ดบาทยี่สิบสามสตางค์')
expect(bahttext(422.26)).toBe('สี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์')
})
test('multi occurrence of 1x', () => {
expect(bahttext(10056518)).toBe('สิบล้านห้าหมื่นหกพันห้าร้อยสิบแปดบาทถ้วน')
})
test('multi occurrence of 2x', () => {
expect(bahttext(20056528)).toBe('ยี่สิบล้านห้าหมื่นหกพันห้าร้อยยี่สิบแปดบาทถ้วน')
})
})
describe('decimals', () => {
test('general', () => {
expect(bahttext(1.01)).toBe('หนึ่งบาทหนึ่งสตางค์')
expect(bahttext(1.02)).toBe('หนึ่งบาทสองสตางค์')
expect(bahttext(1.03)).toBe('หนึ่งบาทสามสตางค์')
expect(bahttext(1.04)).toBe('หนึ่งบาทสี่สตางค์')
expect(bahttext(1.05)).toBe('หนึ่งบาทห้าสตางค์')
expect(bahttext(1.06)).toBe('หนึ่งบาทหกสตางค์')
expect(bahttext(1.07)).toBe('หนึ่งบาทเจ็ดสตางค์')
expect(bahttext(1.08)).toBe('หนึ่งบาทแปดสตางค์')
expect(bahttext(1.09)).toBe('หนึ่งบาทเก้าสตางค์')
expect(bahttext(32.23)).toBe('สามสิบสองบาทยี่สิบสามสตางค์')
expect(bahttext(474.45)).toBe('สี่ร้อยเจ็ดสิบสี่บาทสี่สิบห้าสตางค์')
expect(bahttext(5789.67)).toBe('ห้าพันเจ็ดร้อยแปดสิบเก้าบาทหกสิบเจ็ดสตางค์')
expect(bahttext(63147.89)).toBe('หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์')
})
test('more than 2 decimals, using 2 decimals', () => {
expect(bahttext(25.1230)).toBe('ยี่สิบห้าบาทสิบสองสตางค์')
expect(bahttext(345.2345)).toBe('สามร้อยสี่สิบห้าบาทยี่สิบสามสตางค์')
expect(bahttext(5678.4567)).toBe('ห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์')
})
test('not more than 1', () => {
expect(bahttext(0.25)).toBe('ยี่สิบห้าสตางค์')
expect(bahttext(0.36)).toBe('สามสิบหกสตางค์')
expect(bahttext(0.69)).toBe('หกสิบเก้าสตางค์')
})
})
describe('misc', () => {
test('invalid number', () => {
expect(bahttext('invalid number')).toBe('ศูนย์บาทถ้วน')
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc