diff --git a/BahtRxTS/package-lock.json b/BahtRxTS/package-lock.json index e955133..40d0f30 100644 --- a/BahtRxTS/package-lock.json +++ b/BahtRxTS/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "bahtrext", - "version": "1.0.0", + "version": "1.0.1", "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/BahtRxTS/src/App.tsx b/BahtRxTS/src/App.tsx index fd0204d..498c579 100644 --- a/BahtRxTS/src/App.tsx +++ b/BahtRxTS/src/App.tsx @@ -2,122 +2,7 @@ import { useState } from 'react' import './App.css' import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; - -let SPECIALONE: string = `เอ็ด` -let SPECIALTWO: string = `ยี่` -let TEN: string = `สิบ`; -let BAHT: string = `บาท`; -let MILLION: string = `ล้าน`; - -let SPLITPATTERN: RegExp = /^(\d*)(\.\d{0,2}0*)?$/; -let LAST6DIGITPATTERN: RegExp = /\d{1,6}$/g; - -let THAINUMBERWORDS: string[] = [`ศูนย์`,`หนึ่ง`,`สอง`,`สาม`,`สี่`,`ห้า`,`หก`,`เจ็ด`,`แปด`,`เก้า`,`สิบ`] -let REVERSETHAIDIGITWORDS: string[] = ["แสน", "หมื่น", "พัน", "ร้อย", "สิบ", ""] - -function MoneyInvalid (money: string): string { - return `Your Input is Invalid Format!\nThis is Your Input : ${money}\nTry Again`; -} -function MoneyLaundering (money: string): string { - let removeComma: string = money.replace(/,/g, ""); - let removeCommaAndTrailingZeros: string = removeComma.replace(/^0+/g, ""); - return removeCommaAndTrailingZeros; -}; -function IsMoneyValidate (money: string): boolean { - return SPLITPATTERN.test(money) -} - -function splitIntFrac (money: string): string[] { - let match: RegExpMatchArray | null = money.match(SPLITPATTERN); - let [moneyFull, moneyInt, moneyFrac] = match!; - moneyFrac === undefined - ? (moneyFrac = "") - : (moneyFrac = moneyFrac.replace(/^\./, "")); - return [moneyFull, moneyInt, moneyFrac]; -}; - -let padWithLeadingZeros = (num: string, totalLength: number) => { - // https://bobbyhadz.com/blog/javascript-add-leading-zeros-to-number - return String(num).padStart(totalLength, '0'); -} - -let hundredThousandToOne = (digits: string) => { - let word: string = ``; - let c: number = 0 - let digitspadWithLeadingZeros: string = padWithLeadingZeros(digits,6) - for (let digit of digitspadWithLeadingZeros) { - let numDigit: number = parseInt(digit) - if (!(numDigit === 0)) { - if (c == 4 && numDigit == 2) { - word += `${SPECIALTWO}${TEN}` - } else if (c == 4 && numDigit == 1) { - word += TEN - } else if (c == 5 && numDigit == 1 && parseInt(digitspadWithLeadingZeros[4]) != 0) { - word += SPECIALONE - } else { - word += `${THAINUMBERWORDS[numDigit]}${REVERSETHAIDIGITWORDS[c]}`; - } - } - c++ - } - return word; -}; - -let LeandingEdToOne = (money: string) => money.replace(/^เอ็ด(?=(ล้าน)+)/,`หนึ่ง`) - -let PrintBaht = (money: string) => { - if (!money) return `` - let newMoney: string[] = []; - let f6 = true - while (money != ``) { - let selectedupto6digit = money!.match(LAST6DIGITPATTERN)![0]; - newMoney.push( - `${hundredThousandToOne(selectedupto6digit)}${f6 ? "" : MILLION}` - ); - f6 ? f6 = !f6 : "" - money = money.replace(LAST6DIGITPATTERN, ""); - } - let cleanLeadingEd = LeandingEdToOne(newMoney.reverse().join("")) - return `${cleanLeadingEd}${BAHT}`; -}; - -let SatangFirstDigit = (digit: string) => { - if (digit === "0") return ``; - if (digit === "1") return `${TEN}`; - if (digit === "2") return `${SPECIALTWO}${TEN}`; - return `${THAINUMBERWORDS[parseInt(digit)]}${TEN}`; -}; -let SatangSecondDigit = (digit: string) => { - if (digit[1] === undefined || digit[1] === "0") return ""; - if (digit[0] !== "0" && digit[1] === "1") return SPECIALONE; - return `${THAINUMBERWORDS[parseInt(digit[1])]}`; -}; -let PrintSatangs = (satangs: string) => { - if (satangs.match(/^0*$/)) return "ถ้วน"; - let satangword: string = `${SatangFirstDigit(satangs[0])}${SatangSecondDigit( - satangs - )}สตางค์`; - return satangword; -}; - -// https://www.freecodecamp.org/news/how-to-format-number-as-currency-in-javascript-one-line-of-code/ -let THB: Intl.NumberFormat = new Intl.NumberFormat('th-TH', { - style: 'currency', - currency: 'THB', -}); - -let BahtText = (money: string) => { - if (!money) return `` - let cleanedMoney: string = MoneyLaundering(money); - if (!IsMoneyValidate(cleanedMoney) || money === `.`) return MoneyInvalid(money) - let [moneyFull, moneyInt, moneyFrac] = splitIntFrac(cleanedMoney); - if (moneyFull.match(/^(0*)(\.0*)?$/)) return `${THAINUMBERWORDS[0]}${BAHT}ถ้วน` - // toast(`${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}`,{ - // toastId: money - // }) - return `${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}`; - // return `${THB.format(parseFloat(moneyFull))} อ่านว่า "${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}"`; -} +import BahtRext from './Functions/BahtRext'; function App() { const [num, setNum] = useState(localStorage.getItem(`num`) || 0) @@ -126,7 +11,7 @@ function App() {

