-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPredicate.mjs
More file actions
56 lines (53 loc) · 1.39 KB
/
Predicate.mjs
File metadata and controls
56 lines (53 loc) · 1.39 KB
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
export default class Predicate{
constructor(collect){
this.predicate = null;
this.on_collect = collect
this._or = (p1, p2)=> {
return function(x) {
return p1(x) || p2(x);
}
}
this._and = (p1, p2) => {
return function(x) {
return p1(x) && p2(x);
}
}
this._equals = (p) => {
return function(x) {
return p(x);
}
}
this.__equals = (a,b) =>{
return function(x){
if(x[a]){
if(Array.isArray(x[a])){
return x[a].includes(b)
}
return x[a] == b
}
return false;
}
}
}
initialize(val,prop){
this.predicate = this.__equals(prop,val)
}
collect(){
return this.on_collect(this.predicate)
}
or(options) {
for(let option in options){
this.predicate = this._or(this.predicate, this.__equals(option,options[option]))
}
return this
}
and(options){
for(let option in options){
this.predicate = this._and(this.predicate, this.__equals(option,options[option]))
}
return this
}
getPredicate(){
return this.predicate
}
}