Skip to content

Commit

Permalink
fix: handle int conditions better
Browse files Browse the repository at this point in the history
at present this whole conditional parser needs a rewrite
however dont have enough time to do that right now
short of doing a full rewrite and redoing the regex parsing this will do for now
  • Loading branch information
cdloh committed Jun 18, 2024
1 parent eb61116 commit 0cd89fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/processConditionals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ function strIsNumber(str: string): boolean {
return int.toString() === str;
}

/**
* Converts a string to a number if it is
* passes that to the tester and returns the result
*
* @param {string} str conditional string to split
* @returns {string | number} condition result
*/
function strToNumOrStr(str: string): string | number {
// we have to check varInTag is *actually* a number and doesn't just have leading numbers in it
if (strIsNumber(str)) {
return parseInt(str, 10);
}
return str;
}

/**
* Takes a condition string and splits it into its two sides and operator
* passes that to the tester and returns the result
Expand All @@ -173,9 +188,9 @@ function _esi_condition_lexer(condition: string): boolean {
return false;
}

const left = tokensSplit[1] || tokensSplit[2];
const left = strToNumOrStr(tokensSplit[1] || tokensSplit[2]);
const op = op_replacements[tokensSplit[3]] || tokensSplit[3];
const right = tokensSplit[4] || tokensSplit[5];
const right = strToNumOrStr(tokensSplit[4] || tokensSplit[5]);
return esiConditionTester(left, right, op);
}

Expand All @@ -189,8 +204,8 @@ function _esi_condition_lexer(condition: string): boolean {
* @returns {boolean} condition result
*/
function esiConditionTester(
left: string,
right: string,
left: string | number,
right: string | number,
operator: string,
): boolean {
switch (operator) {
Expand All @@ -208,6 +223,9 @@ function esiConditionTester(
case ">":
return left > right;
case "=~": {
left = left.toString();
right = right.toString();

const regex = right.match(regexExtractor);
if (!regex) return false;
// Bloody javascript!
Expand Down
16 changes: 16 additions & 0 deletions test/esi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,12 @@ test("TEST 17: choose - when - test, conditional syntax", async () => {
"(1 > 2) | (3.02 > 2.4124 & 1 <= 1)",
"(1>2)||(3>2&&2>1)",
"! (1 < 2) | (3 > 2 & 2 >= 1)",
"60 > 100",
"60 < 100",
"60 >= 100",
"60 <= 100",
"100 >= 100",
"100 <= 100",
"'hello' == 'hello'",
"'hello' != 'goodbye'",
"'repeat' != 'function'", // use of lua words in strings
Expand Down Expand Up @@ -943,6 +949,12 @@ test("TEST 17: choose - when - test, conditional syntax", async () => {
(1 > 2) | (3.02 > 2.4124 & 1 <= 1)
(1>2)||(3>2&&2>1)
! (1 < 2) | (3 > 2 & 2 >= 1)
Failed
60 < 100
Failed
60 <= 100
100 >= 100
100 <= 100
'hello' == 'hello'
'hello' != 'goodbye'
'repeat' != 'function'
Expand Down Expand Up @@ -1920,6 +1932,8 @@ describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () =
{ arg: "1719,1922", result: "forth-lineage" },
{ arg: "1719,1926", result: "fith-lineage" },
{ arg: "1719,2000", result: "sixth-lineage" },
{ arg: "2000", result: "seventh-lineage" },
{ arg: "2001", result: "eighth-lineage" },
];

const url = `/esi/test-51`;
Expand All @@ -1934,6 +1948,8 @@ describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () =
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1922)(?:,|$)/'">forth-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) =~ '/(?:^|,)(1926)(?:,|$)/'">fith-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) == '1719,2000'">sixth-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) == '2000'">seventh-lineage</esi:when>
<esi:when test="$(ESI_ARGS{lineage}) >= '800'">eighth-lineage</esi:when>
</esi:choose>`);
});
const res = await makeRequest(`${url}?esi_lineage=${check.arg}`);
Expand Down

0 comments on commit 0cd89fe

Please sign in to comment.