From bb57fcbe110a8ced8603f690ea92dd1d0203d8d7 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 10 Jun 2014 16:11:50 -0700 Subject: [PATCH] Fix the comparison for null/boolean values --- lib/connectors/memory.js | 10 +++++++--- lib/dao.js | 8 ++++++-- test/basic-querying.test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 0b7a36cd..ab54f959 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -402,21 +402,25 @@ function applyFilter(filter) { * @private */ function compare(val1, val2) { + if(val1 == null || val2 == null) { + // Either val1 or val2 is null or undefined + return val1 == val2 ? 0 : NaN; + } if (typeof val1 === 'number') { return val1 - val2; } if (typeof val1 === 'string') { - return (val1 > val2) ? 1 : ((val1 < val2) ? -1 : 0); + return (val1 > val2) ? 1 : ((val1 < val2) ? -1 : (val1 == val2) ? 0 : NaN); } if (typeof val1 === 'boolean') { return val1 - val2; } if (val1 instanceof Date) { - var result = val1.getTime() - ((val2 && val2.getTime()) || 0); + var result = val1 - val2; return result; } // Return NaN if we don't know how to compare - return (val1 === val2) ? 0 : NaN; + return (val1 == val2) ? 0 : NaN; } function testInEquality(example, val) { diff --git a/lib/dao.js b/lib/dao.js index f5a6886f..d4bd40de 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -551,10 +551,14 @@ DataAccessObject._coerce = function (where) { // Coerce the array items if (Array.isArray(val)) { for (var i = 0; i < val.length; i++) { - val[i] = DataType(val[i]); + if (val[i] !== null && val[i] !== undefined) { + val[i] = DataType(val[i]); + } } } else { - val = DataType(val); + if (val !== null && val !== undefined) { + val = DataType(val); + } } // Rebuild {property: {operator: value}} if (operator) { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index ef0bfb7f..eaccb348 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -256,6 +256,33 @@ describe('basic-querying', function () { }); }); + it('should support number "gt" that is satisfied by null value', function (done) { + User.find({order: 'seq', where: { order: { "gt": null } + }}, function (err, users) { + should.not.exist(err); + users.should.have.property('length', 0); + done(); + }); + }); + + it('should support number "lt" that is not satisfied by null value', function (done) { + User.find({order: 'seq', where: { order: { "lt": null } + }}, function (err, users) { + should.not.exist(err); + users.should.have.property('length', 0); + done(); + }); + }); + + it('should support string "gte" that is satisfied by null value', function (done) { + User.find({order: 'seq', where: { name: { "gte": null} + }}, function (err, users) { + should.not.exist(err); + users.should.have.property('length', 0); + done(); + }); + }); + it('should support string "gte" that is satisfied', function (done) { User.find({order: 'seq', where: { name: { "gte": 'Paul McCartney'} }}, function (err, users) {