Skip to content

Commit

Permalink
cc
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed Oct 23, 2024
1 parent 997d1e3 commit 987f9ce
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg?logo=jest)](https://github.com/jestjs/jest)

## Demo / Example
- Webpack
- [Webpack](https://webpack.js.org/)
- [REPO](https://github.com/PingHuskar/webpack-bahtrext)
- [DEMO](https://pinghuskar.github.io/webpack-bahtrext/)
- [scripts](https://pinghuskar.github.io/webpack-bahtrext/main.js)
- React
- [React](https://react.dev/)
- [BahtGame](https://timely-fenglisu-b68fd6.netlify.app/)
- [scroll-trigger](https://github.com/PingHuskar/bahtrext-scroll-trigger)

Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import ABT from "./index.mjs";
console.log(ABT("1234567890"))
console.log(ABT("1234567,891.35"))
2 changes: 1 addition & 1 deletion const/primitive/VERSION.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const VERSION = `2.0.1`;
const VERSION = `2.0.2`;
export default VERSION;
2 changes: 2 additions & 0 deletions const/regex/LeadingZerosRegex.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const LeadingZerosRegex = /^0+/;
export default LeadingZerosRegex;
2 changes: 2 additions & 0 deletions const/regex/ValidMoneyRegex.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const ValidMoneyRegex = /\d*(\.\d+)?/;
export default ValidMoneyRegex;
2 changes: 2 additions & 0 deletions const/regex/ZeroSatangRegex.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const ZeroSatangRegex = /^0*$/;
export default ZeroSatangRegex;
2 changes: 2 additions & 0 deletions const/regex/globalNotDigits.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const globalNotDigits = /[^\d]/g;
export default globalNotDigits;
2 changes: 2 additions & 0 deletions const/regex/globalSpaceRegex.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const globalSpaceRegex = /\s/g;
export default globalSpaceRegex;
4 changes: 3 additions & 1 deletion function/BulkBahtText.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import BahtText from "./BahtText.mjs";
import IsMatchInSkipsPattern from "./IsMatchInSkipsPattern.mjs";
import defaultBulkBahtTextPat from "../const/regex/defaultBulkBahtTextPat.mjs";
import defaultBulkBahtTextSkips from "../const/regex/defaultBulkBahtTextSkips.mjs";
import RepEmt from "./RepEmt.mjs";
import globalNotDigits from "../const/regex/globalNotDigits.mjs";

const BulkBahtText = (
str,
Expand All @@ -17,7 +19,7 @@ const BulkBahtText = (
if (IsMatchInSkipsPattern(match, skips)) continue;
str = str.replace(
match,
BahtText(match.replace(/[^\d]/g, "")).split('"').at(-2),
BahtText(RepEmt(match, globalNotDigits)).split('"').at(-2),
ed
);
}
Expand Down
4 changes: 3 additions & 1 deletion function/IsMoneyValidate.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import SPLITPATTERN from "../const/regex/SPLITPATTERN.mjs";
import ValidMoneyRegex from "../const/regex/ValidMoneyRegex.mjs";

const IsMoneyValidate = (money, rounding) => {
if (rounding === ``) return SPLITPATTERN.test(money);
return /\d*(\.\d+)?/.test(money);
return ValidMoneyRegex.test(money);
};
export default IsMoneyValidate;
6 changes: 4 additions & 2 deletions function/IsValidTB.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import BT from "./BT.mjs";
import TB from "./TB.mjs";
import FULLBAHT from "../const/primitive/FULLBAHT.mjs";
import RepEmt from "./RepEmt.mjs";
import globalSpaceRegex from "../const/regex/globalSpaceRegex.mjs";

const IsValidTB = (str) => {
try {
if (!str) return undefined
if (typeof str !== `string`) return false
const BTTB = BT(TB(str)).replace(/\s/g, "");
return str === BTTB.replace(FULLBAHT, "");
const BTTB = RepEmt(BT(TB(str)), globalSpaceRegex);
return str === RepEmt(BTTB, FULLBAHT);
}
catch {
return false
Expand Down
3 changes: 2 additions & 1 deletion function/IsValidText.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import TEN from "../const/primitive/TEN.mjs";
import SPECIALONE from "../const/primitive/SPECIALONE.mjs";
import SPECIALTWO from "../const/primitive/SPECIALTWO.mjs";
import ONETONINE from "../const/array/ONETONINE.mjs";
import RepEmt from "./RepEmt.mjs";

const IsValidText = (text) => {
if (typeof text !== `string`) return false;
if (text.replace(/ล้าน/g, "") === "") return false;
if (RepEmt(text, /ล้าน/g) === "") return false;
const sixdigitswords = text.split(MILLION);
for (const sixdigitsword of sixdigitswords) {
if (/สองสิบ/.test(sixdigitsword)) return false;
Expand Down
13 changes: 7 additions & 6 deletions function/MoneyLaundering.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import removeLeadingZeros from "../snippet/removeLeadingZeros.mjs";
import LeadingZerosRegex from "../const/regex/LeadingZerosRegex.mjs";
import Replace from "./Replace.mjs";

const MoneyLaundering = (money) => {
const removeComma = money.replace(/,/g, "");
const removeCommaAndUnderScore = removeComma.replace(/_/g, "");
const removeCommaAndUnderScoreAndLeadingZeros = removeLeadingZeros(
removeCommaAndUnderScore
return Replace(
money
, [/,/g]
, [/_/g]
, [LeadingZerosRegex]
);
return removeCommaAndUnderScoreAndLeadingZeros;
};
export default MoneyLaundering;
3 changes: 2 additions & 1 deletion function/PrintBaht.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import LeadingSpecialOneToOne from "../snippet/LeadingSpecialOneToOne.mjs";
import LAST6DIGITPATTERN from "../const/regex/LAST6DIGITPATTERN.mjs";
import MILLION from "../const/primitive/MILLION.mjs";
import BAHT from "../const/primitive/BAHT.mjs";
import RepEmt from "./RepEmt.mjs";

const PrintBaht = (money, ed = false) => {
if (!money) return ``;
let newMoney = [];
while (money != ``) {
let selectedupto6digit = money.match(LAST6DIGITPATTERN)[0];
newMoney.push(`${hundredThousandToOne(selectedupto6digit, ed)}${MILLION}`);
money = money.replace(LAST6DIGITPATTERN, "");
money = RepEmt(money, LAST6DIGITPATTERN);
}
return `${LeadingSpecialOneToOne(newMoney.reverse().join("")).replace(
/ล้าน$/,
Expand Down
7 changes: 4 additions & 3 deletions function/PrintSatangs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import FULLBAHT from "../const/primitive/FULLBAHT.mjs";
import SATANG from "../const/primitive/SATANG.mjs";
import SatangFirstDigit from "./SatangFirstDigit.mjs";
import SatangSecondDigit from "./SatangSecondDigit.mjs";
import op from "operation-strint"
import ZeroSatangRegex from "../const/regex/ZeroSatangRegex.mjs";
import {sum} from "operation-strint"

const PrintSatangs = (satangs, rounding = ``) => {
if (satangs.match(/^0*$/)) return [FULLBAHT, `0`];
if (satangs.match(ZeroSatangRegex)) return [FULLBAHT, `0`];
if ((!/^\d{0,2}$/.test(satangs) && rounding === ``) || /[^\d]/.test(satangs))
return [undefined, `0`];
let first2digit = satangs.slice(0, 2);
Expand All @@ -14,7 +15,7 @@ const PrintSatangs = (satangs, rounding = ``) => {
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);
first2digit = sum(`1`, first2digit);
}
satangs = first2digit;
}
Expand Down
2 changes: 2 additions & 0 deletions function/RepEmt.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const RepEmt = (str, RegOrStr = '') => str.replace(RegOrStr, "");
export default RepEmt;
7 changes: 7 additions & 0 deletions function/Replace.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Replace = (str, ...replaces) => {
for (let i of replaces) {
str = str.replace(i[0], i[1] ?? '')
}
return str
}
export default Replace
17 changes: 10 additions & 7 deletions function/SEP.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SPECIALONE from "../const/primitive/SPECIALONE.mjs";
import SPECIALTWO from "../const/primitive/SPECIALTWO.mjs";
import BAHT from "../const/primitive/BAHT.mjs";
import FULLBAHT from "../const/primitive/FULLBAHT.mjs";
import Replace from "./Replace.mjs";

const SEP = (num, separator = `-`) => {
let ret = ABT(num, true);
Expand All @@ -15,13 +16,15 @@ const SEP = (num, 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(`${BAHT}${FULLBAHT}`, "")
.replace(BAHT, `${BAHT}${separator}`)
.replace(new RegExp(`${separator}$`), ``);
ret = Replace(
ret
,[new RegExp(MILLION, `g`), `${MILLION}${separator}`]
,[new RegExp(SPECIALONE, `g`), `${SPECIALONE}${separator}`]
,[new RegExp(SPECIALTWO, `g`), `${SPECIALTWO}${separator}`]
,[`${BAHT}${FULLBAHT}`]
,[BAHT, `${BAHT}${separator}`]
,[new RegExp(`${separator}$`)]
);
return ret;
};

Expand Down
3 changes: 2 additions & 1 deletion function/TB.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import BAHT from "../const/primitive/BAHT.mjs";
import SATANG from "../const/primitive/SATANG.mjs";
import MILLION from "../const/primitive/MILLION.mjs";
import padWithLeadingZeros from "../snippet/padWithLeadingZeros.mjs";
import RepEmt from "./RepEmt.mjs";

const TB = (BT, error = `Invalid String`) => {
if (!BT) return undefined;
Expand Down Expand Up @@ -57,7 +58,7 @@ const TB = (BT, error = `Invalid String`) => {
);
}
return `${removeLeadingZeros(moneyBahts.reverse().join(""))}.${SatangNum(
moneySatang.replace(SATANG, ``)
RepEmt(moneySatang, SATANG)
)}`;
};
export default TB;
3 changes: 2 additions & 1 deletion function/splitIntFrac.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import RepEmt from "./RepEmt.mjs";
const splitIntFrac = (money) => {
const match = money.match(/(\d*)(\.\d+)?/);
let [moneyFull, moneyInt, moneyFrac] = match;
moneyFrac === undefined
? (moneyFrac = "")
: (moneyFrac = moneyFrac.replace(/^\./, ""));
: (moneyFrac = RepEmt(moneyFrac, /^\./));
return [moneyFull, moneyInt, moneyFrac];
};
export default splitIntFrac;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bahtrext",
"version": "2.0.1",
"version": "2.0.2",
"type": "module",
"description": "BahtText Stringify",
"main": "index.mjs",
Expand All @@ -15,7 +15,7 @@
"cv": "jest --coverage"
},
"author": "Chadin Chaipornpisuth",
"license": "ISC",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/PingHuskar/npm-bahtrext.git"
Expand Down
10 changes: 6 additions & 4 deletions performance.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {BT,BF} from "./index.mjs"
import THBText from "thai-baht-text";
const pass = (val) => {
return
}

const performance = (money_array) => {
const THBText = require("thai-baht-text");
console.time("thai-baht-text");
for (const money of money_array) {
// console.log(THBText(money));
Expand All @@ -14,11 +15,10 @@ const performance = (money_array) => {
};

const performanceBR = (money_array) => {
const BahtRext = require('./index');
console.time("BahtRext");
for (const money of money_array) {
// console.log(BahtRext.BF(money));
pass(BahtRext.BF(money));
pass(BT(money));
}
console.timeEnd("BahtRext");
return `return`;
Expand All @@ -27,10 +27,12 @@ const performanceBR = (money_array) => {
const performance_arr = [];
const performance_arr_s = [];

for (i = 1; i <= 1000000; i++) {
// for (let i = 1; i <= 1000000; i++) {
for (let i = 1; i <= 10000; i+=0.01) {
performance_arr.push(i);
performance_arr_s.push(`${i}`);
}

console.log(performanceBR(performance_arr_s));
console.log(performance(performance_arr));
console.log(`Which one is Faster ?`)
4 changes: 3 additions & 1 deletion snippet/removeLeadingZeros.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const removeLeadingZeros = (string) => string.replace(/^0+/g, "");
import RepEmt from "../function/RepEmt.mjs";
import LeadingZerosRegex from "../const/regex/LeadingZerosRegex.mjs";
const removeLeadingZeros = (string) => RepEmt(string, LeadingZerosRegex);
export default removeLeadingZeros;

0 comments on commit 987f9ce

Please sign in to comment.