Skip to content

Commit

Permalink
feat(Attendee): Add x() method for custom attributes
Browse files Browse the repository at this point in the history
Closes #183
  • Loading branch information
sebbo2002 committed Mar 23, 2021
1 parent f18a78f commit 5d9d686
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ interface ICalInternalAlarmData {
attach: ICalAttachment | null;
description: string | null;
x: [string, string][];
[key: string]: unknown;
}

interface ICalAlarmJSONData {
Expand Down
48 changes: 45 additions & 3 deletions src/attendee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';


import {checkEnum, checkNameAndMail, escape} from './tools';
import {addOrGetCustomAttributes, checkEnum, checkNameAndMail, escape, generateCustomAttributes} from './tools';
import ICalEvent from './event';


Expand All @@ -15,6 +15,7 @@ interface ICalInternalAttendeeData {
type: ICalAttendeeType | null;
delegatedTo: ICalAttendee | null;
delegatedFrom: ICalAttendee | null;
x: [string, string][];
}

export interface ICalAttendeeData {
Expand All @@ -29,6 +30,7 @@ export interface ICalAttendeeData {
delegatedFrom?: ICalAttendee | ICalAttendeeData | string | null;
delegatesTo?: ICalAttendee | ICalAttendeeData | string | null;
delegatesFrom?: ICalAttendee | ICalAttendeeData | string | null;
x?: {key: string, value: string}[] | [string, string][] | Record<string, string>;
}

interface ICalAttendeeJSONData {
Expand All @@ -41,6 +43,7 @@ interface ICalAttendeeJSONData {
type: ICalAttendeeType | null;
delegatedTo: string | null;
delegatedFrom: string | null;
x: {key: string, value: string}[];
}

export enum ICalAttendeeRole {
Expand Down Expand Up @@ -82,7 +85,8 @@ export default class ICalAttendee {
rsvp: null,
type: null,
delegatedTo: null,
delegatedFrom: null
delegatedFrom: null,
x: []
};
this.event = event;
if (!this.event) {
Expand All @@ -100,6 +104,7 @@ export default class ICalAttendee {
data.delegatedFrom && this.delegatedFrom(data.delegatedFrom);
data.delegatesTo && this.delegatesTo(data.delegatesTo);
data.delegatesFrom && this.delegatesFrom(data.delegatesFrom);
data.x && this.x(data.x);
}


Expand Down Expand Up @@ -323,6 +328,34 @@ export default class ICalAttendee {
return a;
}

/**
* Get/Set X-* attributes. Woun't filter double attributes,
* which are also added by another method (e.g. delegatesTo),
* so these attributes may be inserted twice.
*
* @since v2.0.0-develop.8
*/
x (keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): this;
x (keyOrArray: string, value: string): this;
x (): {key: string, value: string}[];
x (keyOrArray?: ({key: string, value: string})[] | [string, string][] | Record<string, string> | string, value?: string): this | void | ({key: string, value: string})[] {
if(keyOrArray === undefined) {
return addOrGetCustomAttributes (this.data);
}

if(typeof keyOrArray === 'string' && typeof value === 'string') {
addOrGetCustomAttributes (this.data, keyOrArray, value);
}
else if(typeof keyOrArray === 'object') {
addOrGetCustomAttributes (this.data, keyOrArray);
}
else {
throw new Error('Either key or value is not a string!');
}

return this;
}


/**
* Export calender as JSON Object to use it later…
Expand All @@ -331,7 +364,8 @@ export default class ICalAttendee {
toJSON(): ICalAttendeeJSONData {
return Object.assign({}, this.data, {
delegatedTo: this.data.delegatedTo?.email() || null,
delegatedFrom: this.data.delegatedFrom?.email() || null
delegatedFrom: this.data.delegatedFrom?.email() || null,
x: this.x()
});
}

Expand Down Expand Up @@ -386,7 +420,15 @@ export default class ICalAttendee {
g += ';EMAIL=' + escape(this.data.email);
}

// CUSTOM X ATTRIBUTES
if(this.data.x.length) {
g += ';' + this.data.x
.map(([key, value]) => key.toUpperCase() + '=' + escape(value))
.join(';');
}

g += ':MAILTO:' + escape(this.data.mailto || this.data.email) + '\r\n';

return g;
}
}
1 change: 1 addition & 0 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ export default class ICalEvent {
* the highest priority, 9 the lowest. 0 specifies an undefined
* priority.
*
* @since v2.0.0-develop.7
* @see https://www.kanzaki.com/docs/ical/priority.html
*/
priority(): number | null;
Expand Down
11 changes: 10 additions & 1 deletion test/attendee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ describe('ical-generator Attendee', function () {
});
});

describe('x()', function () {
it('works as expected', function () {
const a = new ICalAttendee({email: 'foo@example.org'}, new ICalEvent({}, new ICalCalendar()));
assert.deepStrictEqual(a, a.x('X-NUM-GUESTS', '5'));
assert.ok(a.toString().includes('ATTENDEE;ROLE=REQ-PARTICIPANT;X-NUM-GUESTS=5:MAILTO:foo@example.org'));
});
});

describe('toJSON()', function () {
it('should work', function() {
const a = new ICalAttendee({}, new ICalEvent({}, new ICalCalendar()));
Expand All @@ -338,7 +346,8 @@ describe('ical-generator Attendee', function () {
role: 'REQ-PARTICIPANT',
rsvp: null,
status: 'DELEGATED',
type: null
type: null,
x: []
});
});

Expand Down

0 comments on commit 5d9d686

Please sign in to comment.