Skip to content

Commit

Permalink
Merge pull request #17 from arthurdenner/refactor/replacing-some-util…
Browse files Browse the repository at this point in the history
…s-for-date-fns-functions

Replacing some utils for date-fns functions
  • Loading branch information
mkartchner994 authored Apr 11, 2018
2 parents ecd1892 + c6023c9 commit cccc0df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 77 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"react-dom": "^16.0.0",
"serve": "^6.5.4"
},
"dependencies": {},
"dependencies": {
"date-fns": "^1.29.0"
},
"repository": {
"type": "git",
"url": "https://github.com/deseretdigital/dayzed.git"
Expand Down
91 changes: 15 additions & 76 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import addDays from 'date-fns/add_days';
import isBefore from 'date-fns/is_before';
import isToday from 'date-fns/is_today';
import startOfDay from 'date-fns/start_of_day';
import differenceInCalendarMonths from 'date-fns/difference_in_calendar_months';

/**
* This is intended to be used to compose event handlers
* They are executed in order until one of them calls
Expand Down Expand Up @@ -52,7 +58,7 @@ function noop() {}
export function subtractMonth({ calendars, offset, minDate }) {
if (offset > 1 && minDate) {
let { firstDayOfMonth } = calendars[0];
let diffInMonths = monthDiff(minDate, firstDayOfMonth);
let diffInMonths = differenceInCalendarMonths(firstDayOfMonth, minDate);
if (diffInMonths < offset) {
offset = diffInMonths;
}
Expand All @@ -72,7 +78,7 @@ export function subtractMonth({ calendars, offset, minDate }) {
export function addMonth({ calendars, offset, maxDate }) {
if (offset > 1 && maxDate) {
let { lastDayOfMonth } = calendars[calendars.length - 1];
let diffInMonths = monthDiff(lastDayOfMonth, maxDate);
let diffInMonths = differenceInCalendarMonths(maxDate, lastDayOfMonth);
if (diffInMonths < offset) {
offset = diffInMonths;
}
Expand All @@ -93,7 +99,7 @@ export function isBackDisabled({ calendars, minDate }) {
return false;
}
let { firstDayOfMonth } = calendars[0];
let firstDayOfMonthMinusOne = adjustDateByXDays(firstDayOfMonth, -1);
let firstDayOfMonthMinusOne = addDays(firstDayOfMonth, -1);
if (isBefore(firstDayOfMonthMinusOne, minDate)) {
return true;
}
Expand All @@ -113,7 +119,7 @@ export function isForwardDisabled({ calendars, maxDate }) {
return false;
}
let { lastDayOfMonth } = calendars[calendars.length - 1];
let lastDayOfMonthPlusOne = adjustDateByXDays(lastDayOfMonth, 1);
let lastDayOfMonthPlusOne = addDays(lastDayOfMonth, 1);
if (isBefore(maxDate, lastDayOfMonthPlusOne)) {
return true;
}
Expand Down Expand Up @@ -141,15 +147,15 @@ export function getCalendars({
maxDate
}) {
let months = [];
let startDate = normalizeDate(date);
let startDate = startOfDay(date);
if (minDate) {
let minDateNormalized = normalizeDate(minDate);
let minDateNormalized = startOfDay(minDate);
if (isBefore(startDate, minDateNormalized)) {
startDate = minDateNormalized;
}
}
if (maxDate) {
let maxDateNormalized = normalizeDate(maxDate);
let maxDateNormalized = startOfDay(maxDate);
if (isBefore(maxDateNormalized, startDate)) {
startDate = maxDateNormalized;
}
Expand Down Expand Up @@ -210,7 +216,6 @@ function getMonths(month, year, selectedDates, minDate, maxDate) {
};
let thisMonthDays = daysInMonth[month];
let dates = [];
let today = new Date();

// Account for leap year
if (month === 2 && year % 4 === 0) {
Expand All @@ -223,7 +228,7 @@ function getMonths(month, year, selectedDates, minDate, maxDate) {
date,
selected: isSelected(selectedDates, date),
selectable: isSelectable(minDate, maxDate, date),
today: isEqual(date, today)
today: isToday(date)
};
dates.push(dateObj);
}
Expand Down Expand Up @@ -265,17 +270,6 @@ function getMonths(month, year, selectedDates, minDate, maxDate) {
};
}

/**
* Normalizes dates given to the beginning of the day,
* then compares if date comes before dateToCompare.
* @param {Date} date The date to compare with
* @param {Date} dateToCompare The date to compare against
* @returns {Boolean} Whether date is before dateToCompare
*/
function isBefore(date, dateToCompare) {
return normalizeDate(date).getTime() < normalizeDate(dateToCompare).getTime();
}

/**
* Normalizes dates to the beginning of the day,
* then checks to see if the day given is found
Expand All @@ -291,7 +285,7 @@ function isSelected(selectedDates, date) {
return selectedDates.some(selectedDate => {
if (
selectedDate instanceof Date &&
normalizeDate(selectedDate).getTime() === normalizeDate(date).getTime()
startOfDay(selectedDate).getTime() === startOfDay(date).getTime()
) {
return true;
}
Expand All @@ -316,58 +310,3 @@ function isSelectable(minDate, maxDate, date) {
}
return true;
}

/**
* Normalizes dates to the beginning of the day,
* then checks to see if the date given is
* equal to the dateToCompare.
* @param {Date} date The date to compare with
* @param {Date} dateToCompare The date to compare against
* @returns
*/
function isEqual(date, dateToCompare) {
return (
normalizeDate(date).getTime() === normalizeDate(dateToCompare).getTime()
);
}

/**
* Takes the date given and creates a
* new date adjusted by x number of days.
* @param {Date} date The date to adjust
* @param {Number} x The number of days to adjust by
* @returns {Date} The new adjusted date
*/
function adjustDateByXDays(date, x) {
let adjustedDate = new Date(date.getTime());
adjustedDate.setDate(adjustedDate.getDate() + x);
return adjustedDate;
}

/**
* Finds the difference in months between
* the given dates.
* @param {Date} date The date to diff with
* @param {Date} dateToDiff The date to diff against
* @returns {Number} The difference in months
*/
function monthDiff(date, dateToDiff) {
let months = (dateToDiff.getFullYear() - date.getFullYear()) * 12;
months -= date.getMonth();
months += dateToDiff.getMonth();
return months <= 0 ? 0 : months;
}

/**
* Takes a date and creates a
* new date with the hours, minutes,
* seconds, and milliseconds set to 0
* (Beginning of day).
* @param {Date} date The Date to normalize
* @returns {Date} The normalized date
*/
function normalizeDate(date) {
let normalizeDate = new Date(date.getTime());
normalizeDate.setHours(0, 0, 0, 0);
return normalizeDate;
}

0 comments on commit cccc0df

Please sign in to comment.