Skip to content

Commit

Permalink
fix: parsing of inequality operators in formulas (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson authored Jul 26, 2024
1 parent 9fcc6b6 commit 9710192
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 14 additions & 1 deletion v3/src/models/formula/utils/canonicalization-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})

Expand Down
4 changes: 3 additions & 1 deletion v3/src/models/formula/utils/canonicalization-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(?<!!)=(?!=)/g, "==")
// Regular expression developed with the help of ChatGPT.
// Matches `=` when not preceded by '<', '>', '!', or '=` and not followed by `=`, preserving white space.
return formula.replace(/(?<!<|>|!|=)(\s*)=(\s*)(?!=)/g, "$1==$2")
}

export const preprocessDisplayFormula = (formula: string) => customizeDisplayFormula(makeDisplayNamesSafe(formula))
Expand Down

0 comments on commit 9710192

Please sign in to comment.