Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to years(), months(), weeks(), days() #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@ Calendar.prototype = new (function() {
{
return this.events.length;
};


/**
* Performs callback on the each of the range.by iterator items
* @param {string} interval "week", "day", "year", etc..
* @param {DateRange} range DateRange object from moment-range.js
* @param {function} callback callback with moment as argument
*/
this.byFunction = function (interval, range, callback) {
var ranges = range.by(interval);
for (start of ranges) {
callback(start);
}
};

/**
* Returns a calendar within a given year
Expand All @@ -394,7 +408,7 @@ Calendar.prototype = new (function() {
var end = this.end.clone().startOf('year');
var range = moment().range(start, end);

range.by('years', function(start) {
this.byFunction('years', range, function(start) {
var end = start.clone().endOf('year');
years.push(self.findInRange(start, end));
});
Expand Down Expand Up @@ -424,7 +438,7 @@ Calendar.prototype = new (function() {
var end = this.end.clone().startOf('month');
var range = moment().range(start, end);

range.by('months', function(start) {
this.byFunction('months', range, function(start) {
var end = start.clone().endOf('month');
months.push(self.findInRange(start, end));
});
Expand Down Expand Up @@ -454,7 +468,7 @@ Calendar.prototype = new (function() {
var end = this.end.clone().startOf('week');
var range = moment().range(start, end);

range.by('weeks', function(start) {
this.byFunction(('weeks', range, function(start) {
var end = start.clone().endOf('week');
weeks.push(self.findInRange(start, end));
});
Expand Down Expand Up @@ -484,7 +498,7 @@ Calendar.prototype = new (function() {
var end = this.end.clone().startOf('day');
var range = moment().range(start, end);

range.by('days', function(start) {
this.byFunction('days', range, function(start) {
var end = start.clone().endOf('day');
days.push(self.findInRange(start, end));
});
Expand Down
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "0.0.4",
"description": "provides queryable calendar data structure",
"main": "index.js",
"scripts": {
"test": "jake test"
},
"repository": {
"type": "git",
"url": "[email protected]:der-On/moment-calendar.git"
Expand All @@ -21,10 +18,7 @@
},
"homepage": "https://github.com/der-On/moment-calendar",
"dependencies": {
"moment": "^2.8.3",
"moment-range": "^1.0.5"
},
"devDependencies": {
"jake": "^8.0.10"
"moment": "^2.20.1",
"moment-range": "^3.1.1"
}
}