Skip to content

Commit

Permalink
feat: Merge event's description() and htmlDescription()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `htmlDescription()` was removed, use `description()` instead.
  • Loading branch information
sebbo2002 committed Mar 23, 2021
1 parent 9771e20 commit ce537f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 66 deletions.
59 changes: 25 additions & 34 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import ICalCategory, {ICalCategoryData} from './category';
import ICalCalendar from './calendar';
import {
ICalDateTimeValue,
ICalDescription,
ICalEventRepeatingFreq,
ICalGeo,
ICalLocation,
ICalOrganizer,
ICalRepeatingOptions,
Expand Down Expand Up @@ -60,8 +60,7 @@ export interface ICalEventData {
repeating?: ICalRepeatingOptions | null,
summary?: string,
location?: ICalLocation | string | null,
description?: string | null,
htmlDescription?: string | null,
description?: ICalDescription | string | null,
organizer?: ICalOrganizer | string | null,
attendees?: ICalAttendee[] | ICalAttendeeData[],
alarms?: ICalAlarm[] | ICalAlarmData[],
Expand All @@ -72,7 +71,7 @@ export interface ICalEventData {
transparency?: ICalEventTransparency | null,
created?: ICalDateTimeValue | null,
lastModified?: ICalDateTimeValue | null,
x?: ({key: string, value: string})[] | [string, string][] | Record<string, string>;
x?: ({ key: string, value: string })[] | [string, string][] | Record<string, string>;
}

export interface ICalEventInternalData {
Expand All @@ -88,8 +87,7 @@ export interface ICalEventInternalData {
repeating: ICalEventInternalRepeatingData | null,
summary: string,
location: ICalLocation | null,
description: string | null,
htmlDescription: string | null,
description: ICalDescription | null,
organizer: ICalOrganizer | null,
attendees: ICalAttendee[],
alarms: ICalAlarm[],
Expand Down Expand Up @@ -140,7 +138,6 @@ export default class ICalEvent {
summary: '',
location: null,
description: null,
htmlDescription: null,
organizer: null,
attendees: [],
alarms: [],
Expand Down Expand Up @@ -172,7 +169,6 @@ export default class ICalEvent {
data.summary && this.summary(data.summary);
data.location && this.location(data.location);
data.description && this.description(data.description);
data.htmlDescription && this.htmlDescription(data.htmlDescription);
data.organizer && this.organizer(data.organizer);
data.attendees && this.attendees(data.attendees);
data.alarms && this.alarms(data.alarms);
Expand Down Expand Up @@ -537,29 +533,23 @@ export default class ICalEvent {
* Set/Get the event's description
* @since 0.2.0
*/
description(): string | null;
description(description: string | null): this;
description(description?: string | null): this | string | null {
description(): ICalDescription | null;
description(description: ICalDescription | string | null): this;
description(description?: ICalDescription | string | null): this | ICalDescription | null {
if (description === undefined) {
return this.data.description;
}

this.data.description = description ? String(description) : null;
return this;
}

/**
* Set/Get the event's HTML description
* @since 0.2.8
*/
htmlDescription(): string | null;
htmlDescription(description: string | null): this;
htmlDescription(description?: string | null): this | string | null {
if (description === undefined) {
return this.data.htmlDescription;
if (description === null) {
this.data.description = null;
return this;
}

this.data.htmlDescription = description ? String(description) : null;
if (typeof description === 'string') {
this.data.description = {plain: description};
}
else {
this.data.description = description;
}
return this;
}

Expand Down Expand Up @@ -801,7 +791,7 @@ export default class ICalEvent {
* @since 1.9.0
* @returns {ICalEvent|Array<Object<{key: String, value: String}>>}
*/
x (keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): this;
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 })[] {
Expand Down Expand Up @@ -961,23 +951,24 @@ export default class ICalEvent {
'X-APPLE-RADIUS=' + escape(this.data.location.radius) + ';' +
'X-TITLE=' + escape(this.data.location.title) +
':geo:' + escape(this.data.location.geo?.lat) + ',' + escape(this.data.location.geo?.lon) + '\r\n';
} else {
}
else {
g += 'LOCATION:' + escape(this.data.location.title) + '\r\n';
}

if(this.data.location.geo) {
if (this.data.location.geo) {
g += 'GEO:' + escape(this.data.location.geo?.lat) + ';' + escape(this.data.location.geo?.lon) + '\r\n';
}
}

// DESCRIPTION
if (this.data.description) {
g += 'DESCRIPTION:' + escape(this.data.description) + '\r\n';
}
g += 'DESCRIPTION:' + escape(this.data.description.plain) + '\r\n';

// HTML DESCRIPTION
if (this.data.htmlDescription) {
g += 'X-ALT-DESC;FMTTYPE=text/html:' + escape(this.data.htmlDescription) + '\r\n';
// HTML DESCRIPTION
if (this.data.description.html) {
g += 'X-ALT-DESC;FMTTYPE=text/html:' + escape(this.data.description.html) + '\r\n';
}
}

// ORGANIZER
Expand Down
6 changes: 4 additions & 2 deletions test/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ describe('ical-generator Cases', function () {
stamp: new Date('Fr Oct 04 2013 23:34:53 UTC'),
summary: 'Sample Event',
location: 'localhost',
description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\nbeep boop',
htmlDescription: '<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\nbeep boop</p>'
description: {
plain: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\nbeep boop',
html: '<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\nbeep boop</p>'
}
});

const string = cal.toString();
Expand Down
50 changes: 20 additions & 30 deletions test/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,18 @@ describe('ical-generator Event', function () {
assert.strictEqual(e.description(), null);

e.description('I don\'t need a description. I\'m far to awesome for descriptions…');
assert.strictEqual(e.description(), 'I don\'t need a description. I\'m far to awesome for descriptions…');
assert.deepStrictEqual(e.description(), {
plain: 'I don\'t need a description. I\'m far to awesome for descriptions…'
});

e.description({
plain: 'I don\'t need a description. I\'m far to awesome for descriptions…',
html: 'I don\'t need a description.<br />I\'m far to awesome for descriptions…'
});
assert.deepStrictEqual(e.description(), {
plain: 'I don\'t need a description. I\'m far to awesome for descriptions…',
html: 'I don\'t need a description.<br />I\'m far to awesome for descriptions…'
});

e.description(null);
assert.strictEqual(e.description(), null);
Expand All @@ -1048,36 +1059,15 @@ describe('ical-generator Event', function () {
}, new ICalCalendar());

event.description('Well. But other people need descriptions… :/');
assert.strictEqual(event.description(), 'Well. But other people need descriptions… :/');
});
});

describe('htmlDescription()', function () {
it('getter should return value', function () {
const e = new ICalEvent({}, new ICalCalendar());
assert.strictEqual(e.htmlDescription(), null);

e.htmlDescription('<marquee>I\'m the best HTML tag in this universe!</marquee>');
assert.strictEqual(e.htmlDescription(), '<marquee>I\'m the best HTML tag in this universe!</marquee>');

e.htmlDescription(null);
assert.strictEqual(e.htmlDescription(), null);
});

it('setter should return this', function () {
const e = new ICalEvent({}, new ICalCalendar());
assert.deepStrictEqual(e, e.htmlDescription(null));
assert.deepStrictEqual(e, e.htmlDescription('I don\'t need a description. I\'m far to awesome for descriptions…'));
});

it('should change something', function () {
const event = new ICalEvent({
start: moment(),
summary: 'Example Event'
}, new ICalCalendar());
assert.deepStrictEqual(event.description(), {
plain: 'Well. But other people need descriptions… :/'
});

event.htmlDescription('<marquee>I\'m the best HTML tag in this universe!</marquee>');
assert.strictEqual(event.htmlDescription(), '<marquee>I\'m the best HTML tag in this universe!</marquee>');
event.description({
plain: 'I am uncool text.',
html: '<marquee>I\'m the best HTML tag in this universe!</marquee>'
});
assert.ok(event.toString().includes('<marquee>I\'m the best HTML tag in this universe!</marquee>'));
});
});

Expand Down

0 comments on commit ce537f8

Please sign in to comment.