Skip to content

Commit

Permalink
Eliminate not() in lieu of excludes() and out()
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Dec 1, 2010
1 parent f42a85c commit dffb85e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -112,8 +112,9 @@ for more less operators):
* aggregate(<property|function>,...) - Aggregates the array, grouping by objects that are distinct for the provided properties, and then reduces the remaining other property values using the provided functions
* distinct() - Returns a result set with duplicates removed
* in(<property>,<array-of-values>) - Filters for objects where the specified property's value is in the provided array
* any(<property>,<value | expression>) - Filters for objects where the specified property's value is an array and the array contains any value that equals the provided value or satisfies the provided expression.
* all(<property>,<value | expression>) - Filters for objects where the specified property's value is an array and the array contains values that all equal the provided value or satisfy the provided expression.
* out(<property>,<array-of-values>) - Filters for objects where the specified property's value is not in the provided array
* contains(<property>,<value | expression>) - Filters for objects where the specified property's value is an array and the array contains any value that equals the provided value or satisfies the provided expression.
* excludes(<property>,<value | expression>) - Filters for objects where the specified property's value is an array and the array does not contain any of value that equals the provided value or satisfies the provided expression.
* limit(count,start,maxCount) - Returns the given range of objects from the result set
* and(<query>,<query>,...) - Applies all the given queries
* or(<query>,<query>,...) - The union of the given queries
Expand Down
39 changes: 16 additions & 23 deletions lib/js-array.js
Expand Up @@ -19,7 +19,6 @@ exports.jsOperatorMap = {
"lt" : "<",
"gt" : ">"
};
var negated = false;
exports.operators = {
sort: function(){
var terms = [];
Expand Down Expand Up @@ -48,15 +47,10 @@ exports.operators = {
"in": filter(function(value, values){
return values.indexOf(value) > -1;
}),
not: function(expression){
negated = true;
try{
return expression.call(this);
}finally{
negated = false;
}
},
any: filter(function(array, value){
"out": filter(function(value, values){
return values.indexOf(value) == -1;
}),
contains: filter(function(array, value){
if(typeof value == "function"){
return array.some(function(i){
return value([i]).length;
Expand All @@ -66,16 +60,14 @@ exports.operators = {
return array.indexOf(value) > -1;
}
}),
all: filter(function(array, value){
excludes: filter(function(array, value){
if(typeof value == "function"){
return array.every(function(i){
return !array.some(function(i){
return value([i]).length;
});
}
else{
return array.every(function(i){
return i == value;
});
return array.indexOf(value) == -1;
}
}),
or: function(){
Expand All @@ -101,14 +93,15 @@ exports.operators = {
var selected = {};
for(var i = 0; i < argc; i++){
var propertyName = args[i];
if(object.hasOwnProperty(propertyName)){
selected[propertyName] = object[propertyName];
var value = evaluateProperty(object, propertyName);
if(typeof value != "undefined"){
selected[propertyName] = value;
}
}
return selected;
});
},
exclude: function(){
unselect: function(){
var args = arguments;
var argc = arguments.length;
return this.map(function(object){
Expand Down Expand Up @@ -280,7 +273,7 @@ function filter(condition, not){
var filtered = [];
for(var i = 0, length = this.length; i < length; i++){
var item = this[i];
if(!condition(evaluateProperty(item, property), second) == negated){
if(condition(evaluateProperty(item, property), second)){
filtered.push(item);
}
}
Expand Down Expand Up @@ -348,16 +341,16 @@ function query(query, options, target){
// item['foo.bar'] ==> (item && item.foo && item.foo.bar && ...)
var path = value.args[0];
if(path instanceof Array){
var item = "(";
var item = "";
var escaped = [];
for(var i = 0;i < path.length; i++){
escaped.push(stringify(path[i]));
item += (item.length > 1 ? "&&" : "") + "item[" + escaped.join("][") + ']';
item +="&&item[" + escaped.join("][") + ']';
}
}else{
var item = "(item[" + stringify(path) + "]";
var item = "&&item[" + stringify(path) + "]";
}
item += ')';
item = "item" + item;
if (jsOperator === '===' && value.args[1] instanceof RegExp){
// N.B. matching requires String
item = 'String('+item + '||"")';
Expand Down
19 changes: 10 additions & 9 deletions test/js-array.js
Expand Up @@ -4,7 +4,7 @@ var assert = require("assert"),

var data = [
{
"with.dot": "dotted",
"with/slash": "slashed",
"nested": {
"property": "value"
},
Expand All @@ -22,14 +22,15 @@ exports.testFiltering = function() {
assert.equal(executeQuery("price=lt=10", {}, data).length, 1);
assert.equal(executeQuery("price=lt=11", {}, data).length, 2);
assert.equal(executeQuery("nested/property=value", {}, data).length, 1);
assert.equal(executeQuery("with.dot=dotted", {}, data).length, 1);
assert.equal(executeQuery("not(in(price,(5,10)))", {}, data).length, 0);
assert.equal(executeQuery("not(in(price,(5)))", {}, data).length, 1);
assert.equal(executeQuery("any(tags,even)", {}, data).length, 1);
assert.equal(executeQuery("any(tags,fun)", {}, data).length, 2);
assert.equal(executeQuery("all(tags,fun)", {}, data).length, 1);
assert.equal(executeQuery("all(tags,even)", {}, data).length, 0);
assert.equal(executeQuery("not(all(tags,even))", {}, data).length, 2);
assert.equal(executeQuery("with%2Fslash=slashed", {}, data).length, 1);
assert.equal(executeQuery("out(price,(5,10))", {}, data).length, 0);
assert.equal(executeQuery("out(price,(5))", {}, data).length, 1);
assert.equal(executeQuery("contains(tags,even)", {}, data).length, 1);
assert.equal(executeQuery("contains(tags,fun)", {}, data).length, 2);
assert.equal(executeQuery("excludes(tags,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, 2);
};


Expand Down

0 comments on commit dffb85e

Please sign in to comment.