From b0eb3c90534b611a2daa4ee3073aa878f22ea80e Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Tue, 28 May 2013 15:51:12 +0200 Subject: [PATCH] Can compare strings --- lib/model.js | 8 ++++++-- test/model.test.js | 24 ++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/model.js b/lib/model.js index 5c9e1b2..c710898 100644 --- a/lib/model.js +++ b/lib/model.js @@ -144,7 +144,7 @@ function compareNumbersAndStrings (a, b) { * 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 + * 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!) */ function compareThings (a, b) { @@ -152,9 +152,13 @@ function compareThings (a, b) { if (a === null) { return b === 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 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; } } diff --git a/test/model.test.js b/test/model.test.js index 558a23d..cdcfdf8 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -332,7 +332,7 @@ describe('Model', function () { 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]]; + var otherStuff = ["string", "", -1, 0, 5.3, 12, true, false, new Date(12345), {}, { hello: 'world' }, [], ['quite', 5]]; model.compareThings(null, null).should.equal(0); @@ -343,13 +343,16 @@ describe('Model', 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]; 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); + 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) { 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' ==== //