From 60c87efb752e4e6b78f96cae777130cb74f2cec3 Mon Sep 17 00:00:00 2001 From: TristanJM Date: Mon, 4 Dec 2017 16:40:34 +0000 Subject: [PATCH] Date range constructor automatically sorts start/end dates --- lib/moment-range.js | 4 ++++ lib/moment-range_test.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/moment-range.js b/lib/moment-range.js index 15209e2..3199cb3 100644 --- a/lib/moment-range.js +++ b/lib/moment-range.js @@ -37,6 +37,10 @@ export class DateRange { this.start = (s === null) ? moment(-8640000000000000) : moment(s); this.end = (e === null) ? moment(8640000000000000) : moment(e); + + if (this.end.isBefore(this.start)) { + [this.start, this.end] = [this.end, this.start]; + } } adjacent(other) { diff --git a/lib/moment-range_test.js b/lib/moment-range_test.js index 6f038c8..fffafd7 100644 --- a/lib/moment-range_test.js +++ b/lib/moment-range_test.js @@ -131,6 +131,15 @@ describe('DateRange', function() { expect(r.start.isSame(quarterStart)).to.be(true); expect(r.end.isSame(quarterEnd)).to.be(true); }); + + it('should automatically sort the range start and end date', function() { + const a = moment('2005-01-01'); + const b = moment('2008-12-20'); + const r = moment.range(b, a); + + expect(r.start.isSame(a)).to.be(true); + expect(r.end.isSame(b)).to.be(true); + }); }); describe('#adjacent', function() {