Skip to content

Commit

Permalink
fix: Implemented isBetween inclusivity (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
waseemahmad31 authored and iamkun committed Feb 11, 2019
1 parent cb208b0 commit af2f4f1
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/plugin/isBetween/index.js
Original file line number Diff line number Diff line change
@@ -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)))
}
}
305 changes: 305 additions & 0 deletions test/plugin/isBetween.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})

0 comments on commit af2f4f1

Please sign in to comment.