Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"repository": { | ||
@@ -24,2 +24,2 @@ "type": "git", | ||
} | ||
} | ||
} |
@@ -17,5 +17,7 @@ ## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) | ||
- Function filtering | ||
- Deep object searching | ||
- sub object searching | ||
- dot notiation searching | ||
- Supports node.js | ||
## Simple Examples | ||
@@ -282,3 +284,3 @@ | ||
## Deep Searching | ||
## sub object Searching | ||
@@ -301,3 +303,6 @@ | ||
var sifted = sift({ address: { city: 'Minneapolis' }}, people); // count = 1 | ||
//or | ||
var sifted = sift({'address.city': 'minneapolis', people});//count = 1 | ||
``` | ||
32
sift.js
@@ -14,4 +14,22 @@ /* | ||
/** | ||
*/ | ||
var _convertDotToSubObject = function(keyParts, value) { | ||
var subObject = {}, | ||
currentValue = subObject; | ||
for(var i = 0, n = keyParts.length - 1; i < n; i++) { | ||
currentValue = currentValue[keyParts[i]] = {}; | ||
} | ||
currentValue[keyParts[i]] = value; | ||
return subObject; | ||
} | ||
/** | ||
*/ | ||
var _queryParser = new (function() { | ||
@@ -50,3 +68,3 @@ | ||
if(statement) | ||
if(statement) | ||
//if the statement is an object, then we're looking at something like: { key: match } | ||
@@ -69,2 +87,12 @@ if(statement.constructor == Object) { | ||
if(TRAV_OP[operator]) { | ||
//using dot notation? convert into a sub-object | ||
if(k.indexOf(".") > -1) { | ||
var keyParts = k.split("."); | ||
k = keyParts.shift(); //we're using the first key, so remove it | ||
//embed the value into the sub object, and pull | ||
value = _convertDotToSubObject(keyParts, value); | ||
} | ||
@@ -71,0 +99,0 @@ //*if* the value is an array, then we're dealing with something like: $or, $and |
@@ -1,5 +0,5 @@ | ||
(function(){var o=new function(){var e=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},g=this.parse=function(a,b){var c=[];if(a)if(a.constructor==Object)for(var d in a){var i=m[d]?d:"$trav",k=a[d],l=k;if(f[i])if(k instanceof Array)for(var l=[],j=k.length;j--;)l.push(g(k[j]));else l=g(a[d],d);c.push(h(i,d,l))}else c.push(h("$eq",d,a));var n={exprs:c,k:b,test:function(a){return e(n,a)}};return n},f={$and:!0, | ||
$or:!0,$nor:!0,$trav:!0,$not:!0},m={$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!m.$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,d=c;c--;)if(e(a[c],b))return!0;return!d},$nor:function(a,b){for(var c=a.length,d=c;c--;)if(!e(a[c],b))return!0;return!d},$and:function(a,b){for(var c=a.length;c--;)if(!e(a[c],b))return!1;return!0},$trav:function(a,b){if(b instanceof Array){for(var c=b.length;c--;){var d=b[c]; | ||
if(d[a.k]&&e(a,d[a.k]))return!0}return!1}return b?e(a,b[a.k]):!1}},i={$eq:function(a){var b;return a instanceof RegExp?a:{test:a instanceof Function?a:function(b){return a==b}}},$ne:function(a){return i.$eq(a)}},h=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:i[a]?i[a](c):c,e:m[a]}}},p=function(e){var g=o.parse(e),f=function(e){for(var f=[],h=0,a=e.length;h<a;h++)g.test(e[h])&&f.push(e[h]);return f};f.test=g.test;f.query=e;return f},j=function(e,g){var f=p(e);return g?f(g):f};if(typeof module!= | ||
"undefined"&&typeof module.exports!="undefined")module.exports=j;else if(typeof window!="undefined")window.sift=j})(); | ||
(function(){var o=function(d,h){for(var f={},j=f,g=0,i=d.length-1;g<i;g++)j=j[d[g]]={};j[d[g]]=h;return f},p=new function(){var d=this.test=function(a,b){for(var c=a.exprs,e=0,d=c.length;e<d;e++){var f=c[e];if(!f.e(f.v,b instanceof Date?b.getTime():b,b))return!1}return!0},h=this.parse=function(a,b){var c=[];if(a)if(a.constructor==Object)for(var e in a){var g=j[e]?e:"$trav",l=a[e],k=l;if(f[g])if(e.indexOf(".")>-1&&(k=e.split("."),e=k.shift(),l=o(k,l)),l instanceof Array)for(var k=[],m=l.length;m--;)k.push(h(l[m])); | ||
else k=h(a[e],e);c.push(i(g,e,k))}else c.push(i("$eq",e,a));var n={exprs:c,k:b,test:function(a){return d(n,a)}};return n},f={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},j={$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!j.$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 a==b}}},$ne:function(a){return g.$eq(a)}},i=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:g[a]?g[a](c):c,e:j[a]}}},q=function(d){var h=p.parse(d),f=function(d){for(var f=[],i=0,a=d.length;i< | ||
a;i++)h.test(d[i])&&f.push(d[i]);return f};f.test=h.test;f.query=d;return f},m=function(d,h){var f=q(d);return h?f(h):f};if(typeof module!="undefined"&&typeof module.exports!="undefined")module.exports=m;else if(typeof window!="undefined")window.sift=m})(); |
@@ -184,2 +184,3 @@ var sift = require('../'), | ||
'has a sifted hobbies of photography, cooking, or biking count of 2': function(topic) { | ||
@@ -192,3 +193,3 @@ var sifted = sift({ | ||
assert.isTrue(sifted.length == 2); | ||
assert.equal(sifted.length, 2); | ||
}, | ||
@@ -227,5 +228,39 @@ | ||
assert.isTrue(sifted.length == 0); | ||
} | ||
}, | ||
'has sifted subobject hobbies count of 3': function(topic) { | ||
var sifted = sift({ | ||
"hobbies.name": "photography" | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}, | ||
'has sifted dot-notation hobbies of photography, cooking, and biking count of 3': function(topic) { | ||
var sifted = sift({ | ||
"hobbies.name": {$in: ['photography','cooking','biking']} | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}, | ||
'has sifted to complex dot-search count of 2': function(topic) { | ||
var sifted = sift({ | ||
"hobbies.name": "photography", | ||
"hobbies.places": {$in:['costa rica']}, | ||
"address.state": "MN", | ||
"address.phone": {$exists:true} | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}, | ||
} | ||
} | ||
}).export(module); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54752
653
306