Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Aug 19, 2024
1 parent 9aa2800 commit ed365b8
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 265 deletions.
10 changes: 9 additions & 1 deletion compare.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const BR = require("./index");
const oc = require("./octal");
const THBText = require("thai-baht-text");

let a = 29999999999999999;
Expand All @@ -11,4 +12,11 @@ console.log(a.toString());
console.log(THBText(a));
console.log(BR.BT("29999999999999999"));
console.log(THBText(b));
console.log(BR.BT("29999999999999999.99"));
console.log(BR.BT("29999999999999999.99"));

let c = 0o77;
let d = `0o77`
console.log(THBText(c));
console.log(THBText(d));
console.log(oc.toDec(d))
console.log(oc.toDec(c))
5 changes: 5 additions & 0 deletions consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ const large_numbers = [
{ name: "Googol", powof10: 100 },
];

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

module.exports = {
DEBUG,
GoogleSheetsCellCharactersLimit,
Expand Down Expand Up @@ -213,4 +216,6 @@ module.exports = {
ElevenToNineteenRegex,
TwentyToNinetyNine,
large_numbers,
octalRegex1,
octalRegex2,
};
36 changes: 19 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ const {
SPLITPATTERN,
ZERO,
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
octalRegex1,
octalRegex2,
THAINUMBERWORDS,
ONETONINE,
LTHAISATANGWORDS,
Expand All @@ -40,6 +34,7 @@ const {
TwentyToNinetyNine,
large_numbers,
} = require("./consts.js");
const { isOctal, toDec } = require("./octal.js");

const MoneyInvalid = (money) =>
`Your Input is Invalid Format!\nThis is Your Input : ${money}\nTry Again`;
Expand Down Expand Up @@ -162,7 +157,12 @@ const BahtText = (
} ${arrow} "${PrintBaht(moneyInt, ed)}${PrintSatangs(moneyFrac)}"`;
};

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

const rBahtText = BahtText(money, ed);;
if (!rBahtText) return undefined;
const retText = rBahtText.split('"').at(-2);
Expand All @@ -173,7 +173,7 @@ const BT = (money, ed = false) => {
return retText;
};

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

const IsMatchInSkipsPattern = (match, skips) => {
Expand Down Expand Up @@ -417,18 +417,20 @@ const OB = (money) => {
}

const SEP = (num, separator = `-`) => {
let ret = BF(num, true)
let ret = ABT(num, true);
for (let i of ONETONINE) {
ret = ret.replace(new RegExp(i, `g`), `${i}${separator}`);
}
for (let i of REVERSETHAIDIGITWORDS.filter(x => x !== ``)) {
ret = ret.replace(new RegExp(i, `g`), `${i}${separator}`);
}
ret = ret.replace(new RegExp(MILLION, `g`), `${MILLION}${separator}`)
.replace(new RegExp(SPECIALONE, `g`), `${SPECIALONE}${separator}`)
.replace(new RegExp(SPECIALTWO, `g`), `${SPECIALTWO}${separator}`)
.replace("บาทถ้วน", "")
.replace(new RegExp(`${separator}$`),``);
ret = ret
.replace(new RegExp(MILLION, `g`), `${MILLION}${separator}`)
.replace(new RegExp(SPECIALONE, `g`), `${SPECIALONE}${separator}`)
.replace(new RegExp(SPECIALTWO, `g`), `${SPECIALTWO}${separator}`)
.replace(`${BAHT}${FULLBAHT}`, "")
.replace(BAHT, `${BAHT}${separator}`)
.replace(new RegExp(`${separator}$`), ``);
return ret
}

Expand Down
505 changes: 267 additions & 238 deletions index.test.js

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions octal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const {octalRegex1, octalRegex2} = require('./consts')
const op = require(`operation-strint`)
const isOctal = (money) => {
if (typeof(money) !== `string`) return undefined;
return octalRegex1.test(money) || octalRegex2.test(money);
}

const toDec = (num) => {
let val = `0`
if (!isOctal(num)) return num
num = num.replace(/^0+o?/, ``)
let pos = -1
for (let i of num.split("").reverse()) {
let thispos_val = op.multiply(op.pow(`8`, `${pos+1}`), i);
val = op.sum(val, thispos_val);
pos++;
}
return val
}

module.exports = {
isOctal,
toDec,
};
24 changes: 24 additions & 0 deletions octal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const {
isOctal
, toDec
} = require(`./octal`)

test(`isOctal`, () => {
expect(isOctal(`077`)).toBe(true)
expect(isOctal(`000077`)).toBe(true);
expect(isOctal(`0o77`)).toBe(true);
expect(isOctal(`0000877`)).toBe(!true);
expect(isOctal(`000o77`)).toBe(!true);
expect(isOctal(`000oo77`)).toBe(!true);
expect(isOctal(`053256462753462`)).toBe(true);
expect(isOctal(0o77)).toBe(undefined);
expect(isOctal(123)).toBe(undefined);
})

test(`toDec`, () => {
expect(toDec(`077`)).toBe(`63`);
expect(toDec(`0o17`)).toBe(`15`);
expect(toDec(`0532`)).toBe(`346`);
expect(toDec(`05325646`)).toBe(`1420198`);
expect(toDec(234)).toBe(234);
});
11 changes: 5 additions & 6 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bahtrext",
"version": "1.3.3",
"version": "1.4.0",
"description": "Better BahtText",
"main": "index.js",
"scripts": {
Expand All @@ -22,10 +22,10 @@
"devDependencies": {
"bahttext": "^2.3.0",
"jest": "^29.7.0",
"numbaht": "^0.1.1",
"operation-strint": "^1.0.8"
"numbaht": "^0.1.1"
},
"dependencies": {
"operation-strint": "^1.0.8",
"thai-baht-text": "^2.0.5"
}
}

0 comments on commit ed365b8

Please sign in to comment.