Socket
Socket
Sign inDemoInstall

sift

Package Overview
Dependencies
Maintainers
1
Versions
155
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sift - npm Package Compare versions

Comparing version 0.2.4 to 0.4.5

.coveralls.yml

17

package.json
{
"name": "sift",
"description": "mongodb query style array filtering",
"version": "0.2.4",
"version": "0.4.5",
"repository": {},

@@ -9,19 +9,14 @@ "engines": {},

"devDependencies": {
"benchmark": "*",
"browserify": "~3.31.2",
"colors": "*",
"coveralls": "^2.11.2",
"expect.js": "0.2.x",
"istanbul": "^0.3.5",
"jscs": "^1.11.0",
"jshint": "^2.6.0",
"karma": "0.8.x",
"mocha": "1.9.x",
"sardines": "0.4.x",
"uglify-js": "^2.4.16",
"underscore": "*",
"validator": "*"
"mocha": "^2.1.0",
"uglify-js": "^2.4.16"
},
"main": "./sift.js",
"scripts": {
"test": "make lint test-node"
"test": "make lint test-coveralls"
}
}

@@ -1,2 +0,2 @@

## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js)
## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) [![Coverage Status](https://coveralls.io/repos/crcn/sift.js/badge.svg)](https://coveralls.io/r/crcn/sift.js)

@@ -3,0 +3,0 @@ For extended documentation, checkout http://docs.mongodb.org/manual/reference/operator/query/

