From af2f4f193ee84e4cd1322279c9c19d292feb1be5 Mon Sep 17 00:00:00 2001 From: Waseem Ahmad <42496021+waseemahmad31@users.noreply.github.com> Date: Mon, 11 Feb 2019 09:45:47 +0530 Subject: [PATCH] fix: Implemented isBetween inclusivity (#464) --- src/plugin/isBetween/index.js | 11 +- test/plugin/isBetween.test.js | 305 ++++++++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+), 3 deletions(-) diff --git a/src/plugin/isBetween/index.js b/src/plugin/isBetween/index.js index 69f81f25d..9a880551a 100644 --- a/src/plugin/isBetween/index.js +++ b/src/plugin/isBetween/index.js @@ -1,9 +1,14 @@ export default (o, c, d) => { - c.prototype.isBetween = function (a, b, u) { + c.prototype.isBetween = function (a, b, u, i) { const dA = d(a) const dB = d(b) + i = i || '()' + const dAi = i[0] === '(' + const dBi = i[1] === ')' - return (this.isAfter(dA, u) && this.isBefore(dB, u)) - || (this.isBefore(dA, u) && this.isAfter(dB, u)) + return ((dAi ? this.isAfter(dA, u) : !this.isBefore(dA, u)) && + (dBi ? this.isBefore(dB, u) : !this.isAfter(dB, u))) + || ((dAi ? this.isBefore(dA, u) : !this.isAfter(dA, u)) && + (dBi ? this.isAfter(dB, u) : !this.isBefore(dB, u))) } } diff --git a/test/plugin/isBetween.test.js b/test/plugin/isBetween.test.js index 347654d70..0f794c9ea 100644 --- a/test/plugin/isBetween.test.js +++ b/test/plugin/isBetween.test.js @@ -17,6 +17,11 @@ test('bounds can be swapped', () => { expect(dayjs('2018-01-01').isBetween(dayjs('2018-01-02'), dayjs('2017-12-31'))).toBeTruthy() }) +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() +}) + test('is between without units', () => { const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)) const mCopy = dayjs(m) @@ -381,3 +386,303 @@ test('is between millisecond', () => { expect(m.isBetween(m, 'millisecond')).toBe(false, 'same moments are not between the same millisecond') expect(+m).toEqual(+mCopy, 'isBetween millisecond should not change moment') }) + +test('is between without units inclusivity', () => { + const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)) + const mCopy = dayjs(m) + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '()' + )).toBe(false, 'start and end are excluded, start is equal to dayjs') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '()' + )).toBe(false, 'start and end are excluded, end is equal to dayjs') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '()' + )).toBe(true, 'start and end are excluded, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + null, + '()' + )).toBe(false, 'start and end are excluded, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '()' + )).toBe(false, 'start and end are excluded, should fail on same start/end date.') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '(]' + )).toBe(false, 'start is excluded and end is included should fail on same start date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '(]' + )).toBe(true, 'start is excluded and end is included should succeed on end date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '(]' + )).toBe(true, 'start is excluded and end is included, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + null, + '(]' + )).toBe(false, 'start is excluded and end is included, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '(]' + )).toBe(false, 'start is excluded and end is included, should fail on same start/end date.') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '[)' + )).toBe(true, 'start is included and end is excluded should succeed on same start date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '[)' + )).toBe(false, 'start is included and end is excluded should fail on same end date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '[)' + )).toBe(true, 'start is included and end is excluded, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + null, + '[)' + )).toBe(false, 'start is included and end is excluded, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '[)' + )).toBe(false, 'start is included and end is excluded, should fail on same end and start date') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '[]' + )).toBe(true, 'start and end inclusive should succeed on same start date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '[]' + )).toBe(true, 'start and end inclusive should succeed on same end date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + null, + '[]' + )).toBe(true, 'start and end inclusive, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + null, + '[]' + )).toBe(false, 'start and end inclusive, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + null, + '[]' + )).toBe(true, 'start and end inclusive, should handle same end and start date') + + expect(+m).toEqual(+mCopy, 'isBetween millisecond should not change moment') +}) + +test('is between milliseconds inclusivity', () => { + const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)) + const mCopy = dayjs(m) + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds' + )).toBe(true, 'options, no inclusive') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '()' + )).toBe(false, 'start and end are excluded, start is equal to dayjs') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '()' + )).toBe(false, 'start and end are excluded, end is equal to dayjs') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '()' + )).toBe(true, 'start and end are excluded, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '()' + )).toBe(false, 'start and end are excluded, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '()' + )).toBe(false, 'start and end are excluded, should fail on same start/end date.') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '(]' + )).toBe(false, 'start is excluded and end is included should fail on same start date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '(]' + )).toBe(true, 'start is excluded and end is included should succeed on end date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '(]' + )).toBe(true, 'start is excluded and end is included, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '(]' + )).toBe(false, 'start is excluded and end is included, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '(]' + )).toBe(false, 'start is excluded and end is included, should fail on same start/end date.') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[)' + )).toBe(true, 'start is included and end is excluded should succeed on same start date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[)' + )).toBe(false, 'start is included and end is excluded should fail on same end date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[)' + )).toBe(true, 'start is included and end is excluded, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[)' + )).toBe(false, 'start is included and end is excluded, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[)' + )).toBe(false, 'start is included and end is excluded, should fail on same end and start date') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[]' + )).toBe(true, 'start and end inclusive should succeed on same start date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[]' + )).toBe(true, 'start and end inclusive should succeed on same end date') + + expect(m.isBetween( + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[]' + )).toBe(true, 'start and end inclusive, is between') + + expect(m.isBetween( + dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[]' + )).toBe(false, 'start and end inclusive, is not between') + + expect(m.isBetween( + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), + 'milliseconds', + '[]' + )).toBe(true, 'start and end inclusive, should handle same end and start date') + + expect(+m).toEqual(+mCopy, 'isBetween second should not change moment') +})