From 6cec4cbf39877f7dbc16fe6fd0a8be2d610583da Mon Sep 17 00:00:00 2001 From: nathawat Date: Tue, 2 Oct 2018 18:10:19 +0700 Subject: [PATCH 1/3] add function get integer digits and fractional digits --- src/thai-baht-text.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/thai-baht-text.js b/src/thai-baht-text.js index 006bd55..c4a5c66 100644 --- a/src/thai-baht-text.js +++ b/src/thai-baht-text.js @@ -12,9 +12,19 @@ const fullMoney = 'ถ้วน' const numbersText = 'ศูนย์,หนึ่ง,สอง,สาม,สี่,ห้า,หก,เจ็ด,แปด,เก้า,สิบ'.split(',') const unitsText = 'สิบ,ร้อย,พัน,หมื่น,แสน,ล้าน'.split(',') +const getFractionalDigits = (numberInput) => { + return numberInput.split('.')[1] +} -// convert function without async +const hasFractionalDigits = (numberInput) => { + return numberInput !== undefined && numberInput !== '00' +} +const getIntegerDigits = (numberInput) => { + return numberInput.split('.')[0] +} + +// convert function without async const convert = (numberInput) => { let numberStr = numberInput.toString() numberStr = numberStr.split('').reverse().join('') @@ -68,14 +78,13 @@ const convert = (numberInput) => { const convertFullMoney = (numberInput) => { const numberStr = parseFloat(numberInput).toFixed(2) - const decimalStr = numberStr.split('.')[0] - const floatingStr = numberStr.split('.')[1] + const integerDigits = getIntegerDigits(numberStr) + const fractionalDigits = getFractionalDigits(numberStr) - let textOutput = '' + let textOutput = convert(integerDigits) - textOutput = convert(decimalStr) - if (floatingStr !== undefined && floatingStr !== '00') { - textOutput = `${textOutput}${primaryCurrency}${convert(floatingStr)}${secondaryCurrency}` + if (hasFractionalDigits(fractionalDigits)) { + textOutput = `${textOutput}${primaryCurrency}${convert(fractionalDigits)}${secondaryCurrency}` } else { textOutput = `${textOutput}${primaryCurrency}${fullMoney}` } From 93c57c3fcda663a99cec053e65695341eab1f602 Mon Sep 17 00:00:00 2001 From: nathawat Date: Tue, 2 Oct 2018 20:06:41 +0700 Subject: [PATCH 2/3] refactory code for function get text unit --- src/thai-baht-text.js | 51 ++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/thai-baht-text.js b/src/thai-baht-text.js index c4a5c66..5aa0fe2 100644 --- a/src/thai-baht-text.js +++ b/src/thai-baht-text.js @@ -24,28 +24,53 @@ const getIntegerDigits = (numberInput) => { return numberInput.split('.')[0] } +const reverseNumber = (number) => { + const numberStr = number.toString() + return numberStr.split('').reverse().join('') +} + +const isZeroValue = (number) => { + return number == 0 +} + +const isUnitPostition = (position) => { + return position == 0 +} + +const isMillionsPosition = (position) => { + return (position >= 6 && position % 6 === 0) +} + +const getBathUnit = (position, number) => { + + let unitText = "" + + if (!isUnitPostition(position)) { + unitText = unitsText[Math.abs(position - 1) % 6] + } + + if( isZeroValue(number) && !isMillionsPosition(position)){ + unitText = "" + } + + return unitText +} + // convert function without async const convert = (numberInput) => { - let numberStr = numberInput.toString() - numberStr = numberStr.split('').reverse().join('') + const numberReverse = reverseNumber(numberInput) let textOutput = '' - numberStr.split('').map((number, i) => { + numberReverse.split('').map((number, i) => { const currentNumber = Number(number) let numberText = numbersText[currentNumber] - let unitText = '' - - if (i !== 0) { - unitText = unitsText[Math.abs(i - 1) % 6] - } + let unitText if (i % 6 === 1 && currentNumber <= 2) { if (currentNumber === 2) { - unitText = 'สิบ' numberText = 'ยี่' } else if (i > 6 && currentNumber === 1) { - unitText = 'สิบ' numberText = '' } else { numberText = '' @@ -54,13 +79,13 @@ const convert = (numberInput) => { if (i >= 6 && i % 6 === 0) { if (currentNumber === 1) { - if (i + 1 < numberStr.length) { + if (i + 1 < numberReverse.length) { numberText = 'เอ็ด' } } } - if (numberStr.length > 1 && i === 0 && currentNumber === 1) { + if (numberReverse.length > 1 && i === 0 && currentNumber === 1) { numberText = 'เอ็ด' } @@ -69,6 +94,7 @@ const convert = (numberInput) => { numberText = '' } + unitText = getBathUnit(i, number) textOutput = numberText + unitText + textOutput return number }) @@ -88,7 +114,6 @@ const convertFullMoney = (numberInput) => { } else { textOutput = `${textOutput}${primaryCurrency}${fullMoney}` } - return textOutput } From aaba96c226e190068e65a55977fdbeba9cbe0ec5 Mon Sep 17 00:00:00 2001 From: nathawat Date: Tue, 2 Oct 2018 22:26:39 +0700 Subject: [PATCH 3/3] refactor get bath text function --- src/thai-baht-text.js | 81 +++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/src/thai-baht-text.js b/src/thai-baht-text.js index 5aa0fe2..97a3c38 100644 --- a/src/thai-baht-text.js +++ b/src/thai-baht-text.js @@ -5,6 +5,10 @@ // options +const MAX_POSITION = 6 +const UNIT_POSITION = 0 +const TEN_POSITION = 1 + const primaryCurrency = 'บาท' const secondaryCurrency = 'สตางค์' const fullMoney = 'ถ้วน' @@ -34,11 +38,19 @@ const isZeroValue = (number) => { } const isUnitPostition = (position) => { - return position == 0 + return position == UNIT_POSITION +} + +const isTenPostition = (position) => { + return position % MAX_POSITION == TEN_POSITION } const isMillionsPosition = (position) => { - return (position >= 6 && position % 6 === 0) + return (position >= MAX_POSITION && position % MAX_POSITION == 0) +} + +const isLastPosition = (position, lengthOfDigits) => { + return position + 1 < lengthOfDigits } const getBathUnit = (position, number) => { @@ -46,7 +58,7 @@ const getBathUnit = (position, number) => { let unitText = "" if (!isUnitPostition(position)) { - unitText = unitsText[Math.abs(position - 1) % 6] + unitText = unitsText[Math.abs(position - 1) % MAX_POSITION] } if( isZeroValue(number) && !isMillionsPosition(position)){ @@ -56,47 +68,40 @@ const getBathUnit = (position, number) => { return unitText } +const getBathText = (position, number, lengthOfDigits) => { + + let numberText = numbersText[number] + + if(isZeroValue(number)){ + return ""; + } + + if (isTenPostition(position) && number == 1) { + numberText = '' + } + + if (isTenPostition(position) && number == 2) { + numberText = 'ยี่' + } + + if (isMillionsPosition(position) && isLastPosition(position, lengthOfDigits) && number == 1 ) { + numberText = 'เอ็ด' + } + + if ( lengthOfDigits > 1 && isUnitPostition(position) && number == 1) { + numberText = 'เอ็ด' + } + + return numberText +} + // convert function without async const convert = (numberInput) => { const numberReverse = reverseNumber(numberInput) let textOutput = '' - numberReverse.split('').map((number, i) => { - const currentNumber = Number(number) - let numberText = numbersText[currentNumber] - - let unitText - if (i % 6 === 1 && currentNumber <= 2) { - if (currentNumber === 2) { - numberText = 'ยี่' - } else if (i > 6 && currentNumber === 1) { - numberText = '' - } else { - numberText = '' - } - } - - if (i >= 6 && i % 6 === 0) { - if (currentNumber === 1) { - if (i + 1 < numberReverse.length) { - numberText = 'เอ็ด' - } - } - } - - if (numberReverse.length > 1 && i === 0 && currentNumber === 1) { - numberText = 'เอ็ด' - } - - if (currentNumber === 0) { - unitText = (i >= 6 && i % 6 === 0) ? unitText : '' - numberText = '' - } - - unitText = getBathUnit(i, number) - textOutput = numberText + unitText + textOutput - return number + textOutput = getBathText(i, number, numberReverse.length) + getBathUnit(i, number) + textOutput }) return textOutput }