Skip to content

Commit

Permalink
can ceil, floor satang
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Aug 22, 2024
1 parent ed365b8 commit a60ae01
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 90 deletions.
11 changes: 10 additions & 1 deletion consts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const DEBUG = false;

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

const SPECIALONE = `เอ็ด`;
const SPECIALTWO = `ยี่`;
Expand Down Expand Up @@ -172,9 +172,16 @@ const large_numbers = [
{ name: "Googol", powof10: 100 },
];

const MAX_SAFE_INTEGER = 9007199254740991;

const octalRegex1 = /^0o?[0-7]+$/i;
const octalRegex2 = /^0+[0-7]+$/i;

let THB = new Intl.NumberFormat("th-TH", {
style: "currency",
currency: "THB",
});

module.exports = {
DEBUG,
GoogleSheetsCellCharactersLimit,
Expand Down Expand Up @@ -218,4 +225,6 @@ module.exports = {
large_numbers,
octalRegex1,
octalRegex2,
MAX_SAFE_INTEGER,
THB,
};
169 changes: 95 additions & 74 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const {
SPLITPATTERN,
ZERO,
ONE,
octalRegex1,
octalRegex2,
THAINUMBERWORDS,
ONETONINE,
LTHAISATANGWORDS,
Expand All @@ -33,8 +31,11 @@ const {
ElevenToNineteenRegex,
TwentyToNinetyNine,
large_numbers,
MAX_SAFE_INTEGER,
THB,
} = require("./consts.js");
const { isOctal, toDec } = require("./octal.js");
const op = require(`operation-strint`)

const MoneyInvalid = (money) =>
`Your Input is Invalid Format!\nThis is Your Input : ${money}\nTry Again`;
Expand All @@ -49,9 +50,12 @@ const MoneyLaundering = (money) => {
);
return removeCommaAndUnderScoreAndLeadingingZeros;
};
const IsMoneyValidate = (money) => SPLITPATTERN.test(money);
const IsMoneyValidate = (money, rounding) => {
if (rounding === ``) return SPLITPATTERN.test(money);
return /\d*(\.\d+)?/.test(money);
};
const splitIntFrac = (money) => {
const match = money.match(SPLITPATTERN);
const match = money.match(/(\d*)(\.\d+)?/);
let [moneyFull, moneyInt, moneyFrac] = match;
moneyFrac === undefined
? (moneyFrac = "")
Expand Down Expand Up @@ -120,50 +124,70 @@ const SatangSecondDigit = (digit) => {
return `${THAINUMBERWORDS[parseInt(digit[1])]}`;
};

const PrintSatangs = (satangs) => {
if (satangs.match(/^0*$/)) return FULLBAHT;
if (!(/^\d{0,2}$/.test(satangs))) return undefined;
const PrintSatangs = (satangs, rounding=``) => {
if (satangs.match(/^0*$/)) return [FULLBAHT, `0`];
if ((!/^\d{0,2}$/.test(satangs) && rounding === ``) || /[^\d]/.test(satangs)) return [undefined, `0`];
let first2digit = satangs.slice(0, 2);
let ceiling = false
if (rounding === `c`) {
const therest = satangs.slice(2, satangs.length);
if (therest.match(/^\d*[1-9]+/) && therest.match(/^\d*$/)) ceiling = true
if (ceiling) {
first2digit = op.sum(`1`, first2digit);
}
satangs = first2digit;
}
if (satangs === `100`) return [FULLBAHT, `1`]
let satangword = `${SatangFirstDigit(satangs[0])}${SatangSecondDigit(
satangs
)}${SATANG}`;
return satangword;
return [satangword, `0`];
};

let THB = new Intl.NumberFormat("th-TH", {
style: "currency",
currency: "THB",
});

const BahtText = (
money,
ed=false,
ed = false,
currencyformat = THB,
arrow = READAS,
ClErr = MoneyInvalid,
InvalidType = `"Invalid Type"`,
NoInput = null
NoInput = null,
rounding = ``
) => {
if (!money) return NoInput;
if (typeof money !== "string") return InvalidType;
const cleanedMoney = MoneyLaundering(money);
if (!IsMoneyValidate(cleanedMoney) || money === `.`) return ClErr(money);
if (!IsMoneyValidate(cleanedMoney, rounding) || money === `.`) return ClErr(money);
const [moneyFull, moneyInt, moneyFrac] = splitIntFrac(cleanedMoney);
if (moneyFull.match(/^(0*)(\.0*)?$/))
return `${
currencyformat ? currencyformat.format(moneyFull) : moneyFull
} ${arrow} "${THAINUMBERWORDS[0]}${BAHT}${FULLBAHT}"`;
const satang_part = PrintSatangs(moneyFrac, rounding);
const opsum = op.sum(satang_part[1], moneyInt === `` ? `0` : moneyInt);
const new_baht = opsum === `` ? `0` : opsum;

const baht_part = PrintBaht(new_baht, ed).replace(/^$/, ``);
return `${
currencyformat ? currencyformat.format(moneyFull) : moneyFull
} ${arrow} "${PrintBaht(moneyInt, ed)}${PrintSatangs(moneyFrac)}"`;
} ${arrow} "${baht_part}${satang_part[0]}"`;
};

const BT = (money, ed = false, OL = false) => {
const BT = (money, ed = false, OL = false, rounding = ``) => {
const isOL = OL && isOctal(money);
if (isOL) {
money = toDec(money)
}

const rBahtText = BahtText(money, ed);;
const rBahtText = BahtText(
money,
ed,
THB,
READAS,
MoneyInvalid,
`"Invalid Type"`,
null,
rounding
);
if (!rBahtText) return undefined;
const retText = rBahtText.split('"').at(-2);
if (!retText) return undefined;
Expand Down Expand Up @@ -365,7 +389,6 @@ const ABT = (money, ed = false) => {
if (!money) return undefined;
switch (typeof money) {
case "number":
const MAX_SAFE_INTEGER = 9007199254740991
if (money > MAX_SAFE_INTEGER) {
console.warn(`Consider use BahtRext`);
}
Expand Down Expand Up @@ -434,56 +457,54 @@ const SEP = (num, separator = `-`) => {
return ret
}

// if (!DEBUG) {
module.exports = {
VERSION,
SPECIALONE,
SPECIALTWO,
TEN,
BAHT,
SATANG,
FULLBAHT,
MILLION,
LAST6DIGITPATTERN,
SPLITPATTERN,
REVERSETHAIDIGITWORDS,
THAINUMBERWORDS,
FTHAISATANGWORDS,
LTHAISATANGWORDS,
MoneyLaundering,
removeLeadingingZeros,
IsMoneyValidate,
splitIntFrac,
PrintBaht,
PrintSatangs,
THB,
BahtText,
BT,
BulkBahtText,
ValidSATANGRegex,
OneToTenTextRegex,
ElevenToNineteenRegex,
TwentyToNinetyNine,
NumText,
SatangNum,
TB,
IsValidTB,
THAI2ARABICNumerals,
BF,
ABT,
repeat,
large_numbers,
LNBT,
LeadingSpecialOneToOne,
OB,
ONETONINE,
million: MILLION,
HUNDREDTHOUSAND,
TENTHOUSAND,
THOUSAND,
HUNDRED,
TEN,
IsValidText,
SEP,
};
// }
module.exports = {
VERSION,
SPECIALONE,
SPECIALTWO,
TEN,
BAHT,
SATANG,
FULLBAHT,
MILLION,
LAST6DIGITPATTERN,
SPLITPATTERN,
REVERSETHAIDIGITWORDS,
THAINUMBERWORDS,
FTHAISATANGWORDS,
LTHAISATANGWORDS,
MoneyLaundering,
removeLeadingingZeros,
IsMoneyValidate,
splitIntFrac,
PrintBaht,
PrintSatangs,
THB,
BahtText,
BT,
BulkBahtText,
ValidSATANGRegex,
OneToTenTextRegex,
ElevenToNineteenRegex,
TwentyToNinetyNine,
NumText,
SatangNum,
TB,
IsValidTB,
THAI2ARABICNumerals,
BF,
ABT,
repeat,
large_numbers,
LNBT,
LeadingSpecialOneToOne,
OB,
ONETONINE,
million: MILLION,
HUNDREDTHOUSAND,
TENTHOUSAND,
THOUSAND,
HUNDRED,
TEN,
IsValidText,
SEP,
};
66 changes: 53 additions & 13 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ test('NumText', () => {
expect(NumText(84000)).toBe(`Invalid Type`);
});

test(`BT CEIL`,() => {
expect(BT(`4.990001`, false, false, `c`)).toBe(`ห้าบาทถ้วน`);
})

test('BT OL', () => {
expect(BT(`077`)).toBe(`เจ็ดสิบเจ็ดบาทถ้วน`);
Expand All @@ -35,6 +38,10 @@ test('BT OL', () => {

test('BT', () => {
expect(BT(`lol`)).toBe(undefined);
expect(BT(`2000000000000.9`,false,false,`c`)).toBe(`สองล้านล้านบาทเก้าสิบสตางค์`)
expect(BT(`2000000000000.990003`,false,false,`c`)).toBe(`สองล้านล้านหนึ่งบาทถ้วน`)
expect(BT(`2000000000000.99`,false,false,`f`)).toBe(`สองล้านล้านบาทเก้าสิบเก้าสตางค์`);
expect(BT(`2000000000000.990003`,false,false,`f`)).toBe(`สองล้านล้านบาทเก้าสิบเก้าสตางค์`);
expect(BT(`2000000000000.00`)).toBe(`สองล้านล้านบาทถ้วน`)
expect(BT(`1000001000001`)).toBe(`หนึ่งล้านหนึ่งล้านหนึ่งบาทถ้วน`);
expect(BT(`1000001000001`, true)).toBe(`หนึ่งล้านเอ็ดล้านเอ็ดบาทถ้วน`);
Expand Down Expand Up @@ -93,20 +100,31 @@ test("ABT", () => {
});

test('PrintSatangs', () =>{
expect(PrintSatangs(`67`)).toBe(`หกสิบเจ็ดสตางค์`)
expect(PrintSatangs(`37`)).toBe(`สามสิบเจ็ดสตางค์`)
expect(PrintSatangs(`31`)).toBe(`สามสิบเอ็ดสตางค์`)
expect(PrintSatangs(`21`)).toBe(`ยี่สิบเอ็ดสตางค์`)
expect(PrintSatangs(`01`)).toBe(`หนึ่งสตางค์`)
expect(PrintSatangs(`1`)).toBe(`สิบสตางค์`)
expect(PrintSatangs(``)).toBe(`ถ้วน`)
expect(PrintSatangs(`0`)).toBe(`ถ้วน`)
expect(PrintSatangs(`00`)).toBe(`ถ้วน`)
expect(PrintSatangs(`0000`)).toBe(`ถ้วน`)
expect(PrintSatangs(`dd`)).toBe(undefined)
expect(PrintSatangs(`999`)).toBe(undefined);
expect(PrintSatangs(`67`)[0]).toBe(`หกสิบเจ็ดสตางค์`)
expect(PrintSatangs(`37`)[0]).toBe(`สามสิบเจ็ดสตางค์`);
expect(PrintSatangs(`31`)[0]).toBe(`สามสิบเอ็ดสตางค์`);
expect(PrintSatangs(`21`)[0]).toBe(`ยี่สิบเอ็ดสตางค์`);
expect(PrintSatangs(`01`)[0]).toBe(`หนึ่งสตางค์`);
expect(PrintSatangs(`1`)[0]).toBe(`สิบสตางค์`);
expect(PrintSatangs(``)[0]).toBe(FULLBAHT);
expect(PrintSatangs(`0`)[0]).toBe(FULLBAHT);
expect(PrintSatangs(`00`)[0]).toBe(FULLBAHT);
expect(PrintSatangs(`0000`)[0]).toBe(FULLBAHT);
expect(PrintSatangs(`dd`)[0]).toBe(undefined);
expect(PrintSatangs(`999`)[0]).toBe(undefined);
})

test(`PrintSatangs 2d+`, () => {
expect(PrintSatangs(``, `c`)[0]).toBe(FULLBAHT);
expect(PrintSatangs(`9900000000000000001`, `c`)[0]).toBe(FULLBAHT);
expect(PrintSatangs(`99`, `c`)[0]).toBe(`เก้าสิบเก้าสตางค์`);
expect(PrintSatangs(`499`, `c`)[0]).toBe(`ห้าสิบสตางค์`);
expect(PrintSatangs(`490000000000001`, `c`)[0]).toBe(`ห้าสิบสตางค์`);
expect(PrintSatangs(`499`, `c`)[0]).toBe(`ห้าสิบสตางค์`);
expect(PrintSatangs(`499`, `f`)[0]).toBe(`สี่สิบเก้าสตางค์`);
expect(PrintSatangs(`49239480239`, `f`)[0]).toBe(`สี่สิบเก้าสตางค์`);
});

test(`BulkBahtText`, () =>{
expect(BulkBahtText(`อย่าลืมใช้โค้ด 9arm นะครับ ใช้เถอะ เค้าจะได้จ้างผมต่อ`)).toBe(`อย่าลืมใช้โค้ด 9arm นะครับ ใช้เถอะ เค้าจะได้จ้างผมต่อ`)
expect(
Expand Down Expand Up @@ -368,4 +386,26 @@ test(`sep`, () => {
// https://web.facebook.com/kumthai.th/posts/920804870082544
expect(SEP(`2501.33`)).toBe(`สอง-พัน-ห้า-ร้อย-เอ็ด-บาท-สาม-สิบ-สาม-สตางค์`);
expect(SEP(`1234.56`)).toBe(`หนึ่ง-พัน-สอง-ร้อย-สาม-สิบ-สี่-บาท-ห้า-สิบ-หก-สตางค์`);
})
})

test(`data type`, () => {
const d = {
s: `string`,
f: `function`,
o: `object`,
};
expect(typeof FULLBAHT).toBe(d.s);
expect(typeof BAHT).toBe(d.s);
expect(typeof NumText).toBe(d.f);
expect(typeof BT).toBe(d.f);
expect(typeof ABT).toBe(d.f);
expect(typeof PrintSatangs).toBe(d.f);
expect(typeof BulkBahtText).toBe(d.f);
expect(typeof TB).toBe(d.f);
expect(typeof repeat).toBe(d.f);
expect(typeof IsValidTB).toBe(d.f);
expect(typeof IsValidText).toBe(d.f);
expect(typeof OB).toBe(d.f);
expect(typeof LNBT).toBe(d.f);
expect(typeof SEP).toBe(d.f);
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bahtrext",
"version": "1.4.0",
"description": "Better BahtText",
"version": "1.4.1",
"description": "BahtText Stringify",
"main": "index.js",
"scripts": {
"test": "jest",
Expand Down

0 comments on commit a60ae01

Please sign in to comment.