Skip to content

Commit 814f260

Browse files
committed
feat add not contains operator
1 parent d9dffb0 commit 814f260

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ q.or(filters); //returns an array of all objects in `arr` that satisfy SOME of t
8282

8383
*`value`* - The value of the property we will filter on
8484

85-
*`operator`* - The filter operator. Supported operators `equals`, `contains`, `gt`, `gte`, `lt`, `lte`, `ne`
85+
*`operator`* - The filter operator. Supported operators `equals`, `contains`, `notcontains`, `gt`, `gte`, `lt`, `lte`, `ne`
8686

8787
*`matchMissing`* - If `true` the filter will be satisfied even if `field` property is not present on the object. Default is `false`.
8888

lib/operators.js

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const contains = (val1, val2) => {
55
return val1.indexOf(val2) > -1;
66
};
77

8+
const notcontains = (val1, val2) => {
9+
if (!Array.isArray(val1)) {
10+
throw new Error('`val1` must be an array');
11+
}
12+
return val1.indexOf(val2) < 0;
13+
};
14+
815
const equals = (val1, val2) => val1 === val2;
916
const ne = (val1, val2) => val1 !== val2;
1017
const gt = (val1, val2) => val1 > val2;
@@ -14,6 +21,7 @@ const lte = (val1, val2) => val1 <= val2;
1421

1522
module.exports = {
1623
contains,
24+
notcontains,
1725
equals,
1826
ne,
1927
gt,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-objects",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "A utility library which filters objects from an array of objects based on a set of filter conditions",
55
"main": "index.js",
66
"scripts": {

test/lib/operators.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const should = require('chai').Should();
22
const contains = require('../../lib/operators').contains;
3+
const notcontains = require('../../lib/operators').notcontains;
34
const equals = require('../../lib/operators').equals;
45
const ne = require('../../lib/operators').ne;
56
const gt = require('../../lib/operators').gt;
@@ -20,6 +21,17 @@ describe('Operators module tests', () => {
2021

2122
});
2223

24+
it('notcontains(val1, val2) should return true `val1` notcontains `val2` otherwise false', () => {
25+
const val1 = [1, 2, 3];
26+
notcontains(val1, 1).should.be.false;
27+
notcontains(val1, 10).should.be.true;
28+
29+
(() => {
30+
notcontains('', 10);
31+
}).should.throw('`val1` must be an array');
32+
33+
});
34+
2335
it('equals(val1, val2) should return true `val1` equals `val2` otherwise false', () => {
2436
equals(1, 1).should.be.true;
2537
equals(1, 10).should.be.false;

0 commit comments

Comments
 (0)