diff --git a/.gitignore b/.gitignore index badb39e..62c2d4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules dist lib -.DS_Store \ No newline at end of file +.DS_Store +playground.js \ No newline at end of file diff --git a/index.js b/index.js index b477b27..3ca940e 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,17 @@ const THAINUMBERWORDS = [ NINE, TEN, ]; +const ONETONINE = [ + ONE, + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, +]; const LTHAISATANGWORDS = [ ``, SPECIALONE, @@ -294,43 +305,30 @@ const SatangNum = (moneySatang) => { return undefined; }; -const TB = (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 (DEBUG) console.log(moneyBaht, moneySatang); - if (/สตางค์$/.test(moneyBaht) && !moneySatang) { - return `0.${SatangNum(moneyBaht.replace(SATANG, ``))}`; - } - const retSatang = SatangNum(moneySatang.replace(SATANG, ``)); - if (!retSatang) return error; - const moneyBahts = []; - const millions = moneyBaht.split(MILLION).reverse(); - for (const million of millions) { - if (/สองสิบ/.test(million)) return error - if (/สิบหนึ่ง/.test(million)) return error - if (SatangNum(million)) { - moneyBahts.push(padWithLeadingZeros(SatangNum(million), 6)); - continue; +const IsValidText = (text) => { + if (typeof(text) !== `string`) return false + if (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; + for (const REVERSETHAIDIGITWORD of REVERSETHAIDIGITWORDS.slice(0, -1)) { + if ( + (sixdigitsword.match(new RegExp(REVERSETHAIDIGITWORD, "g"))?.length || + 0) > 1 + ) + return false; } - const iHUNDREDTHOUSAND = million.indexOf(HUNDREDTHOUSAND); - const iTENTHOUSAND = million.indexOf(TENTHOUSAND); - const iTHOUSAND = million.indexOf(THOUSAND); - const iHUNDRED = million.indexOf(HUNDRED); - const iTEN = million.indexOf(TEN); + 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; - if (DEBUG) { - console.log(iiTEN); - console.log(iiHUNDRED); - console.log(iiTHOUSAND); - console.log(iiTENTHOUSAND); - console.log(iiHUNDREDTHOUSAND); - } if ( !( ((iiTEN >= iiHUNDRED && @@ -342,13 +340,56 @@ const TB = (BT, error = `Invalid String`) => { iiHUNDRED >= iiTENTHOUSAND && iiHUNDRED >= iiHUNDREDTHOUSAND) || iiHUNDRED == 0) && - ((iiTHOUSAND >= iiTENTHOUSAND && - iiTHOUSAND >= iiHUNDREDTHOUSAND) || - iiTHOUSAND == 0) && + ((iiTHOUSAND >= iiTENTHOUSAND && iiTHOUSAND >= iiHUNDREDTHOUSAND) || + iiTHOUSAND == 0) && (iiTENTHOUSAND >= iiHUNDREDTHOUSAND || iiTENTHOUSAND == 0) ) - ) - return error; + ) { + 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) { + console.log(`line 20`); + return false; + } + continue; + } else if (eachdigits.at(i) === SPECIALTWO) { + if (sixdigitsword.indexOf(`ยี่สิบ`) === -1) { + return false; + } + continue; + } else { + return false; + } + } + } + } + return true; +}; + +const TB = (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 (DEBUG) console.log(moneyBaht, moneySatang); + if (/สตางค์$/.test(moneyBaht) && !moneySatang) { + return `0.${SatangNum(moneyBaht.replace(SATANG, ``))}`; + } + const retSatang = SatangNum(moneySatang.replace(SATANG, ``)); + if (!retSatang) 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) || ZERO; @@ -557,5 +598,13 @@ const OB = (money) => { LNBT, LeadingSpecialOneToOne, OB, + ONETONINE, + million: MILLION, + HUNDREDTHOUSAND, + TENTHOUSAND, + THOUSAND, + HUNDRED, + TEN, + IsValidText, }; // } \ No newline at end of file diff --git a/index.test.js b/index.test.js index 9235276..196317e 100644 --- a/index.test.js +++ b/index.test.js @@ -282,4 +282,22 @@ test(`OB`, () => { typ: "string", val: "s6d7f6d7f6", }); -}) \ No newline at end of file +}) + +test(`IsValidText`, () => { + expect(BR.IsValidText("สามแสนสองหมื่นห้าสิบเอ็ดล้านสองหมื่นสิบล้านสองสิบล้านหนึ่ง")).toBe(false); + expect(BR.IsValidText("สามแสนสองหมื่นห้าสิบเอ็ดล้านสองหมื่นสิบล้านยี่สิบล้านหนึ่ง")).toBe(true); + expect(BR.IsValidText("สามแสนสองหมื่นห้าสิบเอ็ดล้านสองหมื่นสิบล้านยี่สิบล้านหนึ่งล้าน")).toBe(true); + expect(BR.IsValidText("สามแสนสองหมื่นห้าสิบเอ็ด@ล้านสองหมื่นสิบล้านยี่สิบล้านหนึ่งล้าน")).toBe(false); + expect(BR.IsValidText("สองล้าน")).toBe(true); + expect(BR.IsValidText("ล้าน")).toBe(false); + expect(BR.IsValidText("ล้านล้าน")).toBe(false); + expect(BR.IsValidText("ล้านล้านล้าน")).toBe(false); + expect(BR.IsValidText("asdf")).toBe(false); + expect(BR.IsValidText("123")).toBe(false); + expect(BR.IsValidText("")).toBe(false); + expect(BR.IsValidText(undefined)).toBe(false); + expect(BR.IsValidText(null)).toBe(false); + expect(BR.IsValidText(0)).toBe(false); + expect(BR.IsValidText(123)).toBe(false); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1982346..967f564 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "bahtrext", - "version": "1.2.1", + "version": "1.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "bahtrext", - "version": "1.2.1", + "version": "1.3.0", "license": "ISC", "dependencies": { "thai-baht-text": "^1.0.8" @@ -15,7 +15,7 @@ "bahttext": "^2.3.0", "jest": "^29.7.0", "numbaht": "^0.1.1", - "operation-strint": "^1.0.3" + "operation-strint": "^1.0.8" } }, "node_modules/@ampproject/remapping": { @@ -1889,12 +1889,6 @@ "integrity": "sha512-zz06S8t0ozoDXMG+ube26zeCTNXcKIPJZJi8hBrF4idCLms4CG9QtK7qBl1boi5ODzFpjswb5JPmHCbMpjaYzg==", "dev": true }, - "node_modules/is-buffer": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", - "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==", - "dev": true - }, "node_modules/is-core-module": { "version": "2.13.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", @@ -1907,18 +1901,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-even": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-even/-/is-even-1.0.0.tgz", - "integrity": "sha512-LEhnkAdJqic4Dbqn58A0y52IXoHWlsueqQkKfMfdEnIYG8A1sm/GHidKkS6yvXlMoRrkM34csHnXQtOqcb+Jzg==", - "dev": true, - "dependencies": { - "is-odd": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/is-fullwidth-code-point": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", @@ -1946,30 +1928,6 @@ "node": ">=0.12.0" } }, - "node_modules/is-odd": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/is-odd/-/is-odd-0.1.2.tgz", - "integrity": "sha512-Ri7C2K7o5IrUU9UEI8losXJCCD/UtsaIrkR5sxIcFg4xQ9cRJXlWA5DQvTE0yDc0krvSNLsRGXN11UPS6KyfBw==", - "dev": true, - "dependencies": { - "is-number": "^3.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/is-odd/node_modules/is-number": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz", - "integrity": "sha512-4cboCqIpliH+mAvFNegjZQ4kgKc3ZUhQVr3HvWbSh5q3WH2v82ct+T2Y1hdU5Gdtorx/cLifQjqCbL7bpznLTg==", - "dev": true, - "dependencies": { - "kind-of": "^3.0.2" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/is-stream": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", @@ -1982,6 +1940,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/isdivisiblebyx": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/isdivisiblebyx/-/isdivisiblebyx-1.0.2.tgz", + "integrity": "sha512-x2mdQs43gHs0RY9QYLxcAe8XiA0oL9kwMEI+nolPzchtGhhls9L/NhDFnM5J0AiIZOWVDsVuIE7q3wjf9Em3SQ==", + "dev": true, + "dependencies": { + "operation-strint": "^1.0.3" + } + }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -2724,18 +2691,6 @@ "node": ">=6" } }, - "node_modules/kind-of": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", - "integrity": "sha512-NOW9QQXMoZGg/oqnVNoNTTIFEIid1627WCffUBJEdMxYApq7mNE7CpzucIPc+ZQg25Phej7IJSmX3hO+oblOtQ==", - "dev": true, - "dependencies": { - "is-buffer": "^1.1.5" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -2954,12 +2909,12 @@ } }, "node_modules/operation-strint": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/operation-strint/-/operation-strint-1.0.3.tgz", - "integrity": "sha512-qQmd26k/i7EBfKfYF1hX+dy2cAsmKF8F90DPg3I6f+ITmo6EK68rIedRa70cmZ195WLLrBtZZhzm1hqcYAHxIw==", + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/operation-strint/-/operation-strint-1.0.8.tgz", + "integrity": "sha512-TvXxvD/zMMbeko0R0o7VqueC2m4Sb03KCDN0TeGJUwUhPAZ+/TymhwbO5rpXwRPldc/FF6xsg6WntbTwdzc4mg==", "dev": true, "dependencies": { - "is-even": "^1.0.0" + "isdivisiblebyx": "^1.0.2" } }, "node_modules/p-limit": { diff --git a/package.json b/package.json index 9aaf7ea..30f86c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bahtrext", - "version": "1.2.3", + "version": "1.3.0", "description": "Better BahtText", "main": "index.js", "scripts": { @@ -22,7 +22,7 @@ "bahttext": "^2.3.0", "jest": "^29.7.0", "numbaht": "^0.1.1", - "operation-strint": "^1.0.3" + "operation-strint": "^1.0.8" }, "dependencies": { "thai-baht-text": "^1.0.8"