From 07ff9e2183fdc7318c813906f781346ba2419f14 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Tue, 28 May 2013 16:01:12 +0200 Subject: [PATCH] Can compare dates --- lib/model.js | 4 ++++ test/model.test.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/model.js b/lib/model.js index 62990b5..be93bb3 100644 --- a/lib/model.js +++ b/lib/model.js @@ -164,6 +164,10 @@ function compareThings (a, b) { // 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; } + + // Dates + if (util.isDate(a)) { return util.isDate(b) ? compareNSB(a.getTime(), b.getTime()) : -1; } + if (util.isDate(b)) { return util.isDate(a) ? compareNSB(a.getTime(), b.getTime()) : 1; } } diff --git a/test/model.test.js b/test/model.test.js index 8c00017..b00306d 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -396,6 +396,24 @@ describe('Model', function () { }); }); + it('Then dates', function () { + var otherStuff = [{}, { hello: 'world' }, [], ['quite', 5]] + , dates = [new Date(-123), new Date(), new Date(5555), new Date(0)] + , now = new Date(); + + model.compareThings(now, now).should.equal(0); + model.compareThings(new Date(54341), now).should.equal(-1); + model.compareThings(now, new Date(54341)).should.equal(1); + model.compareThings(new Date(0), new Date(-54341)).should.equal(1); + model.compareThings(new Date(123), new Date(4341)).should.equal(-1); + + otherStuff.forEach(function (stuff) { + dates.forEach(function (date) { + model.compareThings(date, stuff).should.equal(-1); + model.compareThings(stuff, date).should.equal(1); + }); + }); + }); }); // ==== End of 'Comparing things' ==== //