Skip to content

Commit

Permalink
1.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Jan 8, 2025
1 parent dd34292 commit 6cf3e41
Show file tree
Hide file tree
Showing 29 changed files with 330 additions and 316 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ lib
playground.js
coverage
app.js
webpack.config.js
webpack.config.js
.vscode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
5. This Number System not working well with large numbers. Is there a better way to read numbers in Thai?

## Changes
- 1.6.2 - [Refact.ai] model gpt-4o-mini + human refactor
- 1.6.1 - allow literal separator (,)
- 1.6.0 - add Hexadecimal
- 1.5.0 - add Binary Literal
Expand Down
20 changes: 9 additions & 11 deletions binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ const isBin = (money) => {
};

const toBin = (num) => {
let val = `0`;
if (!isBin(num)) return num;
num = num.replace(/^0b/i, ``);
let pos = -1;
for (let i of num.split('').reverse()) {
let thispos_val = op.multiply(op.pow(`2`, `${pos + 1}`), i);
val = op.sum(val, thispos_val);
if (val == '') val = '0'
pos++;
}
return val;
};

return num
.split("")
.reverse()
.reduce((acc, digit, index) => {
const thispos_val = op.multiply(op.pow(`2`, `${index}`), digit);
return op.sum(acc, thispos_val) || "0";
}, "0");
};

module.exports = {
isBin,
toBin,
};
};
43 changes: 18 additions & 25 deletions function/ABT.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
const NEG = require(`./NEG`)
const BF = require(`./BF`)
const NEG = require(`./NEG`);
const BF = require(`./BF`);
const MAX_SAFE_INTEGER = require("../const/primitive/MAX_SAFE_INTEGER");
const THBText = require("thai-baht-text");

module.exports = (money, ed = false, allow_neg = false) => {
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");
if (money < 0) {
retVal = `ลบ${THBText(-money)}`;
} else {
retVal = THBText(money);
}
break;
case "string":
if (allow_neg) {
retVal = NEG(money, ed);
} else {
retVal = BF(money, ed);
}
break;
if (!money) return undefined;

if (typeof money === "number") {
if (money > MAX_SAFE_INTEGER) {
console.warn(`Consider using BahtRext`);
}
return money < 0 ? `ลบ${THBText(-money)}` : THBText(money);
}
return retVal;
};

if (typeof money === "string") {
return allow_neg ? NEG(money, ed) : BF(money, ed);
}

return undefined;
};
20 changes: 12 additions & 8 deletions function/BF.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const THAI2ARABICNumerals = require(`../const/array/THAI2ARABICNumerals`);
const BT = require(`./BT`);

module.exports = (flexmoney, ed = false, InvalidType = `Invalid Type`, OL = false) => {
module.exports = (
flexmoney,
ed = false,
InvalidType = `Invalid Type`,
OL = false
) => {
if (!flexmoney) return undefined;
if (typeof flexmoney !== "string") return InvalidType;
let money = flexmoney;
for (const THAI2ARABICNumeral of THAI2ARABICNumerals) {
money = money.replace(
RegExp(THAI2ARABICNumeral.th, `g`),
THAI2ARABICNumeral.a
);
}

let money = THAI2ARABICNumerals.reduce(
(acc, { th, a }) => acc.replace(new RegExp(th, `g`), a),
flexmoney
);

return BT(money, ed, OL);
};
25 changes: 13 additions & 12 deletions function/BT.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
const {isOctal, toDec} = require(`../octal`)
const {isBin, toBin} = require(`../binary`)
const { isOctal, toDec } = require(`../octal`);
const { isBin, toBin } = require(`../binary`);
const MoneyInvalid = require(`../snippet/MoneyInvalid`);
const BahtText = require("./BahtText");
const THB = require('../const/THB')
const THB = require("../const/THB");
const READAS = require("../const/primitive/READAS");
const GoogleSheetsCellCharactersLimit = require("../const/primitive/GoogleSheetsCellCharactersLimit");
const { isHex, toHex } = require("../hexadecimal");

module.exports = (money, ed = false, OL = false, rounding = ``) => {
const isOL = OL && isOctal(money);
const isBL = isBin(money);
const isHD = isHex(money);
if (isOL) {
if (OL && isOctal(money)) {
money = toDec(money);
}
else if (isBL) {
} else if (isBin(money)) {
money = toBin(money);
}
else if (isHD) {
} else if (isHex(money)) {
money = toHex(money);
}

const rBahtText = BahtText(
money,
ed,
Expand All @@ -29,13 +26,17 @@ module.exports = (money, ed = false, OL = false, rounding = ``) => {
null,
rounding
);

if (!rBahtText) return undefined;

const retText = rBahtText.split('"').at(-2);
if (!retText) return undefined;

if (retText.length > GoogleSheetsCellCharactersLimit) {
console.warn(
`return string Exceed Google Sheets Cell Limit (${GoogleSheetsCellCharactersLimit})`
`Return string exceeds Google Sheets Cell Limit (${GoogleSheetsCellCharactersLimit})`
);
}

return retText;
};
16 changes: 11 additions & 5 deletions function/BahtText.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const splitIntFrac = require(`./splitIntFrac`)
const MoneyLaundering = require(`./MoneyLaundering`)
const PrintBaht = require(`./PrintBaht`)
const splitIntFrac = require(`./splitIntFrac`);
const MoneyLaundering = require(`./MoneyLaundering`);
const PrintBaht = require(`./PrintBaht`);
const IsMoneyValidate = require(`./IsMoneyValidate`);
const PrintSatangs = require(`./PrintSatangs`);
const MoneyInvalid = require("../snippet/MoneyInvalid");
const { THAINUMBERWORDS, BAHT, FULLBAHT, THB, READAS } = require(`../const`);
const op = require(`operation-strint`);

module.exports = (
money,
ed = false,
Expand All @@ -18,14 +19,19 @@ module.exports = (
) => {
if (!money) return NoInput;
if (typeof money !== "string") return InvalidType;

const cleanedMoney = MoneyLaundering(money);
if (!IsMoneyValidate(cleanedMoney, rounding) || money === `.`)
if (!IsMoneyValidate(cleanedMoney, rounding) || money === `.`) {
return ClErr(money);
}

const [moneyFull, moneyInt, moneyFrac] = splitIntFrac(cleanedMoney);
if (moneyFull.match(/^(0*)(\.0*)?$/))
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;
Expand Down
17 changes: 10 additions & 7 deletions function/BulkBahtText.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BahtText = require(`./BahtText`)
const BahtText = require(`./BahtText`);
const IsMatchInSkipsPattern = require(`./IsMatchInSkipsPattern`);
const defaultBulkBahtTextPat = require(`../const/regex/defaultBulkBahtTextPat`);
const defaultBulkBahtTextSkips = require(`../const/regex/defaultBulkBahtTextSkips`);
Expand All @@ -11,15 +11,18 @@ module.exports = (
) => {
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),
ed
);

const bahtText = BahtText(match.replace(/[^\d]/g, ""), ed)
.split('"')
.at(-2);
str = str.replace(match, bahtText);
}

return str;
};
};
7 changes: 1 addition & 6 deletions function/IsMatchInSkipsPattern.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
module.exports = (match, skips) => {
for (const skip of skips) {
if (skip.test(match)) return true;
}
return false;
};
module.exports = (match, skips) => skips.some((skip) => skip.test(match));
8 changes: 4 additions & 4 deletions function/IsMoneyValidate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const SPLITPATTERN = require(`../const/regex/SPLITPATTERN`)
const SPLITPATTERN = require(`../const/regex/SPLITPATTERN`);

module.exports = (money, rounding) => {
if (rounding === ``) return SPLITPATTERN.test(money);
return /\d*(\.\d+)?/.test(money);
};
return rounding === `` ? SPLITPATTERN.test(money) : /\d*(\.\d+)?/.test(money);
};
9 changes: 5 additions & 4 deletions function/IsValidTB.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const BT = require(`./BT`)
const TB = require(`./TB`)
const FULLBAHT = require(`../const/primitive/FULLBAHT`)
const BT = require(`./BT`);
const TB = require(`./TB`);
const FULLBAHT = require(`../const/primitive/FULLBAHT`);

module.exports = (str) => {
const BTTB = BT(TB(str)).replace(/\s/g, "");
return str === BTTB.replace(FULLBAHT, "");
};
};
90 changes: 47 additions & 43 deletions function/IsValidText.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,71 @@ const HUNDRED = require("../const/primitive/HUNDRED");
const TEN = require("../const/primitive/TEN");
const REVERSETHAIDIGITWORDS = require("../const/array/REVERSETHAIDIGITWORDS");
const ONETONINE = require("../const/array/ONETONINE");

module.exports = (text) => {
if (typeof text !== `string`) return false;
if (text.replace(//g, "") === "") return false;
if (typeof text !== `string` || text.replace(//g, "") === "")
return false;

const sixdigitswords = text.split(MILLION);

for (const sixdigitsword of sixdigitswords) {
if (//.test(sixdigitsword)) return false;
if (//.test(sixdigitsword)) return false;
if (/|/.test(sixdigitsword)) return false;

for (const REVERSETHAIDIGITWORD of REVERSETHAIDIGITWORDS.slice(0, -1)) {
if (
(sixdigitsword.match(new RegExp(REVERSETHAIDIGITWORD, "g"))?.length ||
0) > 1
)
) {
return false;
}
}
const iHUNDREDTHOUSAND = sixdigitsword.indexOf(HUNDREDTHOUSAND);
const iTENTHOUSAND = sixdigitsword.indexOf(TENTHOUSAND);
const iTHOUSAND = sixdigitsword.indexOf(THOUSAND);
const iHUNDRED = sixdigitsword.indexOf(HUNDRED);
const iTEN = sixdigitsword.indexOf(TEN);
const iiTEN = iTEN == -1 ? 0 : iTEN;
const iiHUNDRED = iHUNDRED == -1 ? 0 : iHUNDRED;
const iiTHOUSAND = iTHOUSAND == -1 ? 0 : iTHOUSAND;
const iiTENTHOUSAND = iTENTHOUSAND == -1 ? 0 : iTENTHOUSAND;
const iiHUNDREDTHOUSAND = iHUNDREDTHOUSAND == -1 ? 0 : iHUNDREDTHOUSAND;

const indices = {
HUNDREDTHOUSAND: sixdigitsword.indexOf(HUNDREDTHOUSAND),
TENTHOUSAND: sixdigitsword.indexOf(TENTHOUSAND),
THOUSAND: sixdigitsword.indexOf(THOUSAND),
HUNDRED: sixdigitsword.indexOf(HUNDRED),
TEN: sixdigitsword.indexOf(TEN),
};

const ii = Object.fromEntries(
Object.entries(indices).map(([key, value]) => [
key,
value === -1 ? 0 : value,
])
);

if (
!(
((iiTEN >= iiHUNDRED &&
iiTEN >= iiTHOUSAND &&
iiTEN >= iiTENTHOUSAND &&
iiTEN >= iiHUNDREDTHOUSAND) ||
iiTEN == 0) &&
((iiHUNDRED >= iiTHOUSAND &&
iiHUNDRED >= iiTENTHOUSAND &&
iiHUNDRED >= iiHUNDREDTHOUSAND) ||
iiHUNDRED == 0) &&
((iiTHOUSAND >= iiTENTHOUSAND && iiTHOUSAND >= iiHUNDREDTHOUSAND) ||
iiTHOUSAND == 0) &&
(iiTENTHOUSAND >= iiHUNDREDTHOUSAND || iiTENTHOUSAND == 0)
((ii.TEN >= ii.HUNDRED &&
ii.TEN >= ii.THOUSAND &&
ii.TEN >= ii.TENTHOUSAND &&
ii.TEN >= ii.HUNDREDTHOUSAND) ||
ii.TEN === 0) &&
((ii.HUNDRED >= ii.THOUSAND &&
ii.HUNDRED >= ii.TENTHOUSAND &&
ii.HUNDRED >= ii.HUNDREDTHOUSAND) ||
ii.HUNDRED === 0) &&
((ii.THOUSAND >= ii.TENTHOUSAND && ii.THOUSAND >= ii.HUNDREDTHOUSAND) ||
ii.THOUSAND === 0) &&
(ii.TENTHOUSAND >= ii.HUNDREDTHOUSAND || ii.TENTHOUSAND === 0)
)
) {
return false;
}
let eachdigits = sixdigitsword.split(/||||/);
for (let i = 0; i < eachdigits.length; i++) {
if (eachdigits.at(i) === "") continue;
if (ONETONINE.indexOf(eachdigits.at(i)) === -1) {
if (eachdigits.at(i) === SPECIALONE) {
// if (sixdigitsword.indexOf(`สิบเอ็ด`) === -1) {
// return false;
// }

const eachdigits = sixdigitsword.split(/||||/);
for (const digit of eachdigits) {
if (digit === "") continue;

if (ONETONINE.indexOf(digit) === -1) {
if (digit === SPECIALONE || digit === SPECIALTWO) {
continue;
} else if (eachdigits.at(i) === SPECIALTWO) {
// if (sixdigitsword.indexOf(`ยี่สิบ`) === -1) {
// return false;
// }
continue;
} else {
return false;
}
return false;
}
}
}

return true;
};
};
Loading

0 comments on commit 6cf3e41

Please sign in to comment.