-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonQL.js
More file actions
111 lines (97 loc) · 2.18 KB
/
jsonQL.js
File metadata and controls
111 lines (97 loc) · 2.18 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
jsonQuery=(function(undefined){
function get(obj,str,s){
str=str.split(".")
for(x in str){
obj=obj[str[x]]
if(obj==undefined) return undefined;
}
return obj;
}
function set(obj,str,val,x,last){
str=str.split(".")
last=str.pop();
for(x in str){
if(obj[str[x]]==undefined){
obj[str[x]]={}
}
obj=obj[str[x]];
}
obj[last]=val;
}
function test(ret,current,what,fn,end){
var cur=[],tests={};
for(x=0;x<current.length;x++) cur[x]=current[x];
for(x in testFns){
(function(x,t){
tests[x]=function(c,x){
for(x=0;x<cur.length;x++){
t(get(cur[x],what),c)&&fn(cur[x]);
}
end&&end();
return ret;
}
})(x,testFns[x]);
}
return tests;
}
function filter(data,fn){
var ret=[];
return {
where:function(what){
return test(this,data,what,function(row){
ret.push(row)
});
},
and:function(what){
var stack=[];
return test(this,ret,what,function(row){
stack.push(row);
},function(){
ret=stack;
})
},
or:function(what){
return test(this,data,what,function(row){
ret.push(row)
});
},
get:function(){
var res=[],x;
for(x=0;x<ret.length;x++) res[x]=fn(ret[x])
return res;
}
}
}
function jsonQuery(data){
return {
select:function(what){
return filter(data, function(row){
return get(row, what);
})
},
update:function(what){
return filter(data, function(row){
return set(row, what);
})
}
}
}
var testFns=jsonQuery.test={
eq:function(a, b){
return a == b;
},
lt:function(a,b){
return a < b;
},
gt:function(a,b){
return a > b;
},
like:function(a,b){
return ~(""+a).indexOf(b)
},
is:function(a,b){
return b(a);
}
}
return jsonQuery
})();