Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update analytics-query-parser.ts

Kiran K8c69b5775bad
diff --git a/apps/web/lib/analytics/utils/analytics-query-parser.ts b/apps/web/lib/analytics/utils/analytics-query-parser.ts
index bc817bfff9..0593c46bc9 100644
--- a/apps/web/lib/analytics/utils/analytics-query-parser.ts
+++ b/apps/web/lib/analytics/utils/analytics-query-parser.ts
@@ -1,6 +1,6 @@
import { EventsFilters } from "../types";
-interface Filter {
+interface InternalFilter {
operand: string;
operator: "=" | ">" | "<" | ">=" | "<=" | "!=";
value: string;
@@ -12,7 +12,7 @@ export const parseFiltersFromQuery = (query: EventsFilters["query"]) => {
return undefined;
}
- const filters: Filter[] = [];
+ const filters: InternalFilter[] = [];
// Split the query by logical operators (AND/OR) to handle multiple conditions
// For now, we'll focus on single conditions, but this structure allows for future expansion
@@ -32,20 +32,17 @@ export const parseFiltersFromQuery = (query: EventsFilters["query"]) => {
}
}
- console.log("filters", filters);
-
return filters.length > 0 ? filters : undefined;
};
// Parses a single condition in the format: field:value, field>value, or metadata['key']:value
-function parseCondition(condition: string): Filter | null {
- // Unified regex pattern that handles both regular fields and metadata fields
+function parseCondition(condition: string): InternalFilter | null {
// This regex captures:
- // 1. field - either a regular field name OR metadata with bracket notation
+ // 1. field - either a regular field name OR metadata with bracket notation (supports both single and double quotes)
// 2. operator - :, >, <, >=, <=, !=
// 3. value - the value after the operator (supports quoted and unquoted values)
const unifiedPattern =
- /^([a-zA-Z_][a-zA-Z0-9_]*|metadata\['[^']*'\](?:\['[^']*'\])*)\s*([:><=!]+)\s*(.+)$/;
+ /^([a-zA-Z_][a-zA-Z0-9_]*|metadata\[['"][^'"]*['"]\](?:\[['"][^'"]*['"]\])*)\s*([:><=!]+)\s*(.+)$/;
const match = condition.match(unifiedPattern);
@@ -56,34 +53,29 @@ function parseCondition(condition: string): Filter | null {
// Extract the matched groups
const [, fieldOrMetadata, operator, value] = match;
- // Clean up the value by removing surrounding quotes if present
- const cleanValue = value.trim().replace(/^['"`]|['"`]$/g, "");
-
- // Determine the operand based on whether it's metadata or a regular field
let operand: string;
+ // Determine the operand based on whether it's metadata or a regular field
if (fieldOrMetadata.startsWith("metadata")) {
const keyPath = fieldOrMetadata.replace(/^metadata/, "");
operand = keyPath
- .replace(/^\['|'\]$/g, "") // Remove leading [' and trailing ']
- .replace(/\['/g, ".") // Replace [' with .
- .replace(/'\]/g, ""); // Remove trailing ']
+ .replace(/^\[['"]|['"]\]$/g, "") // Remove leading [' or [" and trailing '] or "]
+ .replace(/\[['"]/g, ".") // Replace [' or [" with .
+ .replace(/['"]\]/g, ""); // Remove trailing '] or "]
} else {
operand = fieldOrMetadata;
}
- const filterOperator = mapOperator(operator);
-
return {
operand,
- operator: filterOperator,
- value: cleanValue,
+ operator: mapOperator(operator),
+ value: value.trim().replace(/^['"`]|['"`]$/g, ""),
};
}
// Maps operator strings to our internal operator types
-function mapOperator(operator: string): Filter["operator"] {
+function mapOperator(operator: string): InternalFilter["operator"] {
switch (operator) {
case ":":
return "=";