pull/2/head
Bryan English 11 years ago
parent ad7cbc2df3
commit 91a23044c7
  1. 17
      lib/model.js
  2. 18
      test/model.test.js

@ -640,6 +640,23 @@ logicalOperators.$not = function (obj, query) {
};
/**
* Use a function to match
* @param {Model} obj
* @param {Query} query
*/
logicalOperators.$where = function (obj, func) {
var result;
if (!_.isFunction(func)) { throw "$where operator used without a function"; }
result = func.call(obj);
if (!_.isBoolean(result)) { throw "$where function must return boolean"; }
return result;
};
/**
* Tell if a given document matches a query
* @param {Object} obj Document to check

@ -1172,6 +1172,24 @@ describe('Model', function () {
});
describe('Logical operator $where', function () {
it('Function should match and not match correctly', function () {
model.match({ a: 4}, { $where: function () { return this.a === 4; } }).should.equal(true);
model.match({ a: 4}, { $where: function () { return this.a === 5; } }).should.equal(false);
});
it('Should throw an error if the $where function is not, in fact, a function', function () {
(function () { model.match({ a: 4 }, { $where: 'not a function' }); }).should.throw();
});
it('Should throw an error if the $where function returns a non-boolean', function () {
(function () { model.match({ a: 4 }, { $where: function () { return 'not a boolean'; } }); }).should.throw();
});
});
describe('Array fields', function () {
it('Field equality', function () {

Loading…
Cancel
Save