From eb61116a9a0f24c8fe778c9c83df70136d0ffce1 Mon Sep 17 00:00:00 2001 From: Callum Loh Date: Tue, 18 Jun 2024 13:34:53 +0100 Subject: [PATCH] fix: handle empty strings correctly (dont force them to be ints) --- src/processConditionals.ts | 13 ++++++++++--- test/esi.spec.ts | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/processConditionals.ts b/src/processConditionals.ts index a6a77a4..f22e969 100644 --- a/src/processConditionals.ts +++ b/src/processConditionals.ts @@ -50,7 +50,7 @@ export async function process( // eslint-disable-next-line no-inner-declarations, jsdoc/require-jsdoc async function processWhen(match: RegExpMatchArray) { const condition = match[1]; - const conditionValidated = await _esi_evaluate_condition( + const conditionValidated = _esi_evaluate_condition( esiData, condition, ); @@ -127,7 +127,7 @@ function esi_eval_var_in_when_tag( const varInTag = esi_eval_var(eventData, match); // we have to check varInTag is *actually* a number and doesn't just have leading numbers in it if (strIsNumber(varInTag)) { - return parseInt(varInTag, 10).toString(); + return varInTag; } else { // Change to replaceAll once upgraded node return "'" + varInTag.replace(/'/g, "\\'") + "'"; @@ -141,7 +141,14 @@ function esi_eval_var_in_when_tag( * @returns {boolean} check result */ function strIsNumber(str: string): boolean { - return !isNaN(Number(str)); + const int = parseInt(str, 10); + // definitely not an int + if (!int) { + return false; + } + + // double check we have correctly converted it to an int and not been tricked by parseInt + return int.toString() === str; } /** diff --git a/test/esi.spec.ts b/test/esi.spec.ts index 29a92b3..ab69510 100644 --- a/test/esi.spec.ts +++ b/test/esi.spec.ts @@ -1919,6 +1919,7 @@ describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () = { arg: "1719,1918", result: "third-lineage" }, { arg: "1719,1922", result: "forth-lineage" }, { arg: "1719,1926", result: "fith-lineage" }, + { arg: "1719,2000", result: "sixth-lineage" }, ]; const url = `/esi/test-51`; @@ -1932,6 +1933,7 @@ describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () = third-lineage forth-lineage fith-lineage + sixth-lineage `); }); const res = await makeRequest(`${url}?esi_lineage=${check.arg}`); @@ -1941,3 +1943,23 @@ describe("TEST 51: ESI Args that lead with ints shouldn't convert to ints", () = }); }); }); + +test("TEST 52: Empty variables can be compared", async () => { + const url = `/esi/test-52?x=`; + routeHandler.add(url, function (req, res) { + res.writeHead(200, esiHead); + res.end(` + x empty; + x not empty; + x neither empty nor not empty; + + y empty; + y not empty; + y neither empty nor not empty; +`); + }); + const res = await makeRequest(url); + expect(res.ok).toBeTruthy(); + expect(checkSurrogate(res)).toBeTruthy(); + expect(await res.text()).toEqual(`x empty;y empty;`); +});