Can compare strings

pull/2/head
Louis Chatriot 12 years ago
parent 79ae8c737a
commit b0eb3c9053
  1. 8
      lib/model.js
  2. 24
      test/model.test.js

@ -144,7 +144,7 @@ function compareNumbersAndStrings (a, b) {
* Compare things * Compare things
* Things are defined as any native types (string, number, boolean, null, date) and objects * 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 * 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 * If two objects dont have the same type, the (arbitrary) type hierarchy is: null, number, strings, boolean, dates, 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!) * 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) { function compareThings (a, b) {
@ -152,9 +152,13 @@ function compareThings (a, b) {
if (a === null) { return b === null ? 0 : -1; } if (a === null) { return b === null ? 0 : -1; }
if (b === null) { return a === null ? 0 : 1; } if (b === null) { return a === null ? 0 : 1; }
// Comparing numbers // Numbers
if (typeof a === 'number') { return typeof b === 'number' ? compareNumbersAndStrings(a, b) : -1; } 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 b === 'number') { return typeof a === 'number' ? compareNumbersAndStrings(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; }
} }

@ -332,7 +332,7 @@ describe('Model', function () {
describe.only('Comparing things', function () { describe.only('Comparing things', function () {
it('null is less than everything except null', function () { it('null is less than everything except null', function () {
var otherStuff = ["string", "", -1, 0, 5.3, 12, true, false, {}, { hello: 'world' }, [], ['quite', 5]]; var otherStuff = ["string", "", -1, 0, 5.3, 12, true, false, new Date(12345), {}, { hello: 'world' }, [], ['quite', 5]];
model.compareThings(null, null).should.equal(0); model.compareThings(null, null).should.equal(0);
@ -343,13 +343,16 @@ describe('Model', function () {
}); });
it('After null, numbers are the smallest', function () { it('After null, numbers are the smallest', function () {
var otherStuff = ["string", "", true, false, {}, { hello: 'world' }, [], ['quite', 5]] var otherStuff = ["string", "", true, false, new Date(4312), {}, { hello: 'world' }, [], ['quite', 5]]
, numbers = [-12, 0, 12, 5.7]; , numbers = [-12, 0, 12, 5.7];
model.compareThings(-12, 0).should.equal(-1); model.compareThings(-12, 0).should.equal(-1);
model.compareThings(0, -3).should.equal(1); model.compareThings(0, -3).should.equal(1);
model.compareThings(5.7, 2).should.equal(1); model.compareThings(5.7, 2).should.equal(1);
model.compareThings(5.7, 12.3).should.equal(-1); model.compareThings(5.7, 12.3).should.equal(-1);
model.compareThings(0, 0).should.equal(0);
model.compareThings(-2.6, -2.6).should.equal(0);
model.compareThings(5, 5).should.equal(0);
otherStuff.forEach(function (stuff) { otherStuff.forEach(function (stuff) {
numbers.forEach(function (number) { numbers.forEach(function (number) {
@ -359,6 +362,23 @@ describe('Model', function () {
}); });
}); });
it('Then strings', function () {
var otherStuff = [true, false, new Date(4321), {}, { hello: 'world' }, [], ['quite', 5]]
, strings = ['', 'string', 'hello world'];
model.compareThings('', 'hey').should.equal(-1);
model.compareThings('hey', '').should.equal(1);
model.compareThings('hey', 'hew').should.equal(1);
model.compareThings('hey', 'hey').should.equal(0);
otherStuff.forEach(function (stuff) {
strings.forEach(function (string) {
model.compareThings(string, stuff).should.equal(-1);
model.compareThings(stuff, string).should.equal(1);
});
});
});
}); // ==== End of 'Comparing things' ==== // }); // ==== End of 'Comparing things' ==== //

Loading…
Cancel
Save