Fix the comparison for null/boolean values

This commit is contained in:
Raymond Feng 2014-06-10 16:11:50 -07:00
parent b5816506e0
commit bb57fcbe11
3 changed files with 40 additions and 5 deletions

View File

@ -402,21 +402,25 @@ function applyFilter(filter) {
* @private * @private
*/ */
function compare(val1, val2) { 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') { if (typeof val1 === 'number') {
return val1 - val2; return val1 - val2;
} }
if (typeof val1 === 'string') { 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') { if (typeof val1 === 'boolean') {
return val1 - val2; return val1 - val2;
} }
if (val1 instanceof Date) { if (val1 instanceof Date) {
var result = val1.getTime() - ((val2 && val2.getTime()) || 0); var result = val1 - val2;
return result; return result;
} }
// Return NaN if we don't know how to compare // Return NaN if we don't know how to compare
return (val1 === val2) ? 0 : NaN; return (val1 == val2) ? 0 : NaN;
} }
function testInEquality(example, val) { function testInEquality(example, val) {

View File

@ -551,11 +551,15 @@ DataAccessObject._coerce = function (where) {
// Coerce the array items // Coerce the array items
if (Array.isArray(val)) { if (Array.isArray(val)) {
for (var i = 0; i < val.length; i++) { for (var i = 0; i < val.length; i++) {
if (val[i] !== null && val[i] !== undefined) {
val[i] = DataType(val[i]); val[i] = DataType(val[i]);
} }
}
} else { } else {
if (val !== null && val !== undefined) {
val = DataType(val); val = DataType(val);
} }
}
// Rebuild {property: {operator: value}} // Rebuild {property: {operator: value}}
if (operator) { if (operator) {
var value = {}; var value = {};

View File

@ -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) { it('should support string "gte" that is satisfied', function (done) {
User.find({order: 'seq', where: { name: { "gte": 'Paul McCartney'} User.find({order: 'seq', where: { name: { "gte": 'Paul McCartney'}
}}, function (err, users) { }}, function (err, users) {