From 6cf3e412c952ee7b47de855adec7e85b5f857a42 Mon Sep 17 00:00:00 2001 From: Chadin Chaipornpisuth Date: Wed, 8 Jan 2025 14:09:57 +0700 Subject: [PATCH] 1.6.2 --- .gitignore | 3 +- README.md | 1 + binary.js | 20 ++++--- function/ABT.js | 43 +++++++-------- function/BF.js | 20 ++++--- function/BT.js | 25 ++++----- function/BahtText.js | 16 ++++-- function/BulkBahtText.js | 17 +++--- function/IsMatchInSkipsPattern.js | 7 +-- function/IsMoneyValidate.js | 8 +-- function/IsValidTB.js | 9 ++-- function/IsValidText.js | 90 ++++++++++++++++--------------- function/LNBT.js | 42 +++++++-------- function/MoneyLaundering.js | 12 ++--- function/NEG.js | 15 +++--- function/NumText.js | 12 +++-- function/PrintBaht.js | 17 +++--- function/PrintSatangs.js | 26 ++++----- function/SEP.js | 21 +++++--- function/SatangFirstDigit.js | 16 +++--- function/SatangNum.js | 28 +++++----- function/SatangSecondDigit.js | 2 + function/TB.js | 25 ++++++--- function/hundredThousandToOne.js | 50 +++++++++-------- function/repeat.js | 7 +-- function/splitIntFrac.js | 9 ++-- hexadecimal.js | 53 +++++++----------- octal.js | 50 ++++++++--------- package.json | 2 +- 29 files changed, 330 insertions(+), 316 deletions(-) diff --git a/.gitignore b/.gitignore index 44fac6f..a45ffad 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ lib playground.js coverage app.js -webpack.config.js \ No newline at end of file +webpack.config.js +.vscode \ No newline at end of file diff --git a/README.md b/README.md index cd4b625..914564e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/binary.js b/binary.js index 0a8e916..669dab9 100644 --- a/binary.js +++ b/binary.js @@ -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, -}; \ No newline at end of file +}; diff --git a/function/ABT.js b/function/ABT.js index ba386af..197889e 100644 --- a/function/ABT.js +++ b/function/ABT.js @@ -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; -}; \ No newline at end of file + + if (typeof money === "string") { + return allow_neg ? NEG(money, ed) : BF(money, ed); + } + + return undefined; +}; diff --git a/function/BF.js b/function/BF.js index ce06097..52e5980 100644 --- a/function/BF.js +++ b/function/BF.js @@ -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); }; diff --git a/function/BT.js b/function/BT.js index 2dc9312..8ee6910 100644 --- a/function/BT.js +++ b/function/BT.js @@ -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, @@ -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; }; diff --git a/function/BahtText.js b/function/BahtText.js index 7bde410..eae50c0 100644 --- a/function/BahtText.js +++ b/function/BahtText.js @@ -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, @@ -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; diff --git a/function/BulkBahtText.js b/function/BulkBahtText.js index cacaa56..7f75cc3 100644 --- a/function/BulkBahtText.js +++ b/function/BulkBahtText.js @@ -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`); @@ -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; -}; \ No newline at end of file +}; diff --git a/function/IsMatchInSkipsPattern.js b/function/IsMatchInSkipsPattern.js index e169c77..9a71d7a 100644 --- a/function/IsMatchInSkipsPattern.js +++ b/function/IsMatchInSkipsPattern.js @@ -1,6 +1 @@ -module.exports = (match, skips) => { - for (const skip of skips) { - if (skip.test(match)) return true; - } - return false; -}; \ No newline at end of file +module.exports = (match, skips) => skips.some((skip) => skip.test(match)); diff --git a/function/IsMoneyValidate.js b/function/IsMoneyValidate.js index 70da786..a450b55 100644 --- a/function/IsMoneyValidate.js +++ b/function/IsMoneyValidate.js @@ -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); -}; \ No newline at end of file + return rounding === `` ? SPLITPATTERN.test(money) : /\d*(\.\d+)?/.test(money); +}; diff --git a/function/IsValidTB.js b/function/IsValidTB.js index ccaef5c..19d710b 100644 --- a/function/IsValidTB.js +++ b/function/IsValidTB.js @@ -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, ""); -}; \ No newline at end of file +}; diff --git a/function/IsValidText.js b/function/IsValidText.js index c22c911..f574588 100644 --- a/function/IsValidText.js +++ b/function/IsValidText.js @@ -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; -}; \ No newline at end of file +}; diff --git a/function/LNBT.js b/function/LNBT.js index 6346cc3..8fe3e10 100644 --- a/function/LNBT.js +++ b/function/LNBT.js @@ -1,24 +1,24 @@ -const large_numbers = require(`../const/array/large_numbers`) +const large_numbers = require(`../const/array/large_numbers`); const repeat = require("./repeat"); -const BT = require(`./BT`) +const BT = require(`./BT`); + module.exports = (nameorpowerof10, d = `1`) => { - const tnameorpowerof10 = typeof nameorpowerof10; - switch (tnameorpowerof10) { - case `string`: - try { - if (nameorpowerof10 == `Googolplex`) { - return `Don't Try This`; - } - const v = large_numbers.find((n) => n.name === nameorpowerof10).powof10; - if (v < 0) return undefined; - return BT(d + repeat(`0`, [v])); - } catch (error) { - return undefined; - } - case `number`: - if (nameorpowerof10 < 0) return undefined; - return BT(d + repeat(`0`, [nameorpowerof10])); - default: - return undefined; + const type = typeof nameorpowerof10; + + if (type === `string`) { + if (nameorpowerof10 === `Googolplex`) { + return `Don't Try This`; + } + + const largeNumber = large_numbers.find((n) => n.name === nameorpowerof10); + if (!largeNumber || largeNumber.powof10 < 0) return undefined; + + return BT(d + repeat(`0`, [largeNumber.powof10])); } -}; \ No newline at end of file + + if (type === `number` && nameorpowerof10 >= 0) { + return BT(d + repeat(`0`, [nameorpowerof10])); + } + + return undefined; +}; diff --git a/function/MoneyLaundering.js b/function/MoneyLaundering.js index 00a9ed6..d9f7275 100644 --- a/function/MoneyLaundering.js +++ b/function/MoneyLaundering.js @@ -1,9 +1,5 @@ -const removeLeadingingZeros = require(`../snippet/removeLeadingingZeros`) +const removeLeadingingZeros = require(`../snippet/removeLeadingingZeros`); + module.exports = (money) => { - const removeComma = money.replace(/,/g, ""); - const removeCommaAndUnderScore = removeComma.replace(/_/g, ""); - const removeCommaAndUnderScoreAndLeadingingZeros = removeLeadingingZeros( - removeCommaAndUnderScore - ); - return removeCommaAndUnderScoreAndLeadingingZeros; -}; \ No newline at end of file + return removeLeadingingZeros(money.replace(/[, _]/g, "")); +}; diff --git a/function/NEG.js b/function/NEG.js index ab64b40..5195bc1 100644 --- a/function/NEG.js +++ b/function/NEG.js @@ -1,15 +1,14 @@ -const negative = require(`../const/primitive/negative`) -const BF = require(`./BF`) +const negative = require(`../const/primitive/negative`); +const BF = require(`./BF`); + module.exports = (money, ed = false, f = BF, neg = negative) => { - let retVal if ( /^\-([\d๐-๙]*)(\.\[\d๐-๙]{0,2}0*)?/.test(money) && !/^\-{2,}/.test(money) ) { money = money.replace(/^\-/, ``); - retVal = `${neg}${f(money, ed)}`; - } else { - retVal = f(money, ed); + return `${neg}${f(money, ed)}`; } - return retVal -} \ No newline at end of file + + return f(money, ed); +}; diff --git a/function/NumText.js b/function/NumText.js index 1a6a9a8..9f00515 100644 --- a/function/NumText.js +++ b/function/NumText.js @@ -1,9 +1,11 @@ const THAINUMBERWORDS = require(`../const/array/THAINUMBERWORDS`); + module.exports = (str, arr = THAINUMBERWORDS, flag = `g`) => { if (!str) return undefined; if (typeof str !== "string") return `Invalid Type`; - for (const i in arr) { - str = str.replace(new RegExp(i, flag), arr[i]); - } - return str; -}; \ No newline at end of file + + return Object.entries(arr).reduce( + (acc, [key, value]) => acc.replace(new RegExp(key, flag), value), + str + ); +}; diff --git a/function/PrintBaht.js b/function/PrintBaht.js index 5e84c90..c6b4553 100644 --- a/function/PrintBaht.js +++ b/function/PrintBaht.js @@ -1,19 +1,22 @@ -const hundredThousandToOne = require(`./hundredThousandToOne`) -const MILLION = require(`../const/primitive/MILLION`) -const BAHT = require(`../const/primitive/BAHT`) +const hundredThousandToOne = require(`./hundredThousandToOne`); +const MILLION = require(`../const/primitive/MILLION`); +const BAHT = require(`../const/primitive/BAHT`); const LeadingSpecialOneToOne = require(`../snippet/LeadingSpecialOneToOne`); const LAST6DIGITPATTERN = require(`../const/regex/LAST6DIGITPATTERN`); module.exports = (money, ed = false) => { if (!money) return ``; - let newMoney = []; - while (money != ``) { - let selectedupto6digit = money.match(LAST6DIGITPATTERN)[0]; + + const newMoney = []; + + while (money) { + const selectedupto6digit = money.match(LAST6DIGITPATTERN)[0]; newMoney.push(`${hundredThousandToOne(selectedupto6digit, ed)}${MILLION}`); money = money.replace(LAST6DIGITPATTERN, ""); } + return `${LeadingSpecialOneToOne(newMoney.reverse().join("")).replace( /ล้าน$/, `` )}${BAHT}`; -}; \ No newline at end of file +}; diff --git a/function/PrintSatangs.js b/function/PrintSatangs.js index 33cb2a2..c66070a 100644 --- a/function/PrintSatangs.js +++ b/function/PrintSatangs.js @@ -1,28 +1,30 @@ -const { - FULLBAHT, - SATANG -} = require(`../const`) +const { FULLBAHT, SATANG } = require(`../const`); const SatangFirstDigit = require(`./SatangFirstDigit`); const SatangSecondDigit = require(`./SatangSecondDigit`); -const op = require(`operation-strint`) +const op = require(`operation-strint`); module.exports = (satangs, rounding = ``) => { if (satangs.match(/^0*$/)) return [FULLBAHT, `0`]; - if ((!/^\d{0,2}$/.test(satangs) && rounding === ``) || /[^\d]/.test(satangs)) + 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) { + const therest = satangs.slice(2); + if (therest.match(/^\d*[1-9]+/) && therest.match(/^\d*$/)) { first2digit = op.sum(`1`, first2digit); } satangs = first2digit; } + if (satangs === `100`) return [FULLBAHT, `1`]; - let satangword = `${SatangFirstDigit(satangs[0])}${SatangSecondDigit( + + const satangword = `${SatangFirstDigit(satangs[0])}${SatangSecondDigit( satangs )}${SATANG}`; return [satangword, `0`]; -}; \ No newline at end of file +}; diff --git a/function/SEP.js b/function/SEP.js index b6bf6eb..1ab7b87 100644 --- a/function/SEP.js +++ b/function/SEP.js @@ -1,4 +1,4 @@ -const ABT = require(`./ABT`) +const ABT = require(`./ABT`); const ONETONINE = require(`../const/array/ONETONINE`); const REVERSETHAIDIGITWORDS = require(`../const/array/REVERSETHAIDIGITWORDS`); const MILLION = require(`../const/primitive/MILLION`); @@ -9,12 +9,16 @@ const FULLBAHT = require(`../const/primitive/FULLBAHT`); module.exports = (num, separator = `-`) => { 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}`); - } + + const replaceWithSeparator = (arr) => { + arr.forEach((i) => { + ret = ret.replace(new RegExp(i, `g`), `${i}${separator}`); + }); + }; + + replaceWithSeparator(ONETONINE); + replaceWithSeparator(REVERSETHAIDIGITWORDS.filter((x) => x !== ``)); + ret = ret .replace(new RegExp(MILLION, `g`), `${MILLION}${separator}`) .replace(new RegExp(SPECIALONE, `g`), `${SPECIALONE}${separator}`) @@ -22,5 +26,6 @@ module.exports = (num, separator = `-`) => { .replace(`${BAHT}${FULLBAHT}`, "") .replace(BAHT, `${BAHT}${separator}`) .replace(new RegExp(`${separator}$`), ``); + return ret; -}; \ No newline at end of file +}; diff --git a/function/SatangFirstDigit.js b/function/SatangFirstDigit.js index b6ff5fa..ccf66dc 100644 --- a/function/SatangFirstDigit.js +++ b/function/SatangFirstDigit.js @@ -1,11 +1,9 @@ -const { - TEN, - SPECIALTWO, - THAINUMBERWORDS -} = require(`../const`) +const { TEN, SPECIALTWO, THAINUMBERWORDS } = require(`../const`); + module.exports = (digit) => { - if (digit == 0) return ``; - if (digit == 1) return `${TEN}`; - if (digit == 2) return `${SPECIALTWO}${TEN}`; + if (digit === `0`) return ``; + if (digit === `1`) return `${TEN}`; + if (digit === `2`) return `${SPECIALTWO}${TEN}`; + return `${THAINUMBERWORDS[parseInt(digit)]}${TEN}`; -}; \ No newline at end of file +}; diff --git a/function/SatangNum.js b/function/SatangNum.js index e69a080..1e64de4 100644 --- a/function/SatangNum.js +++ b/function/SatangNum.js @@ -1,25 +1,27 @@ -const DEBUG = require(`../const/primitive/DEBUG`) -const FULLBAHT = require(`../const/primitive/FULLBAHT`) -const TEN = require(`../const/primitive/TEN`) -const OneToTenTextRegex = require(`../const/regex/OneToTenTextRegex`) -const ElevenToNineteenRegex = require(`../const/regex/ElevenToNineteenRegex`) -const TwentyToNinetyNine = require(`../const/regex/TwentyToNinetyNine`) +const FULLBAHT = require(`../const/primitive/FULLBAHT`); +const TEN = require(`../const/primitive/TEN`); +const OneToTenTextRegex = require(`../const/regex/OneToTenTextRegex`); +const ElevenToNineteenRegex = require(`../const/regex/ElevenToNineteenRegex`); +const TwentyToNinetyNine = require(`../const/regex/TwentyToNinetyNine`); const FTHAISATANGWORDS = require(`../const/array/FTHAISATANGWORDS`); const LTHAISATANGWORDS = require(`../const/array/LTHAISATANGWORDS`); const THAINUMBERWORDS = require(`../const/array/THAINUMBERWORDS`); const padWithLeadingZeros = require(`../snippet/padWithLeadingZeros`); module.exports = (moneySatang) => { - if (DEBUG) console.log(moneySatang); - if (moneySatang == FULLBAHT) { + if (moneySatang === FULLBAHT) { return `00`; - } else if (OneToTenTextRegex.test(moneySatang)) { - return `${padWithLeadingZeros(THAINUMBERWORDS.indexOf(moneySatang), 2)}`; - } else if (ElevenToNineteenRegex.test(moneySatang)) { + } + if (OneToTenTextRegex.test(moneySatang)) { + return padWithLeadingZeros(THAINUMBERWORDS.indexOf(moneySatang), 2); + } + if (ElevenToNineteenRegex.test(moneySatang)) { return `1${LTHAISATANGWORDS.indexOf(moneySatang.split(TEN).at(-1))}`; - } else if (TwentyToNinetyNine.test(moneySatang)) { + } + if (TwentyToNinetyNine.test(moneySatang)) { const [f, l] = moneySatang.split(TEN); return `${FTHAISATANGWORDS.indexOf(f)}${LTHAISATANGWORDS.indexOf(l)}`; } + return undefined; -}; \ No newline at end of file +}; diff --git a/function/SatangSecondDigit.js b/function/SatangSecondDigit.js index 708b62c..8401ed1 100644 --- a/function/SatangSecondDigit.js +++ b/function/SatangSecondDigit.js @@ -1,6 +1,8 @@ const { SPECIALONE, THAINUMBERWORDS } = require(`../const`); + module.exports = (digit) => { if (digit[1] === undefined || digit[1] === "0") return ""; if (digit[0] !== "0" && digit[1] === "1") return SPECIALONE; + return `${THAINUMBERWORDS[parseInt(digit[1])]}`; }; diff --git a/function/TB.js b/function/TB.js index 517ca84..b280320 100644 --- a/function/TB.js +++ b/function/TB.js @@ -1,4 +1,4 @@ -const SatangNum = require(`./SatangNum`) +const SatangNum = require(`./SatangNum`); const padWithLeadingZeros = require(`../snippet/padWithLeadingZeros`); const removeLeadingingZeros = require(`../snippet/removeLeadingingZeros`); const IsValidText = require(`./IsValidText`); @@ -8,44 +8,53 @@ const ZERO = require("../const/primitive/ZERO"); const MILLION = require("../const/primitive/MILLION"); const SATANG = require("../const/primitive/SATANG"); const THAINUMBERWORDS = require("../const/array/THAINUMBERWORDS"); + module.exports = (BT, error = `Invalid String`) => { if (!BT) return undefined; + if (/บาท$/.test(BT)) BT = `${BT}${FULLBAHT}`; if (!/สตางค์$/.test(BT) && !/ถ้วน$/.test(BT)) return error; + const [moneyBaht, moneySatang] = BT.split(BAHT); + if (/สตางค์$/.test(moneyBaht) && !moneySatang) { return `0.${SatangNum(moneyBaht.replace(SATANG, ``))}`; } + const retSatang = SatangNum(moneySatang.replace(SATANG, ``)); if (!retSatang) return error; + + if (!IsValidText(moneyBaht)) return error; + const moneyBahts = []; const millions = moneyBaht.split(MILLION).reverse(); - if (!IsValidText(moneyBaht)) return error; + for (const million of millions) { if (SatangNum(million)) { moneyBahts.push(padWithLeadingZeros(SatangNum(million), 6)); continue; } + const THUNDREDTHOUSAND = - // /(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?แสน/.exec(million)?.at(1) || million.match(/(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?แสน/)?.at(1) || ZERO; const VHUNDREDTHOUSAND = THAINUMBERWORDS.indexOf(THUNDREDTHOUSAND); + const TTENTHOUSAND = - // /(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?หมื่น/.exec(million)?.at(1) || million.match(/(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?หมื่น/)?.at(1) || ZERO; const VTENTHOUSAND = THAINUMBERWORDS.indexOf(TTENTHOUSAND); + const TTHOUSAND = - // /(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?พัน/.exec(million)?.at(1) || million.match(/(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?พัน/)?.at(1) || ZERO; const VTHOUSAND = THAINUMBERWORDS.indexOf(TTHOUSAND); + const THUNDRED = - // /(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?ร้อย/.exec(million)?.at(1) || million.match(/(หนึ่ง|สอง|สาม|สี่|ห้า|หก|เจ็ด|แปด|เก้า)?ร้อย/)?.at(1) || ZERO; const VHUNDRED = THAINUMBERWORDS.indexOf(THUNDRED); + const VL = SatangNum( million @@ -54,6 +63,7 @@ module.exports = (BT, error = `Invalid String`) => { .replace(/.+พัน/, ``) .replace(/.+ร้อย/, ``) ) || `00`; + moneyBahts.push( padWithLeadingZeros( `${VHUNDREDTHOUSAND}${VTENTHOUSAND}${VTHOUSAND}${VHUNDRED}${VL}`, @@ -61,7 +71,8 @@ module.exports = (BT, error = `Invalid String`) => { ) ); } + return `${removeLeadingingZeros(moneyBahts.reverse().join(""))}.${SatangNum( moneySatang.replace(SATANG, ``) )}`; -}; \ No newline at end of file +}; diff --git a/function/hundredThousandToOne.js b/function/hundredThousandToOne.js index 445dfc9..e991881 100644 --- a/function/hundredThousandToOne.js +++ b/function/hundredThousandToOne.js @@ -1,32 +1,38 @@ const { - REVERSETHAIDIGITWORDS - ,THAINUMBERWORDS - ,SPECIALONE - ,SPECIALTWO - ,TEN -} = require(`../const`) + REVERSETHAIDIGITWORDS, + THAINUMBERWORDS, + SPECIALONE, + SPECIALTWO, + TEN, +} = require(`../const`); + +const padWithLeadingZeros = require(`../snippet/padWithLeadingZeros`); -const padWithLeadingZeros = require(`../snippet/padWithLeadingZeros`) module.exports = (digits, ed = false) => { let word = ``; - let c = 0; const digitspadWithLeadingZeros = padWithLeadingZeros(digits, 6); - for (let digit of digitspadWithLeadingZeros) { + + digitspadWithLeadingZeros.split("").forEach((digit, index) => { digit = parseInt(digit); - if (!(digit === 0)) { - if (c == 4 && digit == 2) { - word += `${SPECIALTWO}${TEN}`; - } else if (c == 4 && digit == 1) { - word += TEN; - } else if (c == 5 && digit == 1 && ed) { - word += SPECIALONE; - } else if (c == 5 && digit == 1 && digitspadWithLeadingZeros[4] != 0) { - word += SPECIALONE; + if (digit !== 0) { + if (index === 4) { + word += + digit === 2 + ? `${SPECIALTWO}${TEN}` + : digit === 1 + ? TEN + : `${THAINUMBERWORDS[digit]}${REVERSETHAIDIGITWORDS[index]}`; + } else if (index === 5) { + if (digit === 1 && (ed || digitspadWithLeadingZeros[4] !== "0")) { + word += SPECIALONE; + } else { + word += `${THAINUMBERWORDS[digit]}${REVERSETHAIDIGITWORDS[index]}`; + } } else { - word += `${THAINUMBERWORDS[digit]}${REVERSETHAIDIGITWORDS[c]}`; + word += `${THAINUMBERWORDS[digit]}${REVERSETHAIDIGITWORDS[index]}`; } } - c++; - } + }); + return word; -}; +}; \ No newline at end of file diff --git a/function/repeat.js b/function/repeat.js index c690627..f3709da 100644 --- a/function/repeat.js +++ b/function/repeat.js @@ -1,6 +1 @@ -module.exports = (str, x) => { - for (const i of x) { - str = `${str}`.repeat(i); - } - return str; -}; \ No newline at end of file +module.exports = (str, x) => x.reduce((acc, i) => acc.repeat(i), `${str}`); diff --git a/function/splitIntFrac.js b/function/splitIntFrac.js index d44ed7a..df20398 100644 --- a/function/splitIntFrac.js +++ b/function/splitIntFrac.js @@ -1,8 +1,9 @@ module.exports = (money) => { const match = money.match(/(\d*)(\.\d+)?/); + if (!match) return [money, "", ""]; // Handle case where match fails + let [moneyFull, moneyInt, moneyFrac] = match; - moneyFrac === undefined - ? (moneyFrac = "") - : (moneyFrac = moneyFrac.replace(/^\./, "")); + moneyFrac = moneyFrac ? moneyFrac.replace(/^\./, "") : ""; + return [moneyFull, moneyInt, moneyFrac]; -}; \ No newline at end of file +}; diff --git a/hexadecimal.js b/hexadecimal.js index 9584f17..9726832 100644 --- a/hexadecimal.js +++ b/hexadecimal.js @@ -9,45 +9,28 @@ const isHex = (money) => { }; const toDec = (atof) => { - let retVal = atof; - switch (atof.toLowerCase()) { - case 'a': - retVal = `10` - break; - case 'b': - retVal = `11` - break; - case 'c': - retVal = `12` - break; - case 'd': - retVal = `13` - break; - case 'e': - retVal = `14` - break; - case 'f': - retVal = `15` - break; - } - return retVal -} + const hexToDecMap = { + a: "10", + b: "11", + c: "12", + d: "13", + e: "14", + f: "15", + }; + return hexToDecMap[atof.toLowerCase()] || atof; +}; const toHex = (num) => { - let val = `0`; if (!isHex(num)) return num; num = num.replace(/^0x/i, ``); - let pos = -1; - for (let i of num.split("").reverse()) { - let thispos_val = op.multiply( - op.pow(`16`, `${pos + 1}`), - toDec(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(`16`, `${index}`), toDec(digit)); + return op.sum(acc, thispos_val) || "0"; + }, `0`); }; module.exports = { diff --git a/octal.js b/octal.js index ce92fd6..df781db 100644 --- a/octal.js +++ b/octal.js @@ -1,31 +1,33 @@ -const {octalRegex1, octalRegex2} = require('./const') -const op = require(`operation-strint`) +const { octalRegex1, octalRegex2 } = require("./const"); +const op = require(`operation-strint`); + const isOctal = (money) => { - if (typeof(money) !== `string`) return undefined; - if (/__/i.test(money)) return false; - if (/^0o.+/i.test(money)) { - money = money.replace(/(?<=[0-7])_(?=[0-7])/g, ""); - } - else if (/_/i.test(money)) { - return false; - } - return octalRegex1.test(money) || octalRegex2.test(money); -} + if (typeof money !== `string`) return undefined; + if (/__/i.test(money)) return false; + + if (/^0o.+/i.test(money)) { + money = money.replace(/(?<=[0-7])_(?=[0-7])/g, ""); + } else if (/_/i.test(money)) { + return false; + } + + 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 -} + if (!isOctal(num)) return num; + num = num.replace(/^0+o?/, ``); + + return num + .split("") + .reverse() + .reduce((acc, digit, index) => { + const thispos_val = op.multiply(op.pow(`8`, `${index}`), digit); + return op.sum(acc, thispos_val) || "0"; + }, "0"); +}; module.exports = { isOctal, toDec, -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 8fcd3bf..bbe2a58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bahtrext", - "version": "1.6.1", + "version": "1.6.2", "description": "BahtText Stringify", "main": "index.js", "scripts": {