Commit 7b5e697c authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

Merge branch 'jysperm-master'

parents 01c5f235 2122438f
......@@ -80,6 +80,21 @@ Appointment date of beginning
#### options.end (Date Object, required)
Appointment date of end
#### options.repeating (Plain Object)
Appointment is a repeating event
```javascript
cal.addEvent({
/* Other options */
repeating: {
freq: 'MONTHLY', // required
count: 5,
interval: 2,
until: new Date("Jan 01 2014 00:00:00 UTC")
}
});
```
#### options.allDay (Bool)
Appointment is for the whole day
......
......@@ -40,6 +40,7 @@ var a = {
addEvent: function(e) {
var _event = {},
allowedMethods = ['PUBLISH', 'REQUEST', 'REPLY', 'ADD', 'CANCEL', 'REFRESH', 'COUNTER', 'DECLINECOUNTER'],
allowedRepeatingFreq = ['SECONDLY', 'MINUTELY', 'HOURLY', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY'],
allowedStatuses = ['CONFIRMED', 'TENATIVE', 'CANCELLED'];
if(!e || typeof e !== 'object') {
......@@ -67,6 +68,41 @@ var a = {
}
_event.end = e.end;
// Repeating Event
if(e.repeating) {
_event.repeating = {};
if(!e.repeating.freq || allowedRepeatingFreq.indexOf(e.repeating.freq.toUpperCase()) === -1) {
throw 'event.repeating.freq is a mandatory item, and must be one of the following: ' + allowedRepeatingFreq.join(', ');
}
_event.repeating.freq = e.repeating.freq;
if(e.repeating.count) {
if(!isFinite(e.repeating.count)) {
throw 'event.repeating.count must be a Number.';
}
_event.repeating.count = e.repeating.count;
}
if(e.repeating.interval) {
if(!isFinite(e.repeating.interval)) {
throw 'event.repeating.interval must be a Number.';
}
_event.repeating.interval = e.repeating.interval;
}
if(e.repeating.until) {
if(Object.prototype.toString.call(e.repeating.until) !== '[object Date]') {
throw 'event.repeating.until must be a Date Object.';
}
_event.repeating.until = e.repeating.until;
}
}
// allDay flag
_event.allDay = e.allDay ? true : false;
......@@ -196,6 +232,24 @@ var a = {
g += 'DTEND:' + _formatDate(e.end) + '\n';
}
if(e.repeating) {
var rrlue = 'RRULE:FREQ=' + e.repeating.freq;
if(e.repeating.count) {
rrlue += ';COUNT=' + e.repeating.count;
}
if(e.repeating.interval) {
rrlue += ';INTERVAL=' + e.repeating.interval;
}
if(e.repeating.until) {
rrlue += ';UNTIL=' + _formatDate(e.repeating.until);
}
g += rrlue + '\n';
}
g += 'SUMMARY:' + _escape(e.summary) + '\n';
if(e.location) {
g += 'LOCATION:' + _escape(e.location) + '\n';
......
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//sebbo.net//ical-generator.tests//EN
BEGIN:VEVENT
UID:20131004T223930Z-1@sebbo.net
DTSTAMP:20131004T233453Z
DTSTART:20131004T223930Z
DTEND:20131006T231500Z
RRULE:FREQ=MONTHLY
SUMMARY:repeating by month
END:VEVENT
BEGIN:VEVENT
UID:20131004T223930Z-2@sebbo.net
DTSTAMP:20131004T233453Z
DTSTART:20131004T223930Z
DTEND:20131006T231500Z
RRULE:FREQ=DAILY;COUNT=2
SUMMARY:repeating by day\, twice
END:VEVENT
BEGIN:VEVENT
UID:20131004T223930Z-3@sebbo.net
DTSTAMP:20131004T233453Z
DTSTART:20131004T223930Z
DTEND:20131006T231500Z
RRULE:FREQ=WEEKLY;INTERVAL=3;UNTIL=20140101T000000Z
SUMMARY:repeating by 3 weeks\, until 2014
END:VEVENT
END:VCALENDAR
\ No newline at end of file
......@@ -219,6 +219,82 @@ describe('ical-generator', function() {
}, /event\.stamp must be a Date Object/);
});
it('should throw error when repeating without freq', function() {
var generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
assert.throws(function() {
cal.addEvent({
start: new Date(),
end: new Date(),
repeating: {}
});
}, /event\.repeating\.freq is a mandatory item, and must be one of the following/);
});
it('should throw error when repeating when freq is not allowed', function() {
var generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
assert.throws(function() {
cal.addEvent({
start: new Date(),
end: new Date(),
repeating: {
freq: 'hello'
}
});
}, /event\.repeating\.freq is a mandatory item, and must be one of the following/);
});
it('should throw error when repeating.count is not a number', function() {
var generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
assert.throws(function() {
cal.addEvent({
start: new Date(),
end: new Date(),
repeating: {
freq: 'DAILY',
count: Infinity
}
});
}, /event\.repeating\.count must be a Number/);
});
it('should throw error when repeating.interval is not a number', function() {
var generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
assert.throws(function() {
cal.addEvent({
start: new Date(),
end: new Date(),
repeating: {
freq: 'DAILY',
interval: 'string'
}
});
}, /event\.repeating\.interval must be a Number/);
});
it('should throw error when repeating.until is not a date', function() {
var generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
assert.throws(function() {
cal.addEvent({
start: new Date(),
end: new Date(),
repeating: {
freq: 'DAILY',
until: 1413277003
}
});
}, /event\.repeating\.until must be a Date Object/);
});
it('should throw error when summary is empty', function() {
var generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
......@@ -374,6 +450,56 @@ describe('ical-generator', function() {
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_03.ics', 'utf8'));
});
it('case #4 (repeating)', function() {
var fs = require('fs'),
generator = require(__dirname + '/../lib/ical-generator.js'),
cal = generator();
cal.setDomain('sebbo.net');
cal.setProdID({
company: 'sebbo.net',
product: 'ical-generator.tests'
});
cal.addEvent({
uid: '1',
start: new Date("Fr Oct 04 2013 22:39:30 UTC"),
end: new Date("Fr Oct 06 2013 23:15:00 UTC"),
stamp: new Date("Fr Oct 04 2013 23:34:53 UTC"),
summary: 'repeating by month',
repeating: {
freq: 'MONTHLY'
}
});
cal.addEvent({
uid: '2',
start: new Date("Fr Oct 04 2013 22:39:30 UTC"),
end: new Date("Fr Oct 06 2013 23:15:00 UTC"),
stamp: new Date("Fr Oct 04 2013 23:34:53 UTC"),
summary: 'repeating by day, twice',
repeating: {
freq: 'DAILY',
count: 2
}
});
cal.addEvent({
uid: '3',
start: new Date("Fr Oct 04 2013 22:39:30 UTC"),
end: new Date("Fr Oct 06 2013 23:15:00 UTC"),
stamp: new Date("Fr Oct 04 2013 23:34:53 UTC"),
summary: 'repeating by 3 weeks, until 2014',
repeating: {
freq: 'WEEKLY',
interval: 3,
until: new Date("We Jan 01 2014 00:00:00 UTC")
}
});
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_04.ics', 'utf8'));
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment