json-calendar
Advanced tools
Comparing version 1.3.1 to 1.4.0
107
index.js
@@ -29,20 +29,8 @@ "use strict"; | ||
class Calendar { | ||
constructor(params) { | ||
params = params || {}; | ||
constructor(params = {}) { | ||
const now = params.today || new Date(); | ||
const today = params.today || new Date(); | ||
this.monthNames = MONTHNAMES; | ||
this.weeks = []; | ||
this.today = new Date( | ||
today.getFullYear(), | ||
today.getMonth(), | ||
today.getDate() | ||
); | ||
var data = {}; | ||
var defaults = { | ||
year: this.today.getFullYear(), | ||
monthIndex: this.today.getMonth(), | ||
const defaults = { | ||
year: now.getFullYear(), | ||
monthIndex: now.getMonth(), | ||
abbreviate: 2, | ||
@@ -52,14 +40,16 @@ firstDayOfWeek: 0, | ||
previousMonth: " ", | ||
nextMonth: " ", | ||
otherMonthClass: "month-other" | ||
nextMonth: " " | ||
}; | ||
var options = Object.assign({}, defaults, params); | ||
this.options = Object.assign({}, defaults, params); | ||
var classNames, date, day, firstDate, lastDate, monthDays, week; | ||
this.monthNames = MONTHNAMES; | ||
firstDate = new Date(options.year, options.monthIndex, 1); | ||
lastDate = new Date(options.year, options.monthIndex + 1, 0); | ||
this.today = this.createDate( | ||
now.getFullYear(), | ||
now.getMonth(), | ||
now.getDate() | ||
); | ||
monthDays = lastDate.getDate(); | ||
var data = {}; | ||
@@ -70,15 +60,40 @@ // Start storing the JSON data into an object. | ||
// Abbreviate the day names when configured to do so. | ||
for (var i = 0, len = DAYNAMES.length; i < len; i++) { | ||
var dayName = DAYNAMES[i]; | ||
var dayAbbr = dayName.substr(0, options.abbreviate); | ||
for (var index = 0, len = DAYNAMES.length; index < len; index++) { | ||
var dayName = DAYNAMES[index]; | ||
var dayAbbr = dayName.substr(0, this.options.abbreviate); | ||
if (dayAbbr !== dayName) { | ||
data.dayNames[i] = { name: dayName, abbr: dayAbbr }; | ||
data.dayNames[index] = { name: dayName, abbr: dayAbbr }; | ||
} else { | ||
data.dayNames[i] = { name: dayName }; | ||
data.dayNames[index] = { name: dayName }; | ||
} | ||
} | ||
i = 1; | ||
this.buildWeeksArray(); | ||
// data.nextMonth = this.getRelativeMonth(firstDate, 1); | ||
// data.previousMonth = this.getRelativeMonth(firstDate, -1); | ||
// data.currentMonth = this.getMonthName(firstDate.getMonth()); | ||
this.data = data; | ||
} | ||
addDaysToDate(date, offset) { | ||
return new Date(date.getTime() + offset * 24 * 60 * 60 * 1000); | ||
} | ||
buildWeeksArray() { | ||
var classNames; | ||
var date; | ||
var day; | ||
var i = 1; | ||
var week; | ||
var options = this.options; | ||
this.weeks = []; | ||
var firstDate = this.createDate(options.year, options.monthIndex, 1); | ||
var monthDays = this.getDaysInMonth(options.year, options.monthIndex); | ||
var firstDateIndex = firstDate.getDay(); | ||
// Loop through week indexes (0..6) | ||
@@ -93,12 +108,16 @@ for (var w = 0; w < 6; w++) { | ||
if (w === 0 && d < firstDate.getDay()) { | ||
if (w === 0 && d < firstDateIndex) { | ||
// Day of Previous Month | ||
date = new Date( | ||
date = this.createDate( | ||
firstDate.getFullYear(), | ||
firstDate.getMonth(), | ||
-(6 - d) + 1 | ||
1 - (firstDateIndex - d) | ||
); | ||
} else if (i > monthDays) { | ||
// Day of Next Month | ||
date = new Date(firstDate.getFullYear(), firstDate.getMonth(), i); | ||
date = this.createDate( | ||
firstDate.getFullYear(), | ||
firstDate.getMonth(), | ||
i | ||
); | ||
i += 1; | ||
@@ -108,3 +127,7 @@ } else { | ||
classNames.push("month-day"); | ||
date = new Date(firstDate.getFullYear(), firstDate.getMonth(), i); | ||
date = this.createDate( | ||
firstDate.getFullYear(), | ||
firstDate.getMonth(), | ||
i | ||
); | ||
@@ -143,12 +166,12 @@ i += 1; | ||
} | ||
} | ||
data.nextMonth = this.getRelativeMonth(firstDate, 1); | ||
data.previousMonth = this.getRelativeMonth(firstDate, -1); | ||
data.currentMonth = this.getMonthName(firstDate.getMonth()); | ||
this.data = data; | ||
changeMonth(year, monthIndex) { | ||
this.options.year = year; | ||
this.options.monthIndex = monthIndex; | ||
this.buildWeeksArray(); | ||
} | ||
addDaysToDate(date, offset) { | ||
return new Date(date.getTime() + offset * 24 * 60 * 60 * 1000); | ||
createDate(yr, mo, day) { | ||
return new Date(yr, mo, day, 0, 0); | ||
} | ||
@@ -155,0 +178,0 @@ |
{ | ||
"name": "json-calendar", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "A data model for displaying dates and date ranges on a calendar interface.", | ||
@@ -20,3 +20,2 @@ "keywords": [ | ||
"scripts": { | ||
"start": "echo \"Error: Need to add start script\" && exit 1", | ||
"lint": "eslint .", | ||
@@ -23,0 +22,0 @@ "test": "TZ=America/Phoenix jest", |
28
test.js
@@ -22,3 +22,3 @@ var JsonCalendar = require("./"); | ||
test("has given date", () => { | ||
test("uses given today param", () => { | ||
var today = new Date(2018, 12, 31, 0, 0); | ||
@@ -41,2 +41,28 @@ var calendar = new JsonCalendar({ today }); | ||
test("displays October 2018 correctly", () => { | ||
var today = new Date(2018, 12, 1, 0, 0); | ||
var calendar = new JsonCalendar({ year: 2018, monthIndex: 9, today }); | ||
expect(calendar.weeks[0][0].day).toBe(30); | ||
expect(calendar.weeks[0][0].date.getMonth()).toBe(8); | ||
expect(calendar.weeks[0][1].day).toBe(1); | ||
expect(calendar.weeks[0][1].date.getMonth()).toBe(9); | ||
}); | ||
test("add days to date", () => { | ||
const date = new Date(2018, 8, 29, 0, 0); | ||
expect(subject.addDaysToDate(date, 0).getDate()).toBe(date.getDate()); | ||
expect(subject.addDaysToDate(date, -3).getDate()).toBe(26); | ||
expect(subject.addDaysToDate(date, 10).getDate()).toBe(9); | ||
}); | ||
test("creates a date without time", () => { | ||
var date = subject.createDate(2018, 11, 32, 0, 0); | ||
expect(date instanceof Date).toBe(true); | ||
expect(date.getFullYear()).toBe(2019); | ||
expect(date.getMonth()).toBe(0); | ||
expect(date.getDate()).toBe(1); | ||
expect(date.getMinutes()).toBe(0); | ||
expect(date.getHours()).toBe(0); | ||
}); | ||
test("get days in month", () => { | ||
@@ -43,0 +69,0 @@ expect(subject.getDaysInMonth(2018, 4)).toBe(31); |
Sorry, the diff of this file is too big to display
223184
6360