Skip to content

Commit

Permalink
bug fixes, contains method, updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mooman committed May 12, 2010
1 parent 4c662bb commit 954c636
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
14 changes: 11 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ JavaScript library to generate recurring dates.

For example:

Every 2 weeks on Monday, Wednesday, and Friday for 5 occurrences starting today
Every month on the last Sunday until 03/30/10 starting on 02/10/09
Every 2 weeks on Monday, Wednesday, and Friday starting today for 5 occurrences
Every month on the last Sunday starting on 02/10/09 until 03/30/10

This library will generate a list of dates for those patterns.

Expand All @@ -19,7 +19,7 @@ USAGE
var r = new Recurrence(pattern);
alert(r.describe());
dates = r.generate();

if (r.contains('03/28/10')) alert('in pattern!');

API

Expand Down Expand Up @@ -52,6 +52,14 @@ Tries to describe the supplied pattern in English.

-----

Boolean contains (date)

Determines whether "date" is in the recurrence pattern. This calls generate(), if it hasn't already been generated, otherwise, it will use the dates generated from the last time generate() was called. Returns true if "date" is in the pattern. "date" can be either a string or a Date object, but please make sure the time portion is all balls.

Note that this only check if "date" is contained within the pattern's starting and ending points. Next version will support indefinite ending date and throwaway dates generation, instead of storing them all in an array.

-----

Date[] generate ([limit])

Generate the dates based on supplied pattern. Returns array of Date objects. Optional argument limit puts an upper limit on how many dates to generate (for preview or to prevent some memory leak).
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
36 changes: 27 additions & 9 deletions recurrence.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
var Recurrence = Class.create({
// takes a JSON object with pattern options
initialize: function (pattern, date_format) {
if (typeof pattern != 'object') throw new TypeError('pattern must be a JSON object');
if (typeof pattern != 'object') throw new TypeError('pattern must be a JSON');

if (!pattern.every || pattern.every.blank()) {
throw new ReferenceError('Every magnitude must be specified');
Expand All @@ -33,6 +33,9 @@ var Recurrence = Class.create({
throw new TypeError('Every magnitude must be a valide number');
}

// stores generated dates based on recurrence pattern
this.dates = [];

this.start = Date.parse(pattern.start);
this.every = parseInt(pattern.every);
this.unit = pattern.unit;
Expand Down Expand Up @@ -60,19 +63,33 @@ var Recurrence = Class.create({
}
t.push('on ' + d.join(', '));
} else if (this.unit == 'm') {
t.push('on the ' + nthword[(this.nth < 0) ? nthword.length : this.nth] + ' ' + week[this.occurrence_of]);
t.push('on the ' + nthword[(this.nth < 0) ? nthword.length-1 : this.nth] + ' ' + week[this.occurrence_of]);
}

t.push('starting on ' + this.start.toString(this.date_format));

if (this.end_condition == 'until') {
t.push('until ' + this.until.toString(this.date_format));
} else if (this.end_condition == 'for') {
t.push('for ' + this.rfor + ' occurrences');
}

t.push('starting on ' + this.start.toString(this.date_format));
return t.join(' ');
},

// determine whether given date is in recurrence
contains: function (d) {
if (this.dates.length == 0) this.generate();

// can be string or date object already
d = Date.parse(d);

for (var i = 0; i < this.dates.length; i++) {
if (Date.equals(this.dates[i], d)) return true;
}
return false;
},

// returns an array of dates base on input pattern
generate: function (max) {
if (!(this.rfor || this.until || max)) {
Expand All @@ -82,7 +99,7 @@ var Recurrence = Class.create({
var end_condition_reached = function (occurrences, current_date) {
if (max && occurrences.length >= max) return true;
if (this.end_condition == 'for' && this.rfor && occurrences.length >= this.rfor) return true;
if (this.end_condition == 'until' && this.until && current_date >= this.until) return true;
if (this.end_condition == 'until' && this.until && current_date > this.until) return true;
return false;
}.bind(this);

Expand All @@ -104,10 +121,10 @@ var Recurrence = Class.create({
while (!end_condition_reached(dates, curr)) {
// scan through the checked days
this.days.each(function (d) {
if (end_condition_reached(dates, curr)) return;

if (curr.getDay() < d) curr.moveToDayOfWeek(d);

if (curr <= this.start) return;
if (end_condition_reached(dates, curr)) return;

dates.push(curr.clone());
}.bind(this));
Expand All @@ -126,7 +143,6 @@ var Recurrence = Class.create({
curr.moveToNthOccurrence(this.occurrence_of, this.nth);
}

// FIXME: breaking at the wrong time for until end conditoin
if (end_condition_reached(dates, curr)) break;

if (curr > this.start) {
Expand All @@ -148,7 +164,9 @@ var Recurrence = Class.create({
dates.push(curr.clone());
}
}

return dates;

// cache results
this.dates = dates;
return this.dates;
}
});

0 comments on commit 954c636

Please sign in to comment.