From 987f9ce4d11b23487a95215cd7729478a12c1baa Mon Sep 17 00:00:00 2001 From: Chadin Chaipornpisuth Date: Wed, 23 Oct 2024 15:01:31 +0700 Subject: [PATCH] cc --- README.md | 4 ++-- app.js | 2 +- const/primitive/VERSION.mjs | 2 +- const/regex/LeadingZerosRegex.mjs | 2 ++ const/regex/ValidMoneyRegex.mjs | 2 ++ const/regex/ZeroSatangRegex.mjs | 2 ++ const/regex/globalNotDigits.mjs | 2 ++ const/regex/globalSpaceRegex.mjs | 2 ++ function/BulkBahtText.mjs | 4 +++- function/IsMoneyValidate.mjs | 4 +++- function/IsValidTB.mjs | 6 ++++-- function/IsValidText.mjs | 3 ++- function/MoneyLaundering.mjs | 13 +++++++------ function/PrintBaht.mjs | 3 ++- function/PrintSatangs.mjs | 7 ++++--- function/RepEmt.mjs | 2 ++ function/Replace.mjs | 7 +++++++ function/SEP.mjs | 17 ++++++++++------- function/TB.mjs | 3 ++- function/splitIntFrac.mjs | 3 ++- package.json | 4 ++-- performance.js | 10 ++++++---- snippet/removeLeadingZeros.mjs | 4 +++- 23 files changed, 73 insertions(+), 35 deletions(-) create mode 100644 const/regex/LeadingZerosRegex.mjs create mode 100644 const/regex/ValidMoneyRegex.mjs create mode 100644 const/regex/ZeroSatangRegex.mjs create mode 100644 const/regex/globalNotDigits.mjs create mode 100644 const/regex/globalSpaceRegex.mjs create mode 100644 function/RepEmt.mjs create mode 100644 function/Replace.mjs diff --git a/README.md b/README.md index 7401dda..6958de8 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/app.js b/app.js index 810379d..a8242d7 100644 --- a/app.js +++ b/app.js @@ -1,2 +1,2 @@ import ABT from "./index.mjs"; -console.log(ABT("1234567890")) \ No newline at end of file +console.log(ABT("1234567,891.35")) \ No newline at end of file diff --git a/const/primitive/VERSION.mjs b/const/primitive/VERSION.mjs index 3e099d3..4813fa5 100644 --- a/const/primitive/VERSION.mjs +++ b/const/primitive/VERSION.mjs @@ -1,2 +1,2 @@ -const VERSION = `2.0.1`; +const VERSION = `2.0.2`; export default VERSION; \ No newline at end of file diff --git a/const/regex/LeadingZerosRegex.mjs b/const/regex/LeadingZerosRegex.mjs new file mode 100644 index 0000000..cc42dcf --- /dev/null +++ b/const/regex/LeadingZerosRegex.mjs @@ -0,0 +1,2 @@ +const LeadingZerosRegex = /^0+/; +export default LeadingZerosRegex; \ No newline at end of file diff --git a/const/regex/ValidMoneyRegex.mjs b/const/regex/ValidMoneyRegex.mjs new file mode 100644 index 0000000..acac8dd --- /dev/null +++ b/const/regex/ValidMoneyRegex.mjs @@ -0,0 +1,2 @@ +const ValidMoneyRegex = /\d*(\.\d+)?/; +export default ValidMoneyRegex; \ No newline at end of file diff --git a/const/regex/ZeroSatangRegex.mjs b/const/regex/ZeroSatangRegex.mjs new file mode 100644 index 0000000..62e0125 --- /dev/null +++ b/const/regex/ZeroSatangRegex.mjs @@ -0,0 +1,2 @@ +const ZeroSatangRegex = /^0*$/; +export default ZeroSatangRegex; \ No newline at end of file diff --git a/const/regex/globalNotDigits.mjs b/const/regex/globalNotDigits.mjs new file mode 100644 index 0000000..f114e08 --- /dev/null +++ b/const/regex/globalNotDigits.mjs @@ -0,0 +1,2 @@ +const globalNotDigits = /[^\d]/g; +export default globalNotDigits; \ No newline at end of file diff --git a/const/regex/globalSpaceRegex.mjs b/const/regex/globalSpaceRegex.mjs new file mode 100644 index 0000000..8a52f34 --- /dev/null +++ b/const/regex/globalSpaceRegex.mjs @@ -0,0 +1,2 @@ +const globalSpaceRegex = /\s/g; +export default globalSpaceRegex; \ No newline at end of file diff --git a/function/BulkBahtText.mjs b/function/BulkBahtText.mjs index 2bd445b..f9d7223 100644 --- a/function/BulkBahtText.mjs +++ b/function/BulkBahtText.mjs @@ -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, @@ -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 ); } diff --git a/function/IsMoneyValidate.mjs b/function/IsMoneyValidate.mjs index c4ff437..fda8e1e 100644 --- a/function/IsMoneyValidate.mjs +++ b/function/IsMoneyValidate.mjs @@ -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; \ No newline at end of file diff --git a/function/IsValidTB.mjs b/function/IsValidTB.mjs index df096a0..24f8e14 100644 --- a/function/IsValidTB.mjs +++ b/function/IsValidTB.mjs @@ -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 diff --git a/function/IsValidText.mjs b/function/IsValidText.mjs index 19ecbad..483cf8a 100644 --- a/function/IsValidText.mjs +++ b/function/IsValidText.mjs @@ -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; diff --git a/function/MoneyLaundering.mjs b/function/MoneyLaundering.mjs index 78ca096..f756174 100644 --- a/function/MoneyLaundering.mjs +++ b/function/MoneyLaundering.mjs @@ -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; \ No newline at end of file diff --git a/function/PrintBaht.mjs b/function/PrintBaht.mjs index 6d48cd2..89e12b7 100644 --- a/function/PrintBaht.mjs +++ b/function/PrintBaht.mjs @@ -3,6 +3,7 @@ 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 ``; @@ -10,7 +11,7 @@ const PrintBaht = (money, ed = false) => { 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( /ล้าน$/, diff --git a/function/PrintSatangs.mjs b/function/PrintSatangs.mjs index 15f6d04..4c0c60b 100644 --- a/function/PrintSatangs.mjs +++ b/function/PrintSatangs.mjs @@ -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); @@ -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; } diff --git a/function/RepEmt.mjs b/function/RepEmt.mjs new file mode 100644 index 0000000..d8ee040 --- /dev/null +++ b/function/RepEmt.mjs @@ -0,0 +1,2 @@ +const RepEmt = (str, RegOrStr = '') => str.replace(RegOrStr, ""); +export default RepEmt; \ No newline at end of file diff --git a/function/Replace.mjs b/function/Replace.mjs new file mode 100644 index 0000000..6e232a6 --- /dev/null +++ b/function/Replace.mjs @@ -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 \ No newline at end of file diff --git a/function/SEP.mjs b/function/SEP.mjs index e958139..67ed34d 100644 --- a/function/SEP.mjs +++ b/function/SEP.mjs @@ -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); @@ -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; }; diff --git a/function/TB.mjs b/function/TB.mjs index e7a7a0c..f04c571 100644 --- a/function/TB.mjs +++ b/function/TB.mjs @@ -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; @@ -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; \ No newline at end of file diff --git a/function/splitIntFrac.mjs b/function/splitIntFrac.mjs index 1a97ecc..5afce3a 100644 --- a/function/splitIntFrac.mjs +++ b/function/splitIntFrac.mjs @@ -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; \ No newline at end of file diff --git a/package.json b/package.json index a3a5af7..1b3615b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bahtrext", - "version": "2.0.1", + "version": "2.0.2", "type": "module", "description": "BahtText Stringify", "main": "index.mjs", @@ -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" diff --git a/performance.js b/performance.js index 2a8d811..4dcddc7 100644 --- a/performance.js +++ b/performance.js @@ -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)); @@ -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`; @@ -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 ?`) \ No newline at end of file diff --git a/snippet/removeLeadingZeros.mjs b/snippet/removeLeadingZeros.mjs index fff7166..a36d170 100644 --- a/snippet/removeLeadingZeros.mjs +++ b/snippet/removeLeadingZeros.mjs @@ -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; \ No newline at end of file