Skip to content

Commit

Permalink
break into components
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Jan 30, 2024
1 parent 412ab17 commit 9f4fc90
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 120 deletions.
2 changes: 1 addition & 1 deletion BahtRxTS/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 4 additions & 119 deletions BahtRxTS/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -126,7 +11,7 @@ function App() {
<div className="App">
<h1>BahtRext</h1>
<div className="output">
{BahtText(num.toString())}
{BahtRext(num.toString())}
</div>
<div className="">
<input type="text" onChange={(e) => {
Expand All @@ -139,9 +24,9 @@ function App() {
<div className="">
<button onClick={(e) => {
e.preventDefault()
navigator.clipboard.writeText(BahtText(num.toString()))
navigator.clipboard.writeText(BahtRext(num.toString()))
toast.dismiss()
toast(`คัดลอก ${BahtText(num.toString())}`,{
toast(`คัดลอก ${BahtRext(num.toString())}`,{
toastId: `copy`
})
}}>Copy</button>
Expand Down
3 changes: 3 additions & 0 deletions BahtRxTS/src/Consts/BAHT.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const BAHT: string = `บาท`

export default BAHT
3 changes: 3 additions & 0 deletions BahtRxTS/src/Consts/LAST6DIGITPATTERN.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const LAST6DIGITPATTERN: RegExp = /\d{1,6}$/g;

export default LAST6DIGITPATTERN
3 changes: 3 additions & 0 deletions BahtRxTS/src/Consts/MILLION.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const MILLION: string = `ล้าน`

export default MILLION
10 changes: 10 additions & 0 deletions BahtRxTS/src/Consts/REVERSETHAIDIGITWORDS.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const REVERSETHAIDIGITWORDS: string[] = [
"แสน",
"หมื่น",
"พัน",
"ร้อย",
"สิบ",
"",
];

export default REVERSETHAIDIGITWORDS;
3 changes: 3 additions & 0 deletions BahtRxTS/src/Consts/SPECIALONE.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const SPECIALONE: string = `เอ็ด`

export default SPECIALONE
3 changes: 3 additions & 0 deletions BahtRxTS/src/Consts/SPECIALTWO.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const SPECIALTWO: string = `ยี่`

export default SPECIALTWO
2 changes: 2 additions & 0 deletions BahtRxTS/src/Consts/SPLITPATTERN.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const SPLITPATTERN: RegExp = /^(\d*)(\.\d{0,2}0*)?$/;
export default SPLITPATTERN
3 changes: 3 additions & 0 deletions BahtRxTS/src/Consts/TEN.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const TEN: string = `สิบ`

export default TEN
15 changes: 15 additions & 0 deletions BahtRxTS/src/Consts/THAINUMBERWORDS.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const THAINUMBERWORDS: string[] = [
`ศูนย์`,
`หนึ่ง`,
`สอง`,
`สาม`,
`สี่`,
`ห้า`,
`หก`,
`เจ็ด`,
`แปด`,
`เก้า`,
`สิบ`,
];

export default THAINUMBERWORDS;
6 changes: 6 additions & 0 deletions BahtRxTS/src/Format/THB.tsx
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions BahtRxTS/src/Functions/BahtRext.tsx
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions BahtRxTS/src/Functions/IsMoneyValidate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import SPLITPATTERN from "../Consts/SPLITPATTERN"
function IsMoneyValidate (money: string): boolean {
return SPLITPATTERN.test(money)
}
export default IsMoneyValidate
4 changes: 4 additions & 0 deletions BahtRxTS/src/Functions/LeandingEdToOne.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function LeandingEdToOne(money: string) {
return money.replace(/^เอ็ด(?=(ล้าน)+)/,`หนึ่ง`)
}
export default LeandingEdToOne
4 changes: 4 additions & 0 deletions BahtRxTS/src/Functions/MoneyInvalid.tsx
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions BahtRxTS/src/Functions/MoneyLaundering.tsx
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions BahtRxTS/src/Functions/PrintBaht.tsx
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions BahtRxTS/src/Functions/PrintSatangs.tsx
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions BahtRxTS/src/Functions/SatangFirstDigit.tsx
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions BahtRxTS/src/Functions/SatangSecondDigit.tsx
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions BahtRxTS/src/Functions/SplitIntFrac.tsx
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions BahtRxTS/src/Functions/hundredThousandToOne.tsx
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions BahtRxTS/src/Functions/padWithLeadingZeros.tsx
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f4fc90

Please sign in to comment.