calendar-month-array
Advanced tools
Comparing version 2.0.3 to 2.1.0
104
index.js
@@ -1,69 +0,43 @@ | ||
/** | ||
* Returns an array of weeks, each containing an array of dates | ||
* | ||
* date: Date | ||
* opts: object { | ||
* weekStartDay: int, | ||
* formatDate: function(currentDate: Date, info: Object { | ||
* dayOfMonth: int, | ||
* siblingMonth: int, | ||
* week: int, | ||
* position: int | ||
* }) | ||
* formatSiblingMonthDate: function(currentDate: Date, info: Object) | ||
* formatHeader: function(currentDate: Date, position: int) | ||
* } | ||
*/ | ||
module.exports = function (date, opts) { | ||
if (typeof date === 'string') date = new Date(date) | ||
if (!date) date = new Date() | ||
if (Object.prototype.toString.call(date) !== '[object Date]') { | ||
opts = date | ||
date = new Date() | ||
module.exports = function (monthDate, opts) { | ||
if (typeof monthDate === 'string') monthDate = new Date(monthDate) | ||
if (!monthDate) monthDate = new Date() | ||
if (Object.prototype.toString.call(monthDate) !== '[object Date]') { | ||
opts = monthDate | ||
monthDate = new Date() | ||
} | ||
if (!opts) opts = {} | ||
var formatDate = opts.formatDate || function (date) { return date } | ||
var formatSiblingMonthDate = opts.formatSiblingMonthDate || formatDate | ||
var weekStartDay = opts.weekStartDay || 0 | ||
var first = new Date(date) | ||
first.setHours(0) | ||
first.setMinutes(0) | ||
first.setSeconds(0) | ||
first.setDate(1) | ||
var first = new Date(monthDate.getYear(), monthDate.getMonth(), 1) | ||
var last = new Date(monthDate.getYear(), monthDate.getMonth() + 1, 0) | ||
var last = new Date(date) | ||
last.setHours(0) | ||
last.setMinutes(0) | ||
last.setSeconds(0) | ||
last.setMonth(last.getMonth()+1) | ||
last.setDate(0) | ||
var monthFirstDayPosition = (7 + first.getDay() - weekStartDay) % 7 | ||
var calendarLastDay = last.getDate() + monthFirstDayPosition | ||
var weeks = Math.ceil((calendarLastDay + monthFirstDayPosition) / 7) | ||
var lines = new Array(weeks) | ||
var headers = new Array(7) | ||
var dayOfMonth = 1 - monthFirstDayPosition | ||
var weekStartDay = opts.weekStartDay || 0 | ||
var formatDate = opts.formatDate || function (date) { return date } | ||
var formatSiblingMonthDate = opts.formatSiblingMonthDate || formatDate | ||
var firstDaysToComplete = (7 + first.getDay() - weekStartDay) % 7 | ||
var day = 1 - firstDaysToComplete | ||
var weeks = Math.ceil((last.getDate() + firstDaysToComplete) / 7) | ||
var lines = [] | ||
var headers = [] | ||
for (var w = 0; w < weeks; w++) { | ||
var row = [] | ||
var row = new Array(7) | ||
for (var d = 0; d < 7; d++) { | ||
var currentDay = day + d | ||
var currentDate = createDateOffset(first, currentDay) | ||
var siblingMonth = checkSiblingMonth(currentDay) | ||
var date = offsetDate(first, dayOfMonth) | ||
var siblingMonth = checkSiblingMonth(dayOfMonth, last.getDate()) | ||
var format = siblingMonth ? formatSiblingMonthDate : formatDate | ||
row.push(format(currentDate, { | ||
dayOfMonth: currentDay, | ||
row[d] = format(date, { | ||
dayOfMonth: dayOfMonth, | ||
siblingMonth: siblingMonth, | ||
week: w, | ||
position: d | ||
})) | ||
}) | ||
if(opts.formatHeader && w === 0) | ||
headers.push(opts.formatHeader(currentDate, d)) | ||
headers[d] = opts.formatHeader(date, d) | ||
dayOfMonth++ | ||
} | ||
day += 7 | ||
lines.push(row) | ||
lines[w] = row | ||
} | ||
@@ -75,14 +49,16 @@ | ||
return lines | ||
function createDateOffset(date, offset) { | ||
var newDate = new Date(date) | ||
newDate.setDate(offset) | ||
return newDate | ||
} | ||
function checkSiblingMonth(day) { | ||
return day <= 0 ? -1 : | ||
day > last.getDate() ? 1 : | ||
0 | ||
} | ||
} | ||
function checkSiblingMonth(day, lastDay) { | ||
return day <= 0 ? -1 : | ||
day > lastDay ? 1 : | ||
0 | ||
} | ||
function offsetDate(date, offset) { | ||
var newDate = new Date(date) | ||
newDate.setDate(offset) | ||
return newDate | ||
} | ||
{ | ||
"name": "calendar-month-array", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Generates a calendar array with the days of a single month organized by week", | ||
@@ -17,3 +17,3 @@ "main": "index.js", | ||
"sprintf": "^0.1.5", | ||
"tape": "^4.7.0" | ||
"tape": "^4.9.1" | ||
}, | ||
@@ -20,0 +20,0 @@ "repository": { |
@@ -6,3 +6,3 @@ # calendar-month-array | ||
This is a fork of module [calendar-month-string](https://github.com/substack/calendar-month-string) by Substack. | ||
The goal is to return a basic date array in place of a formatted string. | ||
The goal is to return dates arrays in place of a formatted string. | ||
@@ -33,3 +33,3 @@ | ||
NOTE: event if you can't see it on the console, they are `Date` objects, not ISO date strings! | ||
NOTE: these are`Date`objects, not ISO date strings! | ||
@@ -79,7 +79,7 @@ | ||
* `info.week` - week index, starting at 0 for first week presented. | ||
* `info.position` - position index in week, starting at 0 for first day of week (does not depend on `opts.weekStartDay`). | ||
* `opts.formatSiblingMonthDate(currentDate, info)` - specify how to format cells for sibling month, default to `opts.formatDate`. | ||
* `opts.formatHeader(currentDate, position)` - specify how to format an optional header row (added only if `opts.formatHeader` is defined) | ||
* `currentDate` - date object similar to first row of days. | ||
* `position` - position index in week. | ||
* `info.position` - day index, or position in week, startting at 0. | ||
* `opts.formatSiblingMonthDate(currentDate, info)` - define how to format cells for sibling month, default to `opts.formatDate`. | ||
* `opts.formatHeader(currentDate, position)` - define how to format an optional header row (added only if `opts.formatHeader` is defined) | ||
* `currentDate` - date object, just to know the day of week. | ||
* `position` - position in week. | ||
@@ -102,4 +102,2 @@ | ||
- https://github.com/substack/calendar-month-string Original work | ||
- https://github.com/kiltjs/month A list of days in a month | ||
- https://github.com/spatie/calendar-months similar work, requires moment.js | ||
- https://github.com/diegoteixeir4/purecalendar.js | ||
- https://github.com/krazylek/calendar-month-view A fully customizable html calendar |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11156
8
173
100