From 7f3213b71f2caabc2237e19854e6885a6b285dd8 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:16:14 -0500 Subject: [PATCH 1/7] Add isDayBefore / isDayAfter functions --- docs/DateUtils.md | 8 ++++++++ src/DateUtils.js | 30 +++++++++++++++++++++++++++++- test/DateUtils.js | 42 ++++++++++++++++++++++++++++++++++++++++++ types/index.d.ts | 2 ++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/docs/DateUtils.md b/docs/DateUtils.md index 4a898b1d9e..159b30b4f0 100644 --- a/docs/DateUtils.md +++ b/docs/DateUtils.md @@ -17,6 +17,14 @@ Return `d` as a new date with `n` months added. Missing days will be added to th Clone the date `d` returning a new date with the same time. +### isDayAfter `(d1: ?Date, d2: ?Date) ⇒ Bool` + +Return `true` if the first day is after the second day. + +### isDayBefore `(d1: ?Date, d2: ?Date) ⇒ Bool` + +Return `true` if the first day is before the second day. + ### isSameDay `(d1: ?Date, d2: ?Date) ⇒ Bool` Return `true` if the two dates `d1` and `d2` are the same day, e.g. ignoring their time. diff --git a/src/DateUtils.js b/src/DateUtils.js index 90274899f2..983a8bd275 100644 --- a/src/DateUtils.js +++ b/src/DateUtils.js @@ -37,6 +37,34 @@ export function isSameDay(d1, d2) { ); } +/** + * Returns `true` if the first day is before the second day. + * + * @export + * @param {Date} d1 + * @param {Date} d2 + * @returns {Boolean} + */ +export function isDayBefore(d1, d2) { + const day1 = clone(d1).setHours(0, 0, 0, 0); + const day2 = clone(d2).setHours(0, 0, 0, 0); + return day1 < day2; +} + +/** + * Returns `true` if the first day is after the second day. + * + * @export + * @param {Date} d1 + * @param {Date} d2 + * @returns {Boolean} + */ +export function isDayAfter(d1, d2) { + const day1 = clone(d1).setHours(0, 0, 0, 0); + const day2 = clone(d2).setHours(0, 0, 0, 0); + return day1 > day2; +} + /** * Return `true` if a day is in the past, e.g. yesterday or any day * before yesterday. @@ -47,7 +75,7 @@ export function isSameDay(d1, d2) { export function isPastDay(d) { const today = new Date(); today.setHours(0, 0, 0, 0); - return d < today; + return isDayBefore(d, today); } /** diff --git a/test/DateUtils.js b/test/DateUtils.js index 6f42602526..5656cf561c 100644 --- a/test/DateUtils.js +++ b/test/DateUtils.js @@ -29,6 +29,48 @@ describe('DateUtils', () => { }); }); + describe('isDayBefore', () => { + it('returns true when the day is before the other day', () => { + const day1 = new Date(2015, 10, 11, 5, 25); + const day2 = new Date(2015, 10, 12, 3, 40); + const isDayBefore = DateUtils.isDayBefore(day1, day2); + expect(isDayBefore).to.be.true; + }); + it('returns false for the same day with different times', () => { + const day1 = new Date(2015, 10, 11, 5, 25); + const day2 = new Date(2015, 10, 11, 6, 40); + const isDayBefore = DateUtils.isDayBefore(day1, day2); + expect(isDayBefore).to.be.false; + }); + it('returns false if the second day is after', () => { + const day1 = new Date(2015, 10, 13, 5, 25); + const day2 = new Date(2015, 10, 12, 1, 40); + const isDayBefore = DateUtils.isDayBefore(day1, day2); + expect(isDayBefore).to.be.false; + }); + }); + + describe('isDayAfter', () => { + it('returns true when the day is after the other day', () => { + const day1 = new Date(2015, 10, 13, 5, 25); + const day2 = new Date(2015, 10, 12, 3, 40); + const isDayAfter = DateUtils.isDayAfter(day1, day2); + expect(isDayAfter).to.be.true; + }); + it('returns false for the same day with different times', () => { + const day1 = new Date(2015, 10, 11, 5, 25); + const day2 = new Date(2015, 10, 11, 6, 40); + const isDayAfter = DateUtils.isDayAfter(day1, day2); + expect(isDayAfter).to.be.false; + }); + it('returns false if the second day is after', () => { + const day1 = new Date(2015, 10, 12, 5, 25); + const day2 = new Date(2015, 10, 13, 1, 40); + const isDayAfter = DateUtils.isDayAfter(day1, day2); + expect(isDayAfter).to.be.false; + }); + }); + describe('isSameDay', () => { it('returns true if two days differ only by time', () => { const day1 = new Date(2015, 10, 11, 5, 25); diff --git a/types/index.d.ts b/types/index.d.ts index c94d6752f1..c84dab442a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,6 +15,8 @@ declare namespace DayPicker { addMonths(d: Date, n: number): Date; clone(d: Date): Date; isSameDay(d1: Date, d2: Date): Date; + isDayBefore(d1: Date, d2: Date): boolean; + isDayAfter(d1: Date, d2: Date): boolean; isPastDay(d: Date): boolean; isFutureDay(d: Date): boolean; isDayBetween(day: Date, begin: Date, end: Date): boolean; From 9aafc8e2ed4a8f94b5549877e07f36156ee41ddd Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:16:30 -0500 Subject: [PATCH 2/7] Fix return type --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index c84dab442a..d97a0ae2d7 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -14,7 +14,7 @@ declare namespace DayPicker { interface DateUtils { addMonths(d: Date, n: number): Date; clone(d: Date): Date; - isSameDay(d1: Date, d2: Date): Date; + isSameDay(d1: Date, d2: Date): boolean; isDayBefore(d1: Date, d2: Date): boolean; isDayAfter(d1: Date, d2: Date): boolean; isPastDay(d: Date): boolean; From 8b2ddf7aec6210379af96dee735f2046549b9332 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:17:07 -0500 Subject: [PATCH 3/7] Sort lines --- types/index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index d97a0ae2d7..32ed475260 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,16 +12,16 @@ declare namespace DayPicker { } interface DateUtils { + addDayToRange(day: Date, range: RangeModifier): RangeModifier; addMonths(d: Date, n: number): Date; clone(d: Date): Date; - isSameDay(d1: Date, d2: Date): boolean; - isDayBefore(d1: Date, d2: Date): boolean; isDayAfter(d1: Date, d2: Date): boolean; - isPastDay(d: Date): boolean; - isFutureDay(d: Date): boolean; + isDayBefore(d1: Date, d2: Date): boolean; isDayBetween(day: Date, begin: Date, end: Date): boolean; - addDayToRange(day: Date, range: RangeModifier): RangeModifier; isDayInRange(day: Date, range: RangeModifier): boolean; + isFutureDay(d: Date): boolean; + isPastDay(d: Date): boolean; + isSameDay(d1: Date, d2: Date): boolean; } interface CaptionElementProps { From 29bcda7e7a1c7e98edac27e19f60298b25d721f3 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:21:43 -0500 Subject: [PATCH 4/7] Update descriptions --- test/Helpers.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Helpers.js b/test/Helpers.js index 3bf81c2eda..a029e6c209 100644 --- a/test/Helpers.js +++ b/test/Helpers.js @@ -88,14 +88,14 @@ describe('Helpers', () => { expect(modifiers.indexOf('maybe')).to.equal(-1); expect(modifiers.indexOf('no')).to.equal(-1); }); - it('accepts a single day', () => { + it('returns the modifier for a single day', () => { const modifiers = Helpers.getModifiersForDay(new Date(2015, 8, 19), { foo: new Date(2015, 8, 19), }); expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.equal(0); }); - it('ignore falsy values', () => { + it('ignores falsy values', () => { const modifiers = Helpers.getModifiersForDay(new Date(2015, 8, 19), { foo: null, bar: false, @@ -103,7 +103,7 @@ describe('Helpers', () => { expect(modifiers).to.have.length(0); expect(modifiers.indexOf('foo')).to.equal(-1); }); - it('accepts an array of days', () => { + it('returns the modifier for an array of days', () => { const modifiersObj = { foo: [ new Date(2015, 8, 19), @@ -147,7 +147,7 @@ describe('Helpers', () => { expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.be.above(-1); }); - it('accepts a single range of days', () => { + it('returns the modifier for a range of days', () => { const range = { foo: { from: new Date(2015, 8, 18), @@ -166,7 +166,7 @@ describe('Helpers', () => { ); expect(modifiers2).to.have.length(0); }); - it('accepts multiple ranges of days', () => { + it('returns the modifier for multiple ranges of days', () => { const ranges = { foo: [ { @@ -190,7 +190,7 @@ describe('Helpers', () => { ); expect(modifiers2.indexOf('foo')).to.equal(0); }); - it('accepts an "after" modifier', () => { + it('returns an "after" modifier', () => { const afterModifier = { foo: { after: new Date(2015, 8, 18), @@ -203,7 +203,7 @@ describe('Helpers', () => { expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.equal(0); }); - it('accepts an "after" modifier in an array of days', () => { + it('returns an "after" modifier in an array of days', () => { const afterModifier = { foo: [{ after: new Date(2015, 8, 18) }], }; @@ -214,7 +214,7 @@ describe('Helpers', () => { expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.equal(0); }); - it('accepts a "before" modifier', () => { + it('returns a "before" modifier', () => { const afterModifier = { foo: { before: new Date(2015, 8, 15), @@ -227,7 +227,7 @@ describe('Helpers', () => { expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.equal(0); }); - it('accepts a "before" modifier in an array of days', () => { + it('returns a "before" modifier in an array of days', () => { const afterModifier = { foo: [{ before: new Date(2015, 8, 15) }], }; @@ -238,7 +238,7 @@ describe('Helpers', () => { expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.equal(0); }); - it('work with a mix of functions and days', () => { + it('works with mixing functions and other objects', () => { const mixedModifiers = { foo: [ { before: new Date(2015, 8, 15) }, From 8b097a1ba4133231e77027b8eaf3eac0c7c78f2a Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:26:09 -0500 Subject: [PATCH 5/7] Fix variable name --- test/Helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Helpers.js b/test/Helpers.js index a029e6c209..f5a057c199 100644 --- a/test/Helpers.js +++ b/test/Helpers.js @@ -215,14 +215,14 @@ describe('Helpers', () => { expect(modifiers.indexOf('foo')).to.equal(0); }); it('returns a "before" modifier', () => { - const afterModifier = { + const beforeModifier = { foo: { before: new Date(2015, 8, 15), }, }; const modifiers = Helpers.getModifiersForDay( new Date(2015, 8, 10), - afterModifier + beforeModifier ); expect(modifiers).to.have.length(1); expect(modifiers.indexOf('foo')).to.equal(0); From c8a5051f8bb9857ba0dce0e38bab4d01d3bc5338 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:41:25 -0500 Subject: [PATCH 6/7] Use isDayBefore/isDayAfter to compare days --- examples/src/examples/RangeAdvanced.js | 2 +- src/DateUtils.js | 14 ++++++-------- src/Helpers.js | 16 +++++++++++----- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/examples/src/examples/RangeAdvanced.js b/examples/src/examples/RangeAdvanced.js index ede88cd724..00b97766b3 100644 --- a/examples/src/examples/RangeAdvanced.js +++ b/examples/src/examples/RangeAdvanced.js @@ -13,7 +13,7 @@ const initialState = { function isSelectingFirstDay(from, to, day) { const firstDayIsNotSelected = !from; - const selectedDayIsBeforeFirstDay = day < from; + const selectedDayIsBeforeFirstDay = DateUtils.isDayBefore(day, from); const rangeIsSelected = from && to; return ( firstDayIsNotSelected || selectedDayIsBeforeFirstDay || rangeIsSelected diff --git a/src/DateUtils.js b/src/DateUtils.js index 983a8bd275..db888564e7 100644 --- a/src/DateUtils.js +++ b/src/DateUtils.js @@ -102,13 +102,11 @@ export function isFutureDay(d) { */ export function isDayBetween(d, d1, d2) { const date = clone(d); - const date1 = clone(d1); - const date2 = clone(d2); - date.setHours(0, 0, 0, 0); - date1.setHours(0, 0, 0, 0); - date2.setHours(0, 0, 0, 0); - return (date1 < date && date < date2) || (date2 < date && date < date1); + return ( + (isDayAfter(date, d1) && isDayBefore(date, d2)) || + (isDayAfter(date, d2) && isDayBefore(date, d1)) + ); } /** @@ -126,14 +124,14 @@ export function addDayToRange(day, range = { from: null, to: null }) { } else if (from && to && isSameDay(from, to) && isSameDay(day, from)) { from = null; to = null; - } else if (to && day < from) { + } else if (to && isDayBefore(day, from)) { from = day; } else if (to && isSameDay(day, to)) { from = day; to = day; } else { to = day; - if (to < from) { + if (isDayBefore(to, from)) { to = from; from = day; } diff --git a/src/Helpers.js b/src/Helpers.js index bb525bd7cb..81de202721 100644 --- a/src/Helpers.js +++ b/src/Helpers.js @@ -1,4 +1,10 @@ -import { clone, isSameDay, isDayInRange } from './DateUtils'; +import { + clone, + isSameDay, + isDayInRange, + isDayAfter, + isDayBefore, +} from './DateUtils'; import { getFirstDayOfWeek } from './LocaleUtils'; export function cancelEvent(e) { @@ -71,10 +77,10 @@ export function getModifiersForDay(d, modifiersObj = {}) { return isDayInRange(d, range); } if (day.after) { - return d > day.after; + return isDayAfter(d, day.after); } if (day.before) { - return d < day.before; + return isDayBefore(d, day.before); } if (typeof day === 'function' && day(d)) { return true; @@ -87,10 +93,10 @@ export function getModifiersForDay(d, modifiersObj = {}) { } else if (isRangeOfDates(value) && isDayInRange(d, value)) { // modifier's value is a range modifiers.push(modifier); - } else if (value.after && d > value.after) { + } else if (value.after && isDayAfter(d, value.after)) { // modifier's value has an after date modifiers.push(modifier); - } else if (value.before && d < value.before) { + } else if (value.before && isDayBefore(d, value.before)) { // modifier's value has an after date modifiers.push(modifier); } else if (typeof value === 'function' && value(d)) { From 33f9fef5dc0c334b09d6ad5170e6286b67ac110e Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Tue, 25 Apr 2017 17:42:59 -0500 Subject: [PATCH 7/7] Use `day` instead of `d` to mark days vs. dates objects --- docs/DateUtils.md | 20 ++++++++++---------- types/index.d.ts | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/DateUtils.md b/docs/DateUtils.md index 159b30b4f0..d284d8765c 100644 --- a/docs/DateUtils.md +++ b/docs/DateUtils.md @@ -17,29 +17,29 @@ Return `d` as a new date with `n` months added. Missing days will be added to th Clone the date `d` returning a new date with the same time. -### isDayAfter `(d1: ?Date, d2: ?Date) ⇒ Bool` +### isDayAfter `(day1: ?Date, day2: ?Date) ⇒ Bool` Return `true` if the first day is after the second day. -### isDayBefore `(d1: ?Date, d2: ?Date) ⇒ Bool` +### isDayBefore `(day1: ?Date, day2: ?Date) ⇒ Bool` Return `true` if the first day is before the second day. -### isSameDay `(d1: ?Date, d2: ?Date) ⇒ Bool` +### isSameDay `(day1: ?Date, day2: ?Date) ⇒ Bool` -Return `true` if the two dates `d1` and `d2` are the same day, e.g. ignoring their time. +Return `true` if `day1` and `day2` are the same day. -### isPastDay `(d: Date) ⇒ Bool` +### isPastDay `(day: Date) ⇒ Bool` -Returns `true` if `d` is in the past, e.g. is yesterday or any day before yesterday. +Returns `true` if `day` is in the past, e.g. is yesterday or any day before yesterday. -### isFutureDay `(d: Date) ⇒ Bool` +### isFutureDay `(day: Date) ⇒ Bool` -Returns `true` if `d` is in the future, e.g. is tomorrow or any day after tomorrow. +Returns `true` if `day` is in the future, e.g. is tomorrow or any day after tomorrow. -### isDayBetween `(day: Date, d1: Date, d2: Date) ⇒ Bool` +### isDayBetween `(day: Date, day1: Date, day2: Date) ⇒ Bool` -Returns `true` if `day` is between the days `d1` and `d2`, without including those days. +Returns `true` if `day` is between the days `day1` and `day2`, without including those days. ### addDayToRange `(day: Date, range: ?object) ⇒ Object` diff --git a/types/index.d.ts b/types/index.d.ts index 32ed475260..7c00950718 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,13 +15,13 @@ declare namespace DayPicker { addDayToRange(day: Date, range: RangeModifier): RangeModifier; addMonths(d: Date, n: number): Date; clone(d: Date): Date; - isDayAfter(d1: Date, d2: Date): boolean; - isDayBefore(d1: Date, d2: Date): boolean; + isDayAfter(day1: Date, day2: Date): boolean; + isDayBefore(day1: Date, day2: Date): boolean; isDayBetween(day: Date, begin: Date, end: Date): boolean; isDayInRange(day: Date, range: RangeModifier): boolean; - isFutureDay(d: Date): boolean; - isPastDay(d: Date): boolean; - isSameDay(d1: Date, d2: Date): boolean; + isFutureDay(day: Date): boolean; + isPastDay(day: Date): boolean; + isSameDay(day1: Date, day2: Date): boolean; } interface CaptionElementProps {