From c4141ab945a105f6686b2b6b17ba0d556b9f0a4a Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 23 May 2013 18:27:52 +0200 Subject: [PATCH] Implemented logical operator --- lib/model.js | 14 ++++++++++++++ test/model.test.js | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index d316b44..b719e7f 100644 --- a/lib/model.js +++ b/lib/model.js @@ -324,6 +324,8 @@ comparisonFunctions.$gte = function (a, b) { /** * Match any of the subqueries + * @param {Model} obj + * @param {Array of Queries} query */ logicalOperators.$or = function (obj, query) { var i; @@ -340,6 +342,8 @@ logicalOperators.$or = function (obj, query) { /** * Match all of the subqueries + * @param {Model} obj + * @param {Array of Queries} query */ logicalOperators.$and = function (obj, query) { var i; @@ -354,6 +358,16 @@ logicalOperators.$and = function (obj, query) { }; +/** + * Inverted match of the query + * @param {Model} obj + * @param {Query} query + */ +logicalOperators.$not = function (obj, query) { + return !match(obj, query); +}; + + /** * Tell if a given document matches a query * @param {Object} obj Document to check diff --git a/test/model.test.js b/test/model.test.js index 2c95e26..037f319 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -482,7 +482,7 @@ describe('Model', function () { }); - describe('Logical operators $or, $and', function () { + describe('Logical operators $or, $and, $not', function () { it('Any of the subqueries should match for an $or to match', function () { model.match({ hello: 'world' }, { $or: [ { hello: 'pluton' }, { hello: 'world' } ] }).should.equal(true); @@ -499,6 +499,11 @@ describe('Model', function () { model.match({ hello: 'world', age: 15 }, { $and: [ { hello: 'pluton' }, { age: { $lt: 20 } } ] }).should.equal(false); }); + it('Subquery should not match for a $not to match', function () { + model.match({ a: 5, b: 10 }, { a: 5 }).should.equal(true); + model.match({ a: 5, b: 10 }, { $not: { a: 5 } }).should.equal(false); + }); + it('Logical operators are all top-level, only other logical operators can be above', function () { (function () { model.match({ a: { b: 7 } }, { a: { $or: [ { b: 5 }, { b: 7 } ] } })}).should.throw(); model.match({ a: { b: 7 } }, { $or: [ { "a.b": 5 }, { "a.b": 7 } ] }).should.equal(true);