Can compare null and numbers

pull/2/head
Louis Chatriot 12 years ago
parent 85626e6d89
commit 79ae8c737a
  1. 30
      lib/model.js
  2. 33
      test/model.test.js

@ -130,6 +130,35 @@ function deepCopy (obj) {
} }
/**
* Utility function for comparing things
* Assumes type checking was already done (a and b already have the same type)
*/
function compareNumbersAndStrings (a, b) {
if (a < b) { return -1; }
if (a > b) { return 1; }
return 0;
}
/**
* Compare things
* Things are defined as any native types (string, number, boolean, null, date) and objects
* In the case of objects and arrays, we compare the serialized versions
* If two objects dont have the same type, the (arbitrary) type hierarchy is: null, number, strings, dates, boolean, objects, arrays
* Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!)
*/
function compareThings (a, b) {
// null is less than everything except null
if (a === null) { return b === null ? 0 : -1; }
if (b === null) { return a === null ? 0 : 1; }
// Comparing numbers
if (typeof a === 'number') { return typeof b === 'number' ? compareNumbersAndStrings(a, b) : -1; }
if (typeof b === 'number') { return typeof a === 'number' ? compareNumbersAndStrings(a, b) : 1; }
}
// ============================================================== // ==============================================================
// Updating documents // Updating documents
// ============================================================== // ==============================================================
@ -479,3 +508,4 @@ module.exports.modify = modify;
module.exports.getDotValue = getDotValue; module.exports.getDotValue = getDotValue;
module.exports.match = match; module.exports.match = match;
module.exports.areThingsEqual = areThingsEqual; module.exports.areThingsEqual = areThingsEqual;
module.exports.compareThings = compareThings;

@ -329,6 +329,39 @@ describe('Model', function () {
}); // ==== End of 'Modifying documents' ==== // }); // ==== End of 'Modifying documents' ==== //
describe.only('Comparing things', function () {
it('null is less than everything except null', function () {
var otherStuff = ["string", "", -1, 0, 5.3, 12, true, false, {}, { hello: 'world' }, [], ['quite', 5]];
model.compareThings(null, null).should.equal(0);
otherStuff.forEach(function (stuff) {
model.compareThings(null, stuff).should.equal(-1);
model.compareThings(stuff, null).should.equal(1);
});
});
it('After null, numbers are the smallest', function () {
var otherStuff = ["string", "", true, false, {}, { hello: 'world' }, [], ['quite', 5]]
, numbers = [-12, 0, 12, 5.7];
model.compareThings(-12, 0).should.equal(-1);
model.compareThings(0, -3).should.equal(1);
model.compareThings(5.7, 2).should.equal(1);
model.compareThings(5.7, 12.3).should.equal(-1);
otherStuff.forEach(function (stuff) {
numbers.forEach(function (number) {
model.compareThings(number, stuff).should.equal(-1);
model.compareThings(stuff, number).should.equal(1);
});
});
});
}); // ==== End of 'Comparing things' ==== //
describe('Querying', function () { describe('Querying', function () {
describe('Comparing things', function () { describe('Comparing things', function () {

Loading…
Cancel
Save