Skip to content

Commit

Permalink
Fix moment#4416 isBetween inclusivity for invalid date
Browse files Browse the repository at this point in the history
  • Loading branch information
Seyi Talabi committed Jun 21, 2018
1 parent 2e2a5b3 commit d14d4af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/lib/moment/compare.js
Expand Up @@ -29,7 +29,10 @@ export function isBefore (input, units) {
}
}

export function isBetween (from, to, units, inclusivity) {
export function isBetween(from, to, units, inclusivity) {
if (!this.isValid()) {
return false;
}
inclusivity = inclusivity || '()';
return (inclusivity[0] === '(' ? this.isAfter(from, units) : !this.isBefore(from, units)) &&
(inclusivity[1] === ')' ? this.isBefore(to, units) : !this.isAfter(to, units));
Expand Down
18 changes: 18 additions & 0 deletions src/test/moment/is_between.js
Expand Up @@ -209,6 +209,24 @@ test('is between milliseconds inclusivity', function (assert) {
moment(new Date(2011, 3, 2, 3, 4, 5, 10)), 'milliseconds', '[]'), true, 'start and end inclusive, should handle same end and start date');
});

test('is between without units inclusivity for invalid date', function (assert) {
var m = moment(new Date('non date')),
mCopy = moment(m);
assert.equal(m.isBetween(
moment(new Date(2011, 3, 2, 3, 4, 5, 10)),
moment(new Date(2012, 3, 2, 3, 4, 5, 10)), null, '()'), false, 'invalid moment');

assert.equal(m.isBetween(
moment(new Date(2011, 3, 2, 3, 4, 5, 10)),
moment(new Date(2012, 3, 2, 3, 4, 5, 10)), null, '(]'), false, 'invalid moment');

assert.equal(m.isBetween(
moment(new Date(2011, 3, 2, 3, 4, 5, 10)),
moment(new Date(2012, 3, 2, 3, 4, 5, 10)), null, '[)'), false, 'invalid moment');

assert.equal(m.isBetween('2010-01', '2011-01', null, '[]'), false, 'invalid moment');
});

test('is between year', function (assert) {
var m = moment(new Date(2011, 1, 2, 3, 4, 5, 6)), mCopy = moment(m);
assert.equal(m.isBetween(
Expand Down

0 comments on commit d14d4af

Please sign in to comment.