Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) | ||
## Use Cases: | ||
- realtime API (pub/sub for mongodb) - [guava](https://github.com/crcn/guava) | ||
- node.js database - [gumbo](https://github.com/crcn/gumbo) | ||
- Caching mongodb queries | ||
## Features: | ||
@@ -11,0 +4,0 @@ |
11
sift.js
@@ -65,5 +65,7 @@ /* | ||
//fixes sift(null, []) issue | ||
if(!statement) statement = { $eq: statement }; | ||
var testers = []; | ||
if(statement) | ||
//if the statement is an object, then we're looking at something like: { key: match } | ||
@@ -352,5 +354,5 @@ if(statement.constructor == Object) { | ||
if(!test(a[i], b)) { | ||
if(test(a[i], b)) { | ||
return true; | ||
return false; | ||
@@ -361,3 +363,3 @@ } | ||
return !n; | ||
return true; | ||
@@ -517,3 +519,2 @@ }, | ||
//build the filter for the sifter | ||
@@ -520,0 +521,0 @@ var filter = _queryParser.parse( query ); |
@@ -1,5 +0,5 @@ | ||
(function(){var o=function(d,e){for(var h={},f=h,g=0,m=d.length-1;g<m;g++)f=f[d[g]]={};f[d[g]]=e;return h},p=new function(){var d=this.test=function(a,b){for(var c=a.exprs,d=0,e=c.length;d<e;d++){var f=c[d];if(!f.e(f.v,b instanceof Date?b.getTime():b,b))return!1}return!0},e=this.parse=function(a,b){var c=[];if(a)if(a.constructor==Object)for(var i in a){var g=f[i]?i:"$trav",k=a[i],j=k;if(h[g])if(i.indexOf(".")>-1&&(j=i.split("."),i=j.shift(),j=k=o(j,k)),k instanceof Array)for(var j=[],l=k.length;l--;)j.push(e(k[l])); | ||
else j=e(k,i);c.push(m(g,i,j))}else c.push(m("$eq",i,a));var n={exprs:c,k:b,test:function(a){return d(n,a)}};return n},h={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},f={$eq:function(a,b){return a.test(b)},$ne:function(a,b){return!a.test(b)},$lt:function(a,b){return a>b},$gt:function(a,b){return a<b},$lte:function(a,b){return a>=b},$gte:function(a,b){return a<=b},$exists:function(a,b){return a==!!b},$in:function(a,b){if(b instanceof Array)for(var c=b.length;c--;){if(a.indexOf(b[c])>-1)return!0}else return a.indexOf(b)> | ||
-1},$not:function(a,b){return!a.test(b)},$type:function(a,b,c){return c?c instanceof a||c.constructor==a:!1},$nin:function(a,b){return!f.$in(a,b)},$mod:function(a,b){return b%a[0]==a[1]},$all:function(a,b){for(var c=a.length;c--;)if(b.indexOf(a[c])==-1)return!1;return!0},$size:function(a,b){return b?a==b.length:!1},$or:function(a,b){for(var c=a.length,e=c;c--;)if(d(a[c],b))return!0;return!e},$nor:function(a,b){for(var c=a.length,e=c;c--;)if(!d(a[c],b))return!0;return!e},$and:function(a,b){for(var c= | ||
a.length;c--;)if(!d(a[c],b))return!1;return!0},$trav:function(a,b){if(b instanceof Array){for(var c=b.length;c--;){var e=b[c];if(e[a.k]&&d(a,e[a.k]))return!0}return!1}return b?d(a,b[a.k]):!1}},g={$eq:function(a){var b;return a instanceof RegExp?a:{test:a instanceof Function?a:function(b){return b instanceof Array?b.indexOf(a)>-1:a==b}}},$ne:function(a){return g.$eq(a)}},m=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:g[a]?g[a](c):c,e:f[a]}}},q=function(d){if(d){if(typeof d=="function")return d}else return function(d){return d}; | ||
throw Error("Unknown sift selector "+d);},r=function(d,e){var h=p.parse(d),f=function(d){for(var f=[],a,b=0,c=d.length;b<c;b++)a=e(d[b]),h.test(a)&&f.push(a);return f};f.test=h.test;f.query=d;return f},l=function(d,e,h){typeof e!="object"&&(h=e,e=void 0);d=r(d,q(h));return e?d(e):d};if(typeof module!="undefined"&&typeof module.exports!="undefined")module.exports=l;else if(typeof window!="undefined")window.sift=l})(); | ||
(function(){var o=function(d,e){for(var h={},f=h,g=0,m=d.length-1;g<m;g++)f=f[d[g]]={};f[d[g]]=e;return h},p=new function(){var d=this.test=function(a,b){for(var c=a.exprs,d=0,e=c.length;d<e;d++){var f=c[d];if(!f.e(f.v,b instanceof Date?b.getTime():b,b))return!1}return!0},e=this.parse=function(a,b){a||(a={$eq:a});var c=[];if(a.constructor==Object)for(var i in a){var g=f[i]?i:"$trav",k=a[i],j=k;if(h[g])if(i.indexOf(".")>-1&&(j=i.split("."),i=j.shift(),j=k=o(j,k)),k instanceof Array)for(var j=[],l= | ||
k.length;l--;)j.push(e(k[l]));else j=e(k,i);c.push(m(g,i,j))}else c.push(m("$eq",i,a));var n={exprs:c,k:b,test:function(a){return d(n,a)}};return n},h={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},f={$eq:function(a,b){return a.test(b)},$ne:function(a,b){return!a.test(b)},$lt:function(a,b){return a>b},$gt:function(a,b){return a<b},$lte:function(a,b){return a>=b},$gte:function(a,b){return a<=b},$exists:function(a,b){return a==!!b},$in:function(a,b){if(b instanceof Array)for(var c=b.length;c--;){if(a.indexOf(b[c])> | ||
-1)return!0}else return a.indexOf(b)>-1},$not:function(a,b){return!a.test(b)},$type:function(a,b,c){return c?c instanceof a||c.constructor==a:!1},$nin:function(a,b){return!f.$in(a,b)},$mod:function(a,b){return b%a[0]==a[1]},$all:function(a,b){for(var c=a.length;c--;)if(b.indexOf(a[c])==-1)return!1;return!0},$size:function(a,b){return b?a==b.length:!1},$or:function(a,b){for(var c=a.length,e=c;c--;)if(d(a[c],b))return!0;return!e},$nor:function(a,b){for(var c=a.length;c--;)if(d(a[c],b))return!1;return!0}, | ||
$and:function(a,b){for(var c=a.length;c--;)if(!d(a[c],b))return!1;return!0},$trav:function(a,b){if(b instanceof Array){for(var c=b.length;c--;){var e=b[c];if(e[a.k]&&d(a,e[a.k]))return!0}return!1}return b?d(a,b[a.k]):!1}},g={$eq:function(a){var b;return a instanceof RegExp?a:{test:a instanceof Function?a:function(b){return b instanceof Array?b.indexOf(a)>-1:a==b}}},$ne:function(a){return g.$eq(a)}},m=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:g[a]?g[a](c):c,e:f[a]}}},q=function(d){if(d){if(typeof d== | ||
"function")return d}else return function(d){return d};throw Error("Unknown sift selector "+d);},r=function(d,e){var h=p.parse(d),f=function(d){for(var f=[],a,b=0,c=d.length;b<c;b++)a=e(d[b]),h.test(a)&&f.push(a);return f};f.test=h.test;f.query=d;return f},l=function(d,e,h){typeof e!="object"&&(h=e,e=void 0);d=r(d,q(h));return e?d(e):d};if(typeof module!="undefined"&&typeof module.exports!="undefined")module.exports=l;else if(typeof window!="undefined")window.sift=l})(); |
@@ -8,3 +8,4 @@ var sift = require('../'), | ||
// console.log(sift(null, [null, undefined, 0, { name: undefined }, { name: 0 }]).length) | ||
// process.exit(); | ||
vows.describe('Sifter').addBatch({ | ||
@@ -20,24 +21,23 @@ | ||
'has a sifted $in count of 3': function(topic) { | ||
assert.isTrue(sift({$in:['craig','john','joe']}, topic).length == 3); | ||
assert.equal(sift({$in:['craig','john','joe']}, topic).length, 3); | ||
}, | ||
'has a sifted $nin count of 2': function(topic) { | ||
assert.isTrue(sift({$nin:['craig','john','joe']}, topic).length == 2); | ||
assert.equal(sift({$nin:['craig','john','joe']}, topic).length,2); | ||
}, | ||
'has a sifted $exists count of 4': function(topic) { | ||
assert.isTrue(sift({$exists:true}, topic).length == 4); | ||
assert.equal(sift({$exists:true}, topic).length, 4); | ||
}, | ||
'has a sifted $and count of 1': function(topic) { | ||
assert.isTrue(sift({$and:['craig']}, topic).length == 1); | ||
assert.equal(sift({$and:['craig']}, topic).length , 1); | ||
}, | ||
'has a sifted $ne count of 4': function(topic) { | ||
assert.isTrue(sift({$ne:null}, topic).length == 4); | ||
assert.equal(sift({$ne:null}, topic).length , 4); | ||
}, | ||
'has a sifted regexp $eq count of 3': function(topic) { | ||
assert.isTrue(sift(/^j\w+$/, topic).length == 3); | ||
assert.equal(sift(/^j\w+$/, topic).length , 3); | ||
}, | ||
@@ -50,19 +50,19 @@ | ||
'has a sifted type string of 4': function(topic) { | ||
assert.isTrue(sift({ $type: String }, topic).length == 4); | ||
assert.equal(sift({ $type: String }, topic).length, 4); | ||
}, | ||
'has a sifted $or count of 2': function(topic) { | ||
assert.isTrue(sift({$or:['craig','jake']}, topic).length == 2); | ||
assert.equal(sift({$or:['craig','jake']}, topic).length, 2); | ||
}, | ||
'has a sifted $nor count of 5': function(topic) { | ||
assert.isTrue(sift({$nor:topic}, topic).length == 5); | ||
assert.equal(sift({$nor:topic}, topic).length , 0); | ||
}, | ||
'has a sifted $not count of 3': function(topic) { | ||
assert.isTrue(sift({$not:{$in:['craig','john']}}, topic).length == 3); | ||
assert.equal(sift({$not:{$in:['craig','john']}}, topic).length, 3); | ||
}, | ||
'has a sifted $size of 2': function(topic) { | ||
assert.isTrue(sift({$size:4}, topic).length == 2); | ||
assert.equal(sift({$size:4}, topic).length, 2); | ||
} | ||
@@ -75,7 +75,7 @@ }, | ||
'has sifted < 200 count of 2': function(topic) { | ||
assert.isTrue(sift({$lt:200}, topic).length == 2); | ||
assert.equal(sift({$lt:200}, topic).length , 2); | ||
}, | ||
'has sifted <= 200 count of 3': function(topic) { | ||
assert.isTrue(sift({$lte:200}, topic).length == 3); | ||
assert.equal(sift({$lte:200}, topic).length , 3); | ||
}, | ||
@@ -85,3 +85,3 @@ | ||
'has sifted > 200 count of 6': function(topic) { | ||
assert.isTrue(sift({$gt:200}, topic).length == 6); | ||
assert.equal(sift({$gt:200}, topic).length , 6); | ||
}, | ||
@@ -91,7 +91,7 @@ | ||
'has sifted >= 200 count of 7': function(topic) { | ||
assert.isTrue(sift({$gte:200}, topic).length == 7); | ||
assert.equal(sift({$gte:200}, topic).length , 7); | ||
}, | ||
'has a sifted modulus 3 count of 3': function(topic) { | ||
assert.isTrue(sift({$mod:[3,0]}, topic).length == 3); | ||
assert.equal(sift({$mod:[3,0]}, topic).length , 3); | ||
}, | ||
@@ -101,3 +101,14 @@ | ||
}, | ||
'with undefined values': { | ||
topic: [null, undefined, 0, { name: undefined }, { name: 0 }], | ||
'has $eq null count of 2': function(topic) { | ||
assert.equal(sift({$eq:null}, topic).length, 2); | ||
}, | ||
'has null count of 2': function(topic) { | ||
assert.equal(sift(null, topic).length, 2); | ||
} | ||
}, | ||
'with simple dates': { | ||
@@ -108,11 +119,11 @@ topic: [new Date(), new Date(1325314860361), new Date(Date.now()+1000), new Date(Date.now()+2000)], | ||
assert.isTrue(sift(new Date(1325314860361), topic).length == 1); | ||
assert.equal(sift(new Date(1325314860361), topic).length , 1); | ||
}, | ||
'has $type count of 4': function(topic) { | ||
assert.isTrue(sift({ $type: Date }, topic).length == 4); | ||
assert.equal(sift({ $type: Date }, topic).length , 4); | ||
}, | ||
'has $gt date count of 2': function(topic) { | ||
assert.isTrue(sift({ $gt: Date.now() }, topic).length == 2); | ||
assert.equal(sift({ $gt: Date.now() }, topic).length , 2); | ||
} | ||
@@ -181,3 +192,3 @@ }, | ||
assert.isTrue(sifted.length == 1); | ||
assert.equal(sifted.length , 1); | ||
@@ -195,3 +206,3 @@ }, | ||
assert.isTrue(sifted.length == 1); | ||
assert.equal(sifted.length , 1); | ||
@@ -227,3 +238,3 @@ }, | ||
assert.isTrue(sifted.length == 2); | ||
assert.equal(sifted.length , 2); | ||
}, | ||
@@ -242,3 +253,3 @@ | ||
assert.isTrue(sifted.length == 0); | ||
assert.equal(sifted.length , 0); | ||
}, | ||
@@ -245,0 +256,0 @@ |
30393
769
331