1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { uniq } from 'lodash/fp';
7 export interface ParsedSearchQuery {
12 export const findToken = (query: string, patterns: RegExp[]) => {
13 for (const pattern of patterns) {
14 const match = query.match(pattern);
22 export const findAllTokens = (query: string, patterns: RegExp[]): string[] => {
23 const token = findToken(query, patterns);
25 ? [token].concat(findAllTokens(query.replace(token, ''), patterns))
29 export const findSearchString = (query: string, tokens: string[]) => {
30 const uniqueWords = uniq(tokens
31 .reduce((q, token) => q.replace(token, ''), query)
33 .filter(word => word !== '')
35 return uniqueWords.join(' ');
38 export const parseSearchQuery = (patterns: RegExp[]) => (query: string): ParsedSearchQuery => {
39 const tokens = findAllTokens(query, patterns);
40 const searchString = findSearchString(query, tokens);
41 return { tokens, searchString };