From 24827f5ab0c5bacf26dc5698db0253a9355e88ed Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 23 May 2013 18:37:38 +0200 Subject: [PATCH] operator --- lib/model.js | 12 ++++++++++++ test/model.test.js | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 4a5af8f..1d6346c 100644 --- a/lib/model.js +++ b/lib/model.js @@ -328,6 +328,18 @@ comparisonFunctions.$ne = function (a, b) { return !areThingsEqual(a, b); }; +comparisonFunctions.$in = function (a, b) { + var i; + + if (!util.isArray(b)) { throw "$in operator called with a non-array"; } + + for (i = 0; i < b.length; i += 1) { + if (areThingsEqual(a, b[i])) { return true; } + } + + return false; +}; + /** * Match any of the subqueries diff --git a/test/model.test.js b/test/model.test.js index 4259d25..720a572 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -459,7 +459,7 @@ describe('Model', function () { // General behaviour is tested in the block about $lt. Here we just test operators work - describe('Other comparison operators: $lte, $gt, $gte, $ne', function () { + describe('Other comparison operators: $lte, $gt, $gte, $ne, $in', function () { it('$lte', function () { model.match({ a: 5 }, { a: { $lte: 6 } }).should.equal(true); @@ -485,6 +485,16 @@ describe('Model', function () { model.match({ a: 5 }, { b: { $ne: 5 } }).should.equal(true); }); + it('$in', function () { + model.match({ a: 5 }, { a: { $in: [6, 8, 9] } }).should.equal(false); + model.match({ a: 6 }, { a: { $in: [6, 8, 9] } }).should.equal(true); + model.match({ a: 7 }, { a: { $in: [6, 8, 9] } }).should.equal(false); + model.match({ a: 8 }, { a: { $in: [6, 8, 9] } }).should.equal(true); + model.match({ a: 9 }, { a: { $in: [6, 8, 9] } }).should.equal(true); + + (function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw(); + }); + });