Skip to content

Commit

Permalink
allow negative
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Aug 24, 2024
1 parent a60ae01 commit a3289d9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
5 changes: 4 additions & 1 deletion consts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const DEBUG = false;

const GoogleSheetsCellCharactersLimit = 50000;
const VERSION = `1.4.1`;
const VERSION = `1.4.2`;

const negative = `ลบ`;

const SPECIALONE = `เอ็ด`;
const SPECIALTWO = `ยี่`;
Expand Down Expand Up @@ -227,4 +229,5 @@ module.exports = {
octalRegex2,
MAX_SAFE_INTEGER,
THB,
negative,
};
33 changes: 23 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
large_numbers,
MAX_SAFE_INTEGER,
THB,
negative,
} = require("./consts.js");
const { isOctal, toDec } = require("./octal.js");
const op = require(`operation-strint`)
Expand Down Expand Up @@ -201,14 +202,12 @@ const BF = (flexmoney, ed = false, InvalidType = `Invalid Type`, OL = false) =>
if (!flexmoney) return undefined;
if (typeof flexmoney !== "string") return InvalidType;
let money = flexmoney;
if (DEBUG) console.log(money);
for (const THAI2ARABICNumeral of THAI2ARABICNumerals) {
money = money.replace(
RegExp(THAI2ARABICNumeral.th, `g`),
THAI2ARABICNumeral.a
);
}
if (DEBUG) console.log(money);
return BT(money, ed, OL);
};

Expand Down Expand Up @@ -385,20 +384,34 @@ const IsValidTB = (str) => {
return str === BTTB.replace(FULLBAHT, "");
};

const ABT = (money, ed = false) => {
if (!money) return undefined;
const ABT = (money, ed = false, allow_neg = false, neg = negative) => {
let retVal = undefined;
if (!money) return retVal;
switch (typeof money) {
case "number":
if (money > MAX_SAFE_INTEGER) {
console.warn(`Consider use BahtRext`);
}
const THBText = require("thai-baht-text");
return THBText(money);
if (money < 0) {
retVal = `Try Another Solution`
} else {
const THBText = require("thai-baht-text");
retVal = THBText(money);
}
break
case "string":
return BF(money, ed);
default:
return undefined;
}
if (allow_neg
&& /^\-([\d๐-๙]*)(\.\[\d๐-๙]{0,2}0*)?/.test(money)
&& !(/^\-{2,}/.test(money))
) {
money = money.replace(/^\-/, ``);
retVal = `${neg}${BF(money, ed)}`;
} else {
retVal = BF(money, ed);
}
break
}
return retVal
}

const repeat = (str,x) => {
Expand Down
26 changes: 26 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,34 @@ test("ABT", () => {
expect(ABT(9007199254740992)).toBe(
`เก้าพันเจ็ดล้านหนึ่งแสนเก้าหมื่นเก้าพันสองร้อยห้าสิบสี่ล้านเจ็ดแสนสี่หมื่นเก้าร้อยเก้าสิบสองบาทถ้วน`
);
expect(ABT(`-0.67`)).toBe(undefined);
});

test("ABT Negative", () => {
expect(ABT(`-0.67`, false, true)).toBe(`ลบหกสิบเจ็ดสตางค์`);
expect(ABT(`--0.67`, false, true)).toBe(undefined);
expect(ABT(`-2000000000000.00`, false, true)).toBe(`ลบสองล้านล้านบาทถ้วน`);
expect(ABT(`-123`, false, true)).toBe(`ลบหนึ่งร้อยยี่สิบสามบาทถ้วน`);
expect(ABT(`-8.00`, false, true)).toBe(`ลบแปดบาทถ้วน`);
expect(ABT(`-5678.00`, false, true)).toBe(`ลบห้าพันหกร้อยเจ็ดสิบแปดบาทถ้วน`);
expect(ABT(`-63147.89`, false, true)).toBe(
`ลบหกหมื่นสามพันหนึ่งร้อยสี่สิบเจ็ดบาทแปดสิบเก้าสตางค์`
);
expect(ABT(`-51000001.00`, false, true)).toBe(`ลบห้าสิบเอ็ดล้านหนึ่งบาทถ้วน`);
expect(ABT(`-317.10`, false, true)).toBe(`ลบสามร้อยสิบเจ็ดบาทสิบสตางค์`);
expect(ABT(`-422.26`, false, true)).toBe(`ลบสี่ร้อยยี่สิบสองบาทยี่สิบหกสตางค์`);
expect(ABT(`-11.11`, false, true)).toBe(`ลบสิบเอ็ดบาทสิบเอ็ดสตางค์`);
expect(ABT(`-191415.11`, false, true)).toBe(
`ลบหนึ่งแสนเก้าหมื่นหนึ่งพันสี่ร้อยสิบห้าบาทสิบเอ็ดสตางค์`
);
expect(ABT(`-1.01`, false, true)).toBe(`ลบหนึ่งบาทหนึ่งสตางค์`);
expect(ABT(`-๑.0๑`, false, true)).toBe(`ลบหนึ่งบาทหนึ่งสตางค์`);
expect(ABT(`-5678.46`, false, true)).toBe(`ลบห้าพันหกร้อยเจ็ดสิบแปดบาทสี่สิบหกสตางค์`);
expect(ABT(`-768,601,800,000,000`, false, true)).toBe(
`ลบเจ็ดร้อยหกสิบแปดล้านหกแสนหนึ่งพันแปดร้อยล้านบาทถ้วน`
);
})

test('PrintSatangs', () =>{
expect(PrintSatangs(`67`)[0]).toBe(`หกสิบเจ็ดสตางค์`)
expect(PrintSatangs(`37`)[0]).toBe(`สามสิบเจ็ดสตางค์`);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

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.4.1",
"version": "1.4.2",
"description": "BahtText Stringify",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit a3289d9

Please sign in to comment.