Skip to content

Commit

Permalink
add IsValidText
Browse files Browse the repository at this point in the history
  • Loading branch information
PingHuskar committed May 23, 2024
1 parent 7230540 commit 6daeb33
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
lib
.DS_Store
.DS_Store
playground.js
121 changes: 85 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ const THAINUMBERWORDS = [
NINE,
TEN,
];
const ONETONINE = [
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
];
const LTHAISATANGWORDS = [
``,
SPECIALONE,
Expand Down Expand Up @@ -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 &&
Expand All @@ -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;
Expand Down Expand Up @@ -557,5 +598,13 @@ const OB = (money) => {
LNBT,
LeadingSpecialOneToOne,
OB,
ONETONINE,
million: MILLION,
HUNDREDTHOUSAND,
TENTHOUSAND,
THOUSAND,
HUNDRED,
TEN,
IsValidText,
};
// }
20 changes: 19 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,22 @@ test(`OB`, () => {
typ: "string",
val: "s6d7f6d7f6",
});
})
})

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);
});
77 changes: 16 additions & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "1.2.3",
"version": "1.3.0",
"description": "Better BahtText",
"main": "index.js",
"scripts": {
Expand All @@ -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"
Expand Down

0 comments on commit 6daeb33

Please sign in to comment.