Skip to content

Commit

Permalink
Merge pull request #374 from Martinarbez/feat/add.ilike.operator
Browse files Browse the repository at this point in the history
feat/add.ilike.operator
  • Loading branch information
joelalejandro authored Dec 26, 2023
2 parents cbd5337 + 0533585 commit b62522b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/utils/operators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type OperatorName = "eq" | "ne" | "lt" | "gt" | "le" | "ge" | "like" | "nlike" | "in" | "nin";
export type OperatorName = "eq" | "ne" | "lt" | "gt" | "le" | "ge" | "like" | "nlike" | "ilike"| "in" | "nin";

export const KnexOperators = {
eq: "=",
Expand All @@ -9,6 +9,7 @@ export const KnexOperators = {
ge: ">=",
like: "like",
nlike: "not like",
ilike: "ilike",
in: "in",
nin: "not in",
};
Expand Down Expand Up @@ -50,6 +51,21 @@ export const FunctionalOperators: { [T in OperatorName]: (actual: any, expected:

return false;
},
ilike: (actual: string, expected: string) => {
if (expected.startsWith("%") && expected.endsWith("%")) {
return actual.includes(expected.replace(/%/g, ""));
}

if (expected.startsWith("%")) {
return actual.endsWith(expected.replace(/%/g, ""));
}

if (expected.endsWith("%")) {
return actual.startsWith(expected.replace(/%/g, ""));
}

return false;
},
in: <T = any>(actual: T, expected: T[]) => expected.includes(actual),
nin: <T = any>(actual: T, expected: T[]) => !expected.includes(actual),
};

0 comments on commit b62522b

Please sign in to comment.