Debian Codegraph @

dojox/jsonPath/query.js

raw symbols SCIP
1 define(["dojo/_base/kernel",
2 "dojo/_base/lang"
3 ], function(kernel, lang){
4 var query = function( /*Object*/ obj, /*String*/ expr, /*Object*/ arg){
5 // summary:
6 // Perform jsonPath query `expr` on javascript object or json string `obj`
7 // obj:
8 // object || json string to perform query on
9 // expr:
10 // jsonPath expression (string) to be evaluated
11 // arg:
12 // {} special arguments.
13 //
14 // - resultType: "VALUE"||"BOTH"||"PATH"} (defaults to value)
15 // - evalType: "RESULT"||"ITEM"} (defaults to ?)
16
17 if(!arg){
18 arg = {};
19 }
20
21 var strs = [];
22
23 function _str(i){
24 return strs[i];
25 }
26 var _strName = _str.name;
27 if(arg.resultType == "PATH" && arg.evalType == "RESULT"){
28 throw Error("RESULT based evaluation not supported with PATH based results");
29 }
30 var P = {
31 resultType: arg.resultType || "VALUE",
32 normalize: function(expr){
33 var subx = [];
34 expr = expr.replace(/'([^']|'')*'/g, function(t){
35 return _strName + "(" + (strs.push(eval(t)) - 1) + ")";
36 });
37 var ll = -1;
38 while(ll != subx.length){
39 ll = subx.length; //TODO: Do expression syntax checking
40 expr = expr.replace(/(\??\([^\(\)]*\))/g, function($0){
41 return "#" + (subx.push($0) - 1);
42 });
43 }
44 expr = expr.replace(/[\['](#[0-9]+)[\]']/g, '[$1]')
45 .replace(/'?\.'?|\['?/g, ";")
46 .replace(/;;;|;;/g, ";..;")
47 .replace(/;$|'?\]|'$/g, "");
48 ll = -1;
49 while(ll != expr){
50 ll = expr;
51 expr = expr.replace(/#([0-9]+)/g, function($0, $1){
52 return subx[$1];
53 });
54 }
55 return expr.split(";");
56 },
57 asPaths: function(paths){
58 var i, j, p, x, n;
59 for(j = 0; j < paths.length; j++){
60 p = "$";
61 x = paths[j];
62 for(i = 1, n = x.length; i < n; i++){
63 p += /^[0-9*]+$/.test(x[i]) ? ("[" + x[i] + "]") : ("['" + x[i] + "']");
64 }
65 paths[j] = p;
66 }
67 return paths;
68 },
69 exec: function(locs, val, rb){
70 var path = ['$'];
71 var result = rb ? val : [val];
72 var paths = [path];
73
74 function add(v, p, def){
75 if(v && v.hasOwnProperty(p) && P.resultType != "VALUE"){
76 paths.push(path.concat([p]));
77 }
78 if(def){
79 result = v[p];
80 }else if(v && v.hasOwnProperty(p)){
81 result.push(v[p]);
82 }
83 }
84
85 function desc(v){
86 result.push(v);
87 paths.push(path);
88 P.walk(v, function(i){
89 if(typeof v[i] === "object"){
90 var oldPath = path;
91 path = path.concat(i);
92 desc(v[i]);
93 path = oldPath;
94 }
95 });
96 }
97
98 function slice(loc, val){
99 if(val instanceof Array){
100 var len = val.length,
101 start = 0,
102 end = len,
103 step = 1;
104 loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, function($0, $1, $2, $3){
105 start = parseInt($1 || start, 10);
106 end = parseInt($2 || end, 10);
107 step = parseInt($3 || step, 10);
108 });
109 start = (start < 0) ? Math.max(0, start + len) : Math.min(len, start);
110 end = (end < 0) ? Math.max(0, end + len) : Math.min(len, end);
111 var i;
112 for(i = start; i < end; i += step){
113 add(val, i);
114 }
115 }
116 }
117
118 function repStr(str, loc){
119 var i = loc.match(/^_str\(([0-9]+)\)$/);
120 return i ? strs[i[1]] : str;
121 }
122
123 function oper(val, loc){
124 if(/^\(.*?\)$/.test(loc)){ // [(expr)]
125 add(val, P.eval(loc, val), rb);
126 }else if(loc === "*"){
127 P.walk(val, rb && val instanceof Array ? // if it is result based, there is no point to just return the same array
128 function(i){
129 P.walk(val[i], function(j){
130 add(val[i], j);
131 });
132 } :
133 function(i){
134 add(val, i);
135 });
136 }else if(loc === ".."){
137 desc(val);
138 }else if(/,/.test(loc)){ // [name1,name2,...]
139 var s, n, i;
140 for(s = loc.split(/'?,'?/), i = 0, n = s.length; i < n; i++){
141 add(val, repStr(s[i], loc));
142 }
143 }else if(/^\?\(.*?\)$/.test(loc)){ // [?(expr)]
144 P.walk(val, function(i){
145 if(P.eval(loc.replace(/^\?\((.*?)\)$/, "$1"), val[i])){
146 add(val, i);
147 }
148 });
149 }else if(/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)){ // [start:end:step] python slice syntax
150 slice(loc, val);
151 }else{
152 loc = repStr(loc, loc);
153 if(rb && val instanceof Array && !/^[0-9*]+$/.test(loc)){
154 P.walk(val, function(i){
155 add(val[i], loc);
156 });
157 }else{
158 add(val, loc, rb);
159 }
160 }
161
162 }
163 var loc, valPaths;
164 while(locs.length){
165 loc = locs.shift();
166 if((val = result) === null || val === undefined){
167 return val;
168 }
169 result = [];
170 valPaths = paths;
171 paths = [];
172 if(rb){
173 oper(val, loc);
174 }else{
175 P.walk(val, function(i){
176 path = valPaths[i] || path;
177 oper(val[i], loc);
178 });
179 }
180 }
181 if(P.resultType == "BOTH"){
182 paths = P.asPaths(paths);
183 var newResult = [];
184 var i;
185 for(i = 0; i < paths.length; i++){
186 newResult.push({
187 path: paths[i],
188 value: result[i]
189 });
190 }
191 return newResult;
192 }
193 return P.resultType == "PATH" ? P.asPaths(paths) : result;
194 },
195 walk: function(val, f){
196 var i, n, m;
197 if(val instanceof Array){
198 for(i = 0, n = val.length; i < n; i++){
199 if(i in val){
200 f(i);
201 }
202 }
203 }else if(typeof val === "object"){
204 for(m in val){
205 if(val.hasOwnProperty(m)){
206 f(m);
207 }
208 }
209 }
210 },
211 "eval": function(x, v){
212 try {
213 return obj && v && eval(x.replace(/@/g, "v"));
214 } catch (e){
215 throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/@/g, "v").replace(/\^/g, "_a"));
216 }
217 }
218 };
219
220 if(expr && obj){
221 return P.exec(P.normalize(expr).slice(1), obj, arg.evalType == "RESULT");
222 }
223
224 return false;
225
226 };
227
228 if(!kernel.isAsync){
229 var obj = lang.getObject("dojox.jsonPath", true);
230 obj.query = query;
231 }
232
233 return query;
234
235 });