Comparing version 0.0.18 to 0.1.0
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "0.0.18", | ||
"version": "0.1.0", | ||
"repository": {}, | ||
@@ -6,0 +6,0 @@ "engines": {}, |
@@ -351,3 +351,3 @@ ## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) | ||
sift.use({ | ||
operations: { | ||
operators: { | ||
band: function(a, b) { | ||
@@ -367,1 +367,2 @@ return (a & b) ? 0 : -1; // 0 = exists, -1 = doesn't exist | ||
sift({ $band: IS_PERSON }, [ S_PERSON|EATS_CEREAL, IS_DOG|EATS_BONES, IS_PERSON ]); | ||
``` |
84
sift.js
/* | ||
* Sift | ||
* | ||
* | ||
* Copryright 2011, Craig Condon | ||
* Licensed under MIT | ||
* | ||
* Inspired by mongodb's query language | ||
* Inspired by mongodb's query language | ||
*/ | ||
@@ -27,3 +27,3 @@ | ||
currentValue[keyParts[i]] = value; | ||
return subObject; | ||
@@ -73,3 +73,3 @@ } | ||
var testers = []; | ||
//if the statement is an object, then we're looking at something like: { key: match } | ||
@@ -80,11 +80,19 @@ if(Object.prototype.toString.call(statement) === "[object Object]") { | ||
//find the apropriate operator. If one doesn't exist, then it's a property, which means | ||
//we create a new statement (traversing) | ||
var operator = !!_testers[k] ? k : '$trav', | ||
//find the apropriate operator. If one doesn't exist and the key does not start | ||
//with a $ character, then it's a property, which means we create a new statement | ||
//(traversing) | ||
var operator; | ||
if (!!_testers[k]) { | ||
operator = k; | ||
} else if (k.substr(0, 1) !== '$') { | ||
operator = '$trav'; | ||
} else { | ||
throw new Error('Unknown operator.'); | ||
} | ||
//value of given statement (the match) | ||
value = statement[k], | ||
var value = statement[k]; | ||
//default = match | ||
exprValue = value; | ||
var exprValue = value; | ||
@@ -102,17 +110,17 @@ //if we're working with a traversable operator, then set the expr value | ||
} | ||
//*if* the value is an array, then we're dealing with something like: $or, $and | ||
if(value instanceof Array) { | ||
exprValue = []; | ||
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); | ||
} | ||
} | ||
} | ||
@@ -122,4 +130,4 @@ testers.push(_getExpr(operator, k, exprValue)); | ||
} | ||
//otherwise we're comparing a particular value, so set to eq | ||
@@ -130,3 +138,3 @@ } else { | ||
var stmt = { | ||
var stmt = { | ||
exprs: testers, | ||
@@ -141,5 +149,5 @@ k: key, | ||
}; | ||
return stmt; | ||
} | ||
@@ -232,3 +240,3 @@ | ||
if(~a.indexOf(b[i])) return i; | ||
} | ||
} | ||
@@ -350,3 +358,3 @@ } else { | ||
if(b instanceof Array) { | ||
for(var i = b.length; i--;) { | ||
@@ -360,3 +368,3 @@ var subb = b[i]; | ||
//continue to traverse even if there isn't a value - this is needed for | ||
//continue to traverse even if there isn't a value - this is needed for | ||
//something like name:{$exists:false} | ||
@@ -378,3 +386,3 @@ return priority(a, b ? b[a.k] : undefined); | ||
var _prepare = { | ||
/** | ||
@@ -384,3 +392,3 @@ */ | ||
$eq: function(a) { | ||
var fn; | ||
@@ -393,5 +401,5 @@ | ||
} else { | ||
fn = function(b) { | ||
if(b instanceof Array) { | ||
fn = function(b) { | ||
if(b instanceof Array) { | ||
return ~b.indexOf(a); | ||
@@ -409,6 +417,6 @@ } else { | ||
}, | ||
/** | ||
*/ | ||
$ne: function(a) { | ||
@@ -425,12 +433,12 @@ return _prepare.$eq(a); | ||
return { | ||
return { | ||
//k key | ||
k: key, | ||
k: key, | ||
//v value | ||
v: _prepare[type] ? _prepare[type](v) : v, | ||
v: _prepare[type] ? _prepare[type](v) : v, | ||
//e eval | ||
e: _testers[type] | ||
e: _testers[type] | ||
}; | ||
@@ -451,3 +459,3 @@ | ||
} else | ||
} else | ||
if(typeof selector == 'function') { | ||
@@ -464,6 +472,6 @@ return selector; | ||
var filter = _queryParser.parse( query ); | ||
//the function used to sift through the given array | ||
var self = function(target) { | ||
var sifted = [], results = [], testValue, value, priority; | ||
@@ -572,10 +580,10 @@ | ||
if((typeof module != 'undefined') && (typeof module.exports != 'undefined')) { | ||
module.exports = sift; | ||
} else | ||
} else | ||
//browser? | ||
if(typeof window != 'undefined') { | ||
window.sift = sift; | ||
@@ -582,0 +590,0 @@ |
@@ -1,7 +0,7 @@ | ||
(function(){var m=new function(){function g(a){return a?0:-1}var e=this.priority=function(a,b){for(var c=a.exprs,f=0,d=0,e=c.length;d<e;d++){var h=c[d];if(!~(h=h.e(h.v,b instanceof Date?b.getTime():b,b)))return-1;f+=h}return f},d=this.parse=function(a,b){a||(a={$eq:a});var c=[];if(a.constructor==Object)for(var f in a){var g=l[f]?f:"$trav",k=a[f],h=k;if(j[g]){if(~f.indexOf(".")){h=f.split(".");f=h.shift();for(var n={},m=n,p=0,s=h.length-1;p<s;p++)m=m[h[p]]={};m[h[p]]=k;h=k=n}if(k instanceof Array){h= | ||
[];for(n=k.length;n--;)h.push(d(k[n]))}else h=d(k,f)}c.push(r(g,f,h))}else c.push(r("$eq",f,a));var q={exprs:c,k:b,test:function(a){return!!~q.priority(a)},priority:function(a){return e(q,a)}};return q},j=this.traversable={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},l=this.testers={$eq:function(a,b){return g(a.test(b))},$ne:function(a,b){return g(!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 g(~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 g(!a.test(b))},$type:function(a,b,c){return c?c instanceof a||c.constructor==a?0:-1:-1},$nin:function(a,b){return~l.$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(!~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,f=c;c--;)if(~e(a[c],b))return c;return 0==f?0:-1},$nor:function(a,b){for(var c=a.length;c--;)if(~e(a[c],b))return-1;return 0},$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 f=b[c];if(f[a.k]&&~e(a,f[a.k]))return c}return-1}return e(a,b?b[a.k]:void 0)},$regex:function(a, | ||
b){return RegExp(a).test(b)?0:-1}},k={$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 k.$eq(a)}},r=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:k[a]?k[a](c):c,e:l[a]}}},j=function(g,e,d){"object"!=typeof e&&(d=e,e=void 0);if(d){if("function"!=typeof d)throw Error("Unknown sift selector "+d);}else d=function(d){return d};var j=d,l=m.parse(g);d=function(d){for(var e=[],a,b, | ||
c,f=0,g=d.length;f<g;f++)b=d[f],a=j(b),~(c=l.priority(a))&&e.push({value:b,priority:c});e.sort(function(a,b){return a.priority>b.priority?-1:1});d=Array(e.length);for(f=e.length;f--;)d[f]=e[f].value;return d};d.test=l.test;d.score=l.priority;d.query=g;return e?d(e):d};j.use=function(g){g.operators&&j.useOperators(g.operators)};j.useOperators=function(g){for(var e in g)j.useOperator(e,g[e])};j.useOperator=function(g,e){var d={},d="object"==typeof e?e:{test:e},j="$"+g;m.testers[j]=d.test;if(d.traversable|| | ||
d.traverse)m.traversable[j]=!0};"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=j:"undefined"!=typeof window&&(window.sift=j)})(); | ||
(function(){var m=new function(){function g(a){return a?0:-1}var e=this.priority=function(a,b){for(var c=a.exprs,f=0,d=0,e=c.length;d<e;d++){var h=c[d];if(!~(h=h.e(h.v,b instanceof Date?b.getTime():b,b)))return-1;f+=h}return f},d=this.parse=function(a,b){a||(a={$eq:a});var c=[];if("[object Object]"===Object.prototype.toString.call(a))for(var f in a){var g=l[f]?f:"$trav",k=a[f],h=k;if(j[g]){if(~f.indexOf(".")){h=f.split(".");f=h.shift();for(var n={},m=n,p=0,s=h.length-1;p<s;p++)m=m[h[p]]={};m[h[p]]= | ||
k;h=k=n}if(k instanceof Array){h=[];for(n=k.length;n--;)h.push(d(k[n]))}else h=d(k,f)}c.push(r(g,f,h))}else c.push(r("$eq",f,a));var q={exprs:c,k:b,test:function(a){return!!~q.priority(a)},priority:function(a){return e(q,a)}};return q},j=this.traversable={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},l=this.testers={$eq:function(a,b){return g(a.test(b))},$ne:function(a,b){return g(!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 g(~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 g(!a.test(b))},$type:function(a,b,c){return c?c instanceof a||c.constructor==a?0:-1:-1},$nin:function(a,b){return~l.$in(a,b)?-1:0},$mod:function(a,b){return b%a[0]==a[1]?0:-1}, | ||
$all:function(a,b){b=b||(b=[]);for(var c=a.length;c--;)if(!~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,f=c;c--;)if(~e(a[c],b))return c;return 0==f?0:-1},$nor:function(a,b){for(var c=a.length;c--;)if(~e(a[c],b))return-1;return 0},$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 f=b[c];if(f[a.k]&&~e(a,f[a.k]))return c}return-1}return e(a, | ||
b?b[a.k]:void 0)},$regex:function(a,b){return RegExp(a).test(b)?0:-1}},k={$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 k.$eq(a)}},r=function(a,b,c){c=c instanceof Date?c.getTime():c;return{k:b,v:k[a]?k[a](c):c,e:l[a]}}},j=function(g,e,d){"object"!=typeof e&&(d=e,e=void 0);if(d){if("function"!=typeof d)throw Error("Unknown sift selector "+d);}else d=function(d){return d};var j=d,l=m.parse(g); | ||
d=function(d){for(var e=[],a,b,c,f=0,g=d.length;f<g;f++)b=d[f],a=j(b),~(c=l.priority(a))&&e.push({value:b,priority:c});e.sort(function(a,b){return a.priority>b.priority?-1:1});d=Array(e.length);for(f=e.length;f--;)d[f]=e[f].value;return d};d.test=l.test;d.score=l.priority;d.query=g;return e?d(e):d};j.use=function(g){g.operators&&j.useOperators(g.operators)};j.useOperators=function(g){for(var e in g)j.useOperator(e,g[e])};j.useOperator=function(g,e){var d={},d="object"==typeof e?e:{test:e},j="$"+g; | ||
m.testers[j]=d.test;if(d.traversable||d.traverse)m.traversable[j]=!0};"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=j:"undefined"!=typeof window&&(window.sift=j)})(); |
@@ -20,2 +20,8 @@ var expect = require("expect.js"), | ||
}); | ||
}); | ||
it("throws an exception on unknown operators", function() { | ||
expect(function () { | ||
sift({$abc: 23}, topic); | ||
}).to.throwError("Unknown operator."); | ||
}); | ||
}); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
367
2
40565
19
1023