-
Notifications
You must be signed in to change notification settings - Fork 8
/
NestedRestriction.ts
46 lines (40 loc) · 1.54 KB
/
NestedRestriction.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
import {Operators} from './Operator';
import {Clause} from './Clause';
import {Restriction} from './Restriction';
/**
* Nested query restrictions.
* @category Filtering
*/
export class NestedRestriction {
/** given a nested restriction JSON structure, return a NestedRestriction object */
public static fromJson(nestedRestriction: any): NestedRestriction {
const newNestedRestriction = new NestedRestriction();
if (nestedRestriction && nestedRestriction.clauses) {
nestedRestriction.clauses.forEach((clause: any) => {
newNestedRestriction.withClause(Clause.fromJson(clause));
});
}
return newNestedRestriction;
}
/** The clauses containing the nested restrictions and their logical operators. */
public clauses: Clause[];
constructor(...clauses: Clause[]) {
this.clauses = clauses;
}
/** Adds an additional restriction using the logical OR operator. */
public withOrRestriction(restriction: Restriction|NestedRestriction) {
return this.withClause(new Clause(restriction, Operators.OR));
}
/** Adds an additional restriction using the logical AND operator. */
public withAndRestriction(restriction: Restriction|NestedRestriction) {
return this.withClause(new Clause(restriction, Operators.AND));
}
/** Adds an additional clause. */
private withClause(clause: Clause) {
if (!this.clauses) {
this.clauses = [] as Clause[];
}
this.clauses.push(clause);
return this;
}
}