c9b7024b921ecb4e1c232779df420ce25c1fa34b
[arvados-workbench2.git] / src / store / search-bar / search-query / arv-parser.ts
1
2 // Copyright (C) The Arvados Authors. All rights reserved.
3 //
4 // SPDX-License-Identifier: AGPL-3.0
5
6 import * as parser from '~/store/search-bar/search-query/parser';
7
8 interface Property {
9     key: string;
10     value: string;
11 }
12
13 export enum Keywords {
14     TYPE = 'type',
15     CLUSTER = 'cluster',
16     PROJECT = 'project',
17     IS = 'is',
18     FROM = 'from',
19     TO = 'to',
20 }
21
22 export enum States {
23     TRASHED = 'trashed'
24 }
25
26 const keyValuePattern = (key: string) => new RegExp(`${key}:([^ ]*)`);
27 const propertyPattern = /has:"(.*?)":"(.*?)"/;
28
29 const patterns = [
30     keyValuePattern(Keywords.TYPE),
31     keyValuePattern(Keywords.CLUSTER),
32     keyValuePattern(Keywords.PROJECT),
33     keyValuePattern(Keywords.IS),
34     keyValuePattern(Keywords.FROM),
35     keyValuePattern(Keywords.TO),
36     propertyPattern
37 ];
38
39 export const parseSearchQuery = parser.parseSearchQuery(patterns);
40
41 export const getValue = (tokens: string[]) => (key: string) => {
42     const pattern = keyValuePattern(key);
43     const token = tokens.find(t => pattern.test(t));
44     if (token) {
45         const [, value] = token.split(':');
46         return value;
47     }
48     return undefined;
49 };
50
51 export const getProperties = (tokens: string[]) =>
52     tokens.reduce((properties, token) => {
53         const match = token.match(propertyPattern);
54         if (match) {
55             const [, key, value] = match;
56             const newProperty = { key, value };
57             return [...properties, newProperty];
58         }
59         return properties;
60     }, [] as Property[]);
61
62
63 export const isTrashed = (tokens: string[]) => {
64     for (const token of tokens) {
65         const match = token.match(keyValuePattern(Keywords.IS)) || ['', ''];
66         if (match) {
67             const [, value] = match;
68             if(value === States.TRASHED) {
69                 return true;
70             }
71         }
72     }
73     return false;
74 };