From 8450492525d43d0de9d01a8f756cb76c43a77ff3 Mon Sep 17 00:00:00 2001 From: Sebastian Pekarek Date: Tue, 23 Mar 2021 20:37:10 +0100 Subject: [PATCH] feat(Event): Make organizer.email optional Closes #137 --- src/event.ts | 6 +++++- src/tools.ts | 5 +---- src/types.ts | 2 +- test/event.ts | 19 ++++++++++++++----- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/event.ts b/src/event.ts index ec49d9376..76ca66ded 100755 --- a/src/event.ts +++ b/src/event.ts @@ -994,10 +994,14 @@ export default class ICalEvent { // ORGANIZER if (this.data.organizer) { g += 'ORGANIZER;CN="' + escape(this.data.organizer.name) + '"'; + if (this.data.organizer.email && this.data.organizer.mailto) { g += ';EMAIL=' + escape(this.data.organizer.email); } - g += ':mailto:' + escape(this.data.organizer.mailto || this.data.organizer.email) + '\r\n'; + if(this.data.organizer.email) { + g += ':mailto:' + escape(this.data.organizer.mailto || this.data.organizer.email); + } + g += '\r\n'; } // ATTENDEES diff --git a/src/tools.ts b/src/tools.ts index 349f176c9..f045ca66c 100755 --- a/src/tools.ts +++ b/src/tools.ts @@ -236,10 +236,7 @@ export function checkNameAndMail (attribute: string, value: string | ICalOrganiz } if (!result.name) { - throw new Error('`organizer.name` is empty!'); - } - if (!result.email) { - throw new Error('`organizer.email` is empty!'); + throw new Error('`' + attribute + '.name` is empty!'); } return result; diff --git a/src/types.ts b/src/types.ts index 40472e4be..2a182f00a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -33,7 +33,7 @@ export interface ICalGeo { export interface ICalOrganizer { name: string; - email: string; + email?: string; mailto?: string; } diff --git a/test/event.ts b/test/event.ts index a9f7e3a4c..95da2bf56 100644 --- a/test/event.ts +++ b/test/event.ts @@ -1197,11 +1197,6 @@ describe('ical-generator Event', function () { it('should throw error when object misses data', function () { const e = new ICalEvent({}, new ICalCalendar()); - assert.throws(function () { - // @ts-ignore - e.organizer({name: 'Sebastian Pekarek'}); - }, /`organizer\.email`/); - assert.throws(function () { // @ts-ignore e.organizer({email: 'foo'}); @@ -1219,6 +1214,20 @@ describe('ical-generator Event', function () { e.organizer(NaN); }, /`organizer`/); }); + + it('should work without an email', function () { + const event = new ICalEvent({ + start: moment(), + summary: 'Example Event' + }, new ICalCalendar()); + + event.organizer({name: 'Sebastian Pekarek'}); + assert.deepStrictEqual(event.organizer(), { + name: 'Sebastian Pekarek', + email: undefined, + mailto: undefined + }); + }); }); describe('createAttendee()', function () {