|
| 1 | +import { EventsFilters } from "./types"; |
| 2 | + |
| 3 | +interface InternalFilter { |
| 4 | + operand: string; |
| 5 | + operator: |
| 6 | + | "equals" |
| 7 | + | "notEquals" |
| 8 | + | "greaterThan" |
| 9 | + | "lessThan" |
| 10 | + | "greaterThanOrEqual" |
| 11 | + | "lessThanOrEqual"; |
| 12 | + value: string; |
| 13 | +} |
| 14 | + |
| 15 | +// Query parser that can parse the query string into a list of filters |
| 16 | +export const queryParser = ( |
| 17 | + query: EventsFilters["query"], |
| 18 | + allowedOperands = ["metadata"], |
| 19 | +) => { |
| 20 | + if (!query) { |
| 21 | + return undefined; |
| 22 | + } |
| 23 | + |
| 24 | + const filters: InternalFilter[] = []; |
| 25 | + |
| 26 | + // Split the query by logical operators (AND/OR) to handle multiple conditions |
| 27 | + // For now, we'll focus on single conditions, but this structure allows for future expansion |
| 28 | + const conditions = query.split(/\s+(?:AND|and|OR|or)\s+/); |
| 29 | + |
| 30 | + for (const condition of conditions) { |
| 31 | + const trimmedCondition = condition.trim(); |
| 32 | + |
| 33 | + if (!trimmedCondition) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + const filter = parseCondition(trimmedCondition); |
| 38 | + |
| 39 | + if (!filter) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + const isAllowed = allowedOperands.some((allowed) => { |
| 44 | + if (filter.operand === allowed) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + if (filter.operand.startsWith(`${allowed}.`)) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + return false; |
| 53 | + }); |
| 54 | + |
| 55 | + if (!isAllowed) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + filters.push(filter); |
| 60 | + } |
| 61 | + |
| 62 | + return filters.length > 0 ? filters : undefined; |
| 63 | +}; |
| 64 | + |
| 65 | +// Parses a single condition in the format: field:value, field>value, or metadata['key']:value |
| 66 | +function parseCondition(condition: string): InternalFilter | null { |
| 67 | + // This regex captures: |
| 68 | + // 1. field - either a regular field name OR metadata with bracket notation (supports both single and double quotes) |
| 69 | + // 2. operator - :, >, <, >=, <=, != |
| 70 | + // 3. value - the value after the operator (supports quoted and unquoted values) |
| 71 | + const unifiedPattern = |
| 72 | + /^([a-zA-Z_][a-zA-Z0-9_]*|metadata\[['"][^'"]*['"]\](?:\[['"][^'"]*['"]\])*)\s*([:><=!]+)\s*(.+)$/; |
| 73 | + |
| 74 | + const match = condition.match(unifiedPattern); |
| 75 | + |
| 76 | + if (!match) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + // Extract the matched groups |
| 81 | + const [, fieldOrMetadata, operator, value] = match; |
| 82 | + |
| 83 | + let operand: string; |
| 84 | + |
| 85 | + // Determine the operand based on whether it's metadata or a regular field |
| 86 | + if (fieldOrMetadata.startsWith("metadata")) { |
| 87 | + const keyPath = fieldOrMetadata.replace(/^metadata/, ""); |
| 88 | + |
| 89 | + const extractedKey = keyPath |
| 90 | + .replace(/^\[['"]|['"]\]$/g, "") // Remove leading [' or [" and trailing '] or "] |
| 91 | + .replace(/\[['"]/g, ".") // Replace [' or [" with . |
| 92 | + .replace(/['"]\]/g, ""); // Remove trailing '] or "] |
| 93 | + |
| 94 | + operand = `metadata.${extractedKey}`; |
| 95 | + } else { |
| 96 | + operand = fieldOrMetadata; |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + operand, |
| 101 | + operator: mapOperator(operator), |
| 102 | + value: value.trim().replace(/^['"`]|['"`]$/g, ""), |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +// Maps operator strings to our internal operator types |
| 107 | +function mapOperator(operator: string): InternalFilter["operator"] { |
| 108 | + switch (operator) { |
| 109 | + case ":": |
| 110 | + case "=": |
| 111 | + return "equals"; |
| 112 | + case ">": |
| 113 | + return "greaterThan"; |
| 114 | + case "<": |
| 115 | + return "lessThan"; |
| 116 | + case ">=": |
| 117 | + return "greaterThanOrEqual"; |
| 118 | + case "<=": |
| 119 | + return "lessThanOrEqual"; |
| 120 | + case "!=": |
| 121 | + return "notEquals"; |
| 122 | + default: |
| 123 | + // For unsupported operators, default to equals |
| 124 | + return "equals"; |
| 125 | + } |
| 126 | +} |
0 commit comments