-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchCriteria.ts
51 lines (38 loc) · 1.45 KB
/
SearchCriteria.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
import {SearchOperator} from "./SearchOperator";
import {SearchOperatorRegistry} from "./SearchOperatorRegistry";
export class SearchCriteria {
public fieldName: string;
public operator: SearchOperator;
public value: any;
public invert = false;
constructor(fieldName: string, operator: SearchOperator|string, rawValue?: any, invert: boolean = false) {
var resolvedOperator = SearchOperatorRegistry.resolve(operator);
var value = resolvedOperator.parseValue(rawValue);
this.fieldName = fieldName;
this.operator = resolvedOperator;
this.value = value;
this.invert = invert;
}
public validate(): void {
if (this.fieldName == null)
throw Error("Search criteria has null field name.");
if (this.operator == null)
throw Error("Search criteria has null operator.");
this.operator.validateValue(this.value);
};
public test(entity: Object): boolean {
var result = this.operator.exec(entity, this.fieldName, this.value);
if (this.invert)
result = !result;
return result;
}
public static parse(key: string, value: any) {
var match = /^(.+?)(?::(.+))?$/.exec(key);
if (match != null) {
var fieldName = match[1];
var operatorName = match[2];
return new SearchCriteria(fieldName, operatorName, value);
}
return null;
};
}