-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Route Builder rules should be case insensitive (#9040)
* override jsonLogic operators on string operands to allow for case insensitive comparisons. Affected Operators: "==", "===", "!=", "!==", "in" * disable no-explicit-any on jsonLogicOverrides file since most of the code there will be from jsonLogic and may not meet our coding style. Majority of overrides require us to copy over functions and their signatures from jsonLogic and then modify their implementation. The signature of functions implementing most operators take the operands typed as "any", which is intended, but doesn't adhere to our coding style. Hence the need to override the eslint rule * run linter to fix issues * Fix bug in in operator when second arg is an array * remove redundant indexOf check on overriden jsonLogic "in" operator. Note: this deviates from the original implementation in the jsonLogic library because our current useage ensures that the second operand is always a string or string[] and will therefore always have .index function. Whenever our invariants change in the future, make sure to modify this implementation to prevent any unexpected --------- Co-authored-by: Hariom Balhara <[email protected]>
- Loading branch information
1 parent
5534153
commit 15e50fc
Showing
3 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/app-store/routing-forms/lib/jsonLogicOverrides.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import jsonLogic from "json-logic-js"; | ||
|
||
// converts input to lowercase if string | ||
function normalize<T extends string | string[]>(input: T): T { | ||
if (typeof input === "string") { | ||
return input.toLowerCase() as T; | ||
} | ||
if (input instanceof Array) { | ||
return input.map((item) => { | ||
if (typeof item === "string") { | ||
return item.toLowerCase(); | ||
} | ||
// if array item is not a string, return it as is | ||
return item; | ||
}) as T; | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Single Select equals and not equals uses it | ||
* Short Text equals and not equals uses it | ||
*/ | ||
jsonLogic.add_operation("==", function (a: any, b: any) { | ||
return normalize(a) == normalize(b); | ||
}); | ||
|
||
jsonLogic.add_operation("===", function (a: any, b: any) { | ||
return normalize(a) === normalize(b); | ||
}); | ||
|
||
jsonLogic.add_operation("!==", function (a: any, b: any) { | ||
return normalize(a) !== normalize(b); | ||
}); | ||
|
||
jsonLogic.add_operation("!=", function (a: any, b: any) { | ||
return normalize(a) != normalize(b); | ||
}); | ||
|
||
/** | ||
* Multiselect "equals" and "not equals" uses it | ||
* Singleselect "any in" and "not in" uses it | ||
* Long Text/Short Text/Email/Phone "contains" also uses it. | ||
*/ | ||
jsonLogic.add_operation("in", function (a: string, b: string | string[]) { | ||
const first = normalize(a); | ||
const second = normalize(b); | ||
if (!second) return false; | ||
return second.indexOf(first) !== -1; | ||
}); | ||
|
||
export default jsonLogic; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15e50fc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ui – ./apps/storybook
ui-git-main-cal.vercel.app
ui-cal.vercel.app
ui.cal.com
www.timelessui.com
timelessui.com
cal-com-storybook.vercel.app