sift
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Sift is a small yet powerful JavaScript library that brings the power of MongoDB querying into JavaScript. Sifting through data to find specific elements can be complex, but Sift makes it easy by providing a range of query filters and operators that you can use, ranging from simple ones like $in
and $nin
, through to advanced ones such as $elemMatch
and $regex
. By using these, you can efficiently filter arrays, check regular expressions, test individual values against custom filters and much more.
Utilizing Sift is straightforward. First, install it via npm using npm install sift
or yarn using yarn add sift
. Then, import it into your JavaScript file with import sift from 'sift';
. The primary function of Sift is to create filters based on MongoDB query operations. Below are a couple of examples demonstrating its usage:
const result = ["hello", "sifted", "array!"].filter(sift({ $in: ["hello", "world"] }));
// result is ['hello']
const regexResult = ["craig", "john", "jake"].filter(sift(/^j/));
// regexResult is ['john','jake']
const customFilter = sift({
name: function(value) {
return value.length == 5;
}
});
const functionResult = [
{
name: "craig"
},
{
name: "john"
},
{
name: "jake"
}
].filter(customFilter);
// functionResult is [{ name: 'craig' }]
// Test single values against your custom filter
console.log(customFilter({ name: "sarah" })); // Outputs: true
console.log(customFilter({ name: "tim" })); // Outputs: false
For the details and more comprehensive examples of using operators and certain functionalities of Sift, the complete documentation can be accessed at the MongoDB official documentation site: http://docs.mongodb.org/manual/reference/operator/query/. This documentation provides extensive details about the different query operators and how you can utilize them in your JavaScript applications.