sift
Advanced tools
Comparing version 11.0.8 to 11.0.9
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "11.0.8", | ||
"version": "11.0.9", | ||
"repository": "crcn/sift.js", | ||
@@ -6,0 +6,0 @@ "sideEffects": false, |
**Installation**: `npm install sift`, or `yarn add sift` | ||
## validate objects & filter arrays with mongodb queries | ||
## Sift is a tiny library for using MongoDB queries in Javascript | ||
@@ -17,7 +17,8 @@ [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) | ||
- Function filtering | ||
- dot notation searching | ||
- Dot notation searching | ||
- Supports node.js, and web | ||
- Custom Operations | ||
- Tree-shaking | ||
## Node.js Examples | ||
## Examples | ||
@@ -60,54 +61,38 @@ ```javascript | ||
## Browser Examples | ||
## API | ||
```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 = ["craig", "john", "jake"].filter(sift(/^j/)); //['john','jake'] | ||
</script> | ||
</head> | ||
<body></body> | ||
</html> | ||
``` | ||
### sift(query: MongoQuery, options?: Options): Function | ||
## API | ||
Creates a filter with all of the built-in MongoDB query operations. | ||
### .sift(query: MongoQuery, options?: SiftOptions): Function | ||
- `query` - the filter to use against the target array | ||
- `options` | ||
- `expressions` - custom expressions | ||
- `operations` - [custom operations](#custom-operations) | ||
- `compare` - compares difference between two values | ||
With an array: | ||
Example: | ||
```javascript | ||
["craig", null].filter(sift({ $exists: true })); //['craig'] | ||
``` | ||
import sift from "sift"; | ||
Without an array, a sifter is returned: | ||
const test = sift({ $gt: 5 })); | ||
```javascript | ||
var existsFilter = sift({ $exists: true }); | ||
console.log(test(6)); // true | ||
console.log(test(4)); // false | ||
existsFilter("craig"); //true | ||
existsFilter(null); //false | ||
["craig", null].filter(existsFilter); //['craig'] | ||
[3, 4, 5, 6, 7].filter(sift({ $exists: true })); // [6, 7] | ||
``` | ||
With your sifter, you can also **test** values: | ||
### createQueryTester(query: Query, options?: Options): Function | ||
```javascript | ||
siftExists(null); //false | ||
siftExists("craig"); //true | ||
``` | ||
Creates a filter function **without** built-in MongoDB query operations. This is useful | ||
if you're looking to omit certain operations from application bundles. See [Omitting built-in operations](#omitting-built-in-operations) for more info. | ||
## Supported Operators: | ||
#### createEqualsOperation(params: any, ownerQuery: Query, options: Options): Operation | ||
Used for [custom operations](#custom-operations). | ||
## Supported Operators | ||
See MongoDB's [advanced queries](http://www.mongodb.org/display/DOCS/Advanced+Queries) for more info. | ||
@@ -422,3 +407,3 @@ | ||
```javascript | ||
import sift, { EqualsOperation } from "sift"; | ||
import sift, { createEqualsOperation } from "sift"; | ||
@@ -432,3 +417,3 @@ var filter = sift( | ||
$customMod(params, ownerQuery, options) { | ||
return new EqualsOperation( | ||
return createEqualsOperation( | ||
value => value % params !== 0, | ||
@@ -435,0 +420,0 @@ ownerQuery, |
@@ -340,3 +340,3 @@ import { | ||
export const createQueryTester = ( | ||
export const createQueryTester = <TItem>( | ||
query: Query, | ||
@@ -349,3 +349,3 @@ { compare, operations }: Partial<Options> = {} | ||
}); | ||
return (item, key?: Key, owner?: any) => { | ||
return (item: TItem, key?: Key, owner?: any) => { | ||
operation.reset(); | ||
@@ -352,0 +352,0 @@ operation.next(item, key, owner); |
@@ -10,7 +10,7 @@ import * as defaultOperations from "./operations"; | ||
const createDefaultQueryTester = ( | ||
const createDefaultQueryTester = <TItem>( | ||
query: Query, | ||
{ compare, operations }: Partial<Options> = {} | ||
) => { | ||
return createQueryTester(query, { | ||
return createQueryTester<TItem>(query, { | ||
compare: compare, | ||
@@ -17,0 +17,0 @@ operations: Object.assign({}, defaultOperations, operations || {}) |
327831
445