Commit cbecd1e1 authored by H. Ryan Jones's avatar H. Ryan Jones
Browse files

adding in type (a.k.a. CUTYPE) field for attendees...

adding in type (a.k.a. CUTYPE) field for attendees https://github.com/sebbo2002/ical-generator/issues/24
parent d9fb80c7
......@@ -16,8 +16,11 @@ var ICalAttendee = function(_data, event) {
}
vars = {
allowedRoles: ['REQ-PARTICIPANT', 'NON-PARTICIPANT'],
allowedStatuses: ['ACCEPTED', 'TENTATIVE', 'DECLINED', 'DELEGATED']
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
}
};
data = {
......@@ -25,6 +28,7 @@ var ICalAttendee = function(_data, event) {
email: null,
status: null,
role: 'REQ-PARTICIPANT',
type: null,
delegatedTo: null,
delegatedFrom: null
};
......@@ -71,15 +75,11 @@ var ICalAttendee = function(_data, event) {
* @returns {ICalAttendee|String}
*/
this.role = function(role) {
if(!role) {
if(role === undefined) {
return data.role;
}
if(vars.allowedRoles.indexOf(role.toUpperCase()) === -1) {
throw '`role` must be one of the following: ' + vars.allowedRoles.join(', ') + '!';
}
data.role = role.toUpperCase();
data.role = getAllowedRole(role);
return this;
};
......@@ -92,15 +92,27 @@ var ICalAttendee = function(_data, event) {
* @returns {ICalAttendee|String}
*/
this.status = function(status) {
if(!status) {
if(status === undefined) {
return data.status;
}
if(vars.allowedStatuses.indexOf(status.toUpperCase()) === -1) {
throw '`status` must be one of the following: ' + vars.allowedStatuses.join(', ') + '!';
}
data.status = getAllowedStatus(status);
return this;
};
data.status = status.toUpperCase();
/**
* Set/Get attendee's type (a.k.a. CUTYPE)
*
* @param {String} type
* @since 0.2.3
* @returns {ICalAttendee|String}
*/
this.type = function(type) {
if (type === undefined) {
return data.type;
}
data.type = getAllowedType(type);
return this;
};
......@@ -188,6 +200,11 @@ var ICalAttendee = function(_data, event) {
// ROLE
g += ';ROLE=' + data.role;
// TYPE
if (data.type) {
g += ';CUTYPE=' + data.type;
}
// PARTSTAT
if(data.status) {
g += ';PARTSTAT=' + data.status;
......@@ -214,10 +231,36 @@ var ICalAttendee = function(_data, event) {
for(i in _data) {
if(_data.hasOwnProperty(i) && ['name', 'email', 'role', 'status', 'delegatedTo', 'delegatedFrom', 'delegatesFrom', 'delegatesTo'].indexOf(i) > -1) {
if(_data.hasOwnProperty(i) && ['name', 'email', 'role', 'status', 'type', 'delegatedTo', 'delegatedFrom', 'delegatesFrom', 'delegatesTo'].indexOf(i) > -1) {
this[i](_data[i]);
}
}
function getAllowedRole(str) {
return getAllowedStringFor('role', str);
}
function getAllowedStatus(str) {
return getAllowedStringFor('status', str);
}
function getAllowedType(str){
return getAllowedStringFor('type', str);
}
function getAllowedStringFor(type, str) {
if (!str || typeof(str) !== 'string') {
throw new Error('Input for `' + type + '` must be a non-empty string. You gave ' + str);
}
str = str.toUpperCase();
if (vars.allowed[type].indexOf(str) === -1) {
throw new Error('`' + type + '` must be one of the following: ' + vars.allowed[type].join(', ') + '!');
}
return str;
}
};
module.exports = ICalAttendee;
\ No newline at end of file
module.exports = ICalAttendee;
......@@ -1228,6 +1228,37 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
});
describe('type()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAttendee();
assert.deepEqual(a, a.type('individual'));
});
it('getter should return value', function() {
var a = ical().createEvent().createAttendee().type('room');
assert.equal(a.type(), 'ROOM');
});
it('should throw error when method not allowed', function() {
var a = ical().createEvent().createAttendee();
assert.throws(function() {
a.type('DRINKING');
}, /`type`/);
});
it('should change something', function() {
var cal = ical(),
event = cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
});
event.createAttendee({email: 'mailing-list@example.com', type: 'group'});
assert.ok(cal.toString().indexOf('GROUP') > -1);
});
});
describe('delegatedTo()', function() {
it('setter should return this', function() {
var a = ical().createEvent().createAttendee();
......
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