Implemented logical operator

pull/2/head
Louis Chatriot 12 years ago
parent b197a945f6
commit c4141ab945
  1. 14
      lib/model.js
  2. 7
      test/model.test.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

@ -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);

Loading…
Cancel
Save