Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Mar 29, 2024
1 parent 7118cc5 commit ed5f932
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@
- [Codepen](https://codepen.io/chadin-chaipornpisuth/full/rNbzyom)

## Must Read
- **Checkout Test Cases in `index.test.js` Before Implement**
- **`101`** ควรจะถูกอ่านอย่างไร ?
- `หนึ่งร้อยหนึ่งบาทถ้วน`
- `Google Sheets`
- `สัญญากู้เงินของธนาคาร`* ลองมากู้ได้เลยครับ ถ้าไม่ใช่บอกผมด้วย
- [**`BahtRext`**](https://github.com/PingHuskar/npm-bahtrext)
- [jojoee/bahttext](https://www.npmjs.com/package/bahttext)
- จะอ่านว่า`เอ็ด`เมื่อหลักสิบมีค่า เช่น `สิบเอ็ด`, `ยี่สิบเอ็ด`
- `หนึ่งร้อยเอ็ดบาทถ้วน`
- `MS Excel`
- [`thai-baht-text`](https://www.npmjs.com/package/thai-baht-text)
- [earthchie/BAHTTEXT.js](https://github.com/earthchie/BAHTTEXT.js)
- **Your Choice**
- [**`BahtRext`**](https://pinghuskar.github.io/npm-bahtrext/)
- [Versions](https://www.npmjs.com/package/bahtrext?activeTab=versions)
- [REPO](https://github.com/PingHuskar/npm-bahtrext)

## Beliefs
1. Money in Thai Baht can be translate to word
2. Decimal places is only 2 digits; Banks stored 6 digits of decimal places if you want more digits, use Crypto
3. CleanCode + Comment as Code
4. All Synchronous function
5. This Number System not working well with large numbers. I hope we have a better solution for Version 2.
5. This Number System not working well with large numbers.

## Fixes
- 1.2.0 - `เอ็ด`
- 1.1.7 - remove `Googolplex`
- 1.1.3 - add LNBT
- 1.1.1
Expand Down
45 changes: 26 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DEBUG = false;

const VERSION = `1.1.8`
const VERSION = `1.2.0`

const SPECIALONE = `เอ็ด`;
const SPECIALTWO = `ยี่`;
Expand Down Expand Up @@ -102,9 +102,10 @@ const padWithLeadingZeros = (num, totalLength) => {
return String(num).padStart(totalLength, "0");
};

const hundredThousandToOne = (digits) => {
const hundredThousandToOne = (digits, ed = false) => {
let word = ``;
let c = 0;
if (DEBUG) console.log(`ed`,ed)
const digitspadWithLeadingZeros = padWithLeadingZeros(digits, 6);
for (let digit of digitspadWithLeadingZeros) {
digit = parseInt(digit);
Expand All @@ -113,6 +114,8 @@ const hundredThousandToOne = (digits) => {
word += `${SPECIALTWO}${TEN}`;
} else if (c == 4 && digit == 1) {
word += TEN;
} else if (c == 5 && digit == 1 && (ed)) {
word += SPECIALONE;
} else if (c == 5 && digit == 1 && digitspadWithLeadingZeros[4] != 0) {
word += SPECIALONE;
} else {
Expand All @@ -124,20 +127,22 @@ const hundredThousandToOne = (digits) => {
return word;
};

const LeandingEdToOne = (money) => money.replace(/^(?=()+)/, ONE);
const LeadingSpecialOneToOne = (money) => money.replace(/^(?=()+)/, ONE);

const PrintBaht = (money) => {
const PrintBaht = (money, ed = false) => {
if (!money) return ``;
let newMoney = [];
while (money != ``) {
let selectedupto6digit = money.match(LAST6DIGITPATTERN)[0];
newMoney.push(
`${hundredThousandToOne(selectedupto6digit)}${MILLION}`
`${hundredThousandToOne(selectedupto6digit, ed)}${MILLION}`
);
money = money.replace(LAST6DIGITPATTERN, "");
}
const cleanLeadingEd = LeandingEdToOne(newMoney.reverse().join(""));
return `${cleanLeadingEd.replace(/$/, ``)}${BAHT}`;
return `${LeadingSpecialOneToOne(newMoney.reverse().join("")).replace(
/$/,
``
)}${BAHT}`;
};

const SatangFirstDigit = (digit) => {
Expand Down Expand Up @@ -169,6 +174,7 @@ let THB = new Intl.NumberFormat("th-TH", {

const BahtText = (
money,
ed=false,
currencyformat = THB,
arrow = READAS,
ClErr = MoneyInvalid,
Expand All @@ -186,11 +192,11 @@ const BahtText = (
} ${arrow} "${THAINUMBERWORDS[0]}${BAHT}${FULLBAHT}"`;
return `${
currencyformat ? currencyformat.format(moneyFull) : moneyFull
} ${arrow} "${PrintBaht(moneyInt)}${PrintSatangs(moneyFrac)}"`;
} ${arrow} "${PrintBaht(moneyInt, ed)}${PrintSatangs(moneyFrac)}"`;
};

const BT = (money) => {
const rBahtText = BahtText(money);
const BT = (money, ed = false) => {
const rBahtText = BahtText(money, ed);;
if (!rBahtText) return undefined;
return rBahtText.split('"').at(-2);
};
Expand All @@ -208,7 +214,7 @@ const THAI2ARABICNumerals = [
{ th: `๙`, a: `9` },
];

const BF = (flexmoney, InvalidType = `Invalid Type`) => {
const BF = (flexmoney, ed = false, InvalidType = `Invalid Type`) => {
if (!flexmoney) return undefined;
if (typeof flexmoney !== "string") return InvalidType;
let money = flexmoney;
Expand All @@ -220,7 +226,7 @@ const BF = (flexmoney, InvalidType = `Invalid Type`) => {
);
}
if (DEBUG) console.log(money);
return BT(money);
return BT(money, ed);
};

const IsMatchInSkipsPattern = (match, skips) => {
Expand All @@ -236,15 +242,16 @@ const defaultBulkBahtTextSkips = [/\b5+\+?\b/];
const BulkBahtText = (
str,
pat = defaultBulkBahtTextPat,
skips = defaultBulkBahtTextSkips
skips = defaultBulkBahtTextSkips,
ed = false
) => {
if (typeof str !== "string") return `Invalid Type`;
if (!str) return null;
const matches = str.match(pat);
if (!matches) return str;
for (const match of matches) {
if (IsMatchInSkipsPattern(match, skips)) continue;
str = str.replace(match, BahtText(match.replace(/[^\d]/g,'')).split('"').at(-2));
str = str.replace(match, BahtText(match.replace(/[^\d]/g,'')).split('"').at(-2), ed);
}
return str;
};
Expand Down Expand Up @@ -376,7 +383,7 @@ const IsValidTB = (str) => {
return str === BT(TB(str)).replace(FULLBAHT, "");
};

const ABT = (money) => {
const ABT = (money, ed = false) => {
if (!money) return undefined;
switch (typeof money) {
case "number":
Expand All @@ -387,7 +394,7 @@ const ABT = (money) => {
const THBText = require("thai-baht-text");
return THBText(money);
case "string":
return BF(money);
return BF(money, ed);
default:
return undefined;
}
Expand Down Expand Up @@ -493,8 +500,7 @@ const LNBT = (nameorpowerof10, d=`1`) => {
}
}

if (!DEBUG) {

// if (!DEBUG) {
module.exports = {
VERSION,
SPECIALONE,
Expand Down Expand Up @@ -534,5 +540,6 @@ if (!DEBUG) {
repeat,
large_numbers,
LNBT,
LeadingSpecialOneToOne,
};
}
// }
17 changes: 17 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ test('NumText', () => {
test('BT', () => {
expect(BR.BT(`lol`)).toBe(undefined);
expect(BR.BT(`2000000000000.00`)).toBe(`สองล้านล้านบาทถ้วน`)
expect(BR.BT(`1000001000001`)).toBe(`หนึ่งล้านหนึ่งล้านหนึ่งบาทถ้วน`);
expect(BR.BT(`1000001000001`, true)).toBe(`หนึ่งล้านเอ็ดล้านเอ็ดบาทถ้วน`);
expect(BR.BT(`1000011000001`, true)).toBe(`หนึ่งล้านสิบเอ็ดล้านเอ็ดบาทถ้วน`);
expect(BR.BT(`101`)).toBe(`หนึ่งร้อยหนึ่งบาทถ้วน`);
expect(BR.BT(`101`, true)).toBe(`หนึ่งร้อยเอ็ดบาทถ้วน`);
expect(BR.BT(`123`)).toBe(`หนึ่งร้อยยี่สิบสามบาทถ้วน`);
expect(BR.BT(`8.00`)).toBe(`แปดบาทถ้วน`)
expect(BR.BT(`5678.00`)).toBe(`ห้าพันหกร้อยเจ็ดสิบแปดบาทถ้วน`)
expect(BR.BT(`63147.89`)).toBe(`หกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์`)
expect(BR.BT(`51000001.00`, true)).toBe(`ห้าสิบเอ็ดล้านเอ็ดบาทถ้วน`)
expect(BR.BT(`51000001.00`)).toBe(`ห้าสิบเอ็ดล้านหนึ่งบาทถ้วน`)
expect(BR.BT(`317.10`)).toBe(`สามร้อยสิบเจ็ดบาทสิบสตางค์`)
expect(BR.BT(`422.26`)).toBe(`สี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์`)
Expand All @@ -26,6 +32,7 @@ test('BT', () => {

test("ABT", () => {
expect(BR.ABT(`lol`)).toBe(undefined);
expect(BR.ABT(37)).toBe(`สามสิบเจ็ดบาทถ้วน`);
expect(BR.ABT(`2000000000000.00`)).toBe(`สองล้านล้านบาทถ้วน`);
expect(BR.ABT(`123`)).toBe(`หนึ่งร้อยยี่สิบสามบาทถ้วน`);
expect(BR.ABT(`8.00`)).toBe(`แปดบาทถ้วน`);
Expand All @@ -50,9 +57,15 @@ test("ABT", () => {

test('PrintSatangs', () =>{
expect(BR.PrintSatangs(`67`)).toBe(`หกสิบเจ็ดสตางค์`)
expect(BR.PrintSatangs(`37`)).toBe(`สามสิบเจ็ดสตางค์`)
expect(BR.PrintSatangs(`31`)).toBe(`สามสิบเอ็ดสตางค์`)
expect(BR.PrintSatangs(`21`)).toBe(`ยี่สิบเอ็ดสตางค์`)
expect(BR.PrintSatangs(`01`)).toBe(`หนึ่งสตางค์`)
expect(BR.PrintSatangs(`1`)).toBe(`สิบสตางค์`)
expect(BR.PrintSatangs(``)).toBe(`ถ้วน`)
expect(BR.PrintSatangs(`0`)).toBe(`ถ้วน`)
expect(BR.PrintSatangs(`00`)).toBe(`ถ้วน`)
expect(BR.PrintSatangs(`0000`)).toBe(`ถ้วน`)
expect(BR.PrintSatangs(`dd`)).toBe(undefined)
expect(BR.PrintSatangs(`999`)).toBe(undefined);
})
Expand Down Expand Up @@ -188,6 +201,7 @@ test('SatangNum', () => {
})

test(`TB`, () => {
expect(BR.TB(`สิบเอ็ดบาทสิบเอ็ดสตางค์`)).toBe(`11.11`);
expect(BR.TB(`สามสิบสามแสนบาทถ้วน`)).toBe(`Invalid String`);
expect(BR.TB(`สองล้านล้านบาทถ้วน`)).toBe(`2000000000000.00`);
expect(BR.TB(`สองล้านล้านยี่สิบบาทถ้วน`)).toBe(`2000000000020.00`);
Expand Down Expand Up @@ -226,4 +240,7 @@ test('Reverse BahtText', () => {

test('repeat',() => {
expect(BR.repeat(`ค`,[3])).toBe(`คคค`);
expect(`ปิดสวิตซ์ ${BR.repeat(`ป`, [3])} ป่าหี่`).toBe(
`ปิดสวิตซ์ ปปป ป่าหี่`
);
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bahtrext",
"version": "1.1.8",
"version": "1.2.0",
"description": "Better BahtText",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit ed5f932

Please sign in to comment.