diff --git a/src/DayPicker.js b/src/DayPicker.js index fca6b3eb61..414362b339 100644 --- a/src/DayPicker.js +++ b/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, @@ -8,7 +9,7 @@ const keys = { SPACE: 32 }; -class DayPicker extends Component { +export default class DayPicker extends Component { static propTypes = { @@ -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() @@ -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 }, () => { @@ -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 }, () => { @@ -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 }); @@ -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 }, () => { @@ -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 }, () => { @@ -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) =>
{ week.map(day => this.renderDay(month, day)) }
@@ -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]; } @@ -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)); } @@ -413,4 +414,3 @@ class DayPicker extends Component { } -export default DayPicker; diff --git a/src/Utils.js b/src/Helpers.js similarity index 79% rename from src/Utils.js rename to src/Helpers.js index 4b1a62e73e..10944f4ecd 100644 --- a/src/Utils.js +++ b/src/Helpers.js @@ -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; }, @@ -69,7 +64,7 @@ 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); } @@ -77,7 +72,7 @@ const Utils = { // 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); } @@ -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()}`; }, @@ -130,5 +115,3 @@ const Utils = { } }; - -export default Utils; diff --git a/test/Utils.js b/test/Helpers.js similarity index 65% rename from test/Utils.js rename to test/Helpers.js index 4ee44e09ab..a8c3d462c7 100644 --- a/test/Utils.js +++ b/test/Helpers.js @@ -1,26 +1,18 @@ import { expect } from "chai"; -import Utils from "../src/Utils"; +import Helpers from "../src/Helpers"; -describe("Utils", () => { - - describe("clone", () => { - it("should clone a date", () => { - const date = new Date(); - const newDate = Utils.clone(date); - expect(newDate).to.not.equal(date); - }); - }); +describe("Helpers", () => { describe("addMonths", () => { it("adds a month", () => { const date = new Date(); - const newDate = Utils.addMonths(date, 1); + const newDate = Helpers.addMonths(date, 1); expect(newDate.getMonth()).to.equal(date.getMonth() + 1); }); it("should remove two months", () => { const date = new Date(); - const newDate = Utils.addMonths(date, -2); + const newDate = Helpers.addMonths(date, -2); expect(newDate.getMonth()).to.equal(date.getMonth() - 2); }); }); @@ -28,7 +20,7 @@ describe("Utils", () => { describe("startOfMonth", () => { it("should set a date as start of its month", () => { const date = new Date(1979, 8, 19); - const newDate = Utils.startOfMonth(date); + const newDate = Helpers.startOfMonth(date); expect(newDate.getFullYear()).to.equal(1979); expect(newDate.getMonth()).to.equal(8); @@ -44,31 +36,31 @@ describe("Utils", () => { describe("getFirstDayOfMonth", () => { it("get the first day of the month", () => { const date1 = new Date(1979, 8, 19); - expect(Utils.getFirstDayOfMonth(date1).getDate()).to.equal(1); + expect(Helpers.getFirstDayOfMonth(date1).getDate()).to.equal(1); const date2 = new Date(1979, 8, 1); - expect(Utils.getFirstDayOfMonth(date2).getDate()).to.equal(1); + expect(Helpers.getFirstDayOfMonth(date2).getDate()).to.equal(1); }); }); describe("getDaysInMonth", () => { it("get the correct number of days", () => { const date = new Date(2015, 1, 10); - expect(Utils.getDaysInMonth(date)).to.equal(28); + expect(Helpers.getDaysInMonth(date)).to.equal(28); const date1 = new Date(2016, 2, 10); - expect(Utils.getDaysInMonth(date1)).to.equal(31); + expect(Helpers.getDaysInMonth(date1)).to.equal(31); const date2 = new Date(2016, 3, 10); - expect(Utils.getDaysInMonth(date2)).to.equal(30); + expect(Helpers.getDaysInMonth(date2)).to.equal(30); }); it("get the correct number of days in a leap month", () => { const date = new Date(2016, 1, 10); - expect(Utils.getDaysInMonth(date)).to.equal(29); + expect(Helpers.getDaysInMonth(date)).to.equal(29); }); }); describe("getWeekArray", () => { it("works with a month starting on sunday (en)", () => { - const weeks = Utils.getWeekArray(new Date(2015, 10, 1)); + const weeks = Helpers.getWeekArray(new Date(2015, 10, 1)); expect(weeks).to.have.length(5); expect(weeks[0][0].getDay()).to.equal(0); expect(weeks[0][0].getDate()).to.equal(1); @@ -77,7 +69,7 @@ describe("Utils", () => { }); it("adds days from the previous month to the first week (en)", () => { - const weeks = Utils.getWeekArray(new Date(2015, 4, 10)); + const weeks = Helpers.getWeekArray(new Date(2015, 4, 10)); expect(weeks).to.have.length(6); @@ -90,7 +82,7 @@ describe("Utils", () => { }); it("adds days from the next month to the last week (en)", () => { - const weeks = Utils.getWeekArray(new Date(2015, 8, 19)); + const weeks = Helpers.getWeekArray(new Date(2015, 8, 19)); expect(weeks).to.have.length(5); // go to october @@ -101,7 +93,7 @@ describe("Utils", () => { }); it("adds days from the next month to the last week (it)", () => { - const weeks = Utils.getWeekArray(new Date(2015, 8, 19), 1); + const weeks = Helpers.getWeekArray(new Date(2015, 8, 19), 1); expect(weeks).to.have.length(5); @@ -112,7 +104,7 @@ describe("Utils", () => { }); it("returns 7 days per week when starting day is sunday", () => { - const weeks = Utils.getWeekArray(new Date(2015, 6, 1)); + const weeks = Helpers.getWeekArray(new Date(2015, 6, 1)); expect(weeks).to.have.length(5); weeks.forEach((week) => { expect(week).to.have.length(7); @@ -120,7 +112,7 @@ describe("Utils", () => { }); it("returns 7 days per week when starting day is monday", () => { - const weeks = Utils.getWeekArray(new Date(2015, 6, 1), 1); + const weeks = Helpers.getWeekArray(new Date(2015, 6, 1), 1); expect(weeks).to.have.length(5); weeks.forEach((week) => { expect(week).to.have.length(7); @@ -128,7 +120,7 @@ describe("Utils", () => { }); it("returns 7 days per week when starting day is saturday", () => { - const weeks = Utils.getWeekArray(new Date(2015, 6, 1), 6); + const weeks = Helpers.getWeekArray(new Date(2015, 6, 1), 6); weeks.forEach((week) => { expect(week).to.have.length(7); }); @@ -145,74 +137,47 @@ describe("Utils", () => { no() { return false; }, maybe(d) { return d.getMonth() === 8; } }; - let modifiers = Utils.getModifiersForDay(new Date(2015, 8, 19), modifierFunctions); + let modifiers = Helpers.getModifiersForDay(new Date(2015, 8, 19), modifierFunctions); expect(modifiers).to.have.length(2); expect(modifiers.indexOf("yes")).to.be.above(-1); expect(modifiers.indexOf("maybe")).to.be.above(-1); expect(modifiers.indexOf("no")).to.equal(-1); - modifiers = Utils.getModifiersForDay(new Date(2015, 9, 19), modifierFunctions); + modifiers = Helpers.getModifiersForDay(new Date(2015, 9, 19), modifierFunctions); expect(modifiers).to.have.length(1); expect(modifiers.indexOf("yes")).to.be.above(-1); expect(modifiers.indexOf("maybe")).to.equal(-1); expect(modifiers.indexOf("no")).to.equal(-1); }); it("works without passing modifiers", () => { - const modifiers = Utils.getModifiersForDay(new Date(2015, 8, 19)); + const modifiers = Helpers.getModifiersForDay(new Date(2015, 8, 19)); expect(modifiers).to.have.length(0); }); }); - describe("isDayOutsideMonth", () => { - it("detects days outside a month", () => { - const isOutside = Utils.isDayOutsideMonth(new Date(2015, 10, 30), new Date(2015, 11, 1)); - expect(isOutside).to.be.true; - }); - it("does detect days inside a month", () => { - const isOutside = Utils.isDayOutsideMonth(new Date(2015, 11, 30), new Date(2015, 11, 1)); - expect(isOutside).to.be.false; - }); - }); - - describe("isSameDay", () => { - it("returns true if two days differ only by time", () => { - const day1 = new Date(2015, 10, 11, 5, 25); - const day2 = new Date(2015, 10, 11, 7, 40); - const isSameDay = Utils.isSameDay(day1, day2); - expect(isSameDay).to.be.true; - }); - it("returns false for different days", () => { - const day1 = new Date(2015, 8, 12); - const day2 = new Date(2015, 5, 11); - const isSameDay = Utils.isSameDay(day1, day2); - expect(isSameDay).to.be.false; - }); - }); - - describe("formatMonthTitle", () => { it("returns month and day as string", () => { const date = new Date(2015, 11, 20); - const formattedDate = Utils.formatMonthTitle(date); + const formattedDate = Helpers.formatMonthTitle(date); expect(formattedDate).to.equal("December 2015"); }); }); describe("formatWeekdayShort", () => { it("returns the short day name as string", () => { - expect(Utils.formatWeekdayShort(0)).to.equal("Su"); + expect(Helpers.formatWeekdayShort(0)).to.equal("Su"); }); }); describe("formatWeekdayLong", () => { it("returns the long day name as string", () => { - expect(Utils.formatWeekdayLong(0)).to.equal("Sunday"); + expect(Helpers.formatWeekdayLong(0)).to.equal("Sunday"); }); }); describe("getFirstDayOfWeek", () => { it("returns sunday", () => { - expect(Utils.getFirstDayOfWeek()).to.equal(0); + expect(Helpers.getFirstDayOfWeek()).to.equal(0); }); }); @@ -220,27 +185,27 @@ describe("Utils", () => { it("returns a positive difference between two days in the same year", () => { const d1 = new Date(2015, 10, 6); const d2 = new Date(2015, 11, 6); - expect(Utils.getMonthsDiff(d1, d2)).to.equal(1); + expect(Helpers.getMonthsDiff(d1, d2)).to.equal(1); }); it("returns a positive difference between two days in different years", () => { const d1 = new Date(2015, 11, 6); const d2 = new Date(2016, 0, 6); - expect(Utils.getMonthsDiff(d1, d2)).to.equal(1); + expect(Helpers.getMonthsDiff(d1, d2)).to.equal(1); }); it("returns a negative difference between two days in the same year", () => { const d1 = new Date(2015, 3, 6); const d2 = new Date(2015, 2, 6); - expect(Utils.getMonthsDiff(d1, d2)).to.equal(-1); + expect(Helpers.getMonthsDiff(d1, d2)).to.equal(-1); }); it("returns a negative difference between two days in different years", () => { const d1 = new Date(2017, 3, 6); const d2 = new Date(2015, 2, 6); - expect(Utils.getMonthsDiff(d1, d2)).to.equal(-25); + expect(Helpers.getMonthsDiff(d1, d2)).to.equal(-25); }); it("returns no difference between two days in the same month", () => { const d1 = new Date(2015, 3, 6); const d2 = new Date(2015, 3, 12); - expect(Utils.getMonthsDiff(d1, d2)).to.equal(0); + expect(Helpers.getMonthsDiff(d1, d2)).to.equal(0); }); });