query.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. define("dojox/json/query", ["dojo/_base/kernel", "dojox", "dojo/_base/array"], function(dojo, dojox){
  2. dojo.getObject("json", true, dojox);
  3. dojox.json._slice = function(obj,start,end,step){
  4. // handles slice operations: [3:6:2]
  5. var len=obj.length,results = [];
  6. end = end || len;
  7. start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start);
  8. end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end);
  9. for(var i=start; i<end; i+=step){
  10. results.push(obj[i]);
  11. }
  12. return results;
  13. }
  14. dojox.json._find = function e(obj,name){
  15. // handles ..name, .*, [*], [val1,val2], [val]
  16. // name can be a property to search for, undefined for full recursive, or an array for picking by index
  17. var results = [];
  18. function walk(obj){
  19. if(name){
  20. if(name===true && !(obj instanceof Array)){
  21. //recursive object search
  22. results.push(obj);
  23. }else if(obj[name]){
  24. // found the name, add to our results
  25. results.push(obj[name]);
  26. }
  27. }
  28. for(var i in obj){
  29. var val = obj[i];
  30. if(!name){
  31. // if we don't have a name we are just getting all the properties values (.* or [*])
  32. results.push(val);
  33. }else if(val && typeof val == 'object'){
  34. walk(val);
  35. }
  36. }
  37. }
  38. if(name instanceof Array){
  39. // this is called when multiple items are in the brackets: [3,4,5]
  40. if(name.length==1){
  41. // this can happen as a result of the parser becoming confused about commas
  42. // in the brackets like [@.func(4,2)]. Fixing the parser would require recursive
  43. // analsys, very expensive, but this fixes the problem nicely.
  44. return obj[name[0]];
  45. }
  46. for(var i = 0; i < name.length; i++){
  47. results.push(obj[name[i]]);
  48. }
  49. }else{
  50. // otherwise we expanding
  51. walk(obj);
  52. }
  53. return results;
  54. }
  55. dojox.json._distinctFilter = function(array, callback){
  56. // does the filter with removal of duplicates in O(n)
  57. var outArr = [];
  58. var primitives = {};
  59. for(var i=0,l=array.length; i<l; ++i){
  60. var value = array[i];
  61. if(callback(value, i, array)){
  62. if((typeof value == 'object') && value){
  63. // with objects we prevent duplicates with a marker property
  64. if(!value.__included){
  65. value.__included = true;
  66. outArr.push(value);
  67. }
  68. }else if(!primitives[value + typeof value]){
  69. // with primitives we prevent duplicates by putting it in a map
  70. primitives[value + typeof value] = true;
  71. outArr.push(value);
  72. }
  73. }
  74. }
  75. for(i=0,l=outArr.length; i<l; ++i){
  76. // cleanup the marker properties
  77. if(outArr[i]){
  78. delete outArr[i].__included;
  79. }
  80. }
  81. return outArr;
  82. }
  83. return dojox.json.query = function(/*String*/query,/*Object?*/obj){
  84. // summary:
  85. // Performs a JSONQuery on the provided object and returns the results.
  86. // If no object is provided (just a query), it returns a "compiled" function that evaluates objects
  87. // according to the provided query.
  88. // query:
  89. // Query string
  90. // obj:
  91. // Target of the JSONQuery
  92. //
  93. // description:
  94. // JSONQuery provides a comprehensive set of data querying tools including filtering,
  95. // recursive search, sorting, mapping, range selection, and powerful expressions with
  96. // wildcard string comparisons and various operators. JSONQuery generally supersets
  97. // JSONPath and provides syntax that matches and behaves like JavaScript where
  98. // possible.
  99. //
  100. // JSONQuery evaluations begin with the provided object, which can referenced with
  101. // $. From
  102. // the starting object, various operators can be successively applied, each operating
  103. // on the result of the last operation.
  104. //
  105. // Supported Operators:
  106. // --------------------
  107. // * .property - This will return the provided property of the object, behaving exactly
  108. // like JavaScript.
  109. // * [expression] - This returns the property name/index defined by the evaluation of
  110. // the provided expression, behaving exactly like JavaScript.
  111. // * [?expression] - This will perform a filter operation on an array, returning all the
  112. // items in an array that match the provided expression. This operator does not
  113. // need to be in brackets, you can simply use ?expression, but since it does not
  114. // have any containment, no operators can be used afterwards when used
  115. // without brackets.
  116. // * [^?expression] - This will perform a distinct filter operation on an array. This behaves
  117. // as [?expression] except that it will remove any duplicate values/objects from the
  118. // result set.
  119. // * [/expression], [\expression], [/expression, /expression] - This performs a sort
  120. // operation on an array, with sort based on the provide expression. Multiple comma delimited sort
  121. // expressions can be provided for multiple sort orders (first being highest priority). /
  122. // indicates ascending order and \ indicates descending order
  123. // * [=expression] - This performs a map operation on an array, creating a new array
  124. // with each item being the evaluation of the expression for each item in the source array.
  125. // * [start:end:step] - This performs an array slice/range operation, returning the elements
  126. // from the optional start index to the optional end index, stepping by the optional step number.
  127. // * [expr,expr] - This a union operator, returning an array of all the property/index values from
  128. // the evaluation of the comma delimited expressions.
  129. // * .* or [*] - This returns the values of all the properties of the current object.
  130. // * $ - This is the root object, If a JSONQuery expression does not being with a $,
  131. // it will be auto-inserted at the beginning.
  132. // * @ - This is the current object in filter, sort, and map expressions. This is generally
  133. // not necessary, names are auto-converted to property references of the current object
  134. // in expressions.
  135. // * ..property - Performs a recursive search for the given property name, returning
  136. // an array of all values with such a property name in the current object and any subobjects
  137. // * expr = expr - Performs a comparison (like JS's ==). When comparing to
  138. // a string, the comparison string may contain wildcards * (matches any number of
  139. // characters) and ? (matches any single character).
  140. // * expr ~ expr - Performs a string comparison with case insensitivity.
  141. // * ..[?expression] - This will perform a deep search filter operation on all the objects and
  142. // subobjects of the current data. Rather than only searching an array, this will search
  143. // property values, arrays, and their children.
  144. // * $1,$2,$3, etc. - These are references to extra parameters passed to the query
  145. // function or the evaluator function.
  146. // * +, -, /, *, &, |, %, (, ), <, >, <=, >=, != - These operators behave just as they do
  147. // in JavaScript.
  148. //
  149. //
  150. //
  151. // | dojox.json.query(queryString,object)
  152. // and
  153. // | dojox.json.query(queryString)(object)
  154. // always return identical results. The first one immediately evaluates, the second one returns a
  155. // function that then evaluates the object.
  156. //
  157. // example:
  158. // | dojox.json.query("foo",{foo:"bar"})
  159. // This will return "bar".
  160. //
  161. // example:
  162. // | evaluator = dojox.json.query("?foo='bar'&rating>3");
  163. // This creates a function that finds all the objects in an array with a property
  164. // foo that is equals to "bar" and with a rating property with a value greater
  165. // than 3.
  166. // | evaluator([{foo:"bar",rating:4},{foo:"baz",rating:2}])
  167. // This returns:
  168. // | {foo:"bar",rating:4}
  169. //
  170. // example:
  171. // | evaluator = dojox.json.query("$[?price<15.00][\rating][0:10]");
  172. // This finds objects in array with a price less than 15.00 and sorts then
  173. // by rating, highest rated first, and returns the first ten items in from this
  174. // filtered and sorted list.
  175. var depth = 0;
  176. var str = [];
  177. query = query.replace(/"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|[\[\]]/g,function(t){
  178. depth += t == '[' ? 1 : t == ']' ? -1 : 0; // keep track of bracket depth
  179. return (t == ']' && depth > 0) ? '`]' : // we mark all the inner brackets as skippable
  180. (t.charAt(0) == '"' || t.charAt(0) == "'") ? "`" + (str.push(t) - 1) :// and replace all the strings
  181. t;
  182. });
  183. var prefix = '';
  184. function call(name){
  185. // creates a function call and puts the expression so far in a parameter for a call
  186. prefix = name + "(" + prefix;
  187. }
  188. function makeRegex(t,a,b,c,d,e,f,g){
  189. // creates a regular expression matcher for when wildcards and ignore case is used
  190. return str[g].match(/[\*\?]/) || f == '~' ?
  191. "/^" + str[g].substring(1,str[g].length-1).replace(/\\([btnfr\\"'])|([^\w\*\?])/g,"\\$1$2").replace(/([\*\?])/g,"[\\w\\W]$1") + (f == '~' ? '$/i' : '$/') + ".test(" + a + ")" :
  192. t;
  193. }
  194. query.replace(/(\]|\)|push|pop|shift|splice|sort|reverse)\s*\(/,function(){
  195. throw new Error("Unsafe function call");
  196. });
  197. query = query.replace(/([^<>=]=)([^=])/g,"$1=$2"). // change the equals to comparisons except operators ==, <=, >=
  198. replace(/@|(\.\s*)?[a-zA-Z\$_]+(\s*:)?/g,function(t){
  199. return t.charAt(0) == '.' ? t : // leave .prop alone
  200. t == '@' ? "$obj" :// the reference to the current object
  201. (t.match(/:|^(\$|Math|true|false|null)$/) ? "" : "$obj.") + t; // plain names should be properties of root... unless they are a label in object initializer
  202. }).
  203. replace(/\.?\.?\[(`\]|[^\]])*\]|\?.*|\.\.([\w\$_]+)|\.\*/g,function(t,a,b){
  204. var oper = t.match(/^\.?\.?(\[\s*\^?\?|\^?\?|\[\s*==)(.*?)\]?$/); // [?expr] and ?expr and [=expr and =expr
  205. if(oper){
  206. var prefix = '';
  207. if(t.match(/^\./)){
  208. // recursive object search
  209. call("dojox.json._find");
  210. prefix = ",true)";
  211. }
  212. call(oper[1].match(/\=/) ? "dojo.map" : oper[1].match(/\^/) ? "dojox.json._distinctFilter" : "dojo.filter");
  213. return prefix + ",function($obj){return " + oper[2] + "})";
  214. }
  215. oper = t.match(/^\[\s*([\/\\].*)\]/); // [/sortexpr,\sortexpr]
  216. if(oper){
  217. // make a copy of the array and then sort it using the sorting expression
  218. return ".concat().sort(function(a,b){" + oper[1].replace(/\s*,?\s*([\/\\])\s*([^,\\\/]+)/g,function(t,a,b){
  219. return "var av= " + b.replace(/\$obj/,"a") + ",bv= " + b.replace(/\$obj/,"b") + // FIXME: Should check to make sure the $obj token isn't followed by characters
  220. ";if(av>bv||bv==null){return " + (a== "/" ? 1 : -1) +";}\n" +
  221. "if(bv>av||av==null){return " + (a== "/" ? -1 : 1) +";}\n";
  222. }) + "return 0;})";
  223. }
  224. oper = t.match(/^\[(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)\]/); // slice [0:3]
  225. if(oper){
  226. call("dojox.json._slice");
  227. return "," + (oper[1] || 0) + "," + (oper[2] || 0) + "," + (oper[3] || 1) + ")";
  228. }
  229. if(t.match(/^\.\.|\.\*|\[\s*\*\s*\]|,/)){ // ..prop and [*]
  230. call("dojox.json._find");
  231. return (t.charAt(1) == '.' ?
  232. ",'" + b + "'" : // ..prop
  233. t.match(/,/) ?
  234. "," + t : // [prop1,prop2]
  235. "") + ")"; // [*]
  236. }
  237. return t;
  238. }).
  239. replace(/(\$obj\s*((\.\s*[\w_$]+\s*)|(\[\s*`([0-9]+)\s*`\]))*)(==|~)\s*`([0-9]+)/g,makeRegex). // create regex matching
  240. replace(/`([0-9]+)\s*(==|~)\s*(\$obj\s*((\.\s*[\w_$]+)|(\[\s*`([0-9]+)\s*`\]))*)/g,function(t,a,b,c,d,e,f,g){ // and do it for reverse =
  241. return makeRegex(t,c,d,e,f,g,b,a);
  242. });
  243. query = prefix + (query.charAt(0) == '$' ? "" : "$") + query.replace(/`([0-9]+|\])/g,function(t,a){
  244. //restore the strings
  245. return a == ']' ? ']' : str[a];
  246. });
  247. // create a function within this scope (so it can use expand and slice)
  248. var executor = eval("1&&function($,$1,$2,$3,$4,$5,$6,$7,$8,$9){var $obj=$;return " + query + "}");
  249. for(var i = 0;i<arguments.length-1;i++){
  250. arguments[i] = arguments[i+1];
  251. }
  252. return obj ? executor.apply(this,arguments) : executor;
  253. };
  254. });