Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
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.0.4 to 0.0.5

.npmignore

2

package.json
{
"name": "sift",
"description": "mongodb query style array filtering",
"version": "0.0.4",
"version": "0.0.5",
"repository": {

@@ -6,0 +6,0 @@ "type": "git",

## MongoDB inspired array filtering [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js)
Sift is a small (2 kb minified), write less, do more filtering library which uses mongodb queries.
## Use Cases:

@@ -19,6 +17,7 @@

- dot notiation searching
- Supports node.js
- Supports node.js, and web
- Small (2 kb minified) library
## Simple Examples
## Node.js Examples

@@ -59,4 +58,18 @@ ```javascript

siftPeople.test({ name: 'sarah' }); //true
siftPeople.test({ name: 'tim' }); //false
siftPeople.test({ name: 'tim' }); //false\
```
## Browser Examples
```html
<html>
<head>
<script src="https://raw.github.com/crcn/sift.js/master/sift.min.js" type="text/javascript"></script>
<script type="text/javascript">
//regexp filter
var sifted = sift(/^j/, ['craig','john','jake']); //['john','jake']
</script>
</head>
<body>
</body>
</html>
```

@@ -66,6 +79,7 @@

### .sift(filter[, array])
### .sift(filter[, array][, selectorFn])
- `filter` - the filter to use against the target array
- `array` - sifts against target array. Without this, a function is returned
- `selectorFn` - selector for the values within the array.

@@ -86,2 +100,20 @@ With an array:

With a selector:
```javascript
var sifter = sift({$exists:true}, function(user) {
return !!user.name;
});
sifter([
{
name: "Craig"
},
{
name: null
}
])
```
With your sifter, you can also **test** values:

@@ -88,0 +120,0 @@

@@ -92,4 +92,3 @@ /*

//embed the value into the sub object, and pull
value = _convertDotToSubObject(keyParts, value);
exprValue = value = _convertDotToSubObject(keyParts, value);
}

@@ -111,3 +110,3 @@

exprValue = parse(statement[k], k);
exprValue = parse(value, k);

@@ -427,4 +426,13 @@ }

fn = function(b) {
if(b instanceof Array) {
return b.indexOf(a) > -1;
}else{
return a == b;
}
return a == b;
}

@@ -446,2 +454,3 @@

$ne: function(a) {
return _prepare.$eq(a);

@@ -477,4 +486,35 @@ }

var sifter = function(query) {
var getSelector = function(selector) {
if(!selector) {
return function(value) {
return value;
};
} else
/*if(typeof selector == 'string') {
return function(value) {
return value[selector];
};
} else */
if(typeof selector == 'function') {
return selector;
}
throw new Error("Unknown sift selector " + selector);
}
var sifter = function(query, selector) {
//build the filter for the sifter

@@ -486,3 +526,3 @@ var filter = _queryParser.parse( query );

var sifted = [];
var sifted = [], value;

@@ -493,4 +533,7 @@ //I'll typically start from the end, but in this case we need to keep the order

if(filter.test( target[i] )) sifted.push(target[i]);
value = selector(target[i]);
if(filter.test( value )) sifted.push(value);
}

@@ -509,8 +552,20 @@

//sifts a given array
var sift = function(query, target) {
/**
* sifts the given function
* @param query the mongodb query
* @param target the target array
* @param rawSelector the selector for plucking data from the given target
*/
var sift = function(query, target, rawSelector) {
var sft = sifter(query);
//must be an array
if(typeof target != "object") {
rawSelector = target;
target = undefined;
}
var sft = sifter(query, getSelector(rawSelector));
//target given? sift through it and return the filtered result

@@ -517,0 +572,0 @@ if(target) return sft(target);

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

(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})();
(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){var c=[];if(a)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,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 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})();

@@ -7,2 +7,4 @@ var sift = require('../'),

vows.describe('Sifter').addBatch({

@@ -117,2 +119,3 @@

age: 90001,
tags:['coder', 'programmer', 'traveler', 'photographer'],
address: {

@@ -123,2 +126,3 @@ city: 'Minneapolis',

},
tags: ['photos','cook'],
hobbies: [{

@@ -142,2 +146,3 @@ name: 'programming',

age: 90001,
tags: ['traveler', 'photographer'],
address: {

@@ -148,2 +153,3 @@ city: 'St. Paul',

},
tags: ['dj'],
hobbies: [{

@@ -266,4 +272,18 @@ name: 'biking',

/**
*/
'has sifted with selector function count of 2': function(topic) {
var sifted = sift({
"name": "photography",
"places": {$in:['costa rica']}
}, topic, function(item) {
return item.hobbies;
});
assert.equal(sifted.length, 2);
}
}
}
}).export(module);
}).export(module);

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