BahtRext

- {BahtText(num.toString())} + {BahtRext(num.toString())}
{ @@ -139,9 +24,9 @@ function App() {
diff --git a/BahtRxTS/src/Consts/BAHT.tsx b/BahtRxTS/src/Consts/BAHT.tsx new file mode 100644 index 0000000..7ad7d89 --- /dev/null +++ b/BahtRxTS/src/Consts/BAHT.tsx @@ -0,0 +1,3 @@ +const BAHT: string = `บาท` + +export default BAHT \ No newline at end of file diff --git a/BahtRxTS/src/Consts/LAST6DIGITPATTERN.tsx b/BahtRxTS/src/Consts/LAST6DIGITPATTERN.tsx new file mode 100644 index 0000000..05ce42d --- /dev/null +++ b/BahtRxTS/src/Consts/LAST6DIGITPATTERN.tsx @@ -0,0 +1,3 @@ +const LAST6DIGITPATTERN: RegExp = /\d{1,6}$/g; + +export default LAST6DIGITPATTERN \ No newline at end of file diff --git a/BahtRxTS/src/Consts/MILLION.tsx b/BahtRxTS/src/Consts/MILLION.tsx new file mode 100644 index 0000000..87e12c5 --- /dev/null +++ b/BahtRxTS/src/Consts/MILLION.tsx @@ -0,0 +1,3 @@ +const MILLION: string = `ล้าน` + +export default MILLION \ No newline at end of file diff --git a/BahtRxTS/src/Consts/REVERSETHAIDIGITWORDS.tsx b/BahtRxTS/src/Consts/REVERSETHAIDIGITWORDS.tsx new file mode 100644 index 0000000..3ff2f6c --- /dev/null +++ b/BahtRxTS/src/Consts/REVERSETHAIDIGITWORDS.tsx @@ -0,0 +1,10 @@ +const REVERSETHAIDIGITWORDS: string[] = [ + "แสน", + "หมื่น", + "พัน", + "ร้อย", + "สิบ", + "", +]; + +export default REVERSETHAIDIGITWORDS; diff --git a/BahtRxTS/src/Consts/SPECIALONE.tsx b/BahtRxTS/src/Consts/SPECIALONE.tsx new file mode 100644 index 0000000..81a83b5 --- /dev/null +++ b/BahtRxTS/src/Consts/SPECIALONE.tsx @@ -0,0 +1,3 @@ +const SPECIALONE: string = `เอ็ด` + +export default SPECIALONE \ No newline at end of file diff --git a/BahtRxTS/src/Consts/SPECIALTWO.tsx b/BahtRxTS/src/Consts/SPECIALTWO.tsx new file mode 100644 index 0000000..e1bbcbb --- /dev/null +++ b/BahtRxTS/src/Consts/SPECIALTWO.tsx @@ -0,0 +1,3 @@ +const SPECIALTWO: string = `ยี่` + +export default SPECIALTWO \ No newline at end of file diff --git a/BahtRxTS/src/Consts/SPLITPATTERN.tsx b/BahtRxTS/src/Consts/SPLITPATTERN.tsx new file mode 100644 index 0000000..5d3644f --- /dev/null +++ b/BahtRxTS/src/Consts/SPLITPATTERN.tsx @@ -0,0 +1,2 @@ +const SPLITPATTERN: RegExp = /^(\d*)(\.\d{0,2}0*)?$/; +export default SPLITPATTERN \ No newline at end of file diff --git a/BahtRxTS/src/Consts/TEN.tsx b/BahtRxTS/src/Consts/TEN.tsx new file mode 100644 index 0000000..bd3cb67 --- /dev/null +++ b/BahtRxTS/src/Consts/TEN.tsx @@ -0,0 +1,3 @@ +const TEN: string = `สิบ` + +export default TEN \ No newline at end of file diff --git a/BahtRxTS/src/Consts/THAINUMBERWORDS.tsx b/BahtRxTS/src/Consts/THAINUMBERWORDS.tsx new file mode 100644 index 0000000..038abe0 --- /dev/null +++ b/BahtRxTS/src/Consts/THAINUMBERWORDS.tsx @@ -0,0 +1,15 @@ +const THAINUMBERWORDS: string[] = [ + `ศูนย์`, + `หนึ่ง`, + `สอง`, + `สาม`, + `สี่`, + `ห้า`, + `หก`, + `เจ็ด`, + `แปด`, + `เก้า`, + `สิบ`, +]; + +export default THAINUMBERWORDS; diff --git a/BahtRxTS/src/Format/THB.tsx b/BahtRxTS/src/Format/THB.tsx new file mode 100644 index 0000000..cc04428 --- /dev/null +++ b/BahtRxTS/src/Format/THB.tsx @@ -0,0 +1,6 @@ +let THB: Intl.NumberFormat = new Intl.NumberFormat('th-TH', { + style: 'currency', + currency: 'THB', +}); +// https://www.freecodecamp.org/news/how-to-format-number-as-currency-in-javascript-one-line-of-code/ +export default THB \ No newline at end of file diff --git a/BahtRxTS/src/Functions/BahtRext.tsx b/BahtRxTS/src/Functions/BahtRext.tsx new file mode 100644 index 0000000..0dd17fa --- /dev/null +++ b/BahtRxTS/src/Functions/BahtRext.tsx @@ -0,0 +1,23 @@ +import BAHT from "../Consts/BAHT"; +import THAINUMBERWORDS from "../Consts/THAINUMBERWORDS"; +import MoneyInvalid from "./MoneyInvalid"; +import MoneyLaundering from "./MoneyLaundering"; +import IsMoneyValidate from "./IsMoneyValidate"; +import SplitIntFrac from "./SplitIntFrac"; +import PrintBaht from "./PrintBaht"; +import PrintSatangs from "./PrintSatangs"; +function BahtRext(money: string) { + if (!money) return ``; + let cleanedMoney: string = MoneyLaundering(money); + if (!IsMoneyValidate(cleanedMoney) || money === `.`) + return MoneyInvalid(money); + let [moneyFull, moneyInt, moneyFrac] = SplitIntFrac(cleanedMoney); + if (moneyFull.match(/^(0*)(\.0*)?$/)) + return `${THAINUMBERWORDS[0]}${BAHT}ถ้วน`; + // toast(`${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}`,{ + // toastId: money + // }) + return `${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}`; + // return `${THB.format(parseFloat(moneyFull))} อ่านว่า "${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}"`; +} +export default BahtRext \ No newline at end of file diff --git a/BahtRxTS/src/Functions/IsMoneyValidate.tsx b/BahtRxTS/src/Functions/IsMoneyValidate.tsx new file mode 100644 index 0000000..5008cf6 --- /dev/null +++ b/BahtRxTS/src/Functions/IsMoneyValidate.tsx @@ -0,0 +1,5 @@ +import SPLITPATTERN from "../Consts/SPLITPATTERN" +function IsMoneyValidate (money: string): boolean { + return SPLITPATTERN.test(money) +} +export default IsMoneyValidate \ No newline at end of file diff --git a/BahtRxTS/src/Functions/LeandingEdToOne.tsx b/BahtRxTS/src/Functions/LeandingEdToOne.tsx new file mode 100644 index 0000000..83a2cd1 --- /dev/null +++ b/BahtRxTS/src/Functions/LeandingEdToOne.tsx @@ -0,0 +1,4 @@ +function LeandingEdToOne(money: string) { + return money.replace(/^เอ็ด(?=(ล้าน)+)/,`หนึ่ง`) +} +export default LeandingEdToOne \ No newline at end of file diff --git a/BahtRxTS/src/Functions/MoneyInvalid.tsx b/BahtRxTS/src/Functions/MoneyInvalid.tsx new file mode 100644 index 0000000..c338c82 --- /dev/null +++ b/BahtRxTS/src/Functions/MoneyInvalid.tsx @@ -0,0 +1,4 @@ +function MoneyInvalid (money: string): string { + return `Your Input is Invalid Format!\nThis is Your Input : ${money}\nTry Again`; +} +export default MoneyInvalid \ No newline at end of file diff --git a/BahtRxTS/src/Functions/MoneyLaundering.tsx b/BahtRxTS/src/Functions/MoneyLaundering.tsx new file mode 100644 index 0000000..4d75bc8 --- /dev/null +++ b/BahtRxTS/src/Functions/MoneyLaundering.tsx @@ -0,0 +1,6 @@ +function MoneyLaundering (money: string): string { + let removeComma: string = money.replace(/,/g, ""); + let removeCommaAndTrailingZeros: string = removeComma.replace(/^0+/g, ""); + return removeCommaAndTrailingZeros; +}; +export default MoneyLaundering \ No newline at end of file diff --git a/BahtRxTS/src/Functions/PrintBaht.tsx b/BahtRxTS/src/Functions/PrintBaht.tsx new file mode 100644 index 0000000..1850b72 --- /dev/null +++ b/BahtRxTS/src/Functions/PrintBaht.tsx @@ -0,0 +1,21 @@ +import LAST6DIGITPATTERN from "../Consts/LAST6DIGITPATTERN"; +import hundredThousandToOne from "./hundredThousandToOne"; +import MILLION from "../Consts/MILLION"; +import LeandingEdToOne from "./LeandingEdToOne"; +import BAHT from "../Consts/BAHT"; +let PrintBaht = (money: string) => { + if (!money) return ``; + let newMoney: string[] = []; + let f6 = true; + while (money != ``) { + let selectedupto6digit = money!.match(LAST6DIGITPATTERN)![0]; + newMoney.push( + `${hundredThousandToOne(selectedupto6digit)}${f6 ? "" : MILLION}` + ); + f6 ? (f6 = !f6) : ""; + money = money.replace(LAST6DIGITPATTERN, ""); + } + let cleanLeadingEd = LeandingEdToOne(newMoney.reverse().join("")); + return `${cleanLeadingEd}${BAHT}`; +}; +export default PrintBaht \ No newline at end of file diff --git a/BahtRxTS/src/Functions/PrintSatangs.tsx b/BahtRxTS/src/Functions/PrintSatangs.tsx new file mode 100644 index 0000000..067e9ef --- /dev/null +++ b/BahtRxTS/src/Functions/PrintSatangs.tsx @@ -0,0 +1,10 @@ +import SatangFirstDigit from "./SatangFirstDigit"; +import SatangSecondDigit from "./SatangSecondDigit"; +function PrintSatangs(satangs: string) { + if (satangs.match(/^0*$/)) return "ถ้วน"; + let satangword: string = `${SatangFirstDigit(satangs[0])}${SatangSecondDigit( + satangs + )}สตางค์`; + return satangword; +} +export default PrintSatangs \ No newline at end of file diff --git a/BahtRxTS/src/Functions/SatangFirstDigit.tsx b/BahtRxTS/src/Functions/SatangFirstDigit.tsx new file mode 100644 index 0000000..f120dd7 --- /dev/null +++ b/BahtRxTS/src/Functions/SatangFirstDigit.tsx @@ -0,0 +1,10 @@ +import TEN from "../Consts/TEN"; +import SPECIALTWO from "../Consts/SPECIALTWO"; +import THAINUMBERWORDS from "../Consts/THAINUMBERWORDS"; +function SatangFirstDigit(digit: string) { + if (digit === "0") return ``; + if (digit === "1") return `${TEN}`; + if (digit === "2") return `${SPECIALTWO}${TEN}`; + return `${THAINUMBERWORDS[parseInt(digit)]}${TEN}`; +} +export default SatangFirstDigit \ No newline at end of file diff --git a/BahtRxTS/src/Functions/SatangSecondDigit.tsx b/BahtRxTS/src/Functions/SatangSecondDigit.tsx new file mode 100644 index 0000000..5b8a19b --- /dev/null +++ b/BahtRxTS/src/Functions/SatangSecondDigit.tsx @@ -0,0 +1,8 @@ +import SPECIALONE from "../Consts/SPECIALONE"; +import THAINUMBERWORDS from "../Consts/THAINUMBERWORDS"; +function SatangSecondDigit(digit: string) { + if (digit[1] === undefined || digit[1] === "0") return ""; + if (digit[0] !== "0" && digit[1] === "1") return SPECIALONE; + return `${THAINUMBERWORDS[parseInt(digit[1])]}`; +} +export default SatangSecondDigit \ No newline at end of file diff --git a/BahtRxTS/src/Functions/SplitIntFrac.tsx b/BahtRxTS/src/Functions/SplitIntFrac.tsx new file mode 100644 index 0000000..18a4eb9 --- /dev/null +++ b/BahtRxTS/src/Functions/SplitIntFrac.tsx @@ -0,0 +1,10 @@ +import SPLITPATTERN from "../Consts/SPLITPATTERN"; +function SplitIntFrac(money: string): string[] { + let match: RegExpMatchArray | null = money.match(SPLITPATTERN); + let [moneyFull, moneyInt, moneyFrac] = match!; + moneyFrac === undefined + ? (moneyFrac = "") + : (moneyFrac = moneyFrac.replace(/^\./, "")); + return [moneyFull, moneyInt, moneyFrac]; +} +export default SplitIntFrac \ No newline at end of file diff --git a/BahtRxTS/src/Functions/hundredThousandToOne.tsx b/BahtRxTS/src/Functions/hundredThousandToOne.tsx new file mode 100644 index 0000000..14827cb --- /dev/null +++ b/BahtRxTS/src/Functions/hundredThousandToOne.tsx @@ -0,0 +1,32 @@ +import padWithLeadingZeros from "./padWithLeadingZeros"; +import SPECIALTWO from "../Consts/SPECIALTWO"; +import TEN from "../Consts/TEN"; +import SPECIALONE from "../Consts/SPECIALONE"; +import THAINUMBERWORDS from "../Consts/THAINUMBERWORDS"; +import REVERSETHAIDIGITWORDS from "../Consts/REVERSETHAIDIGITWORDS"; +function hundredThousandToOne (digits: string) { + let word: string = ``; + let c: number = 0; + let digitspadWithLeadingZeros: string = padWithLeadingZeros(digits, 6); + for (let digit of digitspadWithLeadingZeros) { + let numDigit: number = parseInt(digit); + if (!(numDigit === 0)) { + if (c == 4 && numDigit == 2) { + word += `${SPECIALTWO}${TEN}`; + } else if (c == 4 && numDigit == 1) { + word += TEN; + } else if ( + c == 5 && + numDigit == 1 && + parseInt(digitspadWithLeadingZeros[4]) != 0 + ) { + word += SPECIALONE; + } else { + word += `${THAINUMBERWORDS[numDigit]}${REVERSETHAIDIGITWORDS[c]}`; + } + } + c++; + } + return word; +}; +export default hundredThousandToOne \ No newline at end of file diff --git a/BahtRxTS/src/Functions/padWithLeadingZeros.tsx b/BahtRxTS/src/Functions/padWithLeadingZeros.tsx new file mode 100644 index 0000000..df5bc96 --- /dev/null +++ b/BahtRxTS/src/Functions/padWithLeadingZeros.tsx @@ -0,0 +1,6 @@ +// https://bobbyhadz.com/blog/javascript-add-leading-zeros-to-number +function padWithLeadingZeros (num: string, totalLength: number) { + return String(num).padStart(totalLength, '0'); +} + +export default padWithLeadingZeros \ No newline at end of file