@@ -38,6 +38,5 @@ /*

var priority = this.priority = function(statement, data) {
var test = this.test = function(statement, data) {
var exprs = statement.exprs;
var priority = 0;

@@ -48,11 +47,8 @@ //generally, expressions are ordered from least efficient, to most efficient.

var expr = exprs[i];
var 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 priority;
return true;
};

@@ -85,3 +81,3 @@

} else {
throw new Error("Unknown operator.");
throw new Error("Unknown operator " + k + ".");
}

@@ -133,6 +129,3 @@

test: function(value) {
return !!~stmt.priority(value);
},
priority: function(value) {
return priority(stmt, value);
return test(stmt, value);
}

@@ -146,7 +139,7 @@ };

var TRAV_OP = this.traversable = {
$and: true,
$or: true,
$nor: true,
$trav: true,
$not: true
$and : true,
$or : true,
$nor : true,
$trav : true,
$not : true
};

@@ -163,3 +156,3 @@

function btop(value) {
return value ? 0 : -1;
return !!value;
}

@@ -173,3 +166,3 @@

$eq: function(a, b) {
return btop(a.test(b));
return a.test(b);
},

@@ -181,3 +174,3 @@

$ne: function(a, b) {
return btop(!a.test(b));
return !a.test(b);
},

@@ -189,3 +182,3 @@

$lt: function(a, b) {
return btop(a > b);
return a > b;
},

@@ -197,3 +190,3 @@

$gt: function(a, b) {
return btop(a < b);
return a < b;
},

@@ -205,3 +198,3 @@

$lte: function(a, b) {
return btop(a >= b);
return a >= b;
},

@@ -213,3 +206,3 @@

$gte: function(a, b) {
return btop(a <= b);
return a <= b;
},

@@ -221,3 +214,3 @@

$exists: function(a, b) {
return btop(a === (b != null));
return a === (b != null);
},

@@ -234,10 +227,10 @@

for (var i = b.length; i--;) {
if (~a.indexOf(b[i])) return i;
if (~a.indexOf(b[i])) return true;
}
} else {
return btop(~a.indexOf(b));
return ~a.indexOf(b);
}
return -1;
return false;
},

@@ -250,3 +243,3 @@

if (!a.test) throw new Error("$not test should include an expression, not a value. Use $ne instead.");
return btop(!a.test(b));
return !a.test(b);
},

@@ -258,5 +251,4 @@

$type: function(a, b, org) {
//instanceof doesn't work for strings / boolean. instanceof works with inheritance
return org ? btop(org instanceof a || org.constructor == a) : -1;
return org != null ? org instanceof a || org.constructor == a : false;
},

@@ -268,3 +260,3 @@

$nin: function(a, b) {
return ~_testers.$in(a, b) ? -1 : 0;
return !_testers.$in(a, b);
},

@@ -276,3 +268,3 @@

$mod: function(a, b) {
return b % a[0] == a[1] ? 0 : -1;
return b % a[0] == a[1];
},

@@ -288,6 +280,6 @@

var indexInB = ~b.indexOf(a1);
if (!indexInB) return -1;
if (!indexInB) return false;
}
return 0;
return true;
},

@@ -299,3 +291,3 @@

$size: function(a, b) {
return b ? btop(a == b.length) : -1;
return b ? a === b.length : false;
},

@@ -312,8 +304,8 @@

for (; i--;) {
if (~priority(a[i], b)) {
return i;
if (test(a[i], b)) {
return true;
}
}
return btop(n === 0);
return n === 0;
},

@@ -329,8 +321,8 @@

for (; i--;) {
if (~priority(a[i], b)) {
return -1;
if (test(a[i], b)) {
return false;
}
}
return 0;
return true;
},

@@ -344,8 +336,8 @@

for (var i = a.length; i--;) {
if (!~priority(a[i], b)) {
return -1;
if (!test(a[i], b)) {
return false;
}
}
return 0;
return true;
},

@@ -362,6 +354,6 @@

var subb = b[i];
if (subb[a.k] && ~priority(a, subb[a.k])) return i;
if (subb[a.k] && test(a, subb[a.k])) return true;
}
return -1;
return false;
}

@@ -371,3 +363,3 @@

//something like name:{$exists:false}
return priority(a, b ? b[a.k] : void 0);
return test(a, b ? b[a.k] : void 0);
},

@@ -380,3 +372,3 @@

var aRE = new RegExp(a);
return aRE.test(b) ? 0 : -1;
return aRE.test(b);
}

@@ -404,3 +396,3 @@ };

} else {
return a == b;
return a === b;
}

@@ -458,3 +450,3 @@ };

var sifter = function(query, selector) {
function createSifter(query, selector) {

@@ -470,3 +462,2 @@ //build the filter for the sifter

var value;
var priority;

@@ -480,6 +471,3 @@ //I'll typically start from the end, but in this case we need to keep the order

//priority = -1? it's not something we can use.
if (!~(priority = filter.priority(testValue))) continue;
sifted.push(value);
if (filter.test(testValue)) sifted.push(value);
}

@@ -492,7 +480,6 @@

self.test = filter.test;
self.score = filter.priority;
self.query = query;
return self;
};
}

@@ -506,3 +493,3 @@ /**

var sift = function(query, target, rawSelector) {
function sift(query, target, rawSelector) {

@@ -515,3 +502,3 @@ //must be an array

var sft = sifter(query, getSelector(rawSelector));
var sft = createSifter(query, getSelector(rawSelector));

@@ -523,3 +510,3 @@ //target given? sift through it and return the filtered result

return sft;
};
}

@@ -526,0 +513,0 @@ sift.use = function(options) {

@@ -1,1 +0,1 @@

!function(){function r(r,n){for(var t={},e=t,o=0,i=r.length-1;i>o;o++)e=e[r[o]]={};return e[r[o]]=n,t}function n(){function n(r){return r instanceof Date?r.getTime():r}function t(r){return r?0:-1}function e(r,t,e){var o=n(e);return{k:t,v:s[r]?s[r](o):o,e:f[r]}}var o=this.priority=function(r,t){for(var e=r.exprs,o=0,i=0,u=e.length;u>i;i++){var f,s=e[i];if(!~(f=s.e(s.v,n(t),t)))return-1;o+=f}return o},i=this.parse=function(n,t){n||(n={$eq:n});var s=[];if("[object Object]"===Object.prototype.toString.call(n))for(var a in n){var c;if(f[a])c=a;else{if("$"===a.substr(0,1))throw new Error("Unknown operator.");c="$trav"}var v=n[a],p=v;if(u[c]){if(~a.indexOf(".")){var $=a.split(".");a=$.shift(),p=v=r($,v)}if(v instanceof Array){p=[];for(var l=v.length;l--;)p.push(i(v[l]))}else p=i(v,a)}s.push(e(c,a,p))}else s.push(e("$eq",t,n));var d={exprs:s,k:t,test:function(r){return!!~d.priority(r)},priority:function(r){return o(d,r)}};return d},u=this.traversable={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},f=this.testers={$eq:function(r,n){return t(r.test(n))},$ne:function(r,n){return t(!r.test(n))},$lt:function(r,n){return t(r>n)},$gt:function(r,n){return t(n>r)},$lte:function(r,n){return t(r>=n)},$gte:function(r,n){return t(n>=r)},$exists:function(r,n){return t(r===(null!=n))},$in:function(r,n){if(!(n instanceof Array))return t(~r.indexOf(n));for(var e=n.length;e--;)if(~r.indexOf(n[e]))return e;return-1},$not:function(r,n){if(!r.test)throw new Error("$not test should include an expression, not a value. Use $ne instead.");return t(!r.test(n))},$type:function(r,n,e){return e?t(e instanceof r||e.constructor==r):-1},$nin:function(r,n){return~f.$in(r,n)?-1:0},$mod:function(r,n){return n%r[0]==r[1]?0:-1},$all:function(r,n){n||(n=[]);for(var t=r.length;t--;){var e=r[t],o=~n.indexOf(e);if(!o)return-1}return 0},$size:function(r,n){return n?t(r==n.length):-1},$or:function(r,n){for(var e=r.length,i=e;e--;)if(~o(r[e],n))return e;return t(0===i)},$nor:function(r,n){for(var t=r.length;t--;)if(~o(r[t],n))return-1;return 0},$and:function(r,n){for(var t=r.length;t--;)if(!~o(r[t],n))return-1;return 0},$trav:function(r,n){if(n instanceof Array){for(var t=n.length;t--;){var e=n[t];if(e[r.k]&&~o(r,e[r.k]))return t}return-1}return o(r,n?n[r.k]:void 0)},$regex:function(r,n){var t=new RegExp(r);return t.test(n)?0:-1}},s={$eq:function(r){var n;return r instanceof RegExp?r:(n=r instanceof Function?r:function(n){return n instanceof Array?~n.indexOf(r):r==n},{test:n})},$ne:function(r){return s.$eq(r)}}}var t=new n,e=function(r){if(!r)return function(r){return r};if("function"==typeof r)return r;throw new Error("Unknown sift selector "+r)},o=function(r,n){var e=t.parse(r),o=function(r){for(var t,o,i,u=[],f=0,s=r.length;s>f;f++)o=r[f],t=n(o),~(i=e.priority(t))&&u.push(o);return u};return o.test=e.test,o.score=e.priority,o.query=r,o},i=function(r,n,t){"object"!=typeof n&&(t=n,n=void 0);var i=o(r,e(t));return n?i(n):i};i.use=function(r){r.operators&&i.useOperators(r.operators),"function"==typeof r&&r(i)},i.useOperators=function(r){for(var n in r)i.useOperator(n,r[n])},i.useOperator=function(r,n){var e={};e="object"==typeof n?n:{test:n};var o="$"+r;t.testers[o]=e.test,(e.traversable||e.traverse)&&(t.traversable[o]=!0)},"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=i:"undefined"!=typeof window&&(window.sift=i)}();
!function(){function r(r,n){for(var t={},e=t,o=0,i=r.length-1;i>o;o++)e=e[r[o]]={};return e[r[o]]=n,t}function n(){function n(r){return r instanceof Date?r.getTime():r}function t(r){return r?0:-1}function e(r,t,e){var o=n(e);return{k:t,v:s[r]?s[r](o):o,e:f[r]}}var o=this.priority=function(r,t){for(var e=r.exprs,o=0,i=0,u=e.length;u>i;i++){var f,s=e[i];if(!~(f=s.e(s.v,n(t),t)))return-1;o+=f}return o},i=this.parse=function(n,t){n||(n={$eq:n});var s=[];if("[object Object]"===Object.prototype.toString.call(n))for(var a in n){var c;if(f[a])c=a;else{if("$"===a.substr(0,1))throw new Error("Unknown operator.");c="$trav"}var v=n[a],p=v;if(u[c]){if(~a.indexOf(".")){var l=a.split(".");a=l.shift(),p=v=r(l,v)}if(v instanceof Array){p=[];for(var $=v.length;$--;)p.push(i(v[$]))}else p=i(v,a)}s.push(e(c,a,p))}else s.push(e("$eq",t,n));var d={exprs:s,k:t,test:function(r){return!!~d.priority(r)},priority:function(r){return o(d,r)}};return d},u=this.traversable={$and:!0,$or:!0,$nor:!0,$trav:!0,$not:!0},f=this.testers={$eq:function(r,n){return t(r.test(n))},$ne:function(r,n){return t(!r.test(n))},$lt:function(r,n){return t(r>n)},$gt:function(r,n){return t(n>r)},$lte:function(r,n){return t(r>=n)},$gte:function(r,n){return t(n>=r)},$exists:function(r,n){return t(r===(null!=n))},$in:function(r,n){if(!(n instanceof Array))return t(~r.indexOf(n));for(var e=n.length;e--;)if(~r.indexOf(n[e]))return e;return-1},$not:function(r,n){if(!r.test)throw new Error("$not test should include an expression, not a value. Use $ne instead.");return t(!r.test(n))},$type:function(r,n,e){return null!=e?t(e instanceof r||e.constructor==r):-1},$nin:function(r,n){return~f.$in(r,n)?-1:0},$mod:function(r,n){return n%r[0]==r[1]?0:-1},$all:function(r,n){n||(n=[]);for(var t=r.length;t--;){var e=r[t],o=~n.indexOf(e);if(!o)return-1}return 0},$size:function(r,n){return n?t(r==n.length):-1},$or:function(r,n){for(var e=r.length,i=e;e--;)if(~o(r[e],n))return e;return t(0===i)},$nor:function(r,n){for(var t=r.length;t--;)if(~o(r[t],n))return-1;return 0},$and:function(r,n){for(var t=r.length;t--;)if(!~o(r[t],n))return-1;return 0},$trav:function(r,n){if(n instanceof Array){for(var t=n.length;t--;){var e=n[t];if(e[r.k]&&~o(r,e[r.k]))return t}return-1}return o(r,n?n[r.k]:void 0)},$regex:function(r,n){var t=new RegExp(r);return t.test(n)?0:-1}},s={$eq:function(r){var n;return r instanceof RegExp?r:(n=r instanceof Function?r:function(n){return n instanceof Array?~n.indexOf(r):r===n},{test:n})},$ne:function(r){return s.$eq(r)}}}function t(r,n){var t=o.parse(r),e=function(r){for(var e,o,i,u=[],f=0,s=r.length;s>f;f++)o=r[f],e=n(o),~(i=t.priority(e))&&u.push(o);return u};return e.test=t.test,e.score=t.priority,e.query=r,e}function e(r,n,e){"object"!=typeof n&&(e=n,n=void 0);var o=t(r,i(e));return n?o(n):o}var o=new n,i=function(r){if(!r)return function(r){return r};if("function"==typeof r)return r;throw new Error("Unknown sift selector "+r)};e.use=function(r){r.operators&&e.useOperators(r.operators),"function"==typeof r&&r(e)},e.useOperators=function(r){for(var n in r)e.useOperator(n,r[n])},e.useOperator=function(r,n){var t={};t="object"==typeof n?n:{test:n};var e="$"+r;o.testers[e]=t.test,(t.traversable||t.traverse)&&(o.traversable[e]=!0)},"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=e:"undefined"!=typeof window&&(window.sift=e)}();

@@ -11,2 +11,3 @@ var expect = require("expect.js"),

expect(values.length).to.be(3);

@@ -17,2 +18,51 @@ expect(values[0]).to.be(3);

});
it("can create a custom selector, and use it", function () {
var sifter = sift({ age: { $gt: 5}}, function (item) {
return item.person;
});
var people = [{ person: { age: 6 }}],
filtered = sifter(people);
expect(filtered.length).to.be(1);
expect(filtered[0]).to.be(people[0]);
});
it("throws an errror if the selector is invalid", function () {
var err;
try {
sift({$aaa:1}, 1).test("b");
} catch (e) {
err = e;
}
expect(err.message).to.be("Unknown sift selector 1");
});
it("can use a custom selector as the 3rd param", function () {
var people = [{ person: { age: 6 }}];
var filtered = sift({ age: { $gt: 5}}, people, function (item) {
return item.person;
});
expect(filtered.length).to.be(1);
expect(filtered[0]).to.be(people[0]);
});
it("throws an error", function () {
var err;
try {
sift({$aaa:1}).test("b");
} catch (e) {
err = e;
}
expect(err.message).to.be("Unknown operator $aaa.");
})
});

@@ -1,3 +0,2 @@

var _ = require('underscore'),
expect = require("expect.js"),
var expect = require("expect.js"),
sift = require(".."),

@@ -117,2 +116,3 @@ assert = require("assert");

}, topic);
assert.equal(sifted.length, 2);

@@ -187,2 +187,19 @@ });

});
describe("keypath", function () {
var arr = [
{
a: {
b: {
c: 1,
c2: 1
}
}
}
]
it("can be used", function () {
expect(sift({"a.b.c":1}).test(arr[0])).to.be(true);
});
});
});

@@ -189,0 +206,0 @@

@@ -31,2 +31,39 @@ var expect = require("expect.js"),

it("can make a traversable op", function () {
var i = 0;
sift.use({
operators: {
baab: {
traverse:true,
test: function (a, b) {
i++;
return a && b;
}
}
}
});
sift({a:{$baab:1}}).test({a:1});
expect(i).to.be(1);
});
sift.use({
operators: {
notb: function(a, b) {
return a != b ? true : false;
}
}
});
var topic = [1, 2, 3, 4, 5, 6, 6, 4, 3];
it("can use custom $notb operator", function() {
expect(sift({$notb: 6 }, topic)).not.to.contain(6);
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc