(query: string)
| 38 | * Parse a search query string into structured filters and text search |
| 39 | */ |
| 40 | export function parseQuery(query: string): ParsedQuery { |
| 41 | const filters: ParsedFilter[] = [] |
| 42 | const tokens: string[] = [] |
| 43 | |
| 44 | const filterRegex = /(\w+):((?:[><!]=?|=)?(?:"[^"]*"|[^\s]+))/g |
| 45 | |
| 46 | let lastIndex = 0 |
| 47 | let match |
| 48 | |
| 49 | while ((match = filterRegex.exec(query)) !== null) { |
| 50 | const [fullMatch, field, valueWithOperator] = match |
| 51 | |
| 52 | const beforeText = query.slice(lastIndex, match.index).trim() |
| 53 | if (beforeText) { |
| 54 | tokens.push(beforeText) |
| 55 | } |
| 56 | |
| 57 | const parsedFilter = parseFilter(field, valueWithOperator) |
| 58 | if (parsedFilter) { |
| 59 | filters.push(parsedFilter) |
| 60 | } else { |
| 61 | tokens.push(fullMatch) |
| 62 | } |
| 63 | |
| 64 | lastIndex = match.index + fullMatch.length |
| 65 | } |
| 66 | |
| 67 | const remainingText = query.slice(lastIndex).trim() |
| 68 | if (remainingText) { |
| 69 | tokens.push(remainingText) |
| 70 | } |
| 71 | |
| 72 | return { |
| 73 | filters, |
| 74 | textSearch: tokens.join(' ').trim(), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Parse a single field:value filter |
no test coverage detected