Skip to content

Commit

Permalink
Rename internal utils as helpers (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl committed Nov 3, 2015
1 parent f555948 commit 0eee2ec
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 109 deletions.
38 changes: 19 additions & 19 deletions src/DayPicker.js
@@ -1,5 +1,6 @@
import React, { Component, PropTypes } from "react";
import Utils from "./Utils";
import Helpers from "./Helpers";
import DateUtils from "./DateUtils";

const keys = {
LEFT: 37,
Expand All @@ -8,7 +9,7 @@ const keys = {
SPACE: 32
};

class DayPicker extends Component {
export default class DayPicker extends Component {

static propTypes = {

Expand Down Expand Up @@ -48,7 +49,7 @@ class DayPicker extends Component {
initialMonth: new Date(),
numberOfMonths: 1,
locale: "en",
localeUtils: Utils,
localeUtils: Helpers,
enableOutsideDays: false,
canChangeMonth: true,
renderDay: (day) => day.getDate()
Expand All @@ -57,27 +58,27 @@ class DayPicker extends Component {
constructor(props) {
super(props);
this.state = {
currentMonth: Utils.startOfMonth(props.initialMonth)
currentMonth: Helpers.startOfMonth(props.initialMonth)
};
}

componentWillReceiveProps(nextProps) {
if (this.props.initialMonth !== nextProps.initialMonth) {
this.setState({
currentMonth: Utils.startOfMonth(nextProps.initialMonth)
currentMonth: Helpers.startOfMonth(nextProps.initialMonth)
});
}
}

showMonth(d) {
this.setState({
currentMonth: Utils.startOfMonth(d)
currentMonth: Helpers.startOfMonth(d)
});
}

showNextMonth(callback) {
const { currentMonth } = this.state;
const nextMonth = Utils.addMonths(currentMonth, 1);
const nextMonth = Helpers.addMonths(currentMonth, 1);
this.setState({
currentMonth: nextMonth
}, () => {
Expand All @@ -92,7 +93,7 @@ class DayPicker extends Component {

showPreviousMonth(callback) {
const { currentMonth } = this.state;
const prevMonth = Utils.addMonths(currentMonth, -1);
const prevMonth = Helpers.addMonths(currentMonth, -1);
this.setState({
currentMonth: prevMonth
}, () => {
Expand All @@ -110,15 +111,15 @@ class DayPicker extends Component {
showMonthsForOutsideDay(day) {
const { currentMonth } = this.state;
const { numberOfMonths } = this.props;
const diffInMonths = Utils.getMonthsDiff(currentMonth, day);
const diffInMonths = Helpers.getMonthsDiff(currentMonth, day);
if (diffInMonths > 0 && diffInMonths >= numberOfMonths) {
const nextMonth = Utils.addMonths(currentMonth, numberOfMonths);
const nextMonth = Helpers.addMonths(currentMonth, numberOfMonths);
this.setState({
currentMonth: nextMonth
});
}
else if (diffInMonths < 0) {
const prevMonth = Utils.addMonths(currentMonth, -numberOfMonths);
const prevMonth = Helpers.addMonths(currentMonth, -numberOfMonths);
this.setState({
currentMonth: prevMonth
});
Expand All @@ -138,7 +139,7 @@ class DayPicker extends Component {
if (nodeIndex === 0) {
const { currentMonth } = this.state;
const { numberOfMonths } = this.props;
const prevMonth = Utils.addMonths(currentMonth, -numberOfMonths);
const prevMonth = Helpers.addMonths(currentMonth, -numberOfMonths);
this.setState({
currentMonth: prevMonth
}, () => {
Expand All @@ -165,7 +166,7 @@ class DayPicker extends Component {
if (nodeIndex === dayNodes.length - 1) {
const { currentMonth } = this.state;
const { numberOfMonths } = this.props;
const nextMonth = Utils.addMonths(currentMonth, numberOfMonths);
const nextMonth = Helpers.addMonths(currentMonth, numberOfMonths);
this.setState({
currentMonth: nextMonth
}, () => {
Expand Down Expand Up @@ -312,7 +313,7 @@ class DayPicker extends Component {
renderWeeksInMonth(month) {
const { locale, localeUtils } = this.props;
const firstDayOfWeek = localeUtils.getFirstDayOfWeek(locale);
return Utils.getWeekArray(month, firstDayOfWeek).map((week, i) =>
return Helpers.getWeekArray(month, firstDayOfWeek).map((week, i) =>
<div key={ i } className="DayPicker-Week" role="row">
{ week.map(day => this.renderDay(month, day)) }
</div>
Expand All @@ -328,18 +329,18 @@ class DayPicker extends Component {
let modifiers = [];
const key = `${day.getFullYear()}${day.getMonth()}${day.getDate()}`;

const isToday = Utils.isSameDay(day, new Date());
const isToday = DateUtils.isSameDay(day, new Date());
if (isToday) {
modifiers.push("today");
}

const isOutside = Utils.isDayOutsideMonth(day, month);
const isOutside = DateUtils.isDayOutsideMonth(day, month);
if (isOutside) {
modifiers.push("outside");
}

if (modifierFunctions) {
const customModifiers = Utils.getModifiersForDay(day, modifierFunctions);
const customModifiers = Helpers.getModifiersForDay(day, modifierFunctions);
modifiers = [...modifiers, ...customModifiers];
}

Expand Down Expand Up @@ -394,7 +395,7 @@ class DayPicker extends Component {
const months = [];
let month;
for (let i = 0; i < numberOfMonths; i++) {
month = Utils.addMonths(currentMonth, i);
month = Helpers.addMonths(currentMonth, i);
months.push(this.renderMonth(month, i));
}

Expand All @@ -413,4 +414,3 @@ class DayPicker extends Component {

}

export default DayPicker;
33 changes: 8 additions & 25 deletions src/Utils.js → src/Helpers.js
Expand Up @@ -7,25 +7,20 @@ const WEEKDAYS_SHORT = ["Su", "Mo", "Tu",
const MONTHS = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];

const Utils = {
import DateUtils from "./DateUtils";

export default {

addMonths(d, months) {
const newDate = this.clone(d);
const newDate = DateUtils.clone(d);
newDate.setMonth(d.getMonth() + months);
return newDate;
},

clone(d) {
return new Date(d.getTime());
},

startOfMonth(d) {
const newDate = this.clone(d);
const newDate = DateUtils.clone(d);
newDate.setDate(1);
newDate.setHours(12); // always set noon to avoid time zone issues
newDate.setMinutes(0);
newDate.setSeconds(0);
newDate.setMilliseconds(0);
newDate.setHours(12, 0, 0, 0); // always set noon to avoid time zone issues
return newDate;
},

Expand Down Expand Up @@ -69,15 +64,15 @@ const Utils = {
// unshift days to start the first week
const firstWeek = weekArray[0];
for (let i = 7 - firstWeek.length; i > 0; i--) {
const outsideDate = this.clone(firstWeek[0]);
const outsideDate = DateUtils.clone(firstWeek[0]);
outsideDate.setDate(firstWeek[0].getDate() - 1);
firstWeek.unshift(outsideDate);
}

// push days until the end of the last week
const lastWeek = weekArray[weekArray.length - 1];
for (let i = lastWeek.length; i < 7; i++) {
const outsideDate = this.clone(lastWeek[lastWeek.length - 1]);
const outsideDate = DateUtils.clone(lastWeek[lastWeek.length - 1]);
outsideDate.setDate(lastWeek[lastWeek.length - 1].getDate() + 1);
lastWeek.push(outsideDate);
}
Expand All @@ -99,16 +94,6 @@ const Utils = {
return modifiers;
},

isDayOutsideMonth(d1, d2) {
return d1.getMonth() !== d2.getMonth();
},

isSameDay(d1, d2) {
return d1.getDate() === d2.getDate() &&
d1.getMonth() === d2.getMonth() &&
d1.getFullYear() === d2.getFullYear();
},

formatMonthTitle(d) {
return `${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
},
Expand All @@ -130,5 +115,3 @@ const Utils = {
}

};

export default Utils;

0 comments on commit 0eee2ec

Please sign in to comment.