Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Eliminate not() in lieu of excludes() and out()

  • Loading branch information...
commit dffb85e7223a953774f8d97e50dddae7efaaea39 1 parent f42a85c
Kris Zyp kriszyp authored
Showing with 29 additions and 34 deletions.
  1. +3 −2 README.md
  2. +16 −23 lib/js-array.js
  3. +10 −9 test/js-array.js
5 README.md
View
@@ -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
39 lib/js-array.js
View
@@ -19,7 +19,6 @@ exports.jsOperatorMap = {
"lt" : "<",
"gt" : ">"
};
-var negated = false;
exports.operators = {
sort: function(){
var terms = [];
@@ -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;
@@ -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(){
@@ -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){
@@ -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);
}
}
@@ -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 + '||"")';
19 test/js-array.js
View
@@ -4,7 +4,7 @@ var assert = require("assert"),
var data = [
{
- "with.dot": "dotted",
+ "with/slash": "slashed",
"nested": {
"property": "value"
},
@@ -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);
};
Please sign in to comment.
Something went wrong with that request. Please try again.