Skip to content

Commit

Permalink
operator
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed May 23, 2013
1 parent a70f4ba commit 24827f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});

});


Expand Down

0 comments on commit 24827f5

Please sign in to comment.