Comparing version 0.0.10 to 0.0.12
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "0.0.10", | ||
"version": "0.0.12", | ||
"repository": { | ||
@@ -24,4 +24,4 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha ./test; karma start" | ||
"test": "mocha ./test" | ||
} | ||
} |
@@ -11,3 +11,4 @@ ## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) | ||
- Supports node.js, and web | ||
- Small (2 kb minified) library | ||
- Small (2 kb minified) library | ||
- Custom Expressions | ||
@@ -331,1 +332,25 @@ | ||
``` | ||
## Custom Expressions | ||
You can add your own expressions. For instance - say you want to do some bitmask filtering, you could add this example: | ||
```javascript | ||
sift.use({ | ||
operations: { | ||
band: function(a, b) { | ||
return (a & b) ? 0 : -1; // 0 = exists, -1 = doesn't exist | ||
} | ||
} | ||
}) | ||
// ops | ||
var IS_ANIMAL = 2, | ||
IS_PERSON = IS_ANIMAL << 1, | ||
IS_DOG = IS_PERSON << 1, | ||
EATS_CEREAL = IS_DOG << 1, | ||
EATS_BONES = EATS_CEREAL << 1; | ||
sift({ $band: IS_PERSON }, [ S_PERSON|EATS_CEREAL, IS_DOG|EATS_BONES, IS_PERSON ]); |
@@ -114,3 +114,2 @@ /* | ||
} | ||
@@ -214,3 +213,3 @@ testers.push(_getExpr(operator, k, exprValue)); | ||
$exists: function(a, b) { | ||
return btop(a == !!b); | ||
return btop(a === (b != null)) | ||
}, | ||
@@ -340,2 +339,4 @@ | ||
if(b instanceof Array) { | ||
@@ -351,3 +352,5 @@ | ||
return b ? priority(a, b[a.k]) : -1; | ||
//continue to traverse even if there isn't a value - this is needed for | ||
//something like name:{$exists:false} | ||
return priority(a, b ? b[a.k] : undefined); | ||
} | ||
@@ -354,0 +357,0 @@ } |
@@ -1,6 +0,7 @@ | ||
(function(){var t=new function(){function h(a){return a?0:-1}var f=this.priority=function(a,b){for(var c=a.exprs,e=0,d=0,f=c.length;d<f;d++){var g=c[d];if(!~(g=g.e(g.v,b instanceof Date?b.getTime():b,b)))return-1;e+=g}return e},d=this.parse=function(a,b){a||(a={$eq:a});var c=[];if(a.constructor==Object)for(var e in a){var l=k[e]?e:"$trav",j=a[e],g=j;if(s[l]){if(~e.indexOf(".")){g=e.split(".");e=g.shift();for(var h={},p=h,n=0,m=g.length-1;n<m;n++)p=p[g[n]]={};p[g[n]]=j;g=j=h}if(j instanceof Array){g= | ||
[];for(h=j.length;h--;)g.push(d(j[h]))}else g=d(j,e)}c.push(r(l,e,g))}else c.push(r("$eq",e,a));var q={exprs:c,k:b,test:function(a){return!!~q.priority(a)},priority:function(a){return f(q,a)}};return q},s={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},k={$eq:function(a,b){return h(a.test(b))},$ne:function(a,b){return h(!a.test(b))},$lt:function(a,b){return a>b?0:-1},$gt:function(a,b){return a<b?0:-1},$lte:function(a,b){return a>=b?0:-1},$gte:function(a,b){return a<=b?0:-1},$exists:function(a,b){return a== | ||
!!b?0:-1},$in:function(a,b){if(b instanceof Array)for(var c=b.length;c--;){if(~a.indexOf(b[c]))return c}else return h(~a.indexOf(b));return-1},$not:function(a,b){if(!a.test)throw Error("$not test should include an expression, not a value. Use $ne instead.");return h(!a.test(b))},$type:function(a,b,c){return c?c instanceof a||c.constructor==a?0:-1:-1},$nin:function(a,b){return~k.$in(a,b)?-1:0},$mod:function(a,b){return b%a[0]==a[1]?0:-1},$all:function(a,b){for(var c=a.length;c--;)if(-1==b.indexOf(a[c]))return-1; | ||
return 0},$size:function(a,b){return b?a==b.length?0:-1:-1},$or:function(a,b){for(var c=a.length,e=c;c--;)if(~f(a[c],b))return c;return 0==e?0:-1},$nor:function(a,b){for(var c=a.length;c--;)if(~f(a[c],b))return-1;return 0},$and:function(a,b){for(var c=a.length;c--;)if(!~f(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]&&~f(a,e[a.k]))return c}return-1}return b?f(a,b[a.k]):-1}},l={$eq:function(a){return a instanceof RegExp?a:{test:a instanceof | ||
Function?a:function(b){return b instanceof Array?~b.indexOf(a):a==b}}},$ne:function(a){return l.$eq(a)}},r=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:l[a]?l[a](c):c,e:k[a]}}},m=function(h,f,d){"object"!=typeof f&&(d=f,f=void 0);if(d){if("function"!=typeof d)throw Error("Unknown sift selector "+d);}else d=function(d){return d};var m=d,k=t.parse(h);d=function(d){for(var f=[],a,b,c=0,e=d.length;c<e;c++)if(a=m(d[c]),~(b=k.priority(a)))"boolean"===typeof b&&(b=btop(b)),f.push({value:a, | ||
priority:b});f.sort(function(a,b){return a.priority>b.priority?-1:1});d=Array(f.length);for(c=f.length;c--;)d[c]=f[c].value;return d};d.test=k.test;d.score=k.priority;d.query=h;return f?d(f):d};"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=m:"undefined"!=typeof window&&(window.sift=m)})(); | ||
(function(){var l=new function(){function d(a){return a?0:-1}var f=this.priority=function(a,b){for(var c=a.exprs,e=0,f=0,d=c.length;f<d;f++){var g=c[f];if(!~(g=g.e(g.v,b instanceof Date?b.getTime():b,b)))return-1;e+=g}return e},e=this.parse=function(a,b){a||(a={$eq:a});var c=[];if(a.constructor==Object)for(var d in a){var m=k[d]?d:"$trav",j=a[d],g=j;if(h[m]){if(~d.indexOf(".")){g=d.split(".");d=g.shift();for(var n={},l=n,p=0,s=g.length-1;p<s;p++)l=l[g[p]]={};l[g[p]]=j;g=j=n}if(j instanceof Array){g= | ||
[];for(n=j.length;n--;)g.push(e(j[n]))}else g=e(j,d)}c.push(r(m,d,g))}else c.push(r("$eq",d,a));var q={exprs:c,k:b,test:function(a){return!!~q.priority(a)},priority:function(a){return f(q,a)}};return q},h=this.traversable={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},k=this.testers={$eq:function(a,b){return d(a.test(b))},$ne:function(a,b){return d(!a.test(b))},$lt:function(a,b){return a>b?0:-1},$gt:function(a,b){return a<b?0:-1},$lte:function(a,b){return a>=b?0:-1},$gte:function(a,b){return a<=b?0:-1}, | ||
$exists:function(a,b){return a===(null!=b)?0:-1},$in:function(a,b){if(b instanceof Array)for(var c=b.length;c--;){if(~a.indexOf(b[c]))return c}else return d(~a.indexOf(b));return-1},$not:function(a,b){if(!a.test)throw Error("$not test should include an expression, not a value. Use $ne instead.");return d(!a.test(b))},$type:function(a,b,c){return c?c instanceof a||c.constructor==a?0:-1:-1},$nin:function(a,b){return~k.$in(a,b)?-1:0},$mod:function(a,b){return b%a[0]==a[1]?0:-1},$all:function(a,b){for(var c= | ||
a.length;c--;)if(-1==b.indexOf(a[c]))return-1;return 0},$size:function(a,b){return b?a==b.length?0:-1:-1},$or:function(a,b){for(var c=a.length,d=c;c--;)if(~f(a[c],b))return c;return 0==d?0:-1},$nor:function(a,b){for(var c=a.length;c--;)if(~f(a[c],b))return-1;return 0},$and:function(a,b){for(var c=a.length;c--;)if(!~f(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]&&~f(a,d[a.k]))return c}return-1}return f(a,b?b[a.k]:void 0)}},m={$eq:function(a){return a instanceof | ||
RegExp?a:{test:a instanceof Function?a:function(b){return b instanceof Array?~b.indexOf(a):a==b}}},$ne:function(a){return m.$eq(a)}},r=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:m[a]?m[a](c):c,e:k[a]}}},h=function(d,f,e){"object"!=typeof f&&(e=f,f=void 0);if(e){if("function"!=typeof e)throw Error("Unknown sift selector "+e);}else e=function(d){return d};var h=e,k=l.parse(d);e=function(d){for(var e=[],a,b,c=0,f=d.length;c<f;c++)a=h(d[c]),~(b=k.priority(a))&&e.push({value:a,priority:b}); | ||
e.sort(function(a,b){return a.priority>b.priority?-1:1});d=Array(e.length);for(c=e.length;c--;)d[c]=e[c].value;return d};e.test=k.test;e.score=k.priority;e.query=d;return f?e(f):e};h.use=function(d){d.operators&&h.useOperators(d.operators)};h.useOperators=function(d){for(var f in d)h.useOperator(f,d[f])};h.useOperator=function(d,f){var e={},e="object"==typeof f?f:{test:f},h="$"+d;l.testers[h]=e.test;if(e.traversable||e.traverse)l.traversable[h]=!0};"undefined"!=typeof module&&"undefined"!=typeof module.exports? | ||
module.exports=h:"undefined"!=typeof window&&(window.sift=h)})(); |
@@ -5,2 +5,3 @@ var expect = require("expect.js"), | ||
describe("objects", function () { | ||
var topic = [{ | ||
@@ -7,0 +8,0 @@ name: 'craig', |
@@ -7,2 +7,3 @@ var expect = require("expect.js"), | ||
var topic = ['craig','john','jake','joe', null]; | ||
@@ -9,0 +10,0 @@ |
@@ -7,3 +7,3 @@ var expect = require("expect.js"), | ||
var topic = [null, undefined, 0, { name: undefined }, { name: 0 }]; | ||
var topic = [null, undefined, 0, { name: undefined }, { name: 0 }, { name: null }, { name: "craig" }, { name: null }]; | ||
@@ -18,2 +18,15 @@ it("has $eq null count of 2", function() { | ||
it("has $exists:true count of 6", function() { | ||
assert.equal(sift({$exists:true}, topic).length, 6); | ||
}); | ||
it("has name:$exists:true count of 2", function() { | ||
assert.equal(sift({name:{$exists:true}}, topic).length, 2); | ||
}) | ||
it("has name:$exists:false count of 3", function() { | ||
assert.equal(sift({name:{$exists:false}}, topic).length, 6); | ||
}) | ||
}); |
45537
1267
355