Comparing version 0.5.1 to 0.5.4
@@ -68,8 +68,14 @@ // Copyright (C) 2011 Tri Tech Computers Ltd. | ||
VEvent.prototype.rrule = function() { | ||
var rr = this.getPropertyValue('RRULE'); | ||
if(!rr) return null; | ||
return new RRule(rr, | ||
this.getPropertyValue('DTSTART'), | ||
this.getPropertyValue('DTEND')); | ||
} | ||
VEvent.prototype.inTimeRange = function(start, end) { | ||
var dtstart = this.getPropertyValue('DTSTART'); | ||
var dtend = this.getPropertyValue('DTEND'); | ||
var rr = this.getPropertyValue('RRULE'); | ||
var rr = this.rrule(); | ||
if(rr) { | ||
rr = new RRule(rr, dtstart, dtend); | ||
var next = rr.next(start); | ||
@@ -79,2 +85,4 @@ return (next !== null && (!end || next <= end)); | ||
var dtstart = this.getPropertyValue('DTSTART'); | ||
var dtend = this.getPropertyValue('DTEND'); | ||
if(!dtend) { | ||
@@ -81,0 +89,0 @@ var duration = this.getPropertyValue('DURATION'); |
101
lib/rrule.js
@@ -58,15 +58,17 @@ // Copyright (C) 2011 Tri Tech Computers Ltd. | ||
// These are more comfy to type... | ||
function y(dt) { return dt.getUTCFullYear(); } | ||
function m(dt) { return dt.getUTCMonth()+1; } | ||
function d(dt) { return dt.getUTCDate(); } | ||
function hr(dt) { return dt.getUTCHours(); } | ||
function min(dt) { return dt.getUTCMinutes(); } | ||
function sec(dt) { return dt.getUTCSeconds(); } | ||
function y(dt) { return dt.getUTCFullYear(); } | ||
function m(dt) { return dt.getUTCMonth()+1; } | ||
function d(dt) { return dt.getUTCDate(); } | ||
function hr(dt) { return dt.getUTCHours(); } | ||
function min(dt) { return dt.getUTCMinutes(); } | ||
function sec(dt) { return dt.getUTCSeconds(); } | ||
function ms(dt) { return dt.getUTCMilliseconds(); } | ||
function set_y(dt, v) { dt.setUTCFullYear(v); return dt; } | ||
function set_m(dt, v) { dt.setUTCMonth(v-1); return dt; } | ||
function set_d(dt, v) { dt.setUTCDate(v); return dt; } | ||
function set_hr(dt, v) { dt.setUTCHours(v); return dt; } | ||
function set_min(dt, v) { dt.setUTCMinutes(v); return dt; } | ||
function set_sec(dt, v) { dt.setUTCSeconds(v); return dt; } | ||
function set_y(dt, v) { dt.setUTCFullYear(v); return dt; } | ||
function set_m(dt, v) { dt.setUTCMonth(v-1); return dt; } | ||
function set_d(dt, v) { dt.setUTCDate(v); return dt; } | ||
function set_hr(dt, v) { dt.setUTCHours(v); return dt; } | ||
function set_min(dt, v) { dt.setUTCMinutes(v); return dt; } | ||
function set_sec(dt, v) { dt.setUTCSeconds(v); return dt; } | ||
function set_ms(dt, v) { dt.setUTCMilliseconds(v); return dt; } | ||
@@ -159,3 +161,3 @@ function add_y(dt, v) { return set_y(dt, y(dt)+v); } | ||
if(!after || after < this.start) | ||
return from_utc_date(this.start); | ||
after = new Date(this.start.valueOf() - 1); | ||
if(this.end && after > this.end) return null; | ||
@@ -190,5 +192,5 @@ | ||
RRule.prototype.nextOccurences = function(after, count) { | ||
RRule.prototype.nextOccurences = function(after, count_or_until) { | ||
if(arguments.length === 1) { | ||
count = after; | ||
count_or_until = after; | ||
after = undefined; | ||
@@ -198,7 +200,18 @@ } | ||
var arr = []; | ||
while(count-- && after !== null) { | ||
after = this.next(after); | ||
if(after) | ||
arr.push(after); | ||
if(count_or_until instanceof Date) { | ||
while(true) { | ||
after = this.next(after); | ||
if(after && after <= count_or_until) | ||
arr.push(after); | ||
else | ||
break; | ||
} | ||
} | ||
else { | ||
while(count_or_until-- && after !== null) { | ||
after = this.next(after); | ||
if(after) | ||
arr.push(after); | ||
} | ||
} | ||
return arr; | ||
@@ -267,3 +280,3 @@ } | ||
// Adjust for interval... | ||
var mod_days = daydiff(next, start) % interval; | ||
var mod_days = trunc(daydiff(next, start)) % interval; | ||
if(mod_days) | ||
@@ -277,4 +290,5 @@ add_d(next, interval - mod_days); | ||
set_sec(next, sec(start)); | ||
set_ms(next, ms(start)); | ||
if(after.valueOf() != next.valueOf()) | ||
if(next.valueOf() > after.valueOf()) | ||
break; | ||
@@ -294,6 +308,3 @@ | ||
// Adjust for interval... | ||
var days = daydiff(start, next); | ||
if(days) add_d(next, 7 - (days % 7)); | ||
var mod_weeks = Math.floor(daydiff(next, start) / 7) % interval; | ||
var mod_weeks = trunc(daydiff(start, next) / 7) % interval; | ||
if(mod_weeks) | ||
@@ -304,7 +315,21 @@ add_d(next, (interval - mod_weeks) * 7); | ||
next = byday(rule.BYDAY, next); | ||
// Fall back to the start day of the week | ||
if (!rule.BYDAY || !rule.BYDAY.length) { | ||
startDayOfWeek = wkday(start); | ||
nextDayOfWeek = wkday(next); | ||
// Always move backwards to the start day of week | ||
if (nextDayOfWeek > startDayOfWeek) | ||
add_d(next, startDayOfWeek - nextDayOfWeek); | ||
else if (startDayOfWeek > nextDayOfWeek) | ||
add_d(next, startDayOfWeek - nextDayOfWeek - 7); | ||
} | ||
set_hr(next, hr(start)); | ||
set_min(next, min(start)); | ||
set_sec(next, sec(start)); | ||
set_ms(next, ms(start)); | ||
if(after.valueOf() != next.valueOf() | ||
if(next.valueOf() > after.valueOf() | ||
&& check_bymonth(rule.BYMONTH, next)) | ||
@@ -329,10 +354,18 @@ break; | ||
for(var i=0; i<2; ++i) { | ||
set_d(next, 1); | ||
next = byday(rule.BYDAY, next); | ||
next = bymonthday(rule.BYMONTHDAY, next); | ||
// Fall back to the start day of the month | ||
if ((!rule.BYDAY || !rule.BYDAY.length) && (!rule.BYMONTHDAY || !rule.BYMONTHDAY.length)) | ||
set_d(next, d(start)); | ||
set_hr(next, hr(start)); | ||
set_min(next, min(start)); | ||
set_sec(next, sec(start)); | ||
set_ms(next, ms(start)); | ||
if(after.valueOf() != next.valueOf()) | ||
if(next.valueOf() > after.valueOf()) | ||
break; | ||
@@ -359,6 +392,12 @@ | ||
for(var i=0; i<2; ++i) { | ||
next = bymonth(rule.BYMONTH || m(start), next, i); | ||
next = bymonth(rule.BYMONTH, next, i); | ||
next = bymonthday(rule.BYMONTHDAY, next); | ||
next = byday(rule.BYDAY, next, i); | ||
// Fall back the the start month and day of the month | ||
if (!rule.BYMONTH || !rule.BYMONTH.length) | ||
set_m(next, m(start)); | ||
if ((!rule.BYDAY || !rule.BYDAY.length) && (!rule.BYMONTHDAY || !rule.BYMONTHDAY.length)) | ||
set_d(next, d(start)); | ||
// TODO: Add actual byhour/minute/second methods | ||
@@ -368,5 +407,6 @@ set_hr(next, hr(start)); | ||
set_sec(next, sec(start)); | ||
set_ms(next, ms(start)); | ||
// Don't loop back again if we found a new date | ||
if(after.valueOf() != next.valueOf()) | ||
if(next.valueOf() > after.valueOf()) | ||
break; | ||
@@ -421,5 +461,2 @@ | ||
var candidates = rules.map(function(rule) { | ||
var delta = rule-m(dt); | ||
if(delta < 0) delta += 12; | ||
var newdt = set_d(new Date(dt), rule); | ||
@@ -426,0 +463,0 @@ return (newdt < dt ? null : newdt); |
@@ -27,3 +27,3 @@ // Copyright (C) 2011 Tri Tech Computers Ltd. | ||
d = d || 2; | ||
neg = n < 0; | ||
var neg = n < 0; | ||
if(neg) n *= -1; | ||
@@ -30,0 +30,0 @@ n = n.toString(); |
{ | ||
"name": "icalendar", | ||
"version": "0.5.1", | ||
"version": "0.5.4", | ||
"author": "James Emerton <james@tri-tech.com>", | ||
@@ -20,3 +20,6 @@ "description": "RFC5545 iCalendar parser/generator", | ||
"node": "*" | ||
}, | ||
"scripts": { | ||
"test": "jasmine-node spec" | ||
} | ||
} |
@@ -36,2 +36,13 @@ | ||
it("finds recurrences in a specific timeframe", function() { | ||
var rrule = new RRule('FREQ=MONTHLY', new Date(2011,0,1)); | ||
expect(rrule.nextOccurences(new Date(2010,11,31), new Date(2011,2,15))) | ||
.toEqual([ | ||
new Date(2011,0,1), | ||
new Date(2011,1,1), | ||
new Date(2011,2,1) | ||
]); | ||
}); | ||
describe("yearly recurrence", function() { | ||
@@ -52,2 +63,16 @@ it("handles yearly recurrence", function() { | ||
it("handles yearly recurrences with time values", function() { | ||
var rrule = new RRule('FREQ=YEARLY;BYMONTH=3;BYDAY=1SU', new Date(1970,2,8,2,30,30)); | ||
expect(rrule.next(new Date(2011,2,6,2,30,30,259))) | ||
.toEqual(new Date(2012,2,4,2,30,30,0)); | ||
}); | ||
it("handles yearly recurrences on the start month and day of month", function() { | ||
var rrule = new RRule('FREQ=YEARLY', new Date(1970,2,8)); | ||
expect(rrule.next(new Date(2011,2,9))) | ||
.toEqual(new Date(2012,2,8)); | ||
}); | ||
it("handles yearly recurrence with an interval", function() { | ||
@@ -71,4 +96,25 @@ var rrule5 = new RRule('FREQ=YEARLY;INTERVAL=5;BYMONTH=3;BYDAY=1SU', new Date(1970,2,8,2,0,0)); | ||
.toEqual(new Date(2011,1,6,2,0,0)); | ||
expect(rrule.next(new Date(2012,3,1,2,0,0))) | ||
.toEqual(new Date(2012,4,6,2,0,0)); | ||
expect(rrule.next(new Date(2012,4,6,2,0,0))) | ||
.toEqual(new Date(2012,5,3,2,0,0)); | ||
}); | ||
it("handles monthly recurrences with a time value", function() { | ||
var rrule = new RRule(RRule.parse('FREQ=MONTHLY;BYDAY=1SU'), new Date(2011,0,1,2,30,30)); | ||
expect(rrule.next(new Date(2011,0,3,2,30,30,259))) | ||
.toEqual(new Date(2011,1,6,2,30,30,0)); | ||
}); | ||
it("handles monthly recurrences on the start day of month", function() { | ||
var rrule = new RRule(RRule.parse('FREQ=MONTHLY'), new Date(2011,0,1)); | ||
expect(rrule.next(new Date(2011,0,15))) | ||
.toEqual(new Date(2011,1,1)); | ||
}); | ||
it("handles monthly recurrence with an interval", function() { | ||
@@ -118,2 +164,12 @@ var rrule = new RRule(RRule.parse('FREQ=MONTHLY;BYDAY=1SU;INTERVAL=3'), | ||
it("handles weekly recurrence with time granularity", function() { | ||
var rrule = new RRule('FREQ=WEEKLY;BYDAY=TU', new Date(2012,0,1,2,45,30)); | ||
expect(rrule.next(new Date(2012,0,1,2,45,30))) | ||
.toEqual(new Date(2012,0,3,2,45,30)); | ||
expect(rrule.next(new Date(2012,0,5))) | ||
.toEqual(new Date(2012,0,10,2,45,30)); | ||
}); | ||
it("handles weekly recurrence with BYDAY", function() { | ||
@@ -127,2 +183,9 @@ var rrule = new RRule('FREQ=WEEKLY;BYDAY=TU', new Date(2012,0,1)); | ||
.toEqual(new Date(2012,0,10)); | ||
expect(rrule.next(new Date(2012,0,8))) | ||
.toEqual(new Date(2012,0,10)); | ||
expect(rrule.next(new Date(2012,0,5))) | ||
.toEqual(new Date(2012,0,10)); | ||
}); | ||
@@ -136,2 +199,16 @@ | ||
}); | ||
it("handles weekly recurrence with interval", function() { | ||
var rrule = new RRule('FREQ=WEEKLY;INTERVAL=3', new Date(2012,0,3,12,30,45)); | ||
expect(rrule.next(new Date(2012,0,2))) | ||
.toEqual(new Date(2012,0,3,12,30,45)); | ||
expect(rrule.next(new Date(2012,0,18))) | ||
.toEqual(new Date(2012,0,24,12,30,45)); | ||
expect(rrule.next(new Date(2012,0,3,12,30,45))) | ||
.toEqual(new Date(2012,0,24,12,30,45)); | ||
}); | ||
}); | ||
@@ -148,4 +225,14 @@ | ||
}); | ||
it("handles daily recurrence with time values", function() { | ||
var rrule = new RRule('FREQ=DAILY', new Date(2012,0,1,12,45,30)); | ||
expect(rrule.next(new Date(2012,0,2,12,45,29,999))) | ||
.toEqual(new Date(2012,0,2,12,45,30)); | ||
expect(rrule.next(new Date(2012,0,2,12,45,30))) | ||
.toEqual(new Date(2012,0,3,12,45,30)); | ||
}); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
180830
23
1744