Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented isBetween inclusivity #464

Merged
merged 6 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})