Commit 0067a085 authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

lib/_tools

parent 27d63a84
'use strict';
module.exports = {};
module.exports.formatDate = function _formatDate(d, dateonly, floating) {
var s;
function pad(i) {
return (i < 10 ? '0': '') + i;
}
s = d.getUTCFullYear();
s += pad(d.getUTCMonth() + 1);
s += pad(d.getUTCDate());
if(!dateonly) {
s += 'T';
s += pad(d.getUTCHours());
s += pad(d.getUTCMinutes());
s += pad(d.getUTCSeconds());
if(!floating) {
s += 'Z';
}
}
return s;
};
module.exports.escape = function escape(str) {
return str.replace(/[\\;,\n"]/g, function(match) {
if (match === '\n') {
return '\\n';
}
return '\\' + match;
});
};
\ No newline at end of file
......@@ -445,7 +445,8 @@ var ICalEvent = function(_data) {
* @returns {String}
*/
this.generate = function(calendar) {
var g = '';
var tools = require('./_tools.js'),
g = '';
if(!calendar) {
throw '`calendar` option required!';
......@@ -457,52 +458,16 @@ var ICalEvent = function(_data) {
throw 'No value for `end` in ICalEvent #' + data.id + ' given!';
}
function _formatDate(d, dateonly, floating) {
var s;
function pad(i) {
return (i < 10 ? '0': '') + i;
}
s = d.getUTCFullYear();
s += pad(d.getUTCMonth() + 1);
s += pad(d.getUTCDate());
if(!dateonly) {
s += 'T';
s += pad(d.getUTCHours());
s += pad(d.getUTCMinutes());
s += pad(d.getUTCSeconds());
if(!floating) {
s += 'Z';
}
}
return s;
}
function escape(str) {
return str.replace(/[\\;,\n]/g, function (match) {
if (match === '\n') {
return '\\n';
}
return '\\' + match;
});
}
// DATE & TIME
g += 'BEGIN:VEVENT\n';
g += 'UID:' + _formatDate(data.start) + '-' + data.id + '@' + calendar.domain() + '\n';
g += 'DTSTAMP:' + _formatDate(data.stamp) + '\n';
g += 'UID:' + tools.formatDate(data.start) + '-' + data.id + '@' + calendar.domain() + '\n';
g += 'DTSTAMP:' + tools.formatDate(data.stamp) + '\n';
if(data.allDay) {
g += 'DTSTART;VALUE=DATE:' + _formatDate(data.start, true) + '\n';
g += 'DTEND;VALUE=DATE:' + _formatDate(data.end, true) + '\n';
g += 'DTSTART;VALUE=DATE:' + tools.formatDate(data.start, true) + '\n';
g += 'DTEND;VALUE=DATE:' + tools.formatDate(data.end, true) + '\n';
}else{
g += 'DTSTART:' + _formatDate(data.start, false, data.floating) + '\n';
g += 'DTEND:' + _formatDate(data.end, false, data.floating) + '\n';
g += 'DTSTART:' + tools.formatDate(data.start, false, data.floating) + '\n';
g += 'DTEND:' + tools.formatDate(data.end, false, data.floating) + '\n';
}
// REPEATING
......@@ -518,28 +483,28 @@ var ICalEvent = function(_data) {
}
if(data.repeating.until) {
g += ';UNTIL=' + _formatDate(data.repeating.until);
g += ';UNTIL=' + tools.formatDate(data.repeating.until);
}
g += '\n';
}
// SUMMARY
g += 'SUMMARY:' + escape(data.summary) + '\n';
g += 'SUMMARY:' + tools.escape(data.summary) + '\n';
// LOCATION
if(data.location) {
g += 'LOCATION:' + escape(data.location) + '\n';
g += 'LOCATION:' + tools.escape(data.location) + '\n';
}
// DESCRIPTION
if(data.description) {
g += 'DESCRIPTION:' + escape(data.description )+ '\n';
g += 'DESCRIPTION:' + tools.escape(data.description ) + '\n';
}
// ORGANIZER
if(data.organizer) {
g += 'ORGANIZER;CN="' + data.organizer.name.replace(/"/g, '\\"') + '":mailto:' + data.organizer.email + '\n';
g += 'ORGANIZER;CN="' + tools.escape(data.organizer.name) + '":mailto:' + tools.escape(data.organizer.email) + '\n';
}
// ATTENDEES
......@@ -547,9 +512,14 @@ var ICalEvent = function(_data) {
g += attendee.generate();
});
// ALARMS
data.alarms.forEach(function(alarm) {
g += alarm.generate();
});
// URL
if(data.url) {
g += 'URL;VALUE=URI:' + data.url + '\n';
g += 'URL;VALUE=URI:' + tools.escape(data.url) + '\n';
}
// METHOD & STATUS
......
......@@ -784,7 +784,7 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
assert.deepEqual(event.attendees()[0], attendee);
});
it('setter should add events and return this', function() {
it('setter should add attendees and return this', function() {
var cal = ical(),
event = cal.createEvent(),
foo = event.attendees([{name: 'Person A'}, {name: 'Person B'}]);
......
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