From cf43ec1b6bc232b9540b067a76bb391ec1736095 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 5 Feb 2014 14:43:44 +0100 Subject: [PATCH] One more test --- lib/model.js | 6 +++--- test/model.test.js | 14 +++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/model.js b/lib/model.js index accdc16..f86af72 100644 --- a/lib/model.js +++ b/lib/model.js @@ -645,12 +645,12 @@ logicalOperators.$not = function (obj, query) { * @param {Model} obj * @param {Query} query */ -logicalOperators.$where = function (obj, func) { +logicalOperators.$where = function (obj, fn) { var result; - if (!_.isFunction(func)) { throw "$where operator used without a function"; } + if (!_.isFunction(fn)) { throw "$where operator used without a function"; } - result = func.call(obj); + result = fn.call(obj); if (!_.isBoolean(result)) { throw "$where function must return boolean"; } return result; diff --git a/test/model.test.js b/test/model.test.js index 06f5a4c..499bbdf 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -1172,7 +1172,7 @@ describe('Model', function () { }); - describe('Logical operator $where', function () { + describe('Comparison 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); @@ -1186,6 +1186,18 @@ describe('Model', function () { 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(); }); + + it('Should be able to do the complex matching it must be used for', function () { + var checkEmail = function() { + if (!this.firstName || !this.lastName) { return false; } + return this.firstName.toLowerCase() + "." + this.lastName.toLowerCase() + "@gmail.com" === this.email; + }; + model.match({ firstName: "John", lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(true); + model.match({ firstName: "john", lastName: "doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(true); + model.match({ firstName: "Jane", lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false); + model.match({ firstName: "John", lastName: "Deere", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false); + model.match({ lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false); + }); });