Comparing version 7.0.1 to 8.0.0
@@ -0,1 +1,6 @@ | ||
## 8.0.0 | ||
- Deprecated `sift(query, array)`. You must now use `array.filter(sift(query))` | ||
- Queries now expect exact object shape (based on https://github.com/crcn/sift.js/issues/117). E.g: `[{a: 1, b: 1}, {a: 1}]].filter(sift({ a: 1 })) === [{a: 1}]` | ||
### 7.0.0 | ||
@@ -21,3 +26,3 @@ | ||
} | ||
}) | ||
}); | ||
``` | ||
@@ -24,0 +29,0 @@ |
210
lib/index.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -7,3 +7,15 @@ Object.defineProperty(exports, "__esModule", { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _typeof = | ||
typeof Symbol === "function" && typeof Symbol.iterator === "symbol" | ||
? function(obj) { | ||
return typeof obj; | ||
} | ||
: function(obj) { | ||
return obj && | ||
typeof Symbol === "function" && | ||
obj.constructor === Symbol && | ||
obj !== Symbol.prototype | ||
? "symbol" | ||
: typeof obj; | ||
}; | ||
@@ -21,7 +33,7 @@ exports.default = sift; | ||
/** | ||
*/ | ||
function isFunction(value) { | ||
return typeof value === 'function'; | ||
function typeChecker(type) { | ||
var typeString = "[object " + type + "]"; | ||
return function(value) { | ||
return Object.prototype.toString.call(value) === typeString; | ||
}; | ||
} | ||
@@ -32,5 +44,5 @@ | ||
function isArray(value) { | ||
return Object.prototype.toString.call(value) === '[object Array]'; | ||
} | ||
var isArray = typeChecker("Array"); | ||
var isObject = typeChecker("Object"); | ||
var isFunction = typeChecker("Function"); | ||
@@ -45,3 +57,3 @@ /** | ||
return value.map(comparable); | ||
} else if (value && typeof value.toJSON === 'function') { | ||
} else if (value && typeof value.toJSON === "function") { | ||
return value.toJSON(); | ||
@@ -61,3 +73,3 @@ } else { | ||
function or(validator) { | ||
return function (a, b) { | ||
return function(a, b) { | ||
if (!isArray(b) || !b.length) { | ||
@@ -77,3 +89,3 @@ return validator(a, b); | ||
function and(validator) { | ||
return function (a, b) { | ||
return function(a, b) { | ||
if (!isArray(b) || !b.length) { | ||
@@ -93,8 +105,7 @@ return validator(a, b); | ||
var OPERATORS = { | ||
var operators = { | ||
/** | ||
*/ | ||
$eq: or(function (a, b) { | ||
$eq: or(function(a, b) { | ||
return a(b); | ||
@@ -106,3 +117,3 @@ }), | ||
$ne: and(function (a, b) { | ||
$ne: and(function(a, b) { | ||
return !a(b); | ||
@@ -114,3 +125,3 @@ }), | ||
$gt: or(function (a, b) { | ||
$gt: or(function(a, b) { | ||
return compare(comparable(b), a) > 0; | ||
@@ -122,3 +133,3 @@ }), | ||
$gte: or(function (a, b) { | ||
$gte: or(function(a, b) { | ||
return compare(comparable(b), a) >= 0; | ||
@@ -130,3 +141,3 @@ }), | ||
$lt: or(function (a, b) { | ||
$lt: or(function(a, b) { | ||
return compare(comparable(b), a) < 0; | ||
@@ -138,3 +149,3 @@ }), | ||
$lte: or(function (a, b) { | ||
$lte: or(function(a, b) { | ||
return compare(comparable(b), a) <= 0; | ||
@@ -146,3 +157,3 @@ }), | ||
$mod: or(function (a, b) { | ||
$mod: or(function(a, b) { | ||
return b % a[0] == a[1]; | ||
@@ -155,5 +166,4 @@ }), | ||
$in: function $in(a, b) { | ||
if (b instanceof Array) { | ||
for (var i = b.length; i--;) { | ||
for (var i = b.length; i--; ) { | ||
if (~a.indexOf(comparable(get(b, i)))) { | ||
@@ -165,5 +175,8 @@ return true; | ||
var comparableB = comparable(b); | ||
if (comparableB === b && (typeof b === 'undefined' ? 'undefined' : _typeof(b)) === 'object') { | ||
for (var i = a.length; i--;) { | ||
if (String(a[i]) === String(b) && String(b) !== '[object Object]') { | ||
if ( | ||
comparableB === b && | ||
(typeof b === "undefined" ? "undefined" : _typeof(b)) === "object" | ||
) { | ||
for (var i = a.length; i--; ) { | ||
if (String(a[i]) === String(b) && String(b) !== "[object Object]") { | ||
return true; | ||
@@ -178,4 +191,4 @@ } | ||
*/ | ||
if (typeof comparableB == 'undefined') { | ||
for (var i = a.length; i--;) { | ||
if (typeof comparableB == "undefined") { | ||
for (var i = a.length; i--; ) { | ||
if (a[i] == null) { | ||
@@ -190,6 +203,10 @@ return true; | ||
*/ | ||
for (var i = a.length; i--;) { | ||
for (var i = a.length; i--; ) { | ||
var validator = createRootValidator(get(a, i), undefined); | ||
var result = validate(validator, b, i, a); | ||
if (result && String(result) !== '[object Object]' && String(b) !== '[object Object]') { | ||
if ( | ||
result && | ||
String(result) !== "[object Object]" && | ||
String(b) !== "[object Object]" | ||
) { | ||
return true; | ||
@@ -209,3 +226,3 @@ } | ||
$nin: function $nin(a, b, k, o) { | ||
return !OPERATORS.$in(a, b, k, o); | ||
return !operators.$in(a, b, k, o); | ||
}, | ||
@@ -231,3 +248,3 @@ | ||
$all: function $all(a, b, k, o) { | ||
return OPERATORS.$and(a, b, k, o); | ||
return operators.$and(a, b, k, o); | ||
}, | ||
@@ -248,3 +265,4 @@ | ||
if (validate(get(a, i), b, k, o)) return true; | ||
}return false; | ||
} | ||
return false; | ||
}, | ||
@@ -256,3 +274,3 @@ | ||
$nor: function $nor(a, b, k, o) { | ||
return !OPERATORS.$or(a, b, k, o); | ||
return !operators.$or(a, b, k, o); | ||
}, | ||
@@ -275,4 +293,4 @@ | ||
$regex: or(function (a, b) { | ||
return typeof b === 'string' && a.test(b); | ||
$regex: or(function(a, b) { | ||
return typeof b === "string" && a.test(b); | ||
}), | ||
@@ -309,3 +327,2 @@ | ||
var prepare = { | ||
/** | ||
@@ -315,6 +332,5 @@ */ | ||
$eq: function $eq(a) { | ||
if (a instanceof RegExp) { | ||
return function (b) { | ||
return typeof b === 'string' && a.test(b); | ||
return function(b) { | ||
return typeof b === "string" && a.test(b); | ||
}; | ||
@@ -325,7 +341,7 @@ } else if (a instanceof Function) { | ||
// Special case of a == [] | ||
return function (b) { | ||
return function(b) { | ||
return isArray(b) && !b.length; | ||
}; | ||
} else if (a === null) { | ||
return function (b) { | ||
return function(b) { | ||
//will match both null and undefined | ||
@@ -336,3 +352,3 @@ return b == null; | ||
return function (b) { | ||
return function(b) { | ||
return compare(comparable(b), comparable(a)) === 0; | ||
@@ -395,3 +411,3 @@ }; | ||
$where: function $where(a) { | ||
return typeof a === 'string' ? new Function('obj', 'return ' + a) : a; | ||
return typeof a === "string" ? new Function("obj", "return " + a) : a; | ||
}, | ||
@@ -418,3 +434,2 @@ | ||
function search(array, validator) { | ||
for (var i = 0; i < array.length; i++) { | ||
@@ -450,3 +465,3 @@ var result = get(array, i); | ||
// If the query contains $ne, need to test all elements ANDed together | ||
var inclusive = a && a.q && typeof a.q.$ne !== 'undefined'; | ||
var inclusive = a && a.q && typeof a.q.$ne !== "undefined"; | ||
var allValid = inclusive; | ||
@@ -469,5 +484,3 @@ for (var i = 0; i < values.length; i++) { | ||
function findValues(current, keypath, index, object, values) { | ||
if (index === keypath.length || current == void 0) { | ||
values.push([current, keypath[index - 1], object]); | ||
@@ -514,2 +527,6 @@ return; | ||
if (isExactObject(query)) { | ||
return createValidator(query, isEqual); | ||
} | ||
var validators = []; | ||
@@ -520,21 +537,73 @@ | ||
if (key === '$options') { | ||
if (key === "$options") { | ||
continue; | ||
} | ||
if (OPERATORS[key]) { | ||
if (operators[key]) { | ||
if (prepare[key]) a = prepare[key](a, query); | ||
validators.push(createValidator(comparable(a), OPERATORS[key])); | ||
validators.push(createValidator(comparable(a), operators[key])); | ||
} else { | ||
if (key.charCodeAt(0) === 36) { | ||
throw new Error('Unknown operation ' + key); | ||
throw new Error("Unknown operation " + key); | ||
} | ||
validators.push(createNestedValidator(key.split('.'), parse(a), a)); | ||
var keyParts = key.split("."); | ||
validators.push(createNestedValidator(keyParts, parse(a, key), a)); | ||
} | ||
} | ||
return validators.length === 1 ? validators[0] : createValidator(validators, OPERATORS.$and); | ||
return validators.length === 1 | ||
? validators[0] | ||
: createValidator(validators, operators.$and); | ||
} | ||
function isEqual(a, b) { | ||
if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) { | ||
return false; | ||
} | ||
if (isObject(a)) { | ||
if (Object.keys(a).length !== Object.keys(b).length) { | ||
return false; | ||
} | ||
for (var key in a) { | ||
if (!isEqual(a[key], b[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else if (isArray(a)) { | ||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
for (var i = 0, n = a.length; i < n; i++) { | ||
if (!isEqual(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return a === b; | ||
} | ||
} | ||
function getAllKeys(value, keys) { | ||
if (!isObject(value)) { | ||
return keys; | ||
} | ||
for (var key in value) { | ||
keys.push(key); | ||
getAllKeys(value[key], keys); | ||
} | ||
return keys; | ||
} | ||
function isExactObject(value) { | ||
var allKeysHash = getAllKeys(value, []).join(","); | ||
return allKeysHash.search(/[$.]/) === -1; | ||
} | ||
/** | ||
@@ -559,20 +628,7 @@ */ | ||
function sift(query, array, getter) { | ||
if (isFunction(array)) { | ||
getter = array; | ||
array = void 0; | ||
} | ||
function sift(query, getter) { | ||
var validator = createRootValidator(query, getter); | ||
function filter(b, k, o) { | ||
return function(b, k, o) { | ||
return validate(validator, b, k, o); | ||
} | ||
if (array) { | ||
return array.filter(filter); | ||
} | ||
return filter; | ||
}; | ||
} | ||
@@ -585,3 +641,3 @@ | ||
return search(array, createRootValidator(query, getter)); | ||
}; | ||
} | ||
@@ -593,3 +649,6 @@ /** | ||
if (a === b) return 0; | ||
if ((typeof a === 'undefined' ? 'undefined' : _typeof(a)) === (typeof b === 'undefined' ? 'undefined' : _typeof(b))) { | ||
if ( | ||
(typeof a === "undefined" ? "undefined" : _typeof(a)) === | ||
(typeof b === "undefined" ? "undefined" : _typeof(b)) | ||
) { | ||
if (a > b) { | ||
@@ -602,3 +661,2 @@ return 1; | ||
} | ||
}; | ||
} |
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "7.0.1", | ||
"version": "8.0.0", | ||
"repository": "crcn/sift.js", | ||
@@ -15,3 +15,9 @@ "author": { | ||
"typings": "./index.d.ts", | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
}, | ||
"devDependencies": { | ||
"prettier": "1.15.3", | ||
"babel-cli": "^6.26.0", | ||
@@ -22,4 +28,6 @@ "babel-core": "^6.26.3", | ||
"bson": "^3.0.2", | ||
"husky": "^1.2.1", | ||
"immutable": "^3.7.6", | ||
"mocha": "^5.2.0", | ||
"pretty-quick": "^1.8.0", | ||
"webpack": "^4.20.2", | ||
@@ -26,0 +34,0 @@ "webpack-cli": "^3.1.2", |
387
README.md
## validate objects & filter arrays with mongodb queries | ||
[![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js) | ||
[![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) --> | ||
@@ -10,3 +12,3 @@ <!-- [![Join the chat at https://gitter.im/crcn/sift.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/crcn/sift.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) --> | ||
- Supported operators: [$in](#in), [$nin](#nin), [$exists](#exists), [$gte](#gte), [$gt](#gt), [$lte](#lte), [$lt](#lt), [$eq](#eq), [$ne](#ne), [$mod](#mod), [$all](#all), [$and](#and), [$or](#or), [$nor](#nor), [$not](#not), [$size](#size), [$type](#type), [$regex](#regex), [$where](#where), [$elemMatch](#elemmatch) | ||
- Supported operators: [\$in](#in), [\$nin](#nin), [\$exists](#exists), [\$gte](#gte), [\$gt](#gt), [\$lte](#lte), [\$lt](#lt), [\$eq](#eq), [\$ne](#ne), [\$mod](#mod), [\$all](#all), [\$and](#and), [\$or](#or), [\$nor](#nor), [\$not](#not), [\$size](#size), [\$type](#type), [\$regex](#regex), [\$where](#where), [\$elemMatch](#elemmatch) | ||
- Regexp searches | ||
@@ -21,55 +23,55 @@ - Function filtering | ||
## Node.js Examples | ||
```javascript | ||
import sift from "sift"; | ||
import sift from 'sift'; | ||
//intersecting arrays | ||
var sifted = sift({ $in: ['hello','world'] }, ['hello','sifted','array!']); //['hello'] | ||
var result = ["hello", "sifted", "array!"].filter( | ||
sift({ $in: ["hello", "world"] }) | ||
); //['hello'] | ||
//regexp filter | ||
var sifted = sift(/^j/, ['craig','john','jake']); //['john','jake'] | ||
var result = ["craig", "john", "jake"].filter(sift(/^j/)); //['john','jake'] | ||
//A *sifter* is returned if the second parameter is omitted | ||
var testQuery = sift({ | ||
//you can also filter against functions | ||
name: function(value) { | ||
return value.length == 5; | ||
} | ||
// function filter | ||
var testFilter = sift({ | ||
//you can also filter against functions | ||
name: function(value) { | ||
return value.length == 5; | ||
} | ||
}); | ||
//filtered: [{ name: 'craig' }] | ||
[{ | ||
name: 'craig', | ||
}, | ||
{ | ||
name: 'john' | ||
}, | ||
{ | ||
name: 'jake' | ||
}].filter(testQuery); | ||
var result = [ | ||
{ | ||
name: "craig" | ||
}, | ||
{ | ||
name: "john" | ||
}, | ||
{ | ||
name: "jake" | ||
} | ||
].filter(testFilter); // filtered: [{ name: 'craig' }] | ||
//you can test *single values* against your custom sifter | ||
testQuery({ name: 'sarah' }); //true | ||
testQuery({ name: 'tim' }); //false\ | ||
testQuery({ name: "sarah" }); //true | ||
testQuery({ 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> | ||
<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> | ||
@@ -80,7 +82,7 @@ ``` | ||
### .sift(filter[, array][, selectorFn]) | ||
### .sift(query: MongoQuery, selector?: Function): Function | ||
- `filter` - the filter to use against the target array | ||
- `query` - 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. | ||
- `selector` - selector for the values within the array. | ||
@@ -90,3 +92,3 @@ With an array: | ||
```javascript | ||
sift({$exists:true}, ['craig',null]); //['craig'] | ||
["craig", null].filter(sift({ $exists: true })); //['craig'] | ||
``` | ||
@@ -97,7 +99,7 @@ | ||
```javascript | ||
var siftExists = sift({$exists:true}); | ||
var existsFilter = sift({ $exists: true }); | ||
siftExists('craig'); //true | ||
siftExists(null); //false | ||
['craig',null].filter(siftExists); //['craig'] | ||
existsFilter("craig"); //true | ||
existsFilter(null); //false | ||
["craig", null].filter(existsFilter); //['craig'] | ||
``` | ||
@@ -108,15 +110,14 @@ | ||
```javascript | ||
var sifter = sift({$exists:true}, function(user) { | ||
return !!user.name; | ||
var omitNameFilter = sift({ $exists: true }, function(user) { | ||
return !!user.name; | ||
}); | ||
sifter([ | ||
{ | ||
name: "Craig" | ||
}, | ||
{ | ||
name: null | ||
} | ||
]) | ||
[ | ||
{ | ||
name: "Craig" | ||
}, | ||
{ | ||
name: null | ||
} | ||
].filter(omitNameFilter); | ||
``` | ||
@@ -128,6 +129,5 @@ | ||
siftExists(null); //false | ||
siftExists('craig'); //true | ||
siftExists("craig"); //true | ||
``` | ||
## Supported Operators: | ||
@@ -137,5 +137,5 @@ | ||
### $in | ||
### \$in | ||
array value must be *$in* the given query: | ||
array value must be _\$in_ the given query: | ||
@@ -146,21 +146,27 @@ Intersecting two arrays: | ||
//filtered: ['Brazil'] | ||
sift({ $in: ['Costa Rica','Brazil'] }, ['Brazil','Haiti','Peru','Chile']); | ||
["Brazil", "Haiti", "Peru", "Chile"].filter( | ||
sift({ $in: ["Costa Rica", "Brazil"] }) | ||
); | ||
``` | ||
Here's another example. This acts more like the $or operator: | ||
Here's another example. This acts more like the \$or operator: | ||
```javascript | ||
sift({ location: { $in: ['Costa Rica','Brazil'] } }, [ { name: 'Craig', location: 'Brazil' } ]); | ||
[{ name: "Craig", location: "Brazil" }].filter( | ||
sift({ location: { $in: ["Costa Rica", "Brazil"] } }) | ||
); | ||
``` | ||
### $nin | ||
### \$nin | ||
Opposite of $in: | ||
Opposite of \$in: | ||
```javascript | ||
//filtered: ['Haiti','Peru','Chile'] | ||
sift({ $nin: ['Costa Rica','Brazil'] }, ['Brazil','Haiti','Peru','Chile']); | ||
["Brazil", "Haiti", "Peru", "Chile"].filter( | ||
sift({ $nin: ["Costa Rica", "Brazil"] }) | ||
); | ||
``` | ||
### $exists | ||
### \$exists | ||
@@ -171,3 +177,3 @@ Checks if whether a value exists: | ||
//filtered: ['Craig','Tim'] | ||
sift({ $exists: true }, ['Craig',null,'Tim']); | ||
sift({ $exists: true }, ["Craig", null, "Tim"]); | ||
``` | ||
@@ -179,6 +185,8 @@ | ||
//filtered: [{ name: 'Craig', city: 'Minneapolis' }] | ||
sift({ city: { $exists: false } }, [ { name: 'Craig', city: 'Minneapolis' }, { name: 'Tim' }]); | ||
[{ name: "Craig", city: "Minneapolis" }, { name: "Tim" }].filter( | ||
sift({ city: { $exists: false } }) | ||
); | ||
``` | ||
### $gte | ||
### \$gte | ||
@@ -189,6 +197,6 @@ Checks if a number is >= value: | ||
//filtered: [2, 3] | ||
sift({ $gte: 2 }, [0, 1, 2, 3]); | ||
[0, 1, 2, 3].filter(sift({ $gte: 2 })); | ||
``` | ||
### $gt | ||
### \$gt | ||
@@ -199,6 +207,6 @@ Checks if a number is > value: | ||
//filtered: [3] | ||
sift({ $gt: 2 }, [0, 1, 2, 3]); | ||
[0, 1, 2, 3].filter(sift({ $gt: 2 })); | ||
``` | ||
### $lte | ||
### \$lte | ||
@@ -209,6 +217,6 @@ Checks if a number is <= value. | ||
//filtered: [0, 1, 2] | ||
sift({ $lte: 2 }, [0, 1, 2, 3]); | ||
[0, 1, 2, 3].filter(sift({ $lte: 2 })); | ||
``` | ||
### $lt | ||
### \$lt | ||
@@ -219,12 +227,14 @@ Checks if number is < value. | ||
//filtered: [0, 1] | ||
sift({ $lt: 2 }, [0, 1, 2, 3]); | ||
[0, 1, 2, 3].filter(sift({ $lt: 2 })); | ||
``` | ||
### $eq | ||
### \$eq | ||
Checks if `query === value`. Note that **$eq can be omitted**. For **$eq**, and **$ne** | ||
Checks if `query === value`. Note that **\$eq can be omitted**. For **\$eq**, and **\$ne** | ||
```javascript | ||
//filtered: [{ state: 'MN' }] | ||
sift({ state: {$eq: 'MN' }}, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]); | ||
[{ state: "MN" }, { state: "CA" }, { state: "WI" }].filter( | ||
sift({ state: { $eq: "MN" } }) | ||
); | ||
``` | ||
@@ -236,6 +246,8 @@ | ||
//filtered: [{ state: 'MN' }] | ||
sift({ state: 'MN' }, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]); | ||
[{ state: "MN" }, { state: "CA" }, { state: "WI" }].filter( | ||
sift({ state: "MN" }) | ||
); | ||
``` | ||
### $ne | ||
### \$ne | ||
@@ -246,6 +258,8 @@ Checks if `query !== value`. | ||
//filtered: [{ state: 'CA' }, { state: 'WI'}] | ||
sift({ state: {$ne: 'MN' }}, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]); | ||
[{ state: "MN" }, { state: "CA" }, { state: "WI" }].filter( | ||
sift({ state: { $ne: "MN" } }) | ||
); | ||
``` | ||
### $mod | ||
### \$mod | ||
@@ -256,6 +270,6 @@ Modulus: | ||
//filtered: [300, 600] | ||
sift({ $mod: [3, 0] }, [100, 200, 300, 400, 500, 600]); | ||
[100, 200, 300, 400, 500, 600].filter(sift({ $mod: [3, 0] })); | ||
``` | ||
### $all | ||
### \$all | ||
@@ -266,8 +280,9 @@ values must match **everything** in array: | ||
//filtered: [ { tags: ['books','programming','travel' ]} ] | ||
sift({ tags: {$all: ['books','programming'] }}, [ | ||
{ tags: ['books','programming','travel' ] }, | ||
{ tags: ['travel','cooking'] } ]); | ||
[ | ||
{ tags: ["books", "programming", "travel"] }, | ||
{ tags: ["travel", "cooking"] } | ||
].filter(sift({ tags: { $all: ["books", "programming"] } })); | ||
``` | ||
### $and | ||
### \$and | ||
@@ -279,9 +294,10 @@ ability to use an array of expressions. All expressions must test true. | ||
sift({ $and: [ { name: 'Craig' }, { state: 'MN' } ] }, [ | ||
{ name: 'Craig', state: 'MN' }, | ||
{ name: 'Tim', state: 'MN' }, | ||
{ name: 'Joe', state: 'CA' } ]); | ||
[ | ||
{ name: "Craig", state: "MN" }, | ||
{ name: "Tim", state: "MN" }, | ||
{ name: "Joe", state: "CA" } | ||
].filter(sift({ $and: [{ name: "Craig" }, { state: "MN" }] })); | ||
``` | ||
### $or | ||
### \$or | ||
@@ -292,9 +308,10 @@ OR array of expressions. | ||
//filtered: [ { name: 'Craig', state: 'MN' }, { name: 'Tim', state: 'MN' }] | ||
sift({ $or: [ { name: 'Craig' }, { state: 'MN' } ] }, [ | ||
{ name: 'Craig', state: 'MN' }, | ||
{ name: 'Tim', state: 'MN' }, | ||
{ name: 'Joe', state: 'CA' } ]); | ||
[ | ||
{ name: "Craig", state: "MN" }, | ||
{ name: "Tim", state: "MN" }, | ||
{ name: "Joe", state: "CA" } | ||
].filter(sift({ $or: [{ name: "Craig" }, { state: "MN" }] })); | ||
``` | ||
### $nor | ||
### \$nor | ||
@@ -305,11 +322,11 @@ opposite of or: | ||
//filtered: [ { name: 'Tim', state: 'MN' }, { name: 'Joe', state: 'CA' }] | ||
sift({ $nor: [ { name: 'Craig' }, { state: 'MN' } ] }, [ | ||
{ name: 'Craig', state: 'MN' }, | ||
{ name: 'Tim', state: 'MN' }, | ||
{ name: 'Joe', state: 'CA' } ]); | ||
[ | ||
{ name: "Craig", state: "MN" }, | ||
{ name: "Tim", state: "MN" }, | ||
{ name: "Joe", state: "CA" } | ||
].filter(sift({ $nor: [{ name: "Craig" }, { state: "MN" }] })); | ||
``` | ||
### \$size | ||
### $size | ||
Matches an array - must match given size: | ||
@@ -319,6 +336,8 @@ | ||
//filtered: ['food','cooking'] | ||
sift({ tags: { $size: 2 } }, [ { tags: ['food','cooking'] }, { tags: ['traveling'] }]); | ||
[{ tags: ["food", "cooking"] }, { tags: ["traveling"] }].filter( | ||
sift({ tags: { $size: 2 } }) | ||
); | ||
``` | ||
### $type | ||
### \$type | ||
@@ -328,7 +347,7 @@ Matches a values based on the type | ||
```javascript | ||
sift({ $type: Date }, [new Date(), 4342, 'hello world']); //returns single date | ||
sift({ $type: String }, [new Date(), 4342, 'hello world']); //returns ['hello world'] | ||
[new Date(), 4342, "hello world"].filter(sift({ $type: Date })); //returns single date | ||
[new Date(), 4342, "hello world"].filter(sift({ $type: String })); //returns ['hello world'] | ||
``` | ||
### $regex | ||
### \$regex | ||
@@ -338,7 +357,11 @@ Matches values based on the given regular expression | ||
```javascript | ||
sift({ $regex: /^f/i, $nin: ["frank"] }, ["frank", "fred", "sam", "frost"]); // ["fred", "frost"] | ||
sift({ $regex: "^f", $options: "i", $nin: ["frank"] }, ["frank", "fred", "sam", "frost"]); // ["fred", "frost"] | ||
["frank", "fred", "sam", "frost"].filter( | ||
sift({ $regex: /^f/i, $nin: ["frank"] }) | ||
); // ["fred", "frost"] | ||
["frank", "fred", "sam", "frost"].filter( | ||
sift({ $regex: "^f", $options: "i", $nin: ["frank"] }) | ||
); // ["fred", "frost"] | ||
``` | ||
### $where | ||
### \$where | ||
@@ -348,11 +371,15 @@ Matches based on some javascript comparison | ||
```javascript | ||
sift({ $where: "this.name === 'frank'" }, [{name:'frank'},{name:'joe'}]); // ["frank"] | ||
sift({ | ||
$where: function() { | ||
return this.name === "frank" | ||
} | ||
}, [{name:'frank'},{name:'joe'}]); // ["frank"] | ||
[{ name: "frank" }, { name: "joe" }].filter( | ||
sift({ $where: "this.name === 'frank'" }) | ||
); // ["frank"] | ||
[{ name: "frank" }, { name: "joe" }].filter( | ||
sift({ | ||
$where: function() { | ||
return this.name === "frank"; | ||
} | ||
}) | ||
); // ["frank"] | ||
``` | ||
### $elemMatch | ||
### \$elemMatch | ||
@@ -362,31 +389,43 @@ Matches elements of array | ||
```javascript | ||
var bills = [{ | ||
month: 'july', | ||
casts: [{ | ||
var bills = [ | ||
{ | ||
month: "july", | ||
casts: [ | ||
{ | ||
id: 1, | ||
value: 200 | ||
},{ | ||
}, | ||
{ | ||
id: 2, | ||
value: 1000 | ||
}] | ||
}, | ||
{ | ||
month: 'august', | ||
casts: [{ | ||
} | ||
] | ||
}, | ||
{ | ||
month: "august", | ||
casts: [ | ||
{ | ||
id: 3, | ||
value: 1000, | ||
}, { | ||
value: 1000 | ||
}, | ||
{ | ||
id: 4, | ||
value: 4000 | ||
}] | ||
}]; | ||
} | ||
] | ||
} | ||
]; | ||
var result = sift({ | ||
casts: {$elemMatch:{ | ||
value: {$gt: 1000} | ||
}} | ||
}, bills); // {month:'august', casts:[{id:3, value: 1000},{id: 4, value: 4000}]} | ||
var result = bills.filter( | ||
sift({ | ||
casts: { | ||
$elemMatch: { | ||
value: { $gt: 1000 } | ||
} | ||
} | ||
}) | ||
); // {month:'august', casts:[{id:3, value: 1000},{id: 4, value: 4000}]} | ||
``` | ||
### $not | ||
### \$not | ||
@@ -396,4 +435,4 @@ Not expression: | ||
```javascript | ||
sift({$not:{$in:['craig','tim']}}, ['craig','tim','jake']); //['jake'] | ||
sift({$not:{$size:5}}, ['craig','tim','jake']); //['tim','jake'] | ||
["craig", "tim", "jake"].filter(sift({ $not: { $in: ["craig", "tim"] } })); //['jake'] | ||
["craig", "tim", "jake"].filter(sift({ $not: { $size: 5 } })); //['tim','jake'] | ||
``` | ||
@@ -403,24 +442,24 @@ | ||
```javascript | ||
var people = [{ | ||
name: 'craig', | ||
address: { | ||
city: 'Minneapolis' | ||
} | ||
}, | ||
{ | ||
name: 'tim', | ||
address: { | ||
city: 'St. Paul' | ||
} | ||
}]; | ||
var people = [ | ||
{ | ||
name: "craig", | ||
address: { | ||
city: "Minneapolis" | ||
} | ||
}, | ||
{ | ||
name: "tim", | ||
address: { | ||
city: "St. Paul" | ||
} | ||
} | ||
]; | ||
var sifted = sift({ address: { city: 'Minneapolis' }}, people); // count = 1 | ||
var sifted = people.filter(sift({ address: { city: "Minneapolis" } })); // count = 1 | ||
//or | ||
var sifted = sift({'address.city': 'minneapolis'}, people);//count = 1 | ||
var sifted = people.filter(sift({ "address.city": "minneapolis" })); //count = 1 | ||
``` | ||
## Get index of first matching element | ||
@@ -431,17 +470,19 @@ | ||
```javascript | ||
import {indexOf as siftIndexOf} from 'sift'; | ||
var people = [{ | ||
name: 'craig', | ||
address: { | ||
city: 'Minneapolis' | ||
} | ||
}, | ||
{ | ||
name: 'tim', | ||
address: { | ||
city: 'St. Paul' | ||
} | ||
}]; | ||
import { indexOf as siftIndexOf } from "sift"; | ||
var people = [ | ||
{ | ||
name: "craig", | ||
address: { | ||
city: "Minneapolis" | ||
} | ||
}, | ||
{ | ||
name: "tim", | ||
address: { | ||
city: "St. Paul" | ||
} | ||
} | ||
]; | ||
var index = siftIndexOf({ address: { city: 'Minneapolis' }}, people); // index = 0 | ||
var index = people.filter(siftIndexOf({ address: { city: "Minneapolis" } })); // index = 0 | ||
``` |
388
sift.min.js
@@ -1,1 +0,387 @@ | ||
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.sift=t():n.sift=t()}(window,function(){return function(n){var t={};function r(e){if(t[e])return t[e].exports;var o=t[e]={i:e,l:!1,exports:{}};return n[e].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=n,r.c=t,r.d=function(n,t,e){r.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:e})},r.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},r.t=function(n,t){if(1&t&&(n=r(n)),8&t)return n;if(4&t&&"object"==typeof n&&n&&n.__esModule)return n;var e=Object.create(null);if(r.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:n}),2&t&&"string"!=typeof n)for(var o in n)r.d(e,o,function(t){return n[t]}.bind(null,o));return e},r.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(t,"a",t),t},r.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},r.p="",r(r.s=0)}([function(n,t,r){n.exports=r(1)},function(n,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n};function o(n){return"function"==typeof n}function u(n){return"[object Array]"===Object.prototype.toString.call(n)}function i(n){return n instanceof Date?n.getTime():u(n)?n.map(i):n&&"function"==typeof n.toJSON?n.toJSON():n}function f(n,t){return o(n.get)?n.get(t):n[t]}function c(n){return function(t,r){if(!u(r)||!r.length)return n(t,r);for(var e=0,o=r.length;e<o;e++)if(n(t,f(r,e)))return!0;return!1}}function l(n,t,r,e){return n.v(n.a,t,r,e)}t.default=function(n,t,r){o(t)&&(r=t,t=void 0);var e=y(n,r);function u(n,t,r){return l(e,n,t,r)}if(t)return t.filter(u);return u},t.indexOf=function(n,t,r){return d(t,y(n,r))},t.compare=b;var a={$eq:c(function(n,t){return n(t)}),$ne:function(n){return function(t,r){if(!u(r)||!r.length)return n(t,r);for(var e=0,o=r.length;e<o;e++)if(!n(t,f(r,e)))return!1;return!0}}(function(n,t){return!n(t)}),$gt:c(function(n,t){return b(i(t),n)>0}),$gte:c(function(n,t){return b(i(t),n)>=0}),$lt:c(function(n,t){return b(i(t),n)<0}),$lte:c(function(n,t){return b(i(t),n)<=0}),$mod:c(function(n,t){return t%n[0]==n[1]}),$in:function(n,t){if(!(t instanceof Array)){var r=i(t);if(r===t&&"object"===(void 0===t?"undefined":e(t)))for(u=n.length;u--;)if(String(n[u])===String(t)&&"[object Object]"!==String(t))return!0;if(void 0===r)for(u=n.length;u--;)if(null==n[u])return!0;for(u=n.length;u--;){var o=l(y(f(n,u),void 0),t,u,n);if(o&&"[object Object]"!==String(o)&&"[object Object]"!==String(t))return!0}return!!~n.indexOf(r)}for(var u=t.length;u--;)if(~n.indexOf(i(f(t,u))))return!0;return!1},$nin:function(n,t,r,e){return!a.$in(n,t,r,e)},$not:function(n,t,r,e){return!l(n,t,r,e)},$type:function(n,t){return void 0!=t&&(t instanceof n||t.constructor==n)},$all:function(n,t,r,e){return a.$and(n,t,r,e)},$size:function(n,t){return!!t&&n===t.length},$or:function(n,t,r,e){for(var o=0,u=n.length;o<u;o++)if(l(f(n,o),t,r,e))return!0;return!1},$nor:function(n,t,r,e){return!a.$or(n,t,r,e)},$and:function(n,t,r,e){for(var o=0,u=n.length;o<u;o++)if(!l(f(n,o),t,r,e))return!1;return!0},$regex:c(function(n,t){return"string"==typeof t&&n.test(t)}),$where:function(n,t,r,e){return n.call(t,t,r,e)},$elemMatch:function(n,t,r,e){return u(t)?!!~d(t,n):l(n,t,r,e)},$exists:function(n,t,r,e){return e.hasOwnProperty(r)===n}},p={$eq:function(n){return n instanceof RegExp?function(t){return"string"==typeof t&&n.test(t)}:n instanceof Function?n:u(n)&&!n.length?function(n){return u(n)&&!n.length}:null===n?function(n){return null==n}:function(t){return 0===b(i(t),i(n))}},$ne:function(n){return p.$eq(n)},$and:function(n){return n.map($)},$all:function(n){return p.$and(n)},$or:function(n){return n.map($)},$nor:function(n){return n.map($)},$not:function(n){return $(n)},$regex:function(n,t){return new RegExp(n,t.$options)},$where:function(n){return"string"==typeof n?new Function("obj","return "+n):n},$elemMatch:function(n){return $(n)},$exists:function(n){return!!n}};function d(n,t){for(var r=0;r<n.length;r++){f(n,r);if(l(t,f(n,r)))return r}return-1}function s(n,t){return{a:n,v:t}}function v(n,t){var r=[];if(function n(t,r,e,o,i){if(e===r.length||void 0==t)return void i.push([t,r[e-1],o]);var c=f(r,e);if(u(t)&&isNaN(Number(c)))for(var l=0,a=t.length;l<a;l++)n(f(t,l),r,e,t,i);else n(f(t,c),r,e+1,t,i)}(t,n.k,0,t,r),1===r.length){var e=r[0];return l(n.nv,e[0],e[1],e[2])}for(var o=n&&n.q&&void 0!==n.q.$ne,i=o,c=0;c<r.length;c++){var a=r[c],p=l(n.nv,a[0],a[1],a[2]);o?i&=p:i|=p}return i}function g(n,t,r){return{a:{k:n,nv:t,q:r},v:v}}function $(n){(n=i(n))&&function(n){return n&&n.constructor===Object}(n)||(n={$eq:n});var t=[];for(var r in n){var e=n[r];if("$options"!==r)if(a[r])p[r]&&(e=p[r](e,n)),t.push(s(i(e),a[r]));else{if(36===r.charCodeAt(0))throw new Error("Unknown operation "+r);t.push(g(r.split("."),$(e),e))}}return 1===t.length?t[0]:s(t,a.$and)}function y(n,t){var r=$(n);return t&&(r={a:r,v:function(n,r,e,o){return l(n,t(r),e,o)}}),r}function b(n,t){if(n===t)return 0;if((void 0===n?"undefined":e(n))===(void 0===t?"undefined":e(t))){if(n>t)return 1;if(n<t)return-1}}}])}); | ||
!(function(n, t) { | ||
"object" == typeof exports && "object" == typeof module | ||
? (module.exports = t()) | ||
: "function" == typeof define && define.amd | ||
? define([], t) | ||
: "object" == typeof exports | ||
? (exports.sift = t()) | ||
: (n.sift = t()); | ||
})(window, function() { | ||
return (function(n) { | ||
var t = {}; | ||
function r(e) { | ||
if (t[e]) return t[e].exports; | ||
var u = (t[e] = { i: e, l: !1, exports: {} }); | ||
return n[e].call(u.exports, u, u.exports, r), (u.l = !0), u.exports; | ||
} | ||
return ( | ||
(r.m = n), | ||
(r.c = t), | ||
(r.d = function(n, t, e) { | ||
r.o(n, t) || Object.defineProperty(n, t, { enumerable: !0, get: e }); | ||
}), | ||
(r.r = function(n) { | ||
"undefined" != typeof Symbol && | ||
Symbol.toStringTag && | ||
Object.defineProperty(n, Symbol.toStringTag, { value: "Module" }), | ||
Object.defineProperty(n, "__esModule", { value: !0 }); | ||
}), | ||
(r.t = function(n, t) { | ||
if ((1 & t && (n = r(n)), 8 & t)) return n; | ||
if (4 & t && "object" == typeof n && n && n.__esModule) return n; | ||
var e = Object.create(null); | ||
if ( | ||
(r.r(e), | ||
Object.defineProperty(e, "default", { enumerable: !0, value: n }), | ||
2 & t && "string" != typeof n) | ||
) | ||
for (var u in n) | ||
r.d( | ||
e, | ||
u, | ||
function(t) { | ||
return n[t]; | ||
}.bind(null, u) | ||
); | ||
return e; | ||
}), | ||
(r.n = function(n) { | ||
var t = | ||
n && n.__esModule | ||
? function() { | ||
return n.default; | ||
} | ||
: function() { | ||
return n; | ||
}; | ||
return r.d(t, "a", t), t; | ||
}), | ||
(r.o = function(n, t) { | ||
return Object.prototype.hasOwnProperty.call(n, t); | ||
}), | ||
(r.p = ""), | ||
r((r.s = 0)) | ||
); | ||
})([ | ||
function(n, t, r) { | ||
n.exports = r(1); | ||
}, | ||
function(n, t, r) { | ||
"use strict"; | ||
function e(n) { | ||
var t = "[object " + n + "]"; | ||
return function(n) { | ||
return Object.prototype.toString.call(n) === t; | ||
}; | ||
} | ||
r.r(t), | ||
r.d(t, "default", function() { | ||
return j; | ||
}), | ||
r.d(t, "indexOf", function() { | ||
return O; | ||
}), | ||
r.d(t, "compare", function() { | ||
return m; | ||
}); | ||
var u = e("Array"), | ||
o = e("Object"), | ||
i = e("Function"); | ||
function f(n) { | ||
return n instanceof Date | ||
? n.getTime() | ||
: u(n) | ||
? n.map(f) | ||
: n && "function" == typeof n.toJSON | ||
? n.toJSON() | ||
: n; | ||
} | ||
function c(n, t) { | ||
return i(n.get) ? n.get(t) : n[t]; | ||
} | ||
function a(n) { | ||
return function(t, r) { | ||
if (!u(r) || !r.length) return n(t, r); | ||
for (var e = 0, o = r.length; e < o; e++) | ||
if (n(t, c(r, e))) return !0; | ||
return !1; | ||
}; | ||
} | ||
function l(n, t, r, e) { | ||
return n.v(n.a, t, r, e); | ||
} | ||
var p = { | ||
$eq: a(function(n, t) { | ||
return n(t); | ||
}), | ||
$ne: (function(n) { | ||
return function(t, r) { | ||
if (!u(r) || !r.length) return n(t, r); | ||
for (var e = 0, o = r.length; e < o; e++) | ||
if (!n(t, c(r, e))) return !1; | ||
return !0; | ||
}; | ||
})(function(n, t) { | ||
return !n(t); | ||
}), | ||
$gt: a(function(n, t) { | ||
return m(f(t), n) > 0; | ||
}), | ||
$gte: a(function(n, t) { | ||
return m(f(t), n) >= 0; | ||
}), | ||
$lt: a(function(n, t) { | ||
return m(f(t), n) < 0; | ||
}), | ||
$lte: a(function(n, t) { | ||
return m(f(t), n) <= 0; | ||
}), | ||
$mod: a(function(n, t) { | ||
return t % n[0] == n[1]; | ||
}), | ||
$in: function(n, t) { | ||
if (!(t instanceof Array)) { | ||
var r = f(t); | ||
if (r === t && "object" == typeof t) | ||
for (u = n.length; u--; ) | ||
if ( | ||
String(n[u]) === String(t) && | ||
"[object Object]" !== String(t) | ||
) | ||
return !0; | ||
if (void 0 === r) | ||
for (u = n.length; u--; ) if (null == n[u]) return !0; | ||
for (u = n.length; u--; ) { | ||
var e = l(y(c(n, u), void 0), t, u, n); | ||
if ( | ||
e && | ||
"[object Object]" !== String(e) && | ||
"[object Object]" !== String(t) | ||
) | ||
return !0; | ||
} | ||
return !!~n.indexOf(r); | ||
} | ||
for (var u = t.length; u--; ) if (~n.indexOf(f(c(t, u)))) return !0; | ||
return !1; | ||
}, | ||
$nin: function(n, t, r, e) { | ||
return !p.$in(n, t, r, e); | ||
}, | ||
$not: function(n, t, r, e) { | ||
return !l(n, t, r, e); | ||
}, | ||
$type: function(n, t) { | ||
return void 0 != t && (t instanceof n || t.constructor == n); | ||
}, | ||
$all: function(n, t, r, e) { | ||
return p.$and(n, t, r, e); | ||
}, | ||
$size: function(n, t) { | ||
return !!t && n === t.length; | ||
}, | ||
$or: function(n, t, r, e) { | ||
for (var u = 0, o = n.length; u < o; u++) | ||
if (l(c(n, u), t, r, e)) return !0; | ||
return !1; | ||
}, | ||
$nor: function(n, t, r, e) { | ||
return !p.$or(n, t, r, e); | ||
}, | ||
$and: function(n, t, r, e) { | ||
for (var u = 0, o = n.length; u < o; u++) | ||
if (!l(c(n, u), t, r, e)) return !1; | ||
return !0; | ||
}, | ||
$regex: a(function(n, t) { | ||
return "string" == typeof t && n.test(t); | ||
}), | ||
$where: function(n, t, r, e) { | ||
return n.call(t, t, r, e); | ||
}, | ||
$elemMatch: function(n, t, r, e) { | ||
return u(t) ? !!~g(t, n) : l(n, t, r, e); | ||
}, | ||
$exists: function(n, t, r, e) { | ||
return e.hasOwnProperty(r) === n; | ||
} | ||
}, | ||
s = { | ||
$eq: function(n) { | ||
return n instanceof RegExp | ||
? function(t) { | ||
return "string" == typeof t && n.test(t); | ||
} | ||
: n instanceof Function | ||
? n | ||
: u(n) && !n.length | ||
? function(n) { | ||
return u(n) && !n.length; | ||
} | ||
: null === n | ||
? function(n) { | ||
return null == n; | ||
} | ||
: function(t) { | ||
return 0 === m(f(t), f(n)); | ||
}; | ||
}, | ||
$ne: function(n) { | ||
return s.$eq(n); | ||
}, | ||
$and: function(n) { | ||
return n.map(h); | ||
}, | ||
$all: function(n) { | ||
return s.$and(n); | ||
}, | ||
$or: function(n) { | ||
return n.map(h); | ||
}, | ||
$nor: function(n) { | ||
return n.map(h); | ||
}, | ||
$not: function(n) { | ||
return h(n); | ||
}, | ||
$regex: function(n, t) { | ||
return new RegExp(n, t.$options); | ||
}, | ||
$where: function(n) { | ||
return "string" == typeof n | ||
? new Function("obj", "return " + n) | ||
: n; | ||
}, | ||
$elemMatch: function(n) { | ||
return h(n); | ||
}, | ||
$exists: function(n) { | ||
return !!n; | ||
} | ||
}; | ||
function g(n, t) { | ||
for (var r = 0; r < n.length; r++) { | ||
c(n, r); | ||
if (l(t, c(n, r))) return r; | ||
} | ||
return -1; | ||
} | ||
function v(n, t) { | ||
return { a: n, v: t }; | ||
} | ||
function d(n, t) { | ||
var r = []; | ||
if ( | ||
((function n(t, r, e, o, i) { | ||
if (e === r.length || void 0 == t) | ||
return void i.push([t, r[e - 1], o]); | ||
var f = c(r, e); | ||
if (u(t) && isNaN(Number(f))) | ||
for (var a = 0, l = t.length; a < l; a++) n(c(t, a), r, e, t, i); | ||
else n(c(t, f), r, e + 1, t, i); | ||
})(t, n.k, 0, t, r), | ||
1 === r.length) | ||
) { | ||
var e = r[0]; | ||
return l(n.nv, e[0], e[1], e[2]); | ||
} | ||
for ( | ||
var o = n && n.q && void 0 !== n.q.$ne, i = o, f = 0; | ||
f < r.length; | ||
f++ | ||
) { | ||
var a = r[f], | ||
p = l(n.nv, a[0], a[1], a[2]); | ||
o ? (i &= p) : (i |= p); | ||
} | ||
return i; | ||
} | ||
function $(n, t, r) { | ||
return { a: { k: n, nv: t, q: r }, v: d }; | ||
} | ||
function h(n) { | ||
if ( | ||
(((n = f(n)) && | ||
(function(n) { | ||
return n && n.constructor === Object; | ||
})(n)) || | ||
(n = { $eq: n }), | ||
(function(n) { | ||
return ( | ||
-1 === | ||
(function n(t, r) { | ||
if (!o(t)) return r; | ||
for (var e in t) r.push(e), n(t[e], r); | ||
return r; | ||
})(n, []) | ||
.join(",") | ||
.search(/[$.]/) | ||
); | ||
})(n)) | ||
) | ||
return v(n, b); | ||
var t = []; | ||
for (var r in n) { | ||
var e = n[r]; | ||
if ("$options" !== r) | ||
if (p[r]) s[r] && (e = s[r](e, n)), t.push(v(f(e), p[r])); | ||
else { | ||
if (36 === r.charCodeAt(0)) | ||
throw new Error("Unknown operation " + r); | ||
var u = r.split("."); | ||
t.push($(u, h(e), e)); | ||
} | ||
} | ||
return 1 === t.length ? t[0] : v(t, p.$and); | ||
} | ||
function b(n, t) { | ||
if ( | ||
Object.prototype.toString.call(n) !== | ||
Object.prototype.toString.call(t) | ||
) | ||
return !1; | ||
if (o(n)) { | ||
if (Object.keys(n).length !== Object.keys(t).length) return !1; | ||
for (var r in n) if (!b(n[r], t[r])) return !1; | ||
return !0; | ||
} | ||
if (u(n)) { | ||
if (n.length !== t.length) return !1; | ||
for (var e = 0, i = n.length; e < i; e++) | ||
if (!b(n[e], t[e])) return !1; | ||
return !0; | ||
} | ||
return n === t; | ||
} | ||
function y(n, t) { | ||
var r = h(n); | ||
return ( | ||
t && | ||
(r = { | ||
a: r, | ||
v: function(n, r, e, u) { | ||
return l(n, t(r), e, u); | ||
} | ||
}), | ||
r | ||
); | ||
} | ||
function j(n, t) { | ||
var r = y(n, t); | ||
return function(n, t, e) { | ||
return l(r, n, t, e); | ||
}; | ||
} | ||
function O(n, t, r) { | ||
return g(t, y(n, r)); | ||
} | ||
function m(n, t) { | ||
if (n === t) return 0; | ||
if (typeof n == typeof t) { | ||
if (n > t) return 1; | ||
if (n < t) return -1; | ||
} | ||
} | ||
} | ||
]); | ||
}); |
180
src/index.js
@@ -9,7 +9,7 @@ /* | ||
/** | ||
*/ | ||
function isFunction(value) { | ||
return typeof value === 'function'; | ||
function typeChecker(type) { | ||
var typeString = "[object " + type + "]"; | ||
return function(value) { | ||
return Object.prototype.toString.call(value) === typeString; | ||
}; | ||
} | ||
@@ -20,5 +20,5 @@ | ||
function isArray(value) { | ||
return Object.prototype.toString.call(value) === '[object Array]'; | ||
} | ||
var isArray = typeChecker("Array"); | ||
var isObject = typeChecker("Object"); | ||
var isFunction = typeChecker("Function"); | ||
@@ -33,3 +33,3 @@ /** | ||
return value.map(comparable); | ||
} else if (value && typeof value.toJSON === 'function') { | ||
} else if (value && typeof value.toJSON === "function") { | ||
return value.toJSON(); | ||
@@ -54,6 +54,6 @@ } else { | ||
for (var i = 0, n = b.length; i < n; i++) { | ||
if (validator(a, get(b,i))) return true; | ||
if (validator(a, get(b, i))) return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
} | ||
@@ -80,4 +80,3 @@ | ||
var OPERATORS = { | ||
var operators = { | ||
/** | ||
@@ -136,5 +135,4 @@ */ | ||
$in: function(a, b) { | ||
if (b instanceof Array) { | ||
for (var i = b.length; i--;) { | ||
for (var i = b.length; i--; ) { | ||
if (~a.indexOf(comparable(get(b, i)))) { | ||
@@ -146,5 +144,5 @@ return true; | ||
var comparableB = comparable(b); | ||
if (comparableB === b && typeof b === 'object') { | ||
for (var i = a.length; i--;) { | ||
if (String(a[i]) === String(b) && String(b) !== '[object Object]') { | ||
if (comparableB === b && typeof b === "object") { | ||
for (var i = a.length; i--; ) { | ||
if (String(a[i]) === String(b) && String(b) !== "[object Object]") { | ||
return true; | ||
@@ -159,4 +157,4 @@ } | ||
*/ | ||
if (typeof comparableB == 'undefined') { | ||
for (var i = a.length; i--;) { | ||
if (typeof comparableB == "undefined") { | ||
for (var i = a.length; i--; ) { | ||
if (a[i] == null) { | ||
@@ -171,6 +169,10 @@ return true; | ||
*/ | ||
for (var i = a.length; i--;) { | ||
for (var i = a.length; i--; ) { | ||
var validator = createRootValidator(get(a, i), undefined); | ||
var result = validate(validator, b, i, a); | ||
if ((result) && (String(result) !== '[object Object]') && (String(b) !== '[object Object]')) { | ||
if ( | ||
result && | ||
String(result) !== "[object Object]" && | ||
String(b) !== "[object Object]" | ||
) { | ||
return true; | ||
@@ -190,3 +192,3 @@ } | ||
$nin: function(a, b, k, o) { | ||
return !OPERATORS.$in(a, b, k, o); | ||
return !operators.$in(a, b, k, o); | ||
}, | ||
@@ -206,3 +208,3 @@ | ||
return b != void 0 ? b instanceof a || b.constructor == a : false; | ||
}, | ||
}, | ||
@@ -213,3 +215,3 @@ /** | ||
$all: function(a, b, k, o) { | ||
return OPERATORS.$and(a, b, k, o); | ||
return operators.$and(a, b, k, o); | ||
}, | ||
@@ -228,3 +230,4 @@ | ||
$or: function(a, b, k, o) { | ||
for (var i = 0, n = a.length; i < n; i++) if (validate(get(a, i), b, k, o)) return true; | ||
for (var i = 0, n = a.length; i < n; i++) | ||
if (validate(get(a, i), b, k, o)) return true; | ||
return false; | ||
@@ -237,3 +240,3 @@ }, | ||
$nor: function(a, b, k, o) { | ||
return !OPERATORS.$or(a, b, k, o); | ||
return !operators.$or(a, b, k, o); | ||
}, | ||
@@ -257,3 +260,3 @@ | ||
$regex: or(function(a, b) { | ||
return typeof b === 'string' && a.test(b); | ||
return typeof b === "string" && a.test(b); | ||
}), | ||
@@ -290,3 +293,2 @@ | ||
var prepare = { | ||
/** | ||
@@ -296,6 +298,5 @@ */ | ||
$eq: function(a) { | ||
if (a instanceof RegExp) { | ||
return function(b) { | ||
return typeof b === 'string' && a.test(b); | ||
return typeof b === "string" && a.test(b); | ||
}; | ||
@@ -307,9 +308,9 @@ } else if (a instanceof Function) { | ||
return function(b) { | ||
return (isArray(b) && !b.length); | ||
return isArray(b) && !b.length; | ||
}; | ||
} else if (a === null){ | ||
return function(b){ | ||
} else if (a === null) { | ||
return function(b) { | ||
//will match both null and undefined | ||
return b == null; | ||
} | ||
}; | ||
} | ||
@@ -375,3 +376,3 @@ | ||
$where: function(a) { | ||
return typeof a === 'string' ? new Function('obj', 'return ' + a) : a; | ||
return typeof a === "string" ? new Function("obj", "return " + a) : a; | ||
}, | ||
@@ -398,3 +399,2 @@ | ||
function search(array, validator) { | ||
for (var i = 0; i < array.length; i++) { | ||
@@ -421,3 +421,3 @@ var result = get(array, i); | ||
function nestedValidator(a, b) { | ||
var values = []; | ||
var values = []; | ||
findValues(b, a.k, 0, b, values); | ||
@@ -431,3 +431,3 @@ | ||
// If the query contains $ne, need to test all elements ANDed together | ||
var inclusive = a && a.q && typeof a.q.$ne !== 'undefined'; | ||
var inclusive = a && a.q && typeof a.q.$ne !== "undefined"; | ||
var allValid = inclusive; | ||
@@ -450,5 +450,3 @@ for (var i = 0; i < values.length; i++) { | ||
function findValues(current, keypath, index, object, values) { | ||
if (index === keypath.length || current == void 0) { | ||
values.push([current, keypath[index - 1], object]); | ||
@@ -490,6 +488,11 @@ return; | ||
if (!query || !isVanillaObject(query)) { // cross browser support | ||
if (!query || !isVanillaObject(query)) { | ||
// cross browser support | ||
query = { $eq: query }; | ||
} | ||
if (isExactObject(query)) { | ||
return createValidator(query, isEqual); | ||
} | ||
var validators = []; | ||
@@ -500,21 +503,73 @@ | ||
if (key === '$options') { | ||
if (key === "$options") { | ||
continue; | ||
} | ||
if (OPERATORS[key]) { | ||
if (operators[key]) { | ||
if (prepare[key]) a = prepare[key](a, query); | ||
validators.push(createValidator(comparable(a), OPERATORS[key])); | ||
validators.push(createValidator(comparable(a), operators[key])); | ||
} else { | ||
if (key.charCodeAt(0) === 36) { | ||
throw new Error('Unknown operation ' + key); | ||
throw new Error("Unknown operation " + key); | ||
} | ||
validators.push(createNestedValidator(key.split('.'), parse(a), a)); | ||
var keyParts = key.split("."); | ||
validators.push(createNestedValidator(keyParts, parse(a, key), a)); | ||
} | ||
} | ||
return validators.length === 1 ? validators[0] : createValidator(validators, OPERATORS.$and); | ||
return validators.length === 1 | ||
? validators[0] | ||
: createValidator(validators, operators.$and); | ||
} | ||
function isEqual(a, b) { | ||
if (Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)) { | ||
return false; | ||
} | ||
if (isObject(a)) { | ||
if (Object.keys(a).length !== Object.keys(b).length) { | ||
return false; | ||
} | ||
for (var key in a) { | ||
if (!isEqual(a[key], b[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else if (isArray(a)) { | ||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
for (var i = 0, n = a.length; i < n; i++) { | ||
if (!isEqual(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return a === b; | ||
} | ||
} | ||
function getAllKeys(value, keys) { | ||
if (!isObject(value)) { | ||
return keys; | ||
} | ||
for (var key in value) { | ||
keys.push(key); | ||
getAllKeys(value[key], keys); | ||
} | ||
return keys; | ||
} | ||
function isExactObject(value) { | ||
const allKeysHash = getAllKeys(value, []).join(","); | ||
return allKeysHash.search(/[$.]/) === -1; | ||
} | ||
/** | ||
@@ -539,20 +594,7 @@ */ | ||
export default function sift(query, array, getter) { | ||
if (isFunction(array)) { | ||
getter = array; | ||
array = void 0; | ||
} | ||
export default function sift(query, getter) { | ||
var validator = createRootValidator(query, getter); | ||
function filter(b, k, o) { | ||
return function(b, k, o) { | ||
return validate(validator, b, k, o); | ||
} | ||
if (array) { | ||
return array.filter(filter); | ||
} | ||
return filter; | ||
}; | ||
} | ||
@@ -565,3 +607,3 @@ | ||
return search(array, createRootValidator(query, getter)); | ||
}; | ||
} | ||
@@ -572,4 +614,4 @@ /** | ||
export function compare(a, b) { | ||
if(a===b) return 0; | ||
if(typeof a === typeof b) { | ||
if (a === b) return 0; | ||
if (typeof a === typeof b) { | ||
if (a > b) { | ||
@@ -582,2 +624,2 @@ return 1; | ||
} | ||
}; | ||
} |
@@ -1,12 +0,12 @@ | ||
import * as assert from 'assert'; | ||
import sift, {indexOf as siftIndexOf} from '..'; | ||
import * as assert from "assert"; | ||
import sift, { indexOf as siftIndexOf } from ".."; | ||
describe(__filename + '#', function() { | ||
describe(__filename + "#", function() { | ||
it("doesn't sort arrays", function() { | ||
var values = [9, 8, 7, 6, 5, 4, 3, 2, 1].filter( | ||
sift({ | ||
$or: [3, 2, 1] | ||
}) | ||
); | ||
it('doesn\'t sort arrays', function () { | ||
var values = sift({ | ||
$or: [3, 2, 1] | ||
}, [9,8,7,6,5,4,3,2,1]); | ||
assert.equal(values.length, 3); | ||
@@ -18,11 +18,10 @@ assert.equal(values[0], 3); | ||
it('can create a custom selector, and use it', function () { | ||
var sifter = sift({ age: { $gt: 5}}, function (item) { | ||
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 = people.filter(sifter); | ||
var people = [{ person: { age: 6 } }], | ||
filtered = people.filter(sifter); | ||
assert.equal(filtered.length, 1); | ||
@@ -32,7 +31,6 @@ assert.equal(filtered[0], people[0]); | ||
it('throws an error if the operation is invalid', function () { | ||
it("throws an error if the operation is invalid", function() { | ||
var err; | ||
try { | ||
sift({$aaa:1})('b'); | ||
sift({ $aaa: 1 })("b"); | ||
} catch (e) { | ||
@@ -42,14 +40,14 @@ err = e; | ||
assert.equal(err.message, 'Unknown operation $aaa'); | ||
assert.equal(err.message, "Unknown operation $aaa"); | ||
}); | ||
it('can use a custom selector as the 3rd param', function () { | ||
it("can use a custom selector as the 3rd param", function() { | ||
var people = [{ person: { age: 6 } }]; | ||
var people = [{ person: { age: 6 }}]; | ||
var filtered = people.filter( | ||
sift({ age: { $gt: 5 } }, function(item) { | ||
return item.person; | ||
}) | ||
); | ||
var filtered = sift({ age: { $gt: 5}}, people, function (item) { | ||
return item.person; | ||
}); | ||
assert.equal(filtered.length, 1); | ||
@@ -59,4 +57,9 @@ assert.equal(filtered[0], people[0]); | ||
it('can get the first index of a matching element', function () { | ||
var index = siftIndexOf({ val: { $gt: 5}}, [{val: 4}, {val: 3}, {val: 6}, {val: 7}]); | ||
it("can get the first index of a matching element", function() { | ||
var index = siftIndexOf({ val: { $gt: 5 } }, [ | ||
{ val: 4 }, | ||
{ val: 3 }, | ||
{ val: 6 }, | ||
{ val: 7 } | ||
]); | ||
@@ -66,4 +69,9 @@ assert.equal(index, 2); | ||
it('returns -1 as index if no matching element is found', function () { | ||
var index = siftIndexOf({ val: { $gt: 7}}, [{val: 4}, {val: 3}, {val: 6}, {val: 7}]); | ||
it("returns -1 as index if no matching element is found", function() { | ||
var index = siftIndexOf({ val: { $gt: 7 } }, [ | ||
{ val: 4 }, | ||
{ val: 3 }, | ||
{ val: 6 }, | ||
{ val: 7 } | ||
]); | ||
@@ -73,12 +81,16 @@ assert.equal(index, -1); | ||
it('can match empty arrays', function () { | ||
var statusQuery = {$or: [{status: {$exists: false}}, | ||
{status: []}, | ||
{status: {$in: ['urgent', 'completed', 'today']}} | ||
]}; | ||
it("can match empty arrays", function() { | ||
var statusQuery = { | ||
$or: [ | ||
{ status: { $exists: false } }, | ||
{ status: [] }, | ||
{ status: { $in: ["urgent", "completed", "today"] } } | ||
] | ||
}; | ||
var filtered = sift(statusQuery, [{ status: [] }, | ||
{ status: ['urgent'] }, | ||
{ status: ['nope'] } | ||
]); | ||
var filtered = [ | ||
{ status: [] }, | ||
{ status: ["urgent"] }, | ||
{ status: ["nope"] } | ||
].filter(sift(statusQuery)); | ||
@@ -88,20 +100,17 @@ assert.equal(filtered.length, 2); | ||
it('$ne: null does not hit when field is present', function(){ | ||
var sifter = sift({age: {$ne: null}}); | ||
it("$ne: null does not hit when field is present", function() { | ||
var sifter = sift({ age: { $ne: null } }); | ||
var people = [ | ||
{age: 'matched'}, | ||
{missed: 1} | ||
]; | ||
var people = [{ age: "matched" }, { missed: 1 }]; | ||
var filtered = people.filter(sifter); | ||
assert.equal(filtered.length, 1); | ||
assert.equal(filtered[0].age, 'matched'); | ||
assert.equal(filtered[0].age, "matched"); | ||
}); | ||
it('$ne does not hit when field is different', function () { | ||
var sifter = sift({ age: { $ne: 5 }}); | ||
it("$ne does not hit when field is different", function() { | ||
var sifter = sift({ age: { $ne: 5 } }); | ||
var people = [{ age: 5 }], | ||
filtered = people.filter(sifter); | ||
filtered = people.filter(sifter); | ||
@@ -111,7 +120,7 @@ assert.equal(filtered.length, 0); | ||
it('$ne does hit when field exists with different value', function () { | ||
var sifter = sift({ age: { $ne: 4 }}); | ||
it("$ne does hit when field exists with different value", function() { | ||
var sifter = sift({ age: { $ne: 4 } }); | ||
var people = [{ age: 5 }], | ||
filtered = people.filter(sifter); | ||
filtered = people.filter(sifter); | ||
@@ -121,4 +130,4 @@ assert.equal(filtered.length, 1); | ||
it('$ne does hit when field does not exist', function(){ | ||
var sifter = sift({ age: { $ne: 5 }}); | ||
it("$ne does hit when field does not exist", function() { | ||
var sifter = sift({ age: { $ne: 5 } }); | ||
@@ -131,3 +140,3 @@ var people = [{}], | ||
it('$eq matches objects that serialize to the same value', function() { | ||
it("$eq matches objects that serialize to the same value", function() { | ||
var counter = 0; | ||
@@ -143,7 +152,7 @@ function Book(name) { | ||
var warAndPeace = new Book('War and Peace'); | ||
var warAndPeace = new Book("War and Peace"); | ||
var sifter = sift({ $eq: warAndPeace}); | ||
var sifter = sift({ $eq: warAndPeace }); | ||
var books = [ new Book('War and Peace')]; | ||
var books = [new Book("War and Peace")]; | ||
var filtered = books.filter(sifter); | ||
@@ -154,3 +163,3 @@ | ||
it('$neq does not match objects that serialize to the same value', function() { | ||
it("$neq does not match objects that serialize to the same value", function() { | ||
var counter = 0; | ||
@@ -166,7 +175,7 @@ function Book(name) { | ||
var warAndPeace = new Book('War and Peace'); | ||
var warAndPeace = new Book("War and Peace"); | ||
var sifter = sift({ $ne: warAndPeace}); | ||
var sifter = sift({ $ne: warAndPeace }); | ||
var books = [ new Book('War and Peace')]; | ||
var books = [new Book("War and Peace")]; | ||
var filtered = books.filter(sifter); | ||
@@ -178,45 +187,40 @@ | ||
// https://gist.github.com/jdnichollsc/00ea8cf1204b17d9fb9a991fbd1dfee6 | ||
it('returns a period between start and end dates', function() { | ||
it("returns a period between start and end dates", function() { | ||
var product = { | ||
'productTypeCode': 'productTypeEnergy', | ||
'quantities': [ | ||
productTypeCode: "productTypeEnergy", | ||
quantities: [ | ||
{ | ||
'period': { | ||
'startDate': new Date('2017-01-13T05:00:00.000Z'), | ||
'endDate': new Date('2017-01-31T05:00:00.000Z'), | ||
'dayType': { | ||
'normal': true, | ||
'holiday': true | ||
period: { | ||
startDate: new Date("2017-01-13T05:00:00.000Z"), | ||
endDate: new Date("2017-01-31T05:00:00.000Z"), | ||
dayType: { | ||
normal: true, | ||
holiday: true | ||
}, | ||
'specificDays': [ | ||
'monday', | ||
'wednesday', | ||
'friday' | ||
], | ||
'loadType': { | ||
'high': true, | ||
'medium': false, | ||
'low': false | ||
specificDays: ["monday", "wednesday", "friday"], | ||
loadType: { | ||
high: true, | ||
medium: false, | ||
low: false | ||
} | ||
}, | ||
'type': 'DemandPercentage', | ||
'quantityValue': '44' | ||
type: "DemandPercentage", | ||
quantityValue: "44" | ||
}, | ||
{ | ||
'period': { | ||
'startDate': new Date('2017-01-13T05:00:00.000Z'), | ||
'endDate': new Date('2017-01-31T05:00:00.000Z'), | ||
'dayType': { | ||
'normal': true, | ||
'holiday': true | ||
period: { | ||
startDate: new Date("2017-01-13T05:00:00.000Z"), | ||
endDate: new Date("2017-01-31T05:00:00.000Z"), | ||
dayType: { | ||
normal: true, | ||
holiday: true | ||
}, | ||
'loadType': { | ||
'high': false, | ||
'medium': true, | ||
'low': false | ||
loadType: { | ||
high: false, | ||
medium: true, | ||
low: false | ||
} | ||
}, | ||
'type': 'Value', | ||
'quantityValue': '22' | ||
type: "Value", | ||
quantityValue: "22" | ||
} | ||
@@ -227,26 +231,27 @@ ] | ||
var period = { | ||
'startDate': new Date('2017-01-08T05:00:00.000Z'), | ||
'endDate': new Date('2017-01-29T05:00:00.000Z'), | ||
'dayType': { | ||
'normal': true, | ||
'holiday': true | ||
startDate: new Date("2017-01-08T05:00:00.000Z"), | ||
endDate: new Date("2017-01-29T05:00:00.000Z"), | ||
dayType: { | ||
normal: true, | ||
holiday: true | ||
}, | ||
'loadType': { | ||
'high': true, | ||
'medium': false, | ||
'low': true | ||
loadType: { | ||
high: true, | ||
medium: false, | ||
low: true | ||
}, | ||
specificPeriods : ['3', '4', '5-10'] | ||
specificPeriods: ["3", "4", "5-10"] | ||
}; | ||
var results = product.quantities.filter( | ||
sift({ | ||
$and: [ | ||
{ "period.startDate": { $lte: period.endDate } }, | ||
{ "period.endDate": { $gte: period.startDate } } | ||
] | ||
}) | ||
); | ||
var results = sift({ | ||
$and: [ | ||
{ 'period.startDate': { $lte : period.endDate } }, | ||
{ 'period.endDate': { $gte : period.startDate } } | ||
] | ||
}, product.quantities); | ||
assert.equal(results.length, 2); | ||
}); | ||
}); |
@@ -1,20 +0,19 @@ | ||
import * as assert from 'assert'; | ||
import * as Immutable from 'immutable'; | ||
import * as assert from "assert"; | ||
import * as Immutable from "immutable"; | ||
import sift from '..'; | ||
const ObjectID = require('bson').ObjectID; | ||
import sift from ".."; | ||
const ObjectID = require("bson").ObjectID; | ||
describe(__filename + '#', function() { | ||
describe(__filename + "#", function() { | ||
var topic = Immutable.List([1, 2, 3, 4, 5, 6, 6, 4, 3]); | ||
var persons = Immutable.fromJS([ | ||
{ person: { age: 3 } }, | ||
{ person: { age: 5 } }, | ||
{ person: { age: 8 } } | ||
]); | ||
var persons = Immutable.fromJS([{ person: {age: 3} }, { person: {age: 5} }, { person: {age: 8} }]); | ||
it('works with Immutable.Map in a Immutable.List', function() { | ||
assert.equal(sift({ 'person.age' : { $gt: 4 } }, persons).size, 2); | ||
assert.equal(persons.filter(sift({ 'person.age' : { $gt: 4 } })).size, 2); | ||
it("works with Immutable.Map in a Immutable.List", function() { | ||
assert.equal(persons.filter(sift({ "person.age": { $gt: 4 } })).size, 2); | ||
}); | ||
}); |
@@ -1,409 +0,437 @@ | ||
import assert from 'assert'; | ||
import sift from '..'; | ||
import assert from "assert"; | ||
import sift from ".."; | ||
describe(__filename + '#', function () { | ||
describe(__filename + "#", function() { | ||
var topic = [ | ||
{ | ||
name: "craig", | ||
age: 90001, | ||
tags: ["coder", "programmer", "traveler", "photographer"], | ||
address: { | ||
city: "Minneapolis", | ||
state: "MN", | ||
phone: "9999999999" | ||
}, | ||
tags: ["photos", "cook"], | ||
hobbies: [ | ||
{ | ||
name: "programming", | ||
description: "some desc" | ||
}, | ||
{ | ||
name: "cooking" | ||
}, | ||
{ | ||
name: "photography", | ||
places: ["haiti", "brazil", "costa rica"] | ||
}, | ||
{ | ||
name: "backpacking" | ||
} | ||
] | ||
}, | ||
{ | ||
name: "tim", | ||
age: 90001, | ||
tags: ["traveler", "photographer"], | ||
address: { | ||
city: "St. Paul", | ||
state: "MN", | ||
phone: "765765756765" | ||
}, | ||
tags: ["dj"], | ||
hobbies: [ | ||
{ | ||
name: "biking", | ||
description: "some desc" | ||
}, | ||
{ | ||
name: "DJ" | ||
}, | ||
{ | ||
name: "photography", | ||
places: ["costa rica"] | ||
} | ||
] | ||
} | ||
]; | ||
it("has sifted through photography in brazil count of 1", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
hobbies: { | ||
name: "photography", | ||
places: { | ||
$in: ["brazil"] | ||
} | ||
} | ||
}) | ||
); | ||
assert.equal(sifted.length, 1); | ||
}); | ||
it("has sifted through photography in brazil, haiti, and costa rica count of 1", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
hobbies: { | ||
name: "photography", | ||
places: { | ||
$all: ["brazil", "haiti", "costa rica"] | ||
} | ||
} | ||
}) | ||
); | ||
assert.equal(sifted.length, 1); | ||
assert.equal(sifted[0], topic[0]); | ||
}); | ||
it("has a sifted hobbies of photography, cooking, or biking count of 2", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
hobbies: { | ||
name: { | ||
$in: ["photography", "cooking", "biking"] | ||
} | ||
} | ||
}) | ||
); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it("has sifted to complex count of 2", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
hobbies: { | ||
name: "photography", | ||
places: { | ||
$in: ["costa rica"] | ||
} | ||
}, | ||
address: { | ||
state: "MN", | ||
phone: { | ||
$exists: true | ||
} | ||
} | ||
}) | ||
); | ||
var topic = [ | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it("has sifted to complex count of 0", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
hobbies: { | ||
name: "photos", | ||
places: { | ||
$in: ["costa rica"] | ||
} | ||
} | ||
}) | ||
); | ||
assert.equal(sifted.length, 0); | ||
}); | ||
it("has sifted subobject hobbies count of 3", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
"hobbies.name": "photography" | ||
}) | ||
); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it("has sifted dot-notation hobbies of photography, cooking, and biking count of 3", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
"hobbies.name": { | ||
$in: ["photography", "cooking", "biking"] | ||
} | ||
}) | ||
); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it("has sifted to complex dot-search count of 2", function() { | ||
var sifted = topic.filter( | ||
sift({ | ||
"hobbies.name": "photography", | ||
"hobbies.places": { | ||
$in: ["costa rica"] | ||
}, | ||
"address.state": "MN", | ||
"address.phone": { | ||
$exists: true | ||
} | ||
}) | ||
); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it("has sifted with selector function count of 2", function() { | ||
var sifted = topic.filter( | ||
sift( | ||
{ | ||
name: 'craig', | ||
age: 90001, | ||
tags: ['coder', 'programmer', 'traveler', 'photographer'], | ||
address: { | ||
city: 'Minneapolis', | ||
state: 'MN', | ||
phone: '9999999999' | ||
}, | ||
tags: ['photos', 'cook'], | ||
hobbies: [ | ||
{ | ||
name: 'programming', | ||
description: 'some desc' | ||
}, | ||
{ | ||
name: 'cooking' | ||
}, | ||
{ | ||
name: 'photography', | ||
places: ['haiti', 'brazil', 'costa rica'] | ||
}, | ||
{ | ||
name: 'backpacking' | ||
} | ||
] | ||
name: "photography", | ||
places: { | ||
$in: ["costa rica"] | ||
} | ||
}, | ||
{ | ||
name: 'tim', | ||
age: 90001, | ||
tags: ['traveler', 'photographer'], | ||
address: { | ||
city: 'St. Paul', | ||
state: 'MN', | ||
phone: '765765756765' | ||
}, | ||
tags: ['dj'], | ||
hobbies: [ | ||
{ | ||
name: 'biking', | ||
description: 'some desc' | ||
}, | ||
{ | ||
name: 'DJ' | ||
}, | ||
{ | ||
name: 'photography', | ||
places: ['costa rica'] | ||
} | ||
] | ||
function(item) { | ||
return item.hobbies; | ||
} | ||
]; | ||
xit('throws error if $not is incorrect', function () { | ||
assert.throws(function () { | ||
sift({ | ||
$not: ['abc'] | ||
}, topic); | ||
}, Error); | ||
) | ||
); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
describe("nesting", function() { | ||
it("$eq for nested object", function() { | ||
var sifted = loremArr.filter(sift({ "sub.num": { $eq: 10 } })); | ||
assert(sifted.length > 0); | ||
sifted.forEach(function(v) { | ||
assert.equal(10, v.sub.num); | ||
}); | ||
}); | ||
it('has sifted through photography in brazil count of 1', function () { | ||
var sifted = sift({ | ||
hobbies: { | ||
name: 'photography', | ||
places: { | ||
$in: ['brazil'] | ||
} | ||
} | ||
}, topic); | ||
assert.equal(sifted.length, 1); | ||
it("$ne for nested object", function() { | ||
var sifted = loremArr.filter(sift({ "sub.num": { $ne: 10 } })); | ||
assert(sifted.length > 0); | ||
sifted.forEach(function(v) { | ||
assert.notEqual(10, v.sub.num); | ||
}); | ||
}); | ||
it('has sifted through photography in brazil, haiti, and costa rica count of 1', function () { | ||
var sifted = sift({ | ||
hobbies: { | ||
name: 'photography', | ||
places: { | ||
$all: ['brazil', 'haiti', 'costa rica'] | ||
} | ||
} | ||
}, topic); | ||
assert.equal(sifted.length, 1); | ||
assert.equal(sifted[0], topic[0]); | ||
it("$regex for nested object (one missing key)", function() { | ||
var persons = [ | ||
{ | ||
id: 1, | ||
prof: "Mr. Moriarty" | ||
}, | ||
{ | ||
id: 2, | ||
prof: "Mycroft Holmes" | ||
}, | ||
{ | ||
id: 3, | ||
name: "Dr. Watson", | ||
prof: "Doctor" | ||
}, | ||
{ | ||
id: 4, | ||
name: "Mr. Holmes", | ||
prof: "Detective" | ||
} | ||
]; | ||
var q = { name: { $regex: "n" } }; | ||
var sifted = persons.filter(sift(q)); | ||
assert.deepEqual(sifted, [ | ||
{ | ||
id: 3, | ||
name: "Dr. Watson", | ||
prof: "Doctor" | ||
} | ||
]); | ||
}); | ||
it('has a sifted hobbies of photography, cooking, or biking count of 2', function () { | ||
var sifted = sift({ | ||
hobbies: { | ||
name: { | ||
$in: ['photography', 'cooking', 'biking'] | ||
} | ||
} | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it('has sifted to complex count of 2', function () { | ||
var sifted = sift({ | ||
hobbies: { | ||
name: 'photography', | ||
places: { | ||
$in: ['costa rica'] | ||
} | ||
}, | ||
address: { | ||
state: 'MN', | ||
phone: { | ||
$exists: true | ||
} | ||
} | ||
}, topic); | ||
}); | ||
assert.equal(sifted.length, 2); | ||
describe("arrays of objects", function() { | ||
var objects = [ | ||
{ | ||
things: [ | ||
{ | ||
id: 123 | ||
}, | ||
{ | ||
id: 456 | ||
} | ||
] | ||
}, | ||
{ | ||
things: [ | ||
{ | ||
id: 123 | ||
}, | ||
{ | ||
id: 789 | ||
} | ||
] | ||
} | ||
]; | ||
it("$eq for array of objects, matches if at least one exists", function() { | ||
let q = { | ||
"things.id": 123 | ||
}; | ||
var sifted = objects.filter(sift(q)); | ||
assert.deepEqual(sifted, objects); | ||
let q2 = { | ||
"things.id": 789 | ||
}; | ||
var sifted2 = objects.filter(sift(q2)); | ||
assert.deepEqual(sifted2, [objects[1]]); | ||
}); | ||
it('has sifted to complex count of 0', function () { | ||
var sifted = sift({ | ||
hobbies: { | ||
name: 'photos', | ||
places: { | ||
$in: ['costa rica'] | ||
} | ||
} | ||
}, topic); | ||
assert.equal(sifted.length, 0); | ||
it("$ne for array of objects, returns if none of the array elements match the query", function() { | ||
let q = { | ||
"things.id": { | ||
$ne: 123 | ||
} | ||
}; | ||
var sifted = objects.filter(sift(q)); | ||
assert.deepEqual(sifted, []); | ||
let q2 = { | ||
"things.id": { | ||
$ne: 789 | ||
} | ||
}; | ||
var sifted2 = objects.filter(sift(q2)); | ||
assert.deepEqual(sifted2, [objects[0]]); | ||
}); | ||
it('has sifted subobject hobbies count of 3', function () { | ||
var sifted = sift({ | ||
'hobbies.name': 'photography' | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it('has sifted dot-notation hobbies of photography, cooking, and biking count of 3', function () { | ||
var sifted = sift({ | ||
'hobbies.name': { | ||
$in: ['photography', 'cooking', 'biking'] | ||
} | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it('has sifted to complex dot-search count of 2', function () { | ||
var sifted = sift({ | ||
'hobbies.name': 'photography', | ||
'hobbies.places': { | ||
$in: ['costa rica'] | ||
}, | ||
'address.state': 'MN', | ||
'address.phone': { | ||
$exists: true | ||
} | ||
}, topic); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
it('has sifted with selector function count of 2', function () { | ||
var sifted = sift({ | ||
'name': 'photography', | ||
'places': { | ||
$in: ['costa rica'] | ||
} | ||
}, topic, function (item) { | ||
return item.hobbies; | ||
}); | ||
assert.equal(sifted.length, 2); | ||
}); | ||
}); | ||
describe('nesting', function () { | ||
it('$eq for nested object', function () { | ||
var sifted = sift({'sub.num': {'$eq': 10}}, loremArr); | ||
assert(sifted.length > 0); | ||
sifted.forEach(function (v) { | ||
assert.equal(10, v.sub.num); | ||
}); | ||
}); | ||
describe("$where", function() { | ||
var couples = [ | ||
{ | ||
name: "SMITH", | ||
person: [ | ||
{ | ||
firstName: "craig", | ||
gender: "female", | ||
age: 29 | ||
}, | ||
{ | ||
firstName: "tim", | ||
gender: "male", | ||
age: 32 | ||
} | ||
] | ||
}, | ||
{ | ||
name: "JOHNSON", | ||
person: [ | ||
{ | ||
firstName: "emily", | ||
gender: "female", | ||
age: 35 | ||
}, | ||
{ | ||
firstName: "jacob", | ||
gender: "male", | ||
age: 32 | ||
} | ||
] | ||
} | ||
]; | ||
it('$ne for nested object', function () { | ||
var sifted = sift({'sub.num': {'$ne': 10}}, loremArr); | ||
assert(sifted.length > 0); | ||
sifted.forEach(function (v) { | ||
assert.notEqual(10, v.sub.num); | ||
}); | ||
}); | ||
it("can filter people", function() { | ||
var results = couples.filter( | ||
sift({ person: { $elemMatch: { gender: "female", age: { $lt: 30 } } } }) | ||
); | ||
assert.equal(results[0].name, "SMITH"); | ||
it('$regex for nested object (one missing key)', function () { | ||
var persons = [{ | ||
id: 1, | ||
prof: 'Mr. Moriarty' | ||
}, { | ||
id: 2, | ||
prof: 'Mycroft Holmes' | ||
}, { | ||
id: 3, | ||
name: 'Dr. Watson', | ||
prof: 'Doctor' | ||
}, { | ||
id: 4, | ||
name: 'Mr. Holmes', | ||
prof: 'Detective' | ||
}]; | ||
var q = { 'name': { '$regex': 'n' } }; | ||
var sifted = sift(q, persons); | ||
assert.deepEqual(sifted, [{ | ||
id: 3, | ||
name: 'Dr. Watson', | ||
prof: 'Doctor' | ||
}]); | ||
}); | ||
var results = [couples[0]].filter( | ||
sift({ person: { $elemMatch: { gender: "male", age: { $lt: 30 } } } }) | ||
); | ||
assert.equal(results.length, 0); | ||
}); | ||
}); | ||
describe('arrays of objects', function () { | ||
var objects = [ | ||
{ | ||
things: [ | ||
{ | ||
id: 123 | ||
}, { | ||
id: 456 | ||
} | ||
] | ||
}, { | ||
things: [ | ||
{ | ||
id: 123 | ||
}, | ||
{ | ||
id: 789 | ||
} | ||
] | ||
} | ||
]; | ||
it('$eq for array of objects, matches if at least one exists', function () { | ||
let q = { | ||
'things.id': 123 | ||
} | ||
var sifted = sift(q, objects) | ||
assert.deepEqual(sifted, objects) | ||
let q2 = { | ||
'things.id': 789 | ||
} | ||
var sifted2 = sift(q2, objects) | ||
assert.deepEqual(sifted2, [objects[1]]) | ||
}) | ||
it('$ne for array of objects, returns if none of the array elements match the query', function () { | ||
let q = { | ||
'things.id': { | ||
$ne: 123 | ||
} | ||
} | ||
var sifted = sift(q, objects) | ||
assert.deepEqual(sifted, []) | ||
let q2 = { | ||
'things.id': { | ||
$ne: 789 | ||
} | ||
} | ||
var sifted2 = sift(q2, objects) | ||
assert.deepEqual(sifted2, [objects[0]]) | ||
}) | ||
}) | ||
describe('$where', function() { | ||
var couples = [{ | ||
name: 'SMITH', | ||
person: [{ | ||
firstName: 'craig', | ||
gender: 'female', | ||
age: 29 | ||
}, { | ||
firstName: 'tim', | ||
gender: 'male', | ||
age: 32 | ||
} | ||
] | ||
}, { | ||
name: 'JOHNSON', | ||
person: [{ | ||
firstName: 'emily', | ||
gender: 'female', | ||
age: 35 | ||
}, { | ||
firstName: 'jacob', | ||
gender: 'male', | ||
age: 32 | ||
} | ||
] | ||
}]; | ||
it('can filter people', function() { | ||
var results = sift({'person': {$elemMatch: { 'gender': 'female', 'age': {'$lt': 30}}}}, couples); | ||
assert.equal(results[0].name, 'SMITH'); | ||
var results = sift({'person': {$elemMatch: { 'gender': 'male', 'age': {'$lt': 30}}}}, [couples[0]]); | ||
assert.equal(results.length, 0); | ||
}); | ||
describe("keypath", function() { | ||
var arr = [ | ||
{ | ||
a: { | ||
b: { | ||
c: 1, | ||
c2: 1 | ||
} | ||
} | ||
} | ||
]; | ||
it("can be used", function() { | ||
assert.equal(sift({ "a.b.c": 1 })(arr[0]), true); | ||
}); | ||
describe('keypath', function () { | ||
var arr = [ | ||
{ | ||
a: { | ||
b: { | ||
c: 1, | ||
c2: 1 | ||
} | ||
} | ||
} | ||
] | ||
it('can be used', function () { | ||
assert.equal(sift({'a.b.c':1})(arr[0]), true); | ||
}); | ||
}); | ||
}); | ||
}); | ||
var loremArr = [ | ||
{ | ||
'num': 1, | ||
'pum': 1, | ||
'sub': { | ||
'num': 1, | ||
'pum': 1 | ||
} | ||
}, | ||
{ | ||
'num': 2, | ||
'pum': 2, | ||
'sub': { | ||
'num': 2, | ||
'pum': 2 | ||
} | ||
}, | ||
{ | ||
'num': 3, | ||
'pum': 3, | ||
'sub': { | ||
'num': 3, | ||
'pum': 3 | ||
} | ||
}, | ||
{ | ||
'num': 4, | ||
'pum': 4, | ||
'sub': { | ||
'num': 4, | ||
'pum': 4 | ||
} | ||
}, | ||
{ | ||
'num': 5, | ||
'pum': 5, | ||
'sub': { | ||
'num': 5, | ||
'pum': 5 | ||
} | ||
}, | ||
{ | ||
'num': 6, | ||
'pum': 6, | ||
'sub': { | ||
'num': 6, | ||
'pum': 6 | ||
} | ||
}, | ||
{ | ||
'num': 7, | ||
'pum': 7, | ||
'sub': { | ||
'num': 7, | ||
'pum': 7 | ||
} | ||
}, | ||
{ | ||
'num': 8, | ||
'pum': 8, | ||
'sub': { | ||
'num': 8, | ||
'pum': 8 | ||
} | ||
}, | ||
{ | ||
'num': 9, | ||
'pum': 9, | ||
'sub': { | ||
'num': 9, | ||
'pum': 9 | ||
} | ||
}, | ||
{ | ||
'num': 10, | ||
'pum': 10, | ||
'sub': { | ||
'num': 10, | ||
'pum': 10 | ||
} | ||
}, | ||
{ | ||
'num': 11, | ||
'pum': 11, | ||
'sub': { | ||
'num': 10, | ||
'pum': 10 | ||
} | ||
{ | ||
num: 1, | ||
pum: 1, | ||
sub: { | ||
num: 1, | ||
pum: 1 | ||
} | ||
}, | ||
{ | ||
num: 2, | ||
pum: 2, | ||
sub: { | ||
num: 2, | ||
pum: 2 | ||
} | ||
}, | ||
{ | ||
num: 3, | ||
pum: 3, | ||
sub: { | ||
num: 3, | ||
pum: 3 | ||
} | ||
}, | ||
{ | ||
num: 4, | ||
pum: 4, | ||
sub: { | ||
num: 4, | ||
pum: 4 | ||
} | ||
}, | ||
{ | ||
num: 5, | ||
pum: 5, | ||
sub: { | ||
num: 5, | ||
pum: 5 | ||
} | ||
}, | ||
{ | ||
num: 6, | ||
pum: 6, | ||
sub: { | ||
num: 6, | ||
pum: 6 | ||
} | ||
}, | ||
{ | ||
num: 7, | ||
pum: 7, | ||
sub: { | ||
num: 7, | ||
pum: 7 | ||
} | ||
}, | ||
{ | ||
num: 8, | ||
pum: 8, | ||
sub: { | ||
num: 8, | ||
pum: 8 | ||
} | ||
}, | ||
{ | ||
num: 9, | ||
pum: 9, | ||
sub: { | ||
num: 9, | ||
pum: 9 | ||
} | ||
}, | ||
{ | ||
num: 10, | ||
pum: 10, | ||
sub: { | ||
num: 10, | ||
pum: 10 | ||
} | ||
}, | ||
{ | ||
num: 11, | ||
pum: 11, | ||
sub: { | ||
num: 10, | ||
pum: 10 | ||
} | ||
} | ||
]; |
@@ -1,137 +0,292 @@ | ||
import * as assert from 'assert'; | ||
import sift from '..'; | ||
var ObjectID = require('bson').ObjectID; | ||
import * as assert from "assert"; | ||
import sift from ".."; | ||
var ObjectID = require("bson").ObjectID; | ||
describe(__filename + '#', function () { | ||
describe(__filename + "#", function() { | ||
[ | ||
// $eq | ||
[{$eq:5}, [5,'5', 6], [5]], | ||
['5', [5,'5', 6], ['5']], | ||
[false, [false,'false', true], [false]], | ||
[{ $eq: 5 }, [5, "5", 6], [5]], | ||
["5", [5, "5", 6], ["5"]], | ||
[false, [false, "false", true], [false]], | ||
[true, [1, true], [true]], | ||
[0, [0,'0'], [0]], | ||
[0, [0, "0"], [0]], | ||
[null, [null], [null]], | ||
[void 0, [void 0, null], [void 0]], | ||
[1, [2,3,4,5], []], | ||
[1, [2, 3, 4, 5], []], | ||
[1, [[1]], [[1]]], | ||
[new Date(1), [new Date(), new Date(1), new Date(2), new Date(3)], [new Date(1)]], | ||
[/^a/, ['a','ab','abc','b','bc'], ['a','ab','abc']], | ||
[ | ||
new Date(1), | ||
[new Date(), new Date(1), new Date(2), new Date(3)], | ||
[new Date(1)] | ||
], | ||
[/^a/, ["a", "ab", "abc", "b", "bc"], ["a", "ab", "abc"]], | ||
[function(b) { return b === 1; }, [1,2,3],[1]], | ||
[ | ||
function(b) { | ||
return b === 1; | ||
}, | ||
[1, 2, 3], | ||
[1] | ||
], | ||
[ObjectID('54dd5546b1d296a54d152e84'),[ObjectID(),ObjectID('54dd5546b1d296a54d152e84')],[ObjectID('54dd5546b1d296a54d152e84')]], | ||
[ | ||
ObjectID("54dd5546b1d296a54d152e84"), | ||
[ObjectID(), ObjectID("54dd5546b1d296a54d152e84")], | ||
[ObjectID("54dd5546b1d296a54d152e84")] | ||
], | ||
// check for exactness | ||
[ | ||
{ c: { d: "d" } }, | ||
[ | ||
{ a: "done", c: { d: "d" } }, | ||
{ c: { d: "d" } }, | ||
{ c: { d: "d", e: "f" } } | ||
], | ||
[{ c: { d: "d" } }] | ||
], | ||
[{ a: 1 }, [{ a: 1 }, { a: 2 }, { a: 1, b: 2 }], [{ a: 1 }]], | ||
[ | ||
{ a: [1, 2, 3] }, | ||
[{ a: 1 }, { a: [1, 2] }, { a: [1, 2, 3] }], | ||
[{ a: [1, 2, 3] }] | ||
], | ||
[ | ||
{ a: [{ b: 1 }, { b: 2 }] }, | ||
[{ a: [{ b: 1 }, { b: 2 }] }, { a: [{ b: 1 }] }, { a: [{ b: 1 }], b: 2 }], | ||
[{ a: [{ b: 1 }, { b: 2 }] }] | ||
], | ||
// $ne | ||
[{$ne:5}, [5, '5', 6], ['5', 6]], | ||
[{$ne:'5'}, ['5', 6], [6]], | ||
[{$ne:false}, [false], []], | ||
[{$ne:void 0}, [false, 0, '0', void 0], [false, 0, '0']], | ||
[{$ne:/^a/}, ['a','ab','abc','b','bc'], ['b','bc']], | ||
[{$ne:1}, [[2],[1]], [[2]]], | ||
[{groups:{$ne:111}}, [{groups:[111,222,333,444]},{groups:[222,333,444]}],[{groups:[222,333,444]}]], | ||
[{ $ne: 5 }, [5, "5", 6], ["5", 6]], | ||
[{ $ne: "5" }, ["5", 6], [6]], | ||
[{ $ne: false }, [false], []], | ||
[{ $ne: void 0 }, [false, 0, "0", void 0], [false, 0, "0"]], | ||
[{ $ne: /^a/ }, ["a", "ab", "abc", "b", "bc"], ["b", "bc"]], | ||
[{ $ne: 1 }, [[2], [1]], [[2]]], | ||
[ | ||
{ groups: { $ne: 111 } }, | ||
[{ groups: [111, 222, 333, 444] }, { groups: [222, 333, 444] }], | ||
[{ groups: [222, 333, 444] }] | ||
], | ||
// $lt | ||
[{$lt:5}, [3,4,5,6],[3,4]], | ||
[{$lt:'c'}, ['a','b','c'],['a','b']], | ||
[{$lt:null}, [-3,-4], []], | ||
[{$lt:new Date(3)}, [new Date(1), new Date(2), new Date(3)],[new Date(1), new Date(2)]], | ||
[{ $lt: 5 }, [3, 4, 5, 6], [3, 4]], | ||
[{ $lt: "c" }, ["a", "b", "c"], ["a", "b"]], | ||
[{ $lt: null }, [-3, -4], []], | ||
[ | ||
{ $lt: new Date(3) }, | ||
[new Date(1), new Date(2), new Date(3)], | ||
[new Date(1), new Date(2)] | ||
], | ||
// $lte | ||
[{$lte:5}, [3,4,5,6],[3,4,5]], | ||
[{groups:{$lt:5}}, [{groups:[1,2,3,4]}, {groups:[7,8]}], [{groups:[1,2,3,4]}]], | ||
[{ $lte: 5 }, [3, 4, 5, 6], [3, 4, 5]], | ||
[ | ||
{ groups: { $lt: 5 } }, | ||
[{ groups: [1, 2, 3, 4] }, { groups: [7, 8] }], | ||
[{ groups: [1, 2, 3, 4] }] | ||
], | ||
// $gt | ||
[{$gt:5}, [3,4,5,6],[6]], | ||
[{$gt:null}, [3,4], []], | ||
[{groups:{$gt:5}}, [{groups:[1,2,3,4]}, {groups:[7,8]}], [{groups:[7,8]}]], | ||
[{ $gt: 5 }, [3, 4, 5, 6], [6]], | ||
[{ $gt: null }, [3, 4], []], | ||
[ | ||
{ groups: { $gt: 5 } }, | ||
[{ groups: [1, 2, 3, 4] }, { groups: [7, 8] }], | ||
[{ groups: [7, 8] }] | ||
], | ||
// $gte | ||
[{$gte:5}, [3,4,5,6],[5, 6]], | ||
[{groups:{$gte:5}}, [{groups:[1,2,3,4]}, {groups:[7,8]}], [{groups:[7,8]}]], | ||
[{ $gte: 5 }, [3, 4, 5, 6], [5, 6]], | ||
[ | ||
{ groups: { $gte: 5 } }, | ||
[{ groups: [1, 2, 3, 4] }, { groups: [7, 8] }], | ||
[{ groups: [7, 8] }] | ||
], | ||
// $mod | ||
[{$mod:[2,1]}, [1,2,3,4,5,6],[1,3,5]], | ||
[{groups:{$mod:[2,0]}}, [{groups:[1,2,3,4]}, {groups:[7,9]}], [{groups:[1,2,3,4]}]], | ||
[{ $mod: [2, 1] }, [1, 2, 3, 4, 5, 6], [1, 3, 5]], | ||
[ | ||
{ groups: { $mod: [2, 0] } }, | ||
[{ groups: [1, 2, 3, 4] }, { groups: [7, 9] }], | ||
[{ groups: [1, 2, 3, 4] }] | ||
], | ||
// $exists | ||
[{$exists:false}, [0,false,void 0, null],[]], | ||
[{$exists:true}, [0,false,void 0, 1, {}],[0, false, void 0, 1, {}]], | ||
[{'a.b': {$exists: true}}, [{a: {b: 'exists'}}, {a: {c: 'does not exist'}}], [{a: {b: 'exists'}}]], | ||
[{field: { $exists: false }}, [{a: 1}, {a: 2, field: 5}, {a: 3, field: 0}, {a: 4, field: undefined}, {a: 5}],[{a: 1}, {a: 5}]], | ||
[{ $exists: false }, [0, false, void 0, null], []], | ||
[{ $exists: true }, [0, false, void 0, 1, {}], [0, false, void 0, 1, {}]], | ||
[ | ||
{ "a.b": { $exists: true } }, | ||
[{ a: { b: "exists" } }, { a: { c: "does not exist" } }], | ||
[{ a: { b: "exists" } }] | ||
], | ||
[ | ||
{ field: { $exists: false } }, | ||
[ | ||
{ a: 1 }, | ||
{ a: 2, field: 5 }, | ||
{ a: 3, field: 0 }, | ||
{ a: 4, field: undefined }, | ||
{ a: 5 } | ||
], | ||
[{ a: 1 }, { a: 5 }] | ||
], | ||
// $in | ||
// TODO - {$in:[Date]} doesn't work - make it work? | ||
[{$in:[0,false,1,'1']},[0,1,2,3,4,false],[0,1,false]], | ||
[{$in:[1,'1','2']},['1','2','3'],['1','2']], | ||
[{$in:[new Date(1)]},[new Date(1), new Date(2)],[new Date(1)]], | ||
[{'a.b.status':{'$in': [0]}}, [{'a':{'b':[{'status':0}]}},{'a':{'b':[{'status':2}]}}],[{'a':{'b':[{'status':0}]}}]], | ||
[{'a.b.status':{'$in': [0, 2]}}, [{'a':{'b':[{'status':0}]}},{'a':{'b':[{'status':2}]}}], [{'a':{'b':[{'status':0}]}},{'a':{'b':[{'status':2}]}}]], | ||
[{'x': {$in: [{$regex: '.*aaa.*'}, {$regex: '.*bbb.*'}]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': 'bbb'}, {'x': 'aaa'}]], | ||
[{'x': {$in: [/.*aaa.*/, /.*bbb.*/]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': 'bbb'}, {'x': 'aaa'}]], | ||
[{ $in: [0, false, 1, "1"] }, [0, 1, 2, 3, 4, false], [0, 1, false]], | ||
[{ $in: [1, "1", "2"] }, ["1", "2", "3"], ["1", "2"]], | ||
[{ $in: [new Date(1)] }, [new Date(1), new Date(2)], [new Date(1)]], | ||
[ | ||
{ "a.b.status": { $in: [0] } }, | ||
[{ a: { b: [{ status: 0 }] } }, { a: { b: [{ status: 2 }] } }], | ||
[{ a: { b: [{ status: 0 }] } }] | ||
], | ||
[ | ||
{ "a.b.status": { $in: [0, 2] } }, | ||
[{ a: { b: [{ status: 0 }] } }, { a: { b: [{ status: 2 }] } }], | ||
[{ a: { b: [{ status: 0 }] } }, { a: { b: [{ status: 2 }] } }] | ||
], | ||
[ | ||
{ x: { $in: [{ $regex: ".*aaa.*" }, { $regex: ".*bbb.*" }] } }, | ||
[{ x: { b: "aaa" } }, { x: "bbb" }, { x: "ccc" }, { x: "aaa" }], | ||
[{ x: "bbb" }, { x: "aaa" }] | ||
], | ||
[ | ||
{ x: { $in: [/.*aaa.*/, /.*bbb.*/] } }, | ||
[{ x: { b: "aaa" } }, { x: "bbb" }, { x: "ccc" }, { x: "aaa" }], | ||
[{ x: "bbb" }, { x: "aaa" }] | ||
], | ||
// $nin | ||
[{$nin:[0,false,1,'1']},[0,1,2,3,4,false],[2,3,4]], | ||
[{$nin:[1,'1','2']},['1','2','3'],['3']], | ||
[{$nin:[new Date(1)]},[new Date(1), new Date(2)],[new Date(2)]], | ||
[{'root.notDefined': {$nin: [1, 2, 3]}}, [{'root': {'defined': 1337}}], [{'root': {'defined': 1337}}]], | ||
[{'root.notDefined': {$nin: [1, 2, 3, null]}}, [{'root': {'defined': 1337}}], []], | ||
[{'x': {$nin: [{$regex: '.*aaa.*'}, {$regex: '.*bbb.*'}]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': {'b': 'aaa'}},{'x': 'ccc'}]], | ||
[{'x': {$nin: [/.*aaa.*/, /.*bbb.*/]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': {'b': 'aaa'}},{'x': 'ccc'}]], | ||
[{ $nin: [0, false, 1, "1"] }, [0, 1, 2, 3, 4, false], [2, 3, 4]], | ||
[{ $nin: [1, "1", "2"] }, ["1", "2", "3"], ["3"]], | ||
[{ $nin: [new Date(1)] }, [new Date(1), new Date(2)], [new Date(2)]], | ||
[ | ||
{ "root.notDefined": { $nin: [1, 2, 3] } }, | ||
[{ root: { defined: 1337 } }], | ||
[{ root: { defined: 1337 } }] | ||
], | ||
[ | ||
{ "root.notDefined": { $nin: [1, 2, 3, null] } }, | ||
[{ root: { defined: 1337 } }], | ||
[] | ||
], | ||
[ | ||
{ x: { $nin: [{ $regex: ".*aaa.*" }, { $regex: ".*bbb.*" }] } }, | ||
[{ x: { b: "aaa" } }, { x: "bbb" }, { x: "ccc" }, { x: "aaa" }], | ||
[{ x: { b: "aaa" } }, { x: "ccc" }] | ||
], | ||
[ | ||
{ x: { $nin: [/.*aaa.*/, /.*bbb.*/] } }, | ||
[{ x: { b: "aaa" } }, { x: "bbb" }, { x: "ccc" }, { x: "aaa" }], | ||
[{ x: { b: "aaa" } }, { x: "ccc" }] | ||
], | ||
// $not | ||
[{$not:false},[0,false],[0]], | ||
[{$not:0},[0, false, 1, 2, 3],[false, 1, 2, 3]], | ||
[{$not:{$in:[1,2,3]}},[1,2,3,4,5,6],[4,5,6]], // with expressions | ||
[{ $not: false }, [0, false], [0]], | ||
[{ $not: 0 }, [0, false, 1, 2, 3], [false, 1, 2, 3]], | ||
[{ $not: { $in: [1, 2, 3] } }, [1, 2, 3, 4, 5, 6], [4, 5, 6]], // with expressions | ||
// $type | ||
[{$type:Date}, [0,new Date(1)],[new Date(1)]], | ||
[{$type:Number}, [0,false,1],[0,1]], | ||
[{$type:Boolean}, [0,false, void 0],[false]], | ||
[{$type:String}, ['1',1,false],['1']], | ||
[{ $type: Date }, [0, new Date(1)], [new Date(1)]], | ||
[{ $type: Number }, [0, false, 1], [0, 1]], | ||
[{ $type: Boolean }, [0, false, void 0], [false]], | ||
[{ $type: String }, ["1", 1, false], ["1"]], | ||
// $all | ||
[{$all:[1,2,3]},[[1,2,3,4],[1,2,4]],[[1,2,3,4]]], | ||
[{$all:[0,false]},[[0,1,2],[0,false],['0','false'],void 0],[[0,false]]], | ||
[{$all:['1']},[[1]],[]], | ||
[{$all:[new Date(1),new Date(2)]},[[new Date(1), new Date(2)],[new Date(1)]],[[new Date(1), new Date(2)]]], | ||
[{ $all: [1, 2, 3] }, [[1, 2, 3, 4], [1, 2, 4]], [[1, 2, 3, 4]]], | ||
[ | ||
{ $all: [0, false] }, | ||
[[0, 1, 2], [0, false], ["0", "false"], void 0], | ||
[[0, false]] | ||
], | ||
[{ $all: ["1"] }, [[1]], []], | ||
[ | ||
{ $all: [new Date(1), new Date(2)] }, | ||
[[new Date(1), new Date(2)], [new Date(1)]], | ||
[[new Date(1), new Date(2)]] | ||
], | ||
// $size | ||
[{$size:3},['123',[1,2,3],'1'],['123',[1,2,3]]], | ||
[{$size:1},['123',[1,2,3],'1', void 0],['1']], | ||
[{ $size: 3 }, ["123", [1, 2, 3], "1"], ["123", [1, 2, 3]]], | ||
[{ $size: 1 }, ["123", [1, 2, 3], "1", void 0], ["1"]], | ||
// $or | ||
[{$or:[1,2,3]},[1,2,3,4],[1,2,3]], | ||
[{$or:[{$ne:1},2]},[1,2,3,4,5,6],[2,3,4,5,6]], | ||
[{ $or: [1, 2, 3] }, [1, 2, 3, 4], [1, 2, 3]], | ||
[{ $or: [{ $ne: 1 }, 2] }, [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6]], | ||
[{ $or: [{ a: 1 }, { b: 1 }] }, [{ a: 1, b: 2 }, { a: 1 }], [{ a: 1 }]], | ||
// $nor | ||
[{$nor:[1,2,3]},[1,2,3,4],[4]], | ||
[{$nor:[{$ne:1},2]},[1,2,3,4,5,6],[1]], | ||
[{ $nor: [1, 2, 3] }, [1, 2, 3, 4], [4]], | ||
[{ $nor: [{ $ne: 1 }, 2] }, [1, 2, 3, 4, 5, 6], [1]], | ||
[ | ||
{ $nor: [{ a: 1 }, { b: 1 }] }, | ||
[{ a: 1, b: 2 }, { a: 1 }], | ||
[{ a: 1, b: 2 }] | ||
], | ||
// $and | ||
[{$and:[{$gt:1},{$lt:4}]},[1,2,3,4],[2,3]], | ||
[{$and: [{field: {$not: {$type: String}}}, {field: {$ne: null}}]}, [{a: 1, field: 1}, {a: 2, field: '2'}], [{a: 1, field: 1}]], | ||
[{ $and: [{ $gt: 1 }, { $lt: 4 }] }, [1, 2, 3, 4], [2, 3]], | ||
[ | ||
{ | ||
$and: [{ field: { $not: { $type: String } } }, { field: { $ne: null } }] | ||
}, | ||
[{ a: 1, field: 1 }, { a: 2, field: "2" }], | ||
[{ a: 1, field: 1 }] | ||
], | ||
// $regex | ||
[{$regex:'^a'},['a','ab','abc','bc','bcd'],['a','ab','abc']], | ||
[{a:{$regex:'b|c'}}, [{a:['b']},{a:['c']},{a:'c'},{a:'d'}], [{a:['b']},{a:['c']},{a:'c'}]], | ||
[{ folder: { $regex:'^[0-9]{4}$' }}, [{ folder:['1234','3212'] }], [{ folder:['1234','3212'] }]], | ||
[{ $regex: "^a" }, ["a", "ab", "abc", "bc", "bcd"], ["a", "ab", "abc"]], | ||
[ | ||
{ a: { $regex: "b|c" } }, | ||
[{ a: ["b"] }, { a: ["c"] }, { a: "c" }, { a: "d" }], | ||
[{ a: ["b"] }, { a: ["c"] }, { a: "c" }] | ||
], | ||
[ | ||
{ folder: { $regex: "^[0-9]{4}$" } }, | ||
[{ folder: ["1234", "3212"] }], | ||
[{ folder: ["1234", "3212"] }] | ||
], | ||
// $options | ||
[{$regex:'^a', $options: 'i'},['a','Ab','abc','bc','bcd'],['a','Ab','abc']], | ||
[{'text':{'$regex':'.*lis.*','$options':'i'}}, [{text:['Bob','Melissa','Joe','Sherry']}], [{text:['Bob','Melissa','Joe','Sherry']}]], | ||
[ | ||
{ $regex: "^a", $options: "i" }, | ||
["a", "Ab", "abc", "bc", "bcd"], | ||
["a", "Ab", "abc"] | ||
], | ||
[ | ||
{ text: { $regex: ".*lis.*", $options: "i" } }, | ||
[{ text: ["Bob", "Melissa", "Joe", "Sherry"] }], | ||
[{ text: ["Bob", "Melissa", "Joe", "Sherry"] }] | ||
], | ||
// undefined | ||
[{$regex:'a'},[undefined, null, true, false, 0, 'aa'],['aa']], | ||
[/a/,[undefined, null, true, false, 0, 'aa'],['aa']], | ||
[/.+/,[undefined, null, true, false, 0, 'aa', {}],['aa']], | ||
[{ $regex: "a" }, [undefined, null, true, false, 0, "aa"], ["aa"]], | ||
[/a/, [undefined, null, true, false, 0, "aa"], ["aa"]], | ||
[/.+/, [undefined, null, true, false, 0, "aa", {}], ["aa"]], | ||
// Multiple conditions on an undefined root | ||
[{'a.b': {$exists: true, $nin: [null]}}, [{a: {b: 'exists'}}, {a: {c: 'does not exist'}}], [{a: {b: 'exists'}}]], | ||
[ | ||
{ "a.b": { $exists: true, $nin: [null] } }, | ||
[{ a: { b: "exists" } }, { a: { c: "does not exist" } }], | ||
[{ a: { b: "exists" } }] | ||
], | ||
// $where | ||
[{$where:function () { return this.v === 1 }}, [{v:1},{v:2}],[{v:1}]], | ||
[{$where:'this.v === 1'}, [{v:1},{v:2}],[{v:1}]], | ||
[{$where:'obj.v === 1'}, [{v:1},{v:2}],[{v:1}]], | ||
[ | ||
{ | ||
$where: function() { | ||
return this.v === 1; | ||
} | ||
}, | ||
[{ v: 1 }, { v: 2 }], | ||
[{ v: 1 }] | ||
], | ||
[{ $where: "this.v === 1" }, [{ v: 1 }, { v: 2 }], [{ v: 1 }]], | ||
[{ $where: "obj.v === 1" }, [{ v: 1 }, { v: 2 }], [{ v: 1 }]], | ||
@@ -141,26 +296,48 @@ // $elemMatch | ||
[ | ||
{a:{$elemMatch:{b:1,c:2}}}, | ||
[{a:{b:1,c:2}},{a:[{b:1,c:2,d:3}]},{a:{b:2,c:3}}], [{a:{b:1,c:2}},{a:[{b:1,c:2,d:3}]}] | ||
{ a: { $elemMatch: { b: 1, c: 2 } } }, | ||
[ | ||
{ a: { b: 1, c: 2 } }, | ||
{ a: [{ b: 1, c: 2, d: 3 }] }, | ||
{ a: { b: 2, c: 3 } } | ||
], | ||
[{ a: { b: 1, c: 2 } }] | ||
], | ||
[{a:{$elemMatch:{b:2,c:{$gt:2}}}}, [{a:{b:1,c:2}},{a:{b:1,c:2,d:3}},[{a:{b:2,c:3}}]], [[{a:{b:2,c:3}}]]], | ||
[ | ||
{tags: {$all: [{$elemMatch: {a: 1}}]}}, | ||
[{tags: [{a: 1}]}, {tags: [{a: 1}, {b: 1}]}], [{tags: [{a: 1}]}, {tags: [{a: 1}, {b: 1}]}] | ||
{ a: { $elemMatch: { b: 2, c: { $gt: 2 } } } }, | ||
[ | ||
{ a: { b: 1, c: 2 } }, | ||
{ a: { b: 1, c: 2, d: 3 } }, | ||
[{ a: { b: 2, c: 3 } }] | ||
], | ||
[[{ a: { b: 2, c: 3 } }]] | ||
], | ||
[ | ||
{ tags: { $all: [{ $elemMatch: { a: 1 } }] } }, | ||
[{ tags: [{ a: 1 }] }, { tags: [{ a: 1 }, { b: 1 }] }], | ||
[{ tags: [{ a: 1 }] }, { tags: [{ a: 1 }, { b: 1 }] }] | ||
], | ||
// dot-notation | ||
[ | ||
{'a.b': /c/ }, | ||
[{a:{b:'c'}}, {a:{b:'cd'}}, {'a.b':'c'},{a:{b:'e'}}], | ||
[{a:{b:'c'}}, {a:{b:'cd'}}] | ||
{ "a.b": /c/ }, | ||
[ | ||
{ a: { b: "c" } }, | ||
{ a: { b: "cd" } }, | ||
{ "a.b": "c" }, | ||
{ a: { b: "e" } } | ||
], | ||
[{ a: { b: "c" } }, { a: { b: "cd" } }] | ||
], | ||
[ | ||
{'foo.0': 'baz' }, | ||
[{foo:['bar', 'baz']}, {foo:['baz', 'bar']}], | ||
[{foo:['baz', 'bar']}] | ||
{ "foo.0": "baz" }, | ||
[{ foo: ["bar", "baz"] }, { foo: ["baz", "bar"] }], | ||
[{ foo: ["baz", "bar"] }] | ||
], | ||
[ | ||
{'foo.0.name': 'baz' }, | ||
[{foo:[{ name: 'bar' }, { name: 'baz' }]}, {foo:[{ name: 'baz' }, { name: 'bar' }]}], | ||
[{foo:[{ name: 'baz' }, { name: 'bar' }]}] | ||
{ "foo.0.name": "baz" }, | ||
[ | ||
{ foo: [{ name: "bar" }, { name: "baz" }] }, | ||
{ foo: [{ name: "baz" }, { name: "bar" }] } | ||
], | ||
[{ foo: [{ name: "baz" }, { name: "bar" }] }] | ||
], | ||
@@ -170,36 +347,70 @@ | ||
[ | ||
{ $in: [{ toString: function(){ return 'a'; }}]}, | ||
[{toString: function(){ return 'a'; }}, {toString: function(){ return 'b' }}], | ||
[{toString: function(){ return 'a'; }}] | ||
{ | ||
$in: [ | ||
{ | ||
toString: function() { | ||
return "a"; | ||
} | ||
} | ||
] | ||
}, | ||
[ | ||
{ | ||
toString: function() { | ||
return "a"; | ||
} | ||
}, | ||
{ | ||
toString: function() { | ||
return "b"; | ||
} | ||
} | ||
], | ||
[ | ||
{ | ||
toString: function() { | ||
return "a"; | ||
} | ||
} | ||
] | ||
], | ||
[ | ||
{ $in: [{}]}, | ||
[{}, {}], | ||
[] | ||
], | ||
[{ $in: [{}] }, [{}, {}], []], | ||
// various comparisons | ||
[ | ||
{ c: { d: 'd' }}, | ||
[{ a: 'b', b: 'c', c: { d: 'd', e: 'e' }}, { c: { d: 'e' }}], | ||
[{ a: 'b', b: 'c', c: { d: 'd', e: 'e' }}] | ||
], | ||
// based on https://gist.github.com/jdnichollsc/00ea8cf1204b17d9fb9a991fbd1dfee6 | ||
[ | ||
{ $and: [{ 'a.s': { $lte: new Date('2017-01-29T05:00:00.000Z') }}, {'a.e': { $gte: new Date('2017-01-08T05:00:00.000Z') }}]}, | ||
[{ a: { s: new Date('2017-01-13T05:00:00.000Z'), e: new Date('2017-01-31T05:00:00.000Z') }}], | ||
[{ a: { s: new Date('2017-01-13T05:00:00.000Z'), e: new Date('2017-01-31T05:00:00.000Z') }}] | ||
], | ||
].forEach(function (operation, i) { | ||
var filter = operation[0]; | ||
var array = operation[1]; | ||
{ | ||
$and: [ | ||
{ "a.s": { $lte: new Date("2017-01-29T05:00:00.000Z") } }, | ||
{ "a.e": { $gte: new Date("2017-01-08T05:00:00.000Z") } } | ||
] | ||
}, | ||
[ | ||
{ | ||
a: { | ||
s: new Date("2017-01-13T05:00:00.000Z"), | ||
e: new Date("2017-01-31T05:00:00.000Z") | ||
} | ||
} | ||
], | ||
[ | ||
{ | ||
a: { | ||
s: new Date("2017-01-13T05:00:00.000Z"), | ||
e: new Date("2017-01-31T05:00:00.000Z") | ||
} | ||
} | ||
] | ||
] | ||
].forEach(function(operation, i) { | ||
var filter = operation[0]; | ||
var array = operation[1]; | ||
var matchArray = operation[2]; | ||
it(i + ': ' + JSON.stringify(filter), function() { | ||
assert.equal(JSON.stringify(array.filter(sift(filter))), JSON.stringify(matchArray)); | ||
it(i + ": " + JSON.stringify(filter), function() { | ||
assert.equal( | ||
JSON.stringify(array.filter(sift(filter))), | ||
JSON.stringify(matchArray) | ||
); | ||
}); | ||
}); | ||
}); |
@@ -1,23 +0,22 @@ | ||
const {resolve} = require('path'); | ||
const fs = require('fs'); | ||
const { resolve } = require("path"); | ||
const fs = require("fs"); | ||
module.exports = { | ||
devtool: 'none', | ||
mode: 'production', | ||
devtool: "none", | ||
mode: "production", | ||
entry: { | ||
index: [__dirname + '/lib/index.js'] | ||
index: [__dirname + "/src/index.js"] | ||
}, | ||
output: { | ||
path: __dirname, | ||
library: 'sift', | ||
libraryTarget: 'umd', | ||
filename: 'sift.min.js' | ||
library: "sift", | ||
libraryTarget: "umd", | ||
filename: "sift.min.js" | ||
}, | ||
resolve: { | ||
extensions: ['.js'] | ||
extensions: [".js"] | ||
}, | ||
module: { | ||
rules: [ | ||
] | ||
rules: [] | ||
} | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
2610
459
244739
13