diff --git a/src/plugin/isBetween/index.js b/src/plugin/isBetween/index.js index 9a880551a..7fcfb4c49 100644 --- a/src/plugin/isBetween/index.js +++ b/src/plugin/isBetween/index.js @@ -1,5 +1,6 @@ export default (o, c, d) => { c.prototype.isBetween = function (a, b, u, i) { + if (!this.isValid()) return false const dA = d(a) const dB = d(b) i = i || '()' diff --git a/test/plugin/isBetween.test.js b/test/plugin/isBetween.test.js index 0f794c9ea..df1d78648 100644 --- a/test/plugin/isBetween.test.js +++ b/test/plugin/isBetween.test.js @@ -17,6 +17,17 @@ test('bounds can be swapped', () => { expect(dayjs('2018-01-01').isBetween(dayjs('2018-01-02'), dayjs('2017-12-31'))).toBeTruthy() }) +test('invalid date is never between', () => { + const a = dayjs('2022-01-28') + const b = dayjs('2022-02-28') + const inclusivities = ['()', '[]', '[)', '(]'] + inclusivities.forEach((i) => { + expect(dayjs(null).isBetween(a, b, 'day', i)).toBe(false) + expect(dayjs('not a date').isBetween(a, b, 'day', i)).toBe(false) + expect(dayjs(null).isBetween(a, b, null, i)).toBe(false) + }) +}) + test('bounds can be swapped with inclusivity', () => { expect(dayjs('2018-01-01').isBetween(dayjs('2017-12-31'), dayjs('2018-01-01'), null, '[]')).toBeTruthy() expect(dayjs('2018-01-01').isBetween(dayjs('2018-01-01'), dayjs('2017-12-31'), null, '[]')).toBeTruthy()