export type OperatorName = "eq" | "ne" | "lt" | "gt" | "le" | "ge" | "like" | "nlike" | "ilike"| "in" | "nin"; export const KnexOperators = { eq: "=", ne: "!=", lt: "<", gt: ">", le: "<=", ge: ">=", like: "like", nlike: "not like", ilike: "ilike", in: "in", nin: "not in", }; export const FunctionalOperators: { [T in OperatorName]: (actual: any, expected: any) => boolean } = { eq: (actual: T, expected: T) => actual === expected, ne: (actual: T, expected: T) => actual !== expected, lt: (actual: number, expected: number) => actual < expected, gt: (actual: number, expected: number) => actual > expected, le: (actual: number, expected: number) => actual <= expected, ge: (actual: number, expected: number) => actual >= expected, like: (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; }, nlike: (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; }, 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: (actual: T, expected: T[]) => expected.includes(actual), nin: (actual: T, expected: T[]) => !expected.includes(actual), };