Commit 9f0ed220 authored by Sebastian Pekarek's avatar Sebastian Pekarek
Browse files

cal.toJSON()

parent 73e91e09
......@@ -227,6 +227,14 @@ Send Calendar to the User when using HTTP. See Quick Start above.
Return Calendar as a String.
#### toJSON()
Return a shallow copy of the calendar's options for JSON stringification. Can be used for persistance.
```javascript
var cal = ical(),
json = JSON.stringify(cal);
```
#### length()
Returns the ammount of events in the calendar.
......
......@@ -69,4 +69,38 @@ module.exports.duration = function duration(seconds) {
}
return string;
};
module.exports.toJSON = function(object, attributes, options) {
var result = {};
options = options || {};
options.ignoreAttributes = options.ignoreAttributes || [];
options.hooks = options.hooks || {};
attributes.forEach(function(attribute) {
if(options.ignoreAttributes.indexOf(attribute) !== -1) {
return;
}
var value = object[attribute]();
if(options.hooks[attribute]) {
value = options.hooks[attribute](value);
}
if(!value) {
return;
}
result[attribute] = value;
if(Array.isArray(result[attribute])) {
console.log('ARRAY!');
var newObj = [];
result[attribute].forEach(function(object) {
newObj.push(object.toJSON());
});
result[attribute] = newObj;
}
});
return result;
};
\ No newline at end of file
......@@ -7,7 +7,8 @@
* @constructor ICalAlarm Alarm
*/
var ICalAlarm = function(_data, event) {
var vars,
var attributes = ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat', 'interval', 'attach', 'description'],
vars,
i,
data;
......@@ -204,6 +205,18 @@ var ICalAlarm = function(_data, event) {
};
/**
* Export calender as JSON Object to use it later…
*
* @since 0.2.4
* @returns Object Calendar
*/
this.toJSON = function() {
var tools = require('./_tools.js');
return tools.toJSON(this, attributes);
};
/**
* Export Event to iCal
*
......@@ -275,7 +288,7 @@ var ICalAlarm = function(_data, event) {
for(i in _data) {
if(_data.hasOwnProperty(i) && ['type', 'trigger', 'triggerBefore', 'triggerAfter', 'repeat', 'interval', 'attach', 'description'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && attributes.indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -7,7 +7,8 @@
* @constructor ICalAttendee Attendee
*/
var ICalAttendee = function(_data, event) {
var vars,
var attributes = ['name', 'email', 'role', 'status', 'type', 'delegatedTo', 'delegatedFrom', 'delegatesFrom', 'delegatesTo'],
vars,
i,
data;
......@@ -19,7 +20,7 @@ var ICalAttendee = function(_data, event) {
allowed: {
role: ['REQ-PARTICIPANT', 'NON-PARTICIPANT'],
status: ['ACCEPTED', 'TENTATIVE', 'DECLINED', 'DELEGATED'],
type: ['INDIVIDUAL', 'GROUP', 'RESOURCE', 'ROOM', 'UNKNOWN'], // ref: https://tools.ietf.org/html/rfc2445#section-4.2.3
type: ['INDIVIDUAL', 'GROUP', 'RESOURCE', 'ROOM', 'UNKNOWN'] // ref: https://tools.ietf.org/html/rfc2445#section-4.2.3
}
};
......@@ -212,6 +213,28 @@ var ICalAttendee = function(_data, event) {
};
/**
* Export calender as JSON Object to use it later…
*
* @since 0.2.4
* @returns Object Calendar
*/
this.toJSON = function() {
var tools = require('./_tools.js');
return tools.toJSON(this, attributes, {
ignoreAttributes: ['delegatesTo', 'delegatesFrom'],
hooks: {
delegatedTo: function(value) {
return (value instanceof ICalAttendee ? value.email() : value);
},
delegatedFrom: function(value) {
return (value instanceof ICalAttendee ? value.email() : value);
}
}
});
};
/**
* Export Event to iCal
*
......@@ -259,7 +282,7 @@ var ICalAttendee = function(_data, event) {
for(i in _data) {
if(_data.hasOwnProperty(i) && ['name', 'email', 'role', 'status', 'type', 'delegatedTo', 'delegatedFrom', 'delegatesFrom', 'delegatesTo'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && attributes.indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -8,6 +8,7 @@
*/
var ICalCalendar = function(_data) {
var data = {},
attributes = ['domain', 'prodId', 'name', 'timezone', 'events'],
generate,
i;
......@@ -350,6 +351,18 @@ var ICalCalendar = function(_data) {
};
/**
* Export calender as JSON Object to use it later…
*
* @since 0.2.4
* @returns Object Calendar
*/
this.toJSON = function() {
var tools = require('./_tools.js');
return tools.toJSON(this, attributes);
};
/**
* Get number of events in calendar…
*
......@@ -387,9 +400,13 @@ var ICalCalendar = function(_data) {
};
if(typeof _data === 'string') {
_data = JSON.parse(_data);
}
this.clear();
for(i in _data) {
if(_data.hasOwnProperty(i) && ['domain', 'prodId', 'name', 'timezone', 'events'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && attributes.indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -7,7 +7,8 @@
* @constructor ICalEvent Event
*/
var ICalEvent = function(_data) {
var vars,
var attributes = ['id', 'uid', 'start', 'end', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url'],
vars,
i,
data;
......@@ -76,7 +77,11 @@ var ICalEvent = function(_data) {
return data.start;
}
if(!(start instanceof Date)) {
if(typeof start === 'string') {
start = new Date(start);
}
if(!(start instanceof Date) || !start.getTime()) {
throw '`start` must be a Date Object!';
}
data.start = start;
......@@ -102,7 +107,10 @@ var ICalEvent = function(_data) {
return data.end;
}
if(!(end instanceof Date)) {
if(typeof end === 'string') {
end = new Date(end);
}
if(!(end instanceof Date) || !end.getTime()) {
throw '`end` must be a Date Object!';
}
data.end = end;
......@@ -128,7 +136,10 @@ var ICalEvent = function(_data) {
return data.stamp;
}
if(!(stamp instanceof Date)) {
if(typeof stamp === 'string') {
stamp = new Date(stamp);
}
if(!(stamp instanceof Date) || !stamp.getTime()) {
throw '`stamp` must be a Date Object!';
}
data.stamp = stamp;
......@@ -218,7 +229,10 @@ var ICalEvent = function(_data) {
}
if(repeating.until) {
if(!(repeating.until instanceof Date)) {
if(typeof repeating.until === 'string') {
repeating.until = new Date(repeating.until);
}
if(!(repeating.until instanceof Date) || !repeating.until.getTime()) {
throw '`repeating.until` must be a Date Object!';
}
......@@ -519,6 +533,18 @@ var ICalEvent = function(_data) {
};
/**
* Export calender as JSON Object to use it later…
*
* @since 0.2.4
* @returns Object Calendar
*/
this.toJSON = function() {
var tools = require('./_tools.js');
return tools.toJSON(this, attributes);
};
/**
* Export Event to iCal
*
......@@ -630,7 +656,7 @@ var ICalEvent = function(_data) {
for(i in _data) {
if(_data.hasOwnProperty(i) && ['id', 'uid', 'start', 'end', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && attributes.indexOf(i) > -1) {
this[i](_data[i]);
}
}
......
......@@ -1047,7 +1047,8 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
it('case #1', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.createEvent({
id: '123',
start: new Date('Fr Oct 04 2013 22:39:30 UTC'),
......@@ -1057,11 +1058,16 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_01.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_01.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
it('case #2', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.createEvent({
id: '123',
start: new Date('Fr Oct 04 2013 22:39:30 UTC'),
......@@ -1073,11 +1079,16 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_02.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_02.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
it('case #3', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.createEvent({
id: '123',
start: new Date('Fr Oct 04 2013 22:39:30 UTC'),
......@@ -1092,11 +1103,16 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_03.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_03.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
it('case #7 (repeating: byDay, byMonth, byMonthDay)', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
it('case #4 (repeating)', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.events([
{
id: '1',
......@@ -1105,8 +1121,7 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
stamp: new Date('Fr Oct 04 2013 23:34:53 UTC'),
summary: 'repeating by month',
repeating: {
freq: 'monthly',
byMonth: [1, 4, 7, 10]
freq: 'monthly'
}
},
{
......@@ -1114,11 +1129,10 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
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 on Mo/We/Fr, twice',
summary: 'repeating by day, twice',
repeating: {
freq: 'DAILY',
count: 2,
byDay: ['mo', 'we', 'fr']
count: 2
}
},
{
......@@ -1126,21 +1140,26 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
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 on 1st and 15th',
summary: 'repeating by 3 weeks, until 2014',
repeating: {
freq: 'DAILY',
interval: 1,
byMonthDay: [1, 15]
freq: 'WEEKLY',
interval: 3,
until: new Date('We Jan 01 2014 00:00:00 UTC')
}
}
]);
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_07.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_04.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
it('case #5 (floating)', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.createEvent({
id: '1',
start: new Date('Fr Oct 04 2013 22:39:30 UTC'),
......@@ -1151,11 +1170,16 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_05.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_05.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
it('case #6 (attendee with simple delegation and alarm)', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.createEvent({
id: '123',
start: new Date('Fr Oct 04 2013 22:39:30 UTC'),
......@@ -1194,11 +1218,16 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_06.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_06.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
it('case #4 (repeating)', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'});
it('case #7 (repeating: byDay, byMonth, byMonthDay)', function() {
var cal = ical({domain: 'sebbo.net', prodId: '//sebbo.net//ical-generator.tests//EN'}),
string, json;
cal.events([
{
id: '1',
......@@ -1207,7 +1236,8 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
stamp: new Date('Fr Oct 04 2013 23:34:53 UTC'),
summary: 'repeating by month',
repeating: {
freq: 'monthly'
freq: 'monthly',
byMonth: [1, 4, 7, 10]
}
},
{
......@@ -1215,10 +1245,11 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
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',
summary: 'repeating on Mo/We/Fr, twice',
repeating: {
freq: 'DAILY',
count: 2
count: 2,
byDay: ['mo', 'we', 'fr']
}
},
{
......@@ -1226,17 +1257,21 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
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',
summary: 'repeating on 1st and 15th',
repeating: {
freq: 'WEEKLY',
interval: 3,
until: new Date('We Jan 01 2014 00:00:00 UTC')
freq: 'DAILY',
interval: 1,
byMonthDay: [1, 15]
}
}
]);
/*jslint stupid: true */
assert.equal(cal.toString(), fs.readFileSync(__dirname + '/results/generate_04.ics', 'utf8'));
string = cal.toString();
assert.equal(string, fs.readFileSync(__dirname + '/results/generate_07.ics', 'utf8'));
json = JSON.stringify(cal.toJSON());
assert.equal(ical(json).toString(), string);
});
});
});
......
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