Can compare booleans

pull/2/head
Louis Chatriot 12 years ago
parent b0eb3c9053
commit fdc01721e0
  1. 15
      lib/model.js
  2. 18
      test/model.test.js

@ -132,9 +132,10 @@ function deepCopy (obj) {
/**
* Utility function for comparing things
* Works for numbers, strings and booleans
* Assumes type checking was already done (a and b already have the same type)
*/
function compareNumbersAndStrings (a, b) {
function compareNSB (a, b) {
if (a < b) { return -1; }
if (a > b) { return 1; }
return 0;
@ -153,12 +154,16 @@ function compareThings (a, b) {
if (b === null) { return a === null ? 0 : 1; }
// 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; }
if (typeof a === 'number') { return typeof b === 'number' ? compareNSB(a, b) : -1; }
if (typeof b === 'number') { return typeof a === 'number' ? compareNSB(a, b) : 1; }
// Strings
if (typeof a === 'string') { return typeof b === 'string' ? compareNumbersAndStrings(a, b) : -1; }
if (typeof b === 'string') { return typeof a === 'string' ? compareNumbersAndStrings(a, b) : 1; }
if (typeof a === 'string') { return typeof b === 'string' ? compareNSB(a, b) : -1; }
if (typeof b === 'string') { return typeof a === 'string' ? compareNSB(a, b) : 1; }
// Booleans
if (typeof a === 'boolean') { return typeof b === 'boolean' ? compareNSB(a, b) : -1; }
if (typeof b === 'boolean') { return typeof a === 'boolean' ? compareNSB(a, b) : 1; }
}

@ -379,6 +379,24 @@ describe('Model', function () {
});
});
it('Then booleans', function () {
var otherStuff = [new Date(4321), {}, { hello: 'world' }, [], ['quite', 5]]
, bools = [true, false];
model.compareThings(true, true).should.equal(0);
model.compareThings(false, false).should.equal(0);
model.compareThings(true, false).should.equal(1);
model.compareThings(false, true).should.equal(-1);
otherStuff.forEach(function (stuff) {
bools.forEach(function (bool) {
model.compareThings(bool, stuff).should.equal(-1);
model.compareThings(stuff, bool).should.equal(1);
});
});
});
}); // ==== End of 'Comparing things' ==== //

Loading…
Cancel
Save