-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchOperator.ts
42 lines (33 loc) · 1.41 KB
/
SearchOperator.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
import {ISearchOperatorExecutor} from "./ISearchOperatorExecutor";
import {ArgumentNullError} from "./ArgumentNullError";
export class SearchOperator {
private static Registry = { };
public aliases: Array<string>;
public exec: ISearchOperatorExecutor;
public parseValue: (value: any) => any;
public validateValue: (value: any) => void;
constructor(aliases: string|Array<string>, exec: ISearchOperatorExecutor, parseValue: (value: any) => any = null, validateValue: (value: any) => void = null) {
ArgumentNullError.check(aliases, "aliases");
ArgumentNullError.check(exec, "exec");
this.aliases = (Array.isArray(aliases) ? aliases : [aliases]);
this.exec = this.wrapExec(exec);
if (parseValue != null)
this.parseValue = parseValue;
else
this.parseValue = (value: any) => { return value; };
if (validateValue != null)
this.validateValue = validateValue;
else
this.validateValue = (value: any) => { };
}
private wrapExec(exec: ISearchOperatorExecutor): ISearchOperatorExecutor {
return (entity: Object, fieldName: string, value: any) => {
if (entity.hasOwnProperty(fieldName))
return exec(entity, fieldName, value);
return true;
};
}
public getName(): string {
return this.aliases[0];
}
}