calendar-base
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,1 +0,1 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.calendarBase=f()}})(function(){var define,module,exports;module={exports:exports={}};function Calendar(options){options=options||{};this.startDate=options.startDate;this.endDate=options.endDate;this.maxInterval=options.maxInterval;this.maxConstraint=options.maxConstraint;this.siblingMonths=options.siblingMonths;this.weekNumbers=options.weekNumbers;this.weekStart=options.weekStart;if(this.weekStart===undefined){this.weekStart=0}this.date=new Date(1986,9,14,0,0,0)}Calendar.prototype.getCalendar=function(year,month){this.date.setUTCFullYear(year);this.date.setUTCMonth(month);this.date.setUTCDate(1);var calendar=[],firstDay=this.date.getUTCDay(),firstDate=-((7-this.weekStart+firstDay)%7),lastDate=Calendar.daysInMonth(year,month),lastDay=(lastDate-firstDate)%7,lastDayLastMonth=Calendar.daysInMonth(year,month-1),i=firstDate,max=lastDate-i+(lastDay!=0?7-lastDay:0)+firstDate,currentDay,currentDate,currentDateObject,currentWeekNumber=null,otherMonth,otherYear;while(i<max){currentDate=i+1;currentDay=((i<1?7+i:i)+firstDay)%7;if(currentDate<1||currentDate>lastDate){if(this.siblingMonths){if(currentDate<1){otherMonth=month-1;otherYear=year;if(otherMonth<0){otherMonth=11;otherYear--}currentDate=lastDayLastMonth+currentDate}else if(currentDate>lastDate){otherMonth=month+1;otherYear=year;if(otherMonth>11){otherMonth=0;otherYear++}currentDate=i-lastDate+1}currentDateObject={day:currentDate,weekDay:currentDay,month:otherMonth,year:otherYear,siblingMonth:true}}else{currentDateObject=false}}else{currentDateObject={day:currentDate,weekDay:currentDay,month:month,year:year}}if(currentDateObject&&this.weekNumbers){if(currentWeekNumber===null){currentWeekNumber=Calendar.calculateWeekNumber(currentDateObject)}else if(currentDay==1&¤tWeekNumber==52){currentWeekNumber=1}else if(currentDay==1){currentWeekNumber++}currentDateObject.weekNumber=currentWeekNumber}if(currentDateObject&&this.startDate){currentDateObject.selected=this.isDateSelected(currentDateObject)}calendar.push(currentDateObject);i++}return calendar};Calendar.prototype.isDateSelected=function(date){if(date.year==this.startDate.year&&date.month==this.startDate.month&&date.day==this.startDate.day){return true}else if(this.endDate){if(date.year==this.startDate.year&&date.month==this.startDate.month&&date.day<this.startDate.day){return false}else if(date.year==this.endDate.year&&date.month==this.endDate.month&&date.day>this.endDate.day){return false}else if(date.year==this.startDate.year&&date.month<this.startDate.month){return false}else if(date.year==this.endDate.year&&date.month>this.endDate.month){return false}else if(date.year<this.startDate.year){return false}else if(date.year>this.endDate.year){return false}return true}return false};Calendar.prototype.setStartDate=function(date){this.startDate=date};Calendar.prototype.setEndDate=function(date){this.endDate=date};Calendar.prototype.setDate=Calendar.prototype.setStartDate;Calendar.interval=function(date1,date2){var oDate1=new Date(1986,9,14,0,0,0),oDate2=new Date(1986,9,14,0,0,0);oDate1.setUTCFullYear(date1.year);oDate1.setUTCMonth(date1.month);oDate1.setUTCDate(date1.day);oDate2.setUTCFullYear(date2.year);oDate2.setUTCMonth(date2.month);oDate2.setUTCDate(date2.day);return Math.abs(Math.ceil((oDate2.getTime()-oDate1.getTime())/864e5))+1};Calendar.daysInMonth=function(year,month){return new Date(year,month+1,0).getDate()};Calendar.isLeapYear=function(year){return year%4==0&&year%100!=0||year%400==0};Calendar.calculateWeekNumber=function(date){var current=new Date(1986,9,14,0,0,0);current.setUTCFullYear(date.year);current.setUTCMonth(date.month);current.setUTCDate(date.day);var target=new Date(current.valueOf());var dayNr=(current.getUTCDay()+6)%7;target.setUTCDate(target.getUTCDate()-dayNr+3);var firstThursday=target.valueOf();target.setUTCMonth(0,1);if(target.getUTCDay()!=4){target.setUTCMonth(0,1+(4-target.getUTCDay()+7)%7)}return 1+Math.ceil((firstThursday-target)/6048e5)};module.exports={Calendar:Calendar};return module.exports}); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.calendarBase=f()}})(function(){var define,module,exports;module={exports:exports={}};function Calendar(options){options=options||{};this.startDate=options.startDate;this.endDate=options.endDate;this.siblingMonths=options.siblingMonths;this.weekNumbers=options.weekNumbers;this.weekStart=options.weekStart;if(this.weekStart===undefined){this.weekStart=0}this.date=new Date(1986,9,14,0,0,0)}Calendar.prototype.getCalendar=function(year,month){this.date.setUTCFullYear(year);this.date.setUTCMonth(month);this.date.setUTCDate(1);var calendar=[],firstDay=this.date.getUTCDay(),firstDate=-((7-this.weekStart+firstDay)%7),lastDate=Calendar.daysInMonth(year,month),lastDay=(lastDate-firstDate)%7,lastDatePreviousMonth=Calendar.daysInMonth(year,month-1),i=firstDate,max=lastDate-i+(lastDay!=0?7-lastDay:0)+firstDate,currentDay,currentDate,currentDateObject,currentWeekNumber=null,otherMonth,otherYear;while(i<max){currentDate=i+1;currentDay=((i<1?7+i:i)+firstDay)%7;if(currentDate<1||currentDate>lastDate){if(this.siblingMonths){if(currentDate<1){otherMonth=month-1;otherYear=year;if(otherMonth<0){otherMonth=11;otherYear--}currentDate=lastDatePreviousMonth+currentDate}else if(currentDate>lastDate){otherMonth=month+1;otherYear=year;if(otherMonth>11){otherMonth=0;otherYear++}currentDate=i-lastDate+1}currentDateObject={day:currentDate,weekDay:currentDay,month:otherMonth,year:otherYear,siblingMonth:true}}else{currentDateObject=false}}else{currentDateObject={day:currentDate,weekDay:currentDay,month:month,year:year}}if(currentDateObject&&this.weekNumbers){if(currentWeekNumber===null){currentWeekNumber=Calendar.calculateWeekNumber(currentDateObject)}else if(currentDay==1&¤tWeekNumber==52){currentWeekNumber=1}else if(currentDay==1){currentWeekNumber++}currentDateObject.weekNumber=currentWeekNumber}if(currentDateObject&&this.startDate){currentDateObject.selected=this.isDateSelected(currentDateObject)}calendar.push(currentDateObject);i++}return calendar};Calendar.prototype.isDateSelected=function(date){if(date.year==this.startDate.year&&date.month==this.startDate.month&&date.day==this.startDate.day){return true}else if(this.endDate){if(date.year==this.startDate.year&&date.month==this.startDate.month&&date.day<this.startDate.day){return false}else if(date.year==this.endDate.year&&date.month==this.endDate.month&&date.day>this.endDate.day){return false}else if(date.year==this.startDate.year&&date.month<this.startDate.month){return false}else if(date.year==this.endDate.year&&date.month>this.endDate.month){return false}else if(date.year<this.startDate.year){return false}else if(date.year>this.endDate.year){return false}return true}return false};Calendar.prototype.setStartDate=function(date){this.startDate=date};Calendar.prototype.setEndDate=function(date){this.endDate=date};Calendar.prototype.setDate=Calendar.prototype.setStartDate;Calendar.diff=function(date1,date2){var oDate1=new Date(1986,9,14,0,0,0),oDate2=new Date(1986,9,14,0,0,0);oDate1.setUTCFullYear(date1.year);oDate1.setUTCMonth(date1.month);oDate1.setUTCDate(date1.day);oDate2.setUTCFullYear(date2.year);oDate2.setUTCMonth(date2.month);oDate2.setUTCDate(date2.day);return Math.ceil((oDate1.getTime()-oDate2.getTime())/864e5)};Calendar.interval=function(date1,date2){return Math.abs(Calendar.diff(date1,date2))+1};Calendar.daysInMonth=function(year,month){return new Date(year,month+1,0).getDate()};Calendar.isLeapYear=function(year){return year%4==0&&year%100!=0||year%400==0};Calendar.calculateWeekNumber=function(date){var current=new Date(1986,9,14,0,0,0);current.setUTCFullYear(date.year);current.setUTCMonth(date.month);current.setUTCDate(date.day);var target=new Date(current.valueOf());var dayNr=(current.getUTCDay()+6)%7;target.setUTCDate(target.getUTCDate()-dayNr+3);var firstThursday=target.valueOf();target.setUTCMonth(0,1);if(target.getUTCDay()!=4){target.setUTCMonth(0,1+(4-target.getUTCDay()+7)%7)}return 1+Math.ceil((firstThursday-target)/6048e5)};module.exports={Calendar:Calendar};return module.exports}); |
@@ -0,1 +1,11 @@ | ||
/** | ||
* Calendar constructor | ||
* | ||
* @param {Object} options Calendar options | ||
* @param {Object} options.startDate Date object indicating the selected start date | ||
* @param {Object} options.endDate Date object indicating the selected end date | ||
* @param {Boolean} options.siblingMonths Calculate dates from sibling months (before and after the current month, based on weekStart) | ||
* @param {Boolean} options.weekNumbers Caclulate the week days | ||
* @param {Number} options.weekStart Day of the week to start the calendar, respects `Date.prototype.getDay` (defaults to `0`, Sunday) | ||
*/ | ||
function Calendar (options) { | ||
@@ -6,4 +16,2 @@ options = options || {}; | ||
this.endDate = options.endDate; | ||
this.maxInterval = options.maxInterval; | ||
this.maxConstraint = options.maxConstraint; | ||
this.siblingMonths = options.siblingMonths; | ||
@@ -20,2 +28,9 @@ this.weekNumbers = options.weekNumbers; | ||
/** | ||
* Calculate a calendar month | ||
* | ||
* @param {Number} year Year | ||
* @param {Number} month Month [0-11] | ||
* @return {Array} Calendar days | ||
*/ | ||
Calendar.prototype.getCalendar = function (year, month) { | ||
@@ -28,6 +43,7 @@ this.date.setUTCFullYear(year); | ||
firstDay = this.date.getUTCDay(), | ||
firstDate = - (((7 - this.weekStart) + firstDay) % 7), | ||
lastDate = Calendar.daysInMonth(year, month), | ||
lastDay = ((lastDate - firstDate) % 7), | ||
lastDayLastMonth = Calendar.daysInMonth(year, month - 1), | ||
lastDatePreviousMonth = Calendar.daysInMonth(year, month - 1), | ||
i = firstDate, | ||
@@ -54,3 +70,3 @@ max = (lastDate - i) + (lastDay != 0 ? 7 - lastDay : 0) + firstDate, | ||
} | ||
currentDate = lastDayLastMonth + currentDate; | ||
currentDate = lastDatePreviousMonth + currentDate; | ||
} | ||
@@ -111,2 +127,8 @@ else if (currentDate > lastDate) { | ||
/** | ||
* Checks if a date is selected | ||
* | ||
* @param {Object} date Date object | ||
* @return {Boolean} Selected status of the date | ||
*/ | ||
Calendar.prototype.isDateSelected = function (date) { | ||
@@ -140,2 +162,7 @@ if (date.year == this.startDate.year && date.month == this.startDate.month && date.day == this.startDate.day) { | ||
/** | ||
* Sets the selected period start | ||
* | ||
* @param {Object} date Date object | ||
*/ | ||
Calendar.prototype.setStartDate = function (date) { | ||
@@ -145,2 +172,7 @@ this.startDate = date; | ||
/** | ||
* Sets the selected period end | ||
* | ||
* @param {Object} date Date object | ||
*/ | ||
Calendar.prototype.setEndDate = function (date) { | ||
@@ -150,5 +182,17 @@ this.endDate = date; | ||
/** | ||
* Sets one selected date | ||
* | ||
* @param {Object} date Date object | ||
*/ | ||
Calendar.prototype.setDate = Calendar.prototype.setStartDate; | ||
Calendar.interval = function (date1, date2) { | ||
/** | ||
* Calculates the difference between two dates (date1 - date2), in days | ||
* | ||
* @param {Object} date1 Date object | ||
* @param {Object} date2 Date object | ||
* @return {Number} Days between the dates | ||
*/ | ||
Calendar.diff = function (date1, date2) { | ||
var oDate1 = new Date(1986, 9, 14, 0, 0, 0), oDate2 = new Date(1986, 9, 14, 0, 0, 0); | ||
@@ -164,5 +208,23 @@ | ||
return Math.abs(Math.ceil((oDate2.getTime() - oDate1.getTime()) / 86400000)) + 1; | ||
return Math.ceil((oDate1.getTime() - oDate2.getTime()) / 86400000); | ||
}; | ||
/** | ||
* Calculates the interval between two dates | ||
* | ||
* @param {Object} date1 Date object | ||
* @param {Object} date2 Date object | ||
* @return {Number} Number of days | ||
*/ | ||
Calendar.interval = function (date1, date2) { | ||
return Math.abs(Calendar.diff(date1, date2)) + 1; | ||
}; | ||
/** | ||
* Calculates the number of days in a month | ||
* | ||
* @param {Number} year Year | ||
* @param {Number} month Month [0-11] | ||
* @return {Number} Length of the month | ||
*/ | ||
Calendar.daysInMonth = function (year, month) { | ||
@@ -172,2 +234,8 @@ return new Date(year, month + 1, 0).getDate(); | ||
/** | ||
* Calculates if a given year is a leap year | ||
* | ||
* @param {Number} year Year | ||
* @return {Boolean} Leap year or not | ||
*/ | ||
Calendar.isLeapYear = function (year) { | ||
@@ -177,2 +245,8 @@ return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); | ||
/** | ||
* Calculates the week number for a given date | ||
* | ||
* @param {Object} date Date object | ||
* @return {Number} Week number | ||
*/ | ||
// Adapted from http://techblog.procurios.nl/k/news/view/33796/14863/calculate-iso-8601-week-and-year-in-javascript.html | ||
@@ -215,2 +289,5 @@ Calendar.calculateWeekNumber = function (date) { | ||
/** | ||
* Exports the Calendar | ||
*/ | ||
module.exports = { Calendar: Calendar }; |
{ | ||
"name": "calendar-base", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Base methods for generating calendars using JavaScript.", | ||
@@ -5,0 +5,0 @@ "keywords": [ "calendar", "generation", "base", "core" ], |
@@ -64,2 +64,12 @@ # Calendar Base [![Build Status](https://travis-ci.org/WesleydeSouza/calendar-base.svg?branch=master)](https://travis-ci.org/WesleydeSouza/calendar-base) | ||
#### `Calendar.diff(dateOne, dateTwo)` | ||
Returns the difference in days between `dateOne` and `dateTwo` as a `Number`. | ||
```js | ||
> Calendar.diff({ year: 2010, month: 0, day: 1 }, { year: 2010, month: 0, day: 10 }); | ||
-9 | ||
``` | ||
#### `Calendar.interval(dateOne, dateTwo)` | ||
@@ -71,3 +81,3 @@ | ||
> Calendar.interval({ year: 2010, month: 0, day: 1 }, { year: 2010, month: 0, day: 10 }); | ||
11 | ||
10 | ||
``` | ||
@@ -74,0 +84,0 @@ |
@@ -119,2 +119,8 @@ var assert = require('assert'), | ||
it('should return valid date diffs', function (done) { | ||
assert.equal(Calendar.diff({ year: 2016, month: 2, day: 1 }, { year: 2016, month: 1, day: 1 }), 29); | ||
assert.equal(Calendar.diff({ year: 2010, month: 0, day: 1 }, { year: 2011, month: 0, day: 1 }), -365); | ||
done(); | ||
}); | ||
it('should return valid date intervals', function (done) { | ||
@@ -121,0 +127,0 @@ assert.equal(Calendar.interval({ year: 1986, month: 1, day: 16 }, { year: 1986, month: 1, day: 16 }), 1); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37355
495
172
0