55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
// -----------------------------------------------------
|
|
// UTILITIES
|
|
// -----------------------------------------------------
|
|
|
|
export const extractOperator = (val?: string | null): string | null => {
|
|
if (!val) return null;
|
|
|
|
const match = val.match(/^(==|!=|>=|<=|LIKE|>|<)/);
|
|
return match ? match[0] : null;
|
|
};
|
|
|
|
export const formatWithOperator = (operator: string, value: string) =>
|
|
`${operator}/${value}`;
|
|
|
|
export const normalizeValue = (input: any): string => {
|
|
if (input == null) return "";
|
|
if (typeof input === "string" || typeof input === "number")
|
|
return String(input);
|
|
|
|
if (input?.value) return String(input.value);
|
|
if (input?.id) return String(input.id);
|
|
|
|
if (Array.isArray(input)) {
|
|
return input.map(normalizeValue).join(",");
|
|
}
|
|
|
|
return "";
|
|
};
|
|
|
|
export const encodeFilter = (fullValue: string): string => {
|
|
// Split ONLY on the first slash
|
|
const index = fullValue.indexOf("/");
|
|
if (index === -1) return fullValue;
|
|
|
|
const operator = fullValue.slice(0, index);
|
|
const rawValue = fullValue.slice(index + 1);
|
|
|
|
return `${operator}/${encodeURIComponent(rawValue)}`;
|
|
};
|
|
|
|
export const decodeFilter = (encoded: string): string => {
|
|
const [operator, encodedValue] = encoded.split("/");
|
|
return `${operator}/${decodeURIComponent(encodedValue)}`;
|
|
};
|
|
|
|
// Default operator based on field and type
|
|
export const defaultOperatorForField = (
|
|
field: string,
|
|
type: string
|
|
): string => {
|
|
if (field === "Amount") return ">="; // numeric field
|
|
if (type === "text") return "LIKE"; // string/text search
|
|
return "=="; // everything else (select, etc.)
|
|
};
|