-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
412ab17
commit 9f4fc90
Showing
24 changed files
with
195 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const BAHT: string = `บาท` | ||
|
||
export default BAHT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const LAST6DIGITPATTERN: RegExp = /\d{1,6}$/g; | ||
|
||
export default LAST6DIGITPATTERN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const MILLION: string = `ล้าน` | ||
|
||
export default MILLION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const REVERSETHAIDIGITWORDS: string[] = [ | ||
"แสน", | ||
"หมื่น", | ||
"พัน", | ||
"ร้อย", | ||
"สิบ", | ||
"", | ||
]; | ||
|
||
export default REVERSETHAIDIGITWORDS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const SPECIALONE: string = `เอ็ด` | ||
|
||
export default SPECIALONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const SPECIALTWO: string = `ยี่` | ||
|
||
export default SPECIALTWO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const TEN: string = `สิบ` | ||
|
||
export default TEN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const THAINUMBERWORDS: string[] = [ | ||
`ศูนย์`, | ||
`หนึ่ง`, | ||
`สอง`, | ||
`สาม`, | ||
`สี่`, | ||
`ห้า`, | ||
`หก`, | ||
`เจ็ด`, | ||
`แปด`, | ||
`เก้า`, | ||
`สิบ`, | ||
]; | ||
|
||
export default THAINUMBERWORDS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function LeandingEdToOne(money: string) { | ||
return money.replace(/^เอ็ด(?=(ล้าน)+)/,`หนึ่ง`) | ||
} | ||
export default LeandingEdToOne |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |