Skip to content

Commit

Permalink
perf(bahttext): increase ~18%
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoee committed Apr 20, 2023
1 parent b1157d3 commit a356a6d
Showing 1 changed file with 19 additions and 29 deletions.
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 a356a6d

Please sign in to comment.