From 97101929ce6b1b79f2b7185199d41405ac305cfb Mon Sep 17 00:00:00 2001 From: Kirk Swenson Date: Thu, 25 Jul 2024 19:24:54 -0700 Subject: [PATCH] fix: parsing of inequality operators in formulas (#1377) --- .../formula/utils/canonicalization-utils.test.ts | 15 ++++++++++++++- .../formula/utils/canonicalization-utils.ts | 4 +++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/v3/src/models/formula/utils/canonicalization-utils.test.ts b/v3/src/models/formula/utils/canonicalization-utils.test.ts index ea9b6bd789..03aebb8ca7 100644 --- a/v3/src/models/formula/utils/canonicalization-utils.test.ts +++ b/v3/src/models/formula/utils/canonicalization-utils.test.ts @@ -77,10 +77,23 @@ describe("customizeDisplayFormula", () => { expect(customizeDisplayFormula("a = b")).toEqual("a == b") expect(customizeDisplayFormula("a = b = c")).toEqual("a == b == c") }) - it("doesn't replace unequality operator", () => { + it("doesn't replace double equality operator", () => { + expect(customizeDisplayFormula("a == 1")).toEqual("a == 1") + expect(customizeDisplayFormula("a == b")).toEqual("a == b") + expect(customizeDisplayFormula("a == b = c = d == e")).toEqual("a == b == c == d == e") + }) + it("doesn't replace inequality operators", () => { expect(customizeDisplayFormula("a != 1")).toEqual("a != 1") expect(customizeDisplayFormula("a != b")).toEqual("a != b") expect(customizeDisplayFormula("a != b = c = d != e")).toEqual("a != b == c == d != e") + + expect(customizeDisplayFormula("a <= 1")).toEqual("a <= 1") + expect(customizeDisplayFormula("a <= b")).toEqual("a <= b") + expect(customizeDisplayFormula("a <= b = c = d <= e")).toEqual("a <= b == c == d <= e") + + expect(customizeDisplayFormula("a >= 1")).toEqual("a >= 1") + expect(customizeDisplayFormula("a >= b")).toEqual("a >= b") + expect(customizeDisplayFormula("a >= b = c = d >= e")).toEqual("a >= b == c == d >= e") }) }) diff --git a/v3/src/models/formula/utils/canonicalization-utils.ts b/v3/src/models/formula/utils/canonicalization-utils.ts index e3d2fe0a35..3c00bf73b3 100644 --- a/v3/src/models/formula/utils/canonicalization-utils.ts +++ b/v3/src/models/formula/utils/canonicalization-utils.ts @@ -31,7 +31,9 @@ export const makeDisplayNamesSafe = (formula: string) => { export const customizeDisplayFormula = (formula: string) => { // Over time, this function might grow significantly and require more advanced parsing of the formula. // Replace all the assignment operators with equality operators, as CODAP v2 uses a single "=" for equality check. - return formula.replace(/(?', '!', or '=` and not followed by `=`, preserving white space. + return formula.replace(/(?|!|=)(\s*)=(\s*)(?!=)/g, "$1==$2") } export const preprocessDisplayFormula = (formula: string) => customizeDisplayFormula(makeDisplayNamesSafe(formula))