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

Add wholeMonths interval to weeksOfMonthByDay #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ var valentines = moment.recur().every(14).daysOfMonth()
// The following matches every 1st and 3rd Thursday of the month.
cal = moment.recur().every("Thursday").daysOfWeek()
.every([0, 2]).weeksOfMonthByDay();

// A wholeMonth interval is available for combining with
// the daysOfWeek and weekOfMonthByDay to achieve "nth weekday every nth months" recurrences.
// The following matches every 1st and 3rd Thursday every 2 months.
cal = moment.recur().every("Thursday").daysOfWeek()
.every([0, 2]).weeksOfMonthByDay()
.every(2).wholeMonths();
```


Expand Down
29 changes: 26 additions & 3 deletions moment-recur.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
}
}

if (measure !== 'wholeMonths') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation seems wrong.

measure = measure.toLowerCase();
}

return {
measure: measure.toLowerCase(),
measure: measure,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really like to know why this was toLowerCase() in the first place.

units: units
};
}
Expand All @@ -37,10 +41,20 @@
// Get the difference between the start date and the provided date,
// using the required measure based on the type of rule'
var diff = null;

// if the type is wholeMonths use the whole month
if( date.isBefore(start) ) {
diff = start.diff(date, type, true);
if( type == 'wholeMonths' ) {
diff = start.diff(date, 'month');
} else {
diff = start.diff(date, type, true);
}
} else {
diff = date.diff(start, type, true);
if( type == 'wholeMonths' ) {
diff = date.diff(start, 'month');
} else {
diff = date.diff(start, type, true);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be a function. You've repeated yourself twice.

}
if( type == 'days') {
// if we are dealing with days, we deal with whole days only.
Expand Down Expand Up @@ -184,6 +198,7 @@
"days": "interval",
"weeks": "interval",
"months": "interval",
"wholeMonths": "interval",
"years": "interval",
"daysOfWeek": "calendar",
"daysOfMonth": "calendar",
Expand All @@ -198,6 +213,7 @@
"days": "day",
"weeks": "week",
"months": "month",
"wholeMonths": "wholeMonth",
"years": "year",
"daysOfWeek": "dayOfWeek",
"daysOfMonth": "dayOfMonth",
Expand Down Expand Up @@ -252,6 +268,10 @@
if (rule.measure === 'weeksOfMonthByDay' && !this.hasRule('daysOfWeek')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we also have measure weeksOfMonthByDay so that 'toLowercase' rule seems a bit arbitrary and bogus.

throw Error("weeksOfMonthByDay must be combined with daysOfWeek");
}

if (rule.measure === 'wholeMonths' && (!this.hasRule('daysOfWeek') || !this.hasRule('weeksOfMonthByDay'))) {
throw Error("wholeMonths must be combined with weeksOfMonthByDay and daysOfWeek");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the stuff in line 252 needs to go into some kind of validator function. This function itself is very overstuffed.


// Remove existing rule based on measure
for (var i = 0; i < this.rules.length; i++) {
Expand Down Expand Up @@ -380,6 +400,9 @@

case "month":
return "months";

case "wholeMonth":
return "wholeMonths";

case "year":
return "years";
Expand Down
2 changes: 1 addition & 1 deletion moment-recur.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading