Merge branch '14258-update-project-panel-to-use-filters-tree' into 14258-collection...
[arvados-workbench2.git] / src / services / api / filter-builder.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as _ from "lodash";
6
7 export function joinFilters(filters0?: string, filters1?: string) {
8     return [filters0, filters1].filter(s => s).join(",");
9 }
10
11 export class FilterBuilder {
12     constructor(private filters = "") { }
13
14     public addEqual(field: string, value?: string | boolean, resourcePrefix?: string) {
15         return this.addCondition(field, "=", value, "", "", resourcePrefix );
16     }
17
18     public addLike(field: string, value?: string, resourcePrefix?: string) {
19         return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
20     }
21
22     public addILike(field: string, value?: string, resourcePrefix?: string) {
23         return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
24     }
25
26     public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
27         return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
28     }
29
30     public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
31         return this.addCondition(field, "in", value, "", "", resourcePrefix);
32     }
33
34     public addNotIn(field: string, value?: string | string[], resourcePrefix?: string) {
35         return this.addCondition(field, "not in", value, "", "", resourcePrefix);
36     }
37
38     public addGt(field: string, value?: string, resourcePrefix?: string) {
39         return this.addCondition(field, ">", value, "", "", resourcePrefix);
40     }
41
42     public addGte(field: string, value?: string, resourcePrefix?: string) {
43         return this.addCondition(field, ">=", value, "", "", resourcePrefix);
44     }
45
46     public addLt(field: string, value?: string, resourcePrefix?: string) {
47         return this.addCondition(field, "<", value, "", "", resourcePrefix);
48     }
49
50     public addLte(field: string, value?: string, resourcePrefix?: string) {
51         return this.addCondition(field, "<=", value, "", "", resourcePrefix);
52     }
53
54     public getFilters() {
55         return this.filters;
56     }
57
58     private addCondition(field: string, cond: string, value?: string | string[] | boolean, prefix: string = "", postfix: string = "", resourcePrefix?: string) {
59         if (value) {
60             if (typeof value === "string") {
61                 value = `"${prefix}${value}${postfix}"`;
62             } else if (Array.isArray(value)) {
63                 value = `["${value.join(`","`)}"]`;
64             } else {
65                 value = value ? "true" : "false";
66             }
67
68             const resPrefix = resourcePrefix
69                 ? resourcePrefix + "."
70                 : "";
71
72             this.filters += `${this.filters ? "," : ""}["${resPrefix}${_.snakeCase(field)}","${cond}",${value}]`;
73         }
74         return this;
75     }
76 }