Skip to content

Commit

Permalink
Fixed filtering on contains and excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Dec 1, 2010
1 parent 3676ed1 commit b16713b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
20 changes: 10 additions & 10 deletions lib/js-array.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ exports.operators = {
}), }),
contains: filter(function(array, value){ contains: filter(function(array, value){
if(typeof value == "function"){ if(typeof value == "function"){
return array instanceof Array && array.some(function(i){ return array instanceof Array && array.some(function(v){
return value([i]).length; return value.call([v]).length;
}); });
} }
else{ else{
Expand All @@ -64,8 +64,8 @@ exports.operators = {
}), }),
excludes: filter(function(array, value){ excludes: filter(function(array, value){
if(typeof value == "function"){ if(typeof value == "function"){
return !array.some(function(i){ return !array.some(function(v){
return value([i]).length; return value.call([v]).length;
}); });
} }
else{ else{
Expand Down Expand Up @@ -351,18 +351,18 @@ function query(query, options, target){
// item['foo.bar'] ==> (item && item.foo && item.foo.bar && ...) // item['foo.bar'] ==> (item && item.foo && item.foo.bar && ...)
var path = value.args[0]; var path = value.args[0];
var target = value.args[1]; var target = value.args[1];
if(path instanceof Array){ if (typeof target == "undefined"){
var item = ""; var item = "item";
target = path;
}else if(path instanceof Array){
var item = "item";
var escaped = []; var escaped = [];
for(var i = 0;i < path.length; i++){ for(var i = 0;i < path.length; i++){
escaped.push(stringify(path[i])); escaped.push(stringify(path[i]));
item +="&&item[" + escaped.join("][") + ']'; item +="&&item[" + escaped.join("][") + ']';
} }
}else if (typeof path == "undefined"){
var item = "";
target = path;
}else{ }else{
var item = "&&item[" + stringify(path) + "]"; var item = "item&&item[" + stringify(path) + "]";
} }
// comparison to regexps are special: we cope with eq(), ne(), the others coerce regexp to string // comparison to regexps are special: we cope with eq(), ne(), the others coerce regexp to string
if (value.args[1] instanceof RegExp) { if (value.args[1] instanceof RegExp) {
Expand Down
3 changes: 1 addition & 2 deletions test/js-array.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ exports.testFiltering = function() {
assert.equal(executeQuery("out(price,(5))", {}, data).length, 1); assert.equal(executeQuery("out(price,(5))", {}, data).length, 1);
assert.equal(executeQuery("contains(tags,even)", {}, data).length, 1); assert.equal(executeQuery("contains(tags,even)", {}, data).length, 1);
assert.equal(executeQuery("contains(tags,fun)", {}, data).length, 2); assert.equal(executeQuery("contains(tags,fun)", {}, data).length, 2);
assert.equal(executeQuery("excludes(tags,fun)", {}, data).length, 1); assert.equal(executeQuery("excludes(tags,fun)", {}, data).length, 0);
assert.equal(executeQuery("excludes(tags,ne(fun))", {}, data).length, 1); assert.equal(executeQuery("excludes(tags,ne(fun))", {}, data).length, 1);
assert.equal(executeQuery("excludes(tags,ne(even))", {}, data).length, 0); assert.equal(executeQuery("excludes(tags,ne(even))", {}, data).length, 0);
assert.equal(executeQuery("excludes(tags,ne(even))", {}, data).length, 2);
// eq() on re: should trigger .match() // eq() on re: should trigger .match()
assert.deepEqual(executeQuery("price=re:10", {}, data), [data[0]]); assert.deepEqual(executeQuery("price=re:10", {}, data), [data[0]]);
// ne() on re: should trigger .not(.match()) // ne() on re: should trigger .not(.match())
Expand Down

0 comments on commit b16713b

Please sign in to comment.