From 5ae723f7d8e5383e7ea23073ab606521a6c11960 Mon Sep 17 00:00:00 2001 From: Vincent Romagnoli Date: Mon, 9 Jan 2012 22:33:07 +0100 Subject: [PATCH] fix issue #1 (yearly parsing) --- rrecur-parser-tests.js | 19 ++++++++++++++++--- rrecur-parser.js | 31 ++++++++++++++++++++++++++++--- test.html | 12 ++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100755 test.html diff --git a/rrecur-parser-tests.js b/rrecur-parser-tests.js index ce30934..136d7e2 100755 --- a/rrecur-parser-tests.js +++ b/rrecur-parser-tests.js @@ -1,8 +1,8 @@ /* ---------------------------------------------------------------------------- - Testing case of rrecur-parser.js inspired from + Testing case of scheduler.js inspired from http://www.kanzaki.com/docs/ical/rrule.html - date : dec 14 2011 + date : jan 09 2012 ---------------------------------------------------------------------------- */ @@ -802,4 +802,17 @@ console.assert(occurrences.in_array(new Date(1997, 7, 5, 9).getTime())); console.assert(occurrences.in_array(new Date(1997, 7, 17, 9).getTime())); console.assert(occurrences.in_array(new Date(1997, 7, 19, 9).getTime())); console.assert(occurrences.in_array(new Date(1997, 7, 31, 9).getTime())); -// ==> (1997 EDT)August 5,17,19,31 \ No newline at end of file +// ==> (1997 EDT)August 5,17,19,31 + + +console.log("--- Birthday ---"); +d = new Date(2011, 0, 1); +start_at = new Date(2011, 0, 1); +end_at = new Date(2014, 0, 1); +scheduler = new Scheduler(d, "FREQ=YEARLY;INTERVAL=1", true); +occurrences = scheduler.occurrences_between(start_at, end_at); +console.assert(occurrences.length == 4); +console.assert(occurrences[0] == new Date(2011, 0, 1).getTime()); +console.assert(occurrences[1] == new Date(2012, 0, 1).getTime()); +console.assert(occurrences[2] == new Date(2013, 0, 1).getTime()); +console.assert(occurrences[3] == new Date(2014, 0, 1).getTime()); \ No newline at end of file diff --git a/rrecur-parser.js b/rrecur-parser.js index 3ae1326..ad0dbe9 100755 --- a/rrecur-parser.js +++ b/rrecur-parser.js @@ -1,3 +1,28 @@ +// used for IE < 9 +// adapted from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf +if (!Array.prototype.indexOf) +{ + Array.prototype.indexOf = function(elt /*, from*/) + { + var len = this.length >>> 0; + + var from = Number(arguments[1]) || 0; + from = (from < 0) + ? Math.ceil(from) + : Math.floor(from); + if (from < 0) + from += len; + + for (; from < len; from++) + { + if (from in this && + this[from] === elt) + return from; + } + return -1; + }; +} + Array.prototype.in_array = function(test_var) { // useful method for "class" Array return this.indexOf(test_var, 0) != -1; } @@ -268,9 +293,9 @@ Scheduler.prototype.add_recurrence_rules = function(rfc_rrule) { if (!this.rrule_byday && !this.rrule_bymonthday && !this.rrule_byyearday && (this.rrule_freq == "MONTHLY" || this.rrule_freq == "YEARLY")) { this.rrule_bymonthday = [ this.start_date.getDate().toString() ]; } -// if (!this.rrule_bymonth && (this.rrule_freq == "YEARLY")) { -// this.rrule_bymonth = [ (this.start_date.getMonth() + 1).toString() ]; -// } + if (!this.rrule_byday && !this.rrule_byyearday && !this.rrule_bymonth && this.rrule_freq == "YEARLY") { + this.rrule_bymonth = [ (this.start_date.getMonth() + 1).toString() ]; + } } // removes all RRULEs diff --git a/test.html b/test.html new file mode 100755 index 0000000..d5fad4f --- /dev/null +++ b/test.html @@ -0,0 +1,12 @@ + + + + + + + Tests parseur + + + Done ! + +