Skip to content
Browse files

fix to issue #15 plus test

  • Loading branch information...
1 parent 8901458 commit 21eca01808e8ffe611718db6f8544ed53f4b2b04 @dvv dvv committed Nov 29, 2010
Showing with 9 additions and 2 deletions.
  1. +4 −2 lib/js-array.js
  2. +5 −0 tests/query.js
View
6 lib/js-array.js
@@ -343,14 +343,16 @@ function query(query, options, target){
if(value && typeof value === "object" && !(value instanceof Array)){
var jsOperator = exports.jsOperatorMap[value.name];
if(jsOperator){
- var item = 'item.' + value.args[0];
+ // item['foo.bar'] ==> (item && item.foo && item.foo.bar && ...)
+ var path = String(value.args[0]);
+ var item = path ? path.split('.').map(function(x,i,a){return 'item.'+a.slice(0,i+1).join('.')}).join('&&') : 'item';
if (jsOperator === '===' && value.args[1] instanceof RegExp){
// N.B. matching requires String
item = 'String(item.' + value.args[0] + ')';
jsOperator = '.match('+value.args[1]+')&&""===';
value.args[1] = '';
}
- return "(function(){var filtered = []; for(var i = 0, length = this.length; i < length; i++){var item = this[i];if(" + item + jsOperator + queryToJS(value.args[1]) + "){filtered.push(item);}} return filtered;})";
+ return "(function(){var filtered = []; for(var i = 0, length = this.length; i < length; i++){var item = this[i];if((" + item + ')' + jsOperator + queryToJS(value.args[1]) + "){filtered.push(item);}} return filtered;})";
}else{
return "(function(){return (operators['" + value.name + "']||exports.missingOperator('" + value.name + "')).call(this" +
(value && value.args && value.args.length > 0 ? (", " + value.args.map(queryToJS).join(",")) : "") +
View
5 tests/query.js
@@ -1,6 +1,9 @@
var assert = require("assert"),
Query = require("../lib/query").Query,
parseQuery = require("../lib/parser").parseQuery;
+ executeQuery = require("../lib/js-array").executeQuery;
+
+Array.prototype.match = function(query, params){return executeQuery(query, params || [], this);};
exports.testBehavior = function() {
//assert.error(parseQuery(), "parseQuery requires a string");
@@ -154,6 +157,8 @@ exports.testBindParameters = function() {
exports.testExecution = function() {
// TODO
+ // nested props: https://github.com/kriszyp/rql/issues/#issue/15
+ assert.deepEqual([{"path":[1,2,3]},{"path":[9,3,7]}].match("foo.cat=3"), []);
};
exports.testStringification = function() {

0 comments on commit 21eca01

Please sign in to comment.
Something went wrong with that request. Please try again.