-
Notifications
You must be signed in to change notification settings - Fork 9
/
Comparator.ts
82 lines (66 loc) · 2.1 KB
/
Comparator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {OnmsEnum} from '../internal/OnmsEnum';
/**
* Represents a filter comparator.
* @category Filtering
*/
export class Comparator extends OnmsEnum<number> {
/** Find the comparator that matches the given comparator string. */
public static find(comparator: string) {
for (const key of Object.keys(Comparators)) {
const comp = Comparators[key];
if (comp.matches(comparator)) {
return comp;
}
}
return null;
}
/** Aliases for the command-line. */
private aliases = [] as string[];
constructor(id: number, label: string, ...aliases: string[]) {
super(id, label);
this.aliases = aliases;
}
/** Whether this comparator matches the given comparator string. */
public matches(comparator: string) {
const compareTo = comparator.toUpperCase();
return (compareTo === this.label.toUpperCase())
|| this.aliases.indexOf(compareTo) >= 0;
}
}
/* tslint:disable:object-literal-sort-keys */
/**
* Contains constant instances of all available comparators.
* @category Filtering
*/
export const Comparators = {
/** Equals (`=` or `==`) */
EQ: new Comparator(1, 'EQ', '=', '=='),
/** Not Equals (`!=`) */
NE: new Comparator(2, 'NE', '!='),
/** Case-Insensitive Substring Match (`ILIKE`) */
ILIKE: new Comparator(3, 'ILIKE'),
/** Case-Sensitive Substring Match (`LIKE`) */
LIKE: new Comparator(4, 'LIKE'),
/** Greater Than (`>`) */
GT: new Comparator(5, 'GT', '>'),
/** Less Than (`<`) */
LT: new Comparator(6, 'LT', '<'),
/** Greater Than or Equal To (`>=`) */
GE: new Comparator(7, 'GE', '>='),
/** Less Than or Equal To (`<=`) */
LE: new Comparator(8, 'LE', '<='),
/** Is Null (`NULL`) */
NULL: new Comparator(9, 'NULL', 'ISNULL'),
/** Is Not Null (`NOTNULL`) */
NOTNULL: new Comparator(10, 'NOTNULL'),
/*
ALL: new Comparator(9, 'ALL'),
ANY: new Comparator(10, 'ANY'),
BETWEEN: new Comparator(15, 'BETWEEN'),
NOT: new Comparator(14, 'NOT'),
IN: new Comparator(13, 'IN'),
IPLIKE: new Comparator(17, 'IPLIKE'),
SQL: new Comparator(16, 'SQL'),
*/
} as { [key: string]: Comparator };
Object.freeze(Comparators);