From 0cd89feee6f1f0ab4ef3ab45918803fd8188c791 Mon Sep 17 00:00:00 2001 From: Callum Loh Date: Tue, 18 Jun 2024 15:56:01 +0100 Subject: [PATCH] fix: handle int conditions better 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 --- src/processConditionals.ts | 26 ++++++++++++++++++++++---- test/esi.spec.ts | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/processConditionals.ts b/src/processConditionals.ts index f22e969..eae8756 100644 --- a/src/processConditionals.ts +++ b/src/processConditionals.ts @@ -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 @@ -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); } @@ -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) { @@ -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! diff --git a/test/esi.spec.ts b/test/esi.spec.ts index ab69510..7104b5b 100644 --- a/test/esi.spec.ts +++ b/test/esi.spec.ts @@ -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 @@ -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' @@ -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`; @@ -1934,6 +1948,8 @@ describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () = forth-lineage fith-lineage sixth-lineage + seventh-lineage + = '800'">eighth-lineage `); }); const res = await makeRequest(`${url}?esi_lineage=${check.arg}`);