Skip to content

Commit

Permalink
Merge pull request #10 from jojoee/feature/increase-performance
Browse files Browse the repository at this point in the history
Feature/increase performance
  • Loading branch information
jojoee authored Apr 20, 2023
2 parents 36ab915 + a356a6d commit 09ab103
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
29 changes: 29 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { bahttext } = require('./src')
const THBText = require('thai-baht-text')
const { ThaiBaht } = require('thai-baht-text-ts')
const { convert } = require('baht')

const allTestcases = require('./misc/testcases.json')
const nTestCases = allTestcases.length
const testcases = allTestcases.slice(0, nTestCases)
const numbers = testcases.map(item => parseFloat(item.number))
const nIterations = 10000

const testedLib = {
bahttext: n => bahttext(n),
baht: n => convert(n),
'thai-baht-text': n => THBText(n),
'thai-baht-text-ts': n => ThaiBaht(n)
}

Object.entries(testedLib).forEach(([name, fn]) => {
const start = new Date()

new Array(nIterations).fill(0).forEach(_ => {
numbers.forEach(number => fn(number))
})

const end = new Date()
const elapsed = end - start
console.log(`${name}: elapsed: ${elapsed} ms, start: ${start.toISOString()}, end: ${end.toISOString()}`)
})
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
"devDependencies": {
"@stryker-mutator/core": "^6.3.1",
"@stryker-mutator/jest-runner": "^6.3.1",
"@to-da-moon/thai-baht-lib": "^0.0.12",
"baht": "^0.7.1",
"jest": "^26.6.3",
"jest-expect-message": "^1.0.2",
"semantic-release": "^19.0.5",
"standard": "^15.0.1"
"standard": "^15.0.1",
"thai-baht-text": "^1.0.8",
"thai-baht-text-ts": "^1.1.0"
},
"jest": {
"setupFilesAfterEnv": [
Expand Down
48 changes: 19 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ function bahtxtNum2Word (nums) {
* @returns {string}
*/
function bahtxtGrammarFix (str) {
let result = str

result = result.replace(//g, 'สิบ')
result = result.replace(//g, 'ยี่สิบ')
result = result.replace(//g, 'สิบเอ็ด')

return result
return str.replace(//g, 'สิบ')
.replace(//g, 'ยี่สิบ')
.replace(//g, 'สิบเอ็ด')
}

/**
Expand All @@ -55,19 +51,15 @@ function bahtxtGrammarFix (str) {
* @returns {string}
*/
function bahtxtCombine (baht, satang) {
let result = ''

if (baht === '' && satang === '') {
result = bahtxtConst.defaultResult
} else if (baht !== '' && satang === '') {
result = baht + 'บาท' + 'ถ้วน'
} else if (baht === '' && satang !== '') {
result = satang + 'สตางค์'
if (!baht && !satang) {
return bahtxtConst.defaultResult
} else if (baht && !satang) {
return baht + 'บาท' + 'ถ้วน'
} else if (!baht && satang) {
return satang + 'สตางค์'
} else {
result = baht + 'บาท' + satang + 'สตางค์'
return baht + 'บาท' + satang + 'สตางค์'
}

return result
}

/**
Expand All @@ -77,24 +69,22 @@ function bahtxtCombine (baht, satang) {
* @returns {string}
*/
function bahttext (num) {
// no null
if (!num) return bahtxtConst.defaultResult
// no boolean
if (typeof num === 'boolean') return bahtxtConst.defaultResult
// must be number only
if (isNaN(Number(num))) return bahtxtConst.defaultResult
// not less than Number.MIN_SAFE_INTEGER
if (num < Number.MIN_SAFE_INTEGER) return bahtxtConst.defaultResult
// no more than Number.MAX_SAFE_INTEGER
if (num > Number.MAX_SAFE_INTEGER) return bahtxtConst.defaultResult
if (!num || // no null
typeof num === 'boolean' || // no boolean
isNaN(Number(num)) || // must be number only
num < Number.MIN_SAFE_INTEGER || // not less than Number.MIN_SAFE_INTEGER
num > Number.MAX_SAFE_INTEGER // no more than Number.MAX_SAFE_INTEGER
) {
return bahtxtConst.defaultResult
}

// set
const positiveNum = Math.abs(num)

// split baht and satang e.g. 432.214567 >> 432, 21
const bahtStr = Math.floor(positiveNum).toString()
/** @type {string} */
const satangStr = (positiveNum % 1 * 100).toFixed(2).split('.')[0]
const satangStr = (positiveNum % 1 * 100).toFixed(0)

/** @type {number[]} */
const bahtArr = Array.from(bahtStr).map(Number)
Expand Down

0 comments on commit 09ab103

Please sign in to comment.