Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
224
sift.js
@@ -40,18 +40,21 @@ /* | ||
var test = this.test = function(statement, data) { | ||
var priority = this.priority = function(statement, data) { | ||
var exprs = statement.exprs; | ||
var exprs = statement.exprs, | ||
priority = 0; | ||
//generally, expressions are ordered from least efficient, to most efficient. | ||
for(var i = 0, n = exprs.length; i < n; i++) { | ||
var expr = exprs[i]; | ||
var expr = exprs[i], | ||
p; | ||
if(!~(p = expr.e(expr.v, _comparable(data), data))) return -1; | ||
if(!expr.e(expr.v, _comparable(data), data)) return false; | ||
priority += p; | ||
} | ||
return true; | ||
return priority; | ||
} | ||
@@ -91,3 +94,3 @@ | ||
//using dot notation? convert into a sub-object | ||
if(k.indexOf(".") > -1) { | ||
if(~k.indexOf(".")) { | ||
var keyParts = k.split("."); | ||
@@ -105,12 +108,8 @@ k = keyParts.shift(); //we're using the first key, so remove it | ||
for(var i = value.length; i--;) { | ||
exprValue.push(parse(value[i])); | ||
exprValue.push(parse(value[i])); | ||
} | ||
//otherwise we're dealing with $trav | ||
} else { | ||
} else { | ||
exprValue = parse(value, k); | ||
} | ||
@@ -127,17 +126,14 @@ } | ||
} else { | ||
testers.push(_getExpr('$eq', k, statement)); | ||
} | ||
var stmt = { | ||
exprs: testers, | ||
k: key, | ||
test: function(value) { | ||
return test(stmt, value); | ||
} | ||
return !!~stmt.priority(value); | ||
}, | ||
priority: function(value) { | ||
return priority(stmt, value); | ||
} | ||
}; | ||
@@ -152,3 +148,2 @@ | ||
var TRAV_OP = { | ||
$and: true, | ||
@@ -159,19 +154,16 @@ $or: true, | ||
$not: true | ||
}; | ||
} | ||
function _comparable(value) { | ||
if(value instanceof Date) { | ||
return value.getTime(); | ||
} else { | ||
return value; | ||
} | ||
} | ||
function btop(value) { | ||
return value ? 0 : -1; | ||
} | ||
@@ -184,5 +176,3 @@ var _testers = { | ||
$eq: function(a, b) { | ||
return a.test(b); | ||
return btop(a.test(b)); | ||
}, | ||
@@ -194,5 +184,3 @@ | ||
$ne: function(a, b) { | ||
return !a.test(b); | ||
return btop(!a.test(b)); | ||
}, | ||
@@ -204,5 +192,3 @@ | ||
$lt: function(a, b) { | ||
return a > b; | ||
return btop(a > b); | ||
}, | ||
@@ -214,5 +200,3 @@ | ||
$gt: function(a, b) { | ||
return a < b; | ||
return btop(a < b); | ||
}, | ||
@@ -224,5 +208,3 @@ | ||
$lte: function(a, b) { | ||
return a >= b; | ||
return btop(a >= b); | ||
}, | ||
@@ -234,5 +216,3 @@ | ||
$gte: function(a, b) { | ||
return a <= b; | ||
return btop(a <= b); | ||
}, | ||
@@ -245,5 +225,3 @@ | ||
$exists: function(a, b) { | ||
return a == !!b; | ||
return btop(a == !!b); | ||
}, | ||
@@ -260,13 +238,11 @@ | ||
for(var i = b.length; i--;) { | ||
if(a.indexOf(b[i]) > -1) return true; | ||
if(~a.indexOf(b[i])) return i; | ||
} | ||
} else { | ||
return btop(~a.indexOf(b)); | ||
} | ||
return a.indexOf(b) > -1; | ||
} | ||
return -1; | ||
}, | ||
@@ -278,3 +254,3 @@ | ||
$not: function(a, b) { | ||
return !a.test(b); | ||
return btop(!a.test(b)); | ||
}, | ||
@@ -288,4 +264,3 @@ | ||
//instanceof doesn't work for strings / boolean. instanceof works with inheritance | ||
return org ? org instanceof a || org.constructor == a : false; | ||
return org ? btop(org instanceof a || org.constructor == a) : -1; | ||
}, | ||
@@ -298,5 +273,3 @@ | ||
$nin: function(a, b) { | ||
return !_testers.$in(a, b); | ||
return ~_testers.$in(a, b) ? -1 : 0; | ||
}, | ||
@@ -308,5 +281,3 @@ | ||
$mod: function(a, b) { | ||
return b % a[0] == a[1]; | ||
return b % a[0] == a[1] ? 0 : -1; | ||
}, | ||
@@ -319,13 +290,7 @@ | ||
for(var i = a.length; i--;) { | ||
var v = a[i]; | ||
if(b.indexOf(v) == -1) return false; | ||
if(b.indexOf(a[i]) == -1) return i; | ||
} | ||
return true; | ||
return -1; | ||
}, | ||
@@ -337,5 +302,3 @@ | ||
$size: function(a, b) { | ||
return b ? a == b.length : false; | ||
return b ? btop(a == b.length) : -1; | ||
}, | ||
@@ -348,16 +311,11 @@ | ||
var i = a.length, n = i; | ||
var i = a.length, p, n = i; | ||
for(; i--;) { | ||
if(test(a[i], b)) { | ||
return true; | ||
if(~priority(a[i], b)) { | ||
return i; | ||
} | ||
} | ||
return !n; | ||
return btop(n == 0); | ||
}, | ||
@@ -373,13 +331,8 @@ | ||
for(; i--;) { | ||
if(test(a[i], b)) { | ||
return false; | ||
if(~priority(a[i], b)) { | ||
return -1; | ||
} | ||
} | ||
return true; | ||
return 0; | ||
}, | ||
@@ -393,11 +346,8 @@ | ||
for(var i = a.length; i--;) { | ||
if(!test(a[i], b)) { | ||
return false; | ||
if(!~priority(a[i], b)) { | ||
return -1; | ||
} | ||
} | ||
return true; | ||
return 0; | ||
}, | ||
@@ -413,15 +363,10 @@ | ||
for(var i = b.length; i--;) { | ||
var subb = b[i]; | ||
if(subb[a.k] && test(a, subb[a.k])) return true; | ||
if(subb[a.k] && ~priority(a, subb[a.k])) return i; | ||
} | ||
return false; | ||
return -1; | ||
} | ||
return b ? test(a, b[a.k]) : false; | ||
return b ? priority(a, b[a.k]) : -1; | ||
} | ||
@@ -440,31 +385,18 @@ } | ||
if(a instanceof RegExp) { | ||
return a; | ||
} else if (a instanceof Function) { | ||
fn = a; | ||
} else { | ||
fn = function(b) { | ||
if(b instanceof Array) { | ||
return b.indexOf(a) > -1; | ||
}else{ | ||
fn = function(b) { | ||
if(b instanceof Array) { | ||
return ~b.indexOf(a); | ||
} else { | ||
return a == b; | ||
} | ||
} | ||
} | ||
return { | ||
test: fn | ||
} | ||
@@ -478,3 +410,2 @@ | ||
$ne: function(a) { | ||
return _prepare.$eq(a); | ||
@@ -492,5 +423,2 @@ } | ||
//type | ||
// t: type, | ||
//k key | ||
@@ -517,21 +445,8 @@ k: key, | ||
return function(value) { | ||
return value; | ||
}; | ||
} else | ||
/*if(typeof selector == 'string') { | ||
return function(value) { | ||
return value[selector]; | ||
}; | ||
} else */ | ||
if(typeof selector == 'function') { | ||
return selector; | ||
} | ||
@@ -550,3 +465,3 @@ | ||
var sifted = [], value; | ||
var sifted = [], results = [], value, priority; | ||
@@ -557,10 +472,28 @@ //I'll typically start from the end, but in this case we need to keep the order | ||
value = selector(target[i]); | ||
if(filter.test( value )) sifted.push(value); | ||
//priority = -1? it's not something we can use. | ||
if(!~(priority = filter.priority( value ))) continue; | ||
//push all the sifted values to be sorted later. This is important particularly for statements | ||
//such as $or | ||
sifted.push({ | ||
value: value, | ||
priority: priority | ||
}); | ||
} | ||
return sifted; | ||
//sort the values | ||
sifted.sort(function(a, b) { | ||
return a.priority > b.priority ? -1 : 1; | ||
}); | ||
var values = Array(sifted.length); | ||
//finally, fetch the values & return them. | ||
for(var i = sifted.length; i--;) { | ||
values[i] = sifted[i].value; | ||
} | ||
return values; | ||
} | ||
@@ -570,2 +503,3 @@ | ||
self.test = filter.test; | ||
self.score = filter.priority; | ||
self.query = query; | ||
@@ -572,0 +506,0 @@ |
@@ -1,5 +0,6 @@ | ||
(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})(); | ||
(function(){var o=function(b,e){for(var j={},g=j,f=0,h=b.length-1;f<h;f++)g=g[b[f]]={};g[b[f]]=e;return j},p=new function(){function b(a){return a?0:-1}var e=this.priority=function(a,c){for(var d=a.exprs,b=0,e=0,f=d.length;e<f;e++){var i=d[e];if(!~(i=i.e(i.v,c instanceof Date?c.getTime():c,c)))return-1;b+=i}return b},j=this.parse=function(a,c){a||(a={$eq:a});var d=[];if(a.constructor==Object)for(var b in a){var h=f[b]?b:"$trav",k=a[b],i=k;if(g[h])if(~b.indexOf(".")&&(i=b.split("."),b=i.shift(),i= | ||
k=o(i,k)),k instanceof Array)for(var i=[],l=k.length;l--;)i.push(j(k[l]));else i=j(k,b);d.push(n(h,b,i))}else d.push(n("$eq",b,a));var m={exprs:d,k:c,test:function(a){return!!~m.priority(a)},priority:function(a){return e(m,a)}};return m},g={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},f={$eq:function(a,c){return b(a.test(c))},$ne:function(a,c){return b(!a.test(c))},$lt:function(a,c){return b(a>c)},$gt:function(a,c){return b(a<c)},$lte:function(a,c){return b(a>=c)},$gte:function(a,c){return b(a<=c)},$exists:function(a, | ||
c){return b(a==!!c)},$in:function(a,c){if(c instanceof Array)for(var d=c.length;d--;){if(~a.indexOf(c[d]))return d}else return b(~a.indexOf(c));return-1},$not:function(a,c){return b(!a.test(c))},$type:function(a,c,d){return d?b(d instanceof a||d.constructor==a):-1},$nin:function(a,c){return~f.$in(a,c)?-1:0},$mod:function(a,c){return c%a[0]==a[1]?0:-1},$all:function(a,c){for(var b=a.length;b--;)if(-1==c.indexOf(a[b]))return b;return-1},$size:function(a,c){return c?b(a==c.length):-1},$or:function(a, | ||
c){for(var d=a.length,f=d;d--;)if(~e(a[d],c))return d;return b(0==f)},$nor:function(a,c){for(var b=a.length;b--;)if(~e(a[b],c))return-1;return 0},$and:function(a,c){for(var b=a.length;b--;)if(!~e(a[b],c))return-1;return 0},$trav:function(a,c){if(c instanceof Array){for(var b=c.length;b--;){var f=c[b];if(f[a.k]&&~e(a,f[a.k]))return b}return-1}return c?e(a,c[a.k]):-1}},h={$eq:function(a){return a instanceof RegExp?a:{test:a instanceof Function?a:function(c){return c instanceof Array?~c.indexOf(a):a== | ||
c}}},$ne:function(a){return h.$eq(a)}},n=function(a,c,b){b=b instanceof Date?b.getTime():b;return{k:c,v:h[a]?h[a](b):b,e:f[a]}}},q=function(b){if(b){if("function"==typeof b)return b}else return function(b){return b};throw Error("Unknown sift selector "+b);},r=function(b,e){var j=p.parse(b),g=function(b){for(var h=[],g,a,c=0,d=b.length;c<d;c++)g=e(b[c]),~(a=j.priority(g))&&h.push({value:g,priority:a});h.sort(function(a,b){return a.priority>b.priority?-1:1});b=Array(h.length);for(c=h.length;c--;)b[c]= | ||
h[c].value;return b};g.test=j.test;g.score=j.priority;g.query=b;return g},l=function(b,e,j){"object"!=typeof e&&(j=e,e=void 0);b=r(b,q(j));return e?b(e):b};"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=l:"undefined"!=typeof window&&(window.sift=l)})(); |
@@ -79,3 +79,3 @@ var sift = require('../'); | ||
on('cycle', function(event, bench) { | ||
console.log(String(bench)); | ||
console.log(String(event)); | ||
}). | ||
@@ -82,0 +82,0 @@ on('complete', function() { |
@@ -184,2 +184,3 @@ var sift = require('../'), | ||
assert.equal(sifted.length , 1); | ||
@@ -186,0 +187,0 @@ |
31232
790