Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for the iCalendar fields "CREATED" and "LAST-MODIFIED" #71

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,13 @@ Appointment URL

Appointment status. May be any of the following: `confirmed`, `tenative`, `cancelled`.

#### created([_Date_ created])

Date object of the time the appointment was created.

#### lastModified([_Date_ lastModified])

Date object of the time the appointent was last modified.

### Attendee

Expand Down
62 changes: 60 additions & 2 deletions lib/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @constructor ICalEvent Event
*/
var ICalEvent = function(_data, calendar) {
var attributes = ['id', 'uid', 'sequence', 'start', 'end', 'timezone', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url'],
var attributes = ['id', 'uid', 'sequence', 'start', 'end', 'timezone', 'stamp', 'timestamp', 'allDay', 'floating', 'repeating', 'summary', 'location', 'description', 'organizer', 'attendees', 'alarms', 'method', 'status', 'url', 'created', 'lastModified'],
vars,
i,
data;
Expand Down Expand Up @@ -39,7 +39,9 @@ var ICalEvent = function(_data, calendar) {
attendees: [],
alarms: [],
status: null,
url: null
url: null,
created: null,
lastModified: null
};

/**
Expand Down Expand Up @@ -645,6 +647,52 @@ var ICalEvent = function(_data, calendar) {
};


/**
* Set/Get the event's created field
*
* @param {Date} created
* @since 0.2.11
* @returns {ICalEvent|Date}
*/
this.created = function(created) {
if(!created) {
return data.created;
}

if(typeof created === 'string') {
created = new Date(created);
}
if(!(created instanceof Date) || isNaN(created.getTime())) {
throw '`created` must be a Date Object!';
}
data.created = created;
return this;
};


/**
* Set/Get the event's lastModified field
*
* @param {Date} lastModified
* @since 0.2.11
* @returns {ICalEvent|Date}
*/
this.lastModified = function(lastModified) {
if(!lastModified) {
return data.lastModified;
}

if(typeof lastModified === 'string') {
lastModified = new Date(lastModified);
}
if(!(lastModified instanceof Date) || isNaN(lastModified.getTime())) {
throw '`lastModified` must be a Date Object!';
}
data.lastModified = lastModified;
return this;
};


/**
* Export calender as JSON Object to use it later…
*
Expand Down Expand Up @@ -787,6 +835,16 @@ var ICalEvent = function(_data, calendar) {
g += 'STATUS:' + data.status.toUpperCase() + '\r\n';
}

// CREATED
if(data.created) {
g += 'CREATED:' + tools.formatDate(data.created) + '\r\n';
}

// LAST-MODIFIED
if(data.lastModified) {
g += 'LAST-MODIFIED:' + tools.formatDate(data.lastModified) + '\r\n';
}

g += 'END:VEVENT\r\n';
return g;
};
Expand Down
40 changes: 40 additions & 0 deletions test/test_0.2.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,46 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
});

describe('created()', function () {
it('setter should return this', function () {
var e = ical().createEvent();
assert.deepEqual(e, e.created(new Date()));
});

it('getter should return value', function () {
var now = new Date(),
e = ical().createEvent().created(now);
assert.deepEqual(e.created(), now);
});

it('should throw error when created is not a Date', function () {
var e = ical().createEvent();
assert.throws(function () {
e.created('hallo');
}, /`created`/);
});
});

describe('lastModified()', function () {
it('setter should return this', function () {
var e = ical().createEvent();
assert.deepEqual(e, e.lastModified(new Date()));
});

it('getter should return value', function () {
var now = new Date(),
e = ical().createEvent().lastModified(now);
assert.deepEqual(e.lastModified(), now);
});

it('should throw error when lastModified is not a Date', function () {
var e = ical().createEvent();
assert.throws(function () {
e.lastModified('hallo');
}, /`lastModified`/);
});
});

describe('generate()', function() {
it('shoult throw an error without calendar', function() {
var e = ical().createEvent({
Expand Down