From a1b615bea07f7a220dc6eda38ca4ba546115a9d0 Mon Sep 17 00:00:00 2001 From: khris-xp Date: Fri, 27 Oct 2023 07:00:15 +0700 Subject: [PATCH 1/2] refac: convert and get bath text --- src/thai-baht-text.js | 143 ++++++++++++------------------------------ 1 file changed, 41 insertions(+), 102 deletions(-) diff --git a/src/thai-baht-text.js b/src/thai-baht-text.js index d4e4573..c642e8d 100644 --- a/src/thai-baht-text.js +++ b/src/thai-baht-text.js @@ -1,124 +1,63 @@ /** - * @author Jirachai Chansivanon * @see {@link https://github.com/antronic/that-baht-text-js|GitHub} */ -// options +// Constants +const MAX_POSITION = 6; +const PRIMARY_UNIT = 'บาท'; +const SECONDARY_UNIT = 'สตางค์'; +const WHOLE_NUMBER_TEXT = 'ถ้วน'; -const MAX_POSITION = 6 -const UNIT_POSITION = 0 -const TEN_POSITION = 1 +const NUMBER_TEXTS = 'ศูนย์,หนึ่ง,สอง,สาม,สี่,ห้า,หก,เจ็ด,แปด,เก้า,สิบ'.split(','); +const UNIT_TEXTS = 'สิบ,ร้อย,พัน,หมื่น,แสน,ล้าน'.split(','); -const PRIMARY_UNIT = 'บาท' -const SECONDARY_UNIT = 'สตางค์' -const WHOLE_NUMBER_TEXT = 'ถ้วน' - -const NUMBER_TEXTS = 'ศูนย์,หนึ่ง,สอง,สาม,สี่,ห้า,หก,เจ็ด,แปด,เก้า,สิบ'.split(',') -const UNIT_TEXTS = 'สิบ,ร้อย,พัน,หมื่น,แสน,ล้าน'.split(',') - -const getIntegerDigits = numberInput => parseInt(numberInput.split('.')[0], 10).toString() -const getFractionalDigits = numberInput => parseInt(numberInput.split('.')[1], 10).toString() - -const hasFractionalDigits = numberInput => numberInput !== undefined && numberInput != '0' - -const isZeroValue = number => number == 0 -const isUnitPosition = position => position == UNIT_POSITION -const isTenPosition = position => position % MAX_POSITION == TEN_POSITION -const isMillionsPosition = position => (position >= MAX_POSITION && position % MAX_POSITION == 0) -const isLastPosition = (position, lengthOfDigits) => position + 1 < lengthOfDigits - -const reverseNumber = (number) => { - const numberStr = number.toString() - return numberStr.split('').reverse().join('') -} - -const getBathUnit = (position, number) => { - let unitText = '' - - if (!isUnitPosition(position)) { - unitText = UNIT_TEXTS[Math.abs(position - 1) % MAX_POSITION] - } - - if (isZeroValue(number) && !isMillionsPosition(position)) { - unitText = '' - } - - return unitText -} +// Helper Functions +const reverseNumber = number => number.toString().split('').reverse().join(''); const getBathText = (position, number, lengthOfDigits) => { - let numberText = NUMBER_TEXTS[number] + let numberText = NUMBER_TEXTS[number]; - if (isZeroValue(number)) { - return '' - } + if (number === 0) { + return ''; + } - if (isTenPosition(position) && number == 1) { - numberText = '' - } + if (position % MAX_POSITION === 1 && number === 1) { + numberText = lengthOfDigits === 2 ? 'เอ็ด' : ''; + } - if (isTenPosition(position) && number == 2) { - numberText = 'ยี่' - } + if (lengthOfDigits > 1 && position % MAX_POSITION === 0 && number === 1) { + numberText = 'เอ็ด'; + } - if (isMillionsPosition(position) && isLastPosition(position, lengthOfDigits) && number == 1) { - numberText = 'เอ็ด' - } + return numberText; +}; - if (lengthOfDigits == 2 && isLastPosition(position, lengthOfDigits) && number == 1) { - numberText = 'เอ็ด' - } +const convert = numberInput => { + const numberReverse = reverseNumber(numberInput); + let textOutput = ''; - if (lengthOfDigits > 1 && isUnitPosition(position) && number == 1) { - numberText = 'เอ็ด' - } + numberReverse.split('').forEach((number, i) => { + textOutput = `${getBathText(i, number, numberReverse.length)}${UNIT_TEXTS[Math.abs(i - 1) % MAX_POSITION]}${textOutput}`; + }); - return numberText -} + return textOutput; +}; -// convert function without async -const convert = (numberInput) => { - const numberReverse = reverseNumber(numberInput) - let textOutput = '' - // console.log('>', numberReverse.split('')) - numberReverse.split('').forEach((number, i) => { - textOutput = `${getBathText(i, number, numberReverse.length)}${getBathUnit(i, number)}${textOutput}` - }) - return textOutput -} +const convertFullMoney = numberInput => { + const numberStr = parseFloat(numberInput).toFixed(2); + const [integerPart, fractionalPart] = numberStr.split('.'); -const parseFloatWithPrecision = (number, precision = 2) => { - const numberFloatStr = parseFloat(number).toString().split('.') - const integerUnitStr = numberFloatStr[0] - const fractionalUnitStr = (numberFloatStr.length == 2) ? numberFloatStr[1].substring(0, precision) : '00' - return parseFloat(`${integerUnitStr}.${fractionalUnitStr}`).toFixed(precision) -} + const intTextOutput = convert(reverseNumber(integerPart)); + const textOutput = [intTextOutput && `${intTextOutput}${PRIMARY_UNIT}`, fractionalPart && convert(reverseNumber(fractionalPart)) && `${convert(reverseNumber(fractionalPart))}${SECONDARY_UNIT}`].filter(Boolean).join(''); -const convertFullMoney = (numberInput) => { - const numberStr = parseFloatWithPrecision(numberInput) - - const integerDigits = getIntegerDigits(numberStr) - const fractionalDigits = getFractionalDigits(numberStr) - - const intTextOutput = convert(integerDigits) - const textOutput = [] - if (intTextOutput) { - textOutput.push(`${[intTextOutput, PRIMARY_UNIT].join('')}`) - } - if (intTextOutput && !hasFractionalDigits(fractionalDigits)) { - textOutput.push(WHOLE_NUMBER_TEXT) - } - if (hasFractionalDigits(fractionalDigits) && convert(fractionalDigits)) { - textOutput.push(`${[convert(fractionalDigits), SECONDARY_UNIT].join('')}`) - } - - return textOutput.join('') -} + return textOutput || WHOLE_NUMBER_TEXT; +}; +// Export if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { - module.exports = convertFullMoney - exports.default = convertFullMoney - exports.THBText = convertFullMoney + module.exports = convertFullMoney; + exports.default = convertFullMoney; + exports.THBText = convertFullMoney; } else { - window.THBText = convertFullMoney + window.THBText = convertFullMoney; } From fa9a170170034f31cddeb4f2d4880cdeb1391d64 Mon Sep 17 00:00:00 2001 From: khris-xp Date: Fri, 27 Oct 2023 10:13:07 +0700 Subject: [PATCH 2/2] feat: not convert very small amount --- src/thai-baht-text.js | 89 +++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/src/thai-baht-text.js b/src/thai-baht-text.js index c642e8d..1fa868f 100644 --- a/src/thai-baht-text.js +++ b/src/thai-baht-text.js @@ -3,61 +3,74 @@ */ // Constants -const MAX_POSITION = 6; -const PRIMARY_UNIT = 'บาท'; -const SECONDARY_UNIT = 'สตางค์'; -const WHOLE_NUMBER_TEXT = 'ถ้วน'; +const MAX_POSITION = 6 +const PRIMARY_UNIT = 'บาท' +const SECONDARY_UNIT = 'สตางค์' +const WHOLE_NUMBER_TEXT = 'ถ้วน' -const NUMBER_TEXTS = 'ศูนย์,หนึ่ง,สอง,สาม,สี่,ห้า,หก,เจ็ด,แปด,เก้า,สิบ'.split(','); -const UNIT_TEXTS = 'สิบ,ร้อย,พัน,หมื่น,แสน,ล้าน'.split(','); +const NUMBER_TEXTS = 'ศูนย์,หนึ่ง,สอง,สาม,สี่,ห้า,หก,เจ็ด,แปด,เก้า,สิบ'.split(',') +const UNIT_TEXTS = 'สิบ,ร้อย,พัน,หมื่น,แสน,ล้าน'.split(',') // Helper Functions -const reverseNumber = number => number.toString().split('').reverse().join(''); +const reverseNumber = (number) => number.toString().split('').reverse().join('') const getBathText = (position, number, lengthOfDigits) => { - let numberText = NUMBER_TEXTS[number]; + let numberText = NUMBER_TEXTS[number] - if (number === 0) { - return ''; - } + if (number === 0) { + return '' + } - if (position % MAX_POSITION === 1 && number === 1) { - numberText = lengthOfDigits === 2 ? 'เอ็ด' : ''; - } + if (position % MAX_POSITION === 1 && number === 1) { + numberText = lengthOfDigits === 2 ? 'เอ็ด' : '' + } - if (lengthOfDigits > 1 && position % MAX_POSITION === 0 && number === 1) { - numberText = 'เอ็ด'; - } + if (lengthOfDigits > 1 && position % MAX_POSITION === 0 && number === 1) { + numberText = 'เอ็ด' + } - return numberText; -}; + return numberText +} + +const convert = (numberInput) => { + const numberReverse = reverseNumber(numberInput) + let textOutput = '' -const convert = numberInput => { - const numberReverse = reverseNumber(numberInput); - let textOutput = ''; + numberReverse.split('').forEach((number, i) => { + textOutput = `${getBathText(i, number, numberReverse.length)}${UNIT_TEXTS[Math.abs(i - 1) % MAX_POSITION]}${textOutput}` + }) + + return textOutput +} - numberReverse.split('').forEach((number, i) => { - textOutput = `${getBathText(i, number, numberReverse.length)}${UNIT_TEXTS[Math.abs(i - 1) % MAX_POSITION]}${textOutput}`; - }); +const convertFullMoney = (numberInput) => { + const SMALL_AMOUNT_THRESHOLD = 0.01 - return textOutput; -}; + const number = parseFloat(numberInput) -const convertFullMoney = numberInput => { - const numberStr = parseFloat(numberInput).toFixed(2); - const [integerPart, fractionalPart] = numberStr.split('.'); + if (number < 0) { + return `ลบ${convertFullMoney(Math.abs(number))}` + } - const intTextOutput = convert(reverseNumber(integerPart)); - const textOutput = [intTextOutput && `${intTextOutput}${PRIMARY_UNIT}`, fractionalPart && convert(reverseNumber(fractionalPart)) && `${convert(reverseNumber(fractionalPart))}${SECONDARY_UNIT}`].filter(Boolean).join(''); + if (number < SMALL_AMOUNT_THRESHOLD) { + return '' + } - return textOutput || WHOLE_NUMBER_TEXT; -}; + const numberStr = number.toFixed(2) + const [integerPart, fractionalPart] = numberStr.split('.') + + const intTextOutput = convert(reverseNumber(integerPart)) + const textOutput = [intTextOutput && `${intTextOutput}${PRIMARY_UNIT}`, + fractionalPart && convert(reverseNumber(fractionalPart)) && `${convert(reverseNumber(fractionalPart))}${SECONDARY_UNIT}`].filter(Boolean).join('') + + return textOutput || WHOLE_NUMBER_TEXT +} // Export if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { - module.exports = convertFullMoney; - exports.default = convertFullMoney; - exports.THBText = convertFullMoney; + module.exports = convertFullMoney + exports.default = convertFullMoney + exports.THBText = convertFullMoney } else { - window.THBText = convertFullMoney; + window.THBText = convertFullMoney }