Skip to content

Commit

Permalink
feat(Event): Merge location(), appleLocation() and geo()
Browse files Browse the repository at this point in the history
Closes #187

BREAKING CHANGE: `geo()` and `appleLocation()` are not available anymore, use `location()` instead and pass an location object (with title, radius, etc.)
  • Loading branch information
sebbo2002 committed Mar 22, 2021
1 parent 3879576 commit 62c1516
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 328 deletions.
18 changes: 9 additions & 9 deletions src/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ export default class ICalAlarm {
throw new Error('`event` option required!');
}

data?.type && this.type(data.type);
data?.trigger && this.trigger(data.trigger);
data?.triggerBefore && this.triggerBefore(data.triggerBefore);
data?.triggerAfter && this.triggerAfter(data.triggerAfter);
data?.repeat && this.repeat(data.repeat);
data?.interval && this.interval(data.interval);
data?.attach && this.attach(data.attach);
data?.description && this.description(data.description);
data?.x && this.x(data.x);
data.type && this.type(data.type);
data.trigger && this.trigger(data.trigger);
data.triggerBefore && this.triggerBefore(data.triggerBefore);
data.triggerAfter && this.triggerAfter(data.triggerAfter);
data.repeat && this.repeat(data.repeat);
data.interval && this.interval(data.interval);
data.attach && this.attach(data.attach);
data.description && this.description(data.description);
data.x && this.x(data.x);
}


Expand Down
22 changes: 11 additions & 11 deletions src/attendee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ export default class ICalAttendee {
throw new Error('`event` option required!');
}

data?.name && this.name(data.name);
data?.email && this.email(data.email);
data?.mailto && this.mailto(data.mailto);
data?.status && this.status(data.status);
data?.role && this.role(data.role);
data?.rsvp && this.rsvp(data.rsvp);
data?.type && this.type(data.type);
data?.delegatedTo && this.delegatedTo(data.delegatedTo);
data?.delegatedFrom && this.delegatedFrom(data.delegatedFrom);
data?.delegatesTo && this.delegatesTo(data.delegatesTo);
data?.delegatesFrom && this.delegatesFrom(data.delegatesFrom);
data.name && this.name(data.name);
data.email && this.email(data.email);
data.mailto && this.mailto(data.mailto);
data.status && this.status(data.status);
data.role && this.role(data.role);
data.rsvp && this.rsvp(data.rsvp);
data.type && this.type(data.type);
data.delegatedTo && this.delegatedTo(data.delegatedTo);
data.delegatedFrom && this.delegatedFrom(data.delegatedFrom);
data.delegatesTo && this.delegatesTo(data.delegatesTo);
data.delegatesFrom && this.delegatesFrom(data.delegatesFrom);
}


Expand Down
22 changes: 11 additions & 11 deletions src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export enum ICalCalendarMethod {
export default class ICalCalendar {
private readonly data: ICalCalendarInternalData;

constructor(data?: ICalCalendarData) {
constructor(data: ICalCalendarData = {}) {
this.data = {
prodId: '//sebbo.net//ical-generator//EN',
method: null,
Expand All @@ -69,16 +69,16 @@ export default class ICalCalendar {
x: []
};

data?.prodId && this.prodId(data.prodId);
data?.method && this.method(data.method);
data?.name && this.name(data.name);
data?.description && this.description(data.description);
data?.timezone && this.timezone(data.timezone);
data?.url && this.url(data.url);
data?.scale && this.scale(data.scale);
data?.ttl && this.ttl(data.ttl);
data?.events && this.events(data.events);
data?.x && this.x(data.x);
data.prodId && this.prodId(data.prodId);
data.method && this.method(data.method);
data.name && this.name(data.name);
data.description && this.description(data.description);
data.timezone && this.timezone(data.timezone);
data.url && this.url(data.url);
data.scale && this.scale(data.scale);
data.ttl && this.ttl(data.ttl);
data.events && this.events(data.events);
data.x && this.x(data.x);
}


Expand Down
2 changes: 1 addition & 1 deletion src/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class ICalCategory {
name: null
};

data?.name && this.name(data.name);
data.name && this.name(data.name);
}


Expand Down
157 changes: 54 additions & 103 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ export interface ICalEventData {
floating?: boolean,
repeating?: ICalRepeatingOptions | null,
summary?: string,
location?: string | null,
appleLocation?: ICalLocation | null,
geo?: ICalGeo | null,
location?: ICalLocation | string | null,
description?: string | null,
htmlDescription?: string | null,
organizer?: ICalOrganizer | string | null,
Expand Down Expand Up @@ -87,9 +85,7 @@ export interface ICalEventInternalData {
floating: boolean,
repeating: ICalEventInternalRepeatingData | null,
summary: string,
location: string | null,
appleLocation: ICalLocation | null,
geo: ICalGeo | null,
location: ICalLocation | null,
description: string | null,
htmlDescription: string | null,
organizer: ICalOrganizer | null,
Expand Down Expand Up @@ -141,8 +137,6 @@ export default class ICalEvent {
repeating: null,
summary: '',
location: null,
appleLocation: null,
geo: null,
description: null,
htmlDescription: null,
organizer: null,
Expand All @@ -163,33 +157,31 @@ export default class ICalEvent {
throw new Error('`calendar` option required!');
}

data?.id && this.id(data.id);
data?.sequence && this.sequence(data.sequence);
data?.start && this.start(data.start);
data?.end && this.end(data.end);
data?.recurrenceId && this.recurrenceId(data.recurrenceId);
data?.timezone && this.timezone(data.timezone);
data?.stamp && this.stamp(data.stamp);
data?.allDay && this.allDay(data.allDay);
data?.floating && this.floating(data.floating);
data?.repeating && this.repeating(data.repeating);
data?.summary && this.summary(data.summary);
data?.location && this.location(data.location);
data?.appleLocation && this.appleLocation(data.appleLocation);
data?.geo && this.geo(data.geo);
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);
data?.categories && this.categories(data.categories);
data?.status && this.status(data.status);
data?.busystatus && this.busystatus(data.busystatus);
data?.url && this.url(data.url);
data?.transparency && this.transparency(data.transparency);
data?.created && this.created(data.created);
data?.lastModified && this.lastModified(data.lastModified);
data?.x && this.x(data.x);
data.id && this.id(data.id);
data.sequence && this.sequence(data.sequence);
data.start && this.start(data.start);
data.end && this.end(data.end);
data.recurrenceId && this.recurrenceId(data.recurrenceId);
data.timezone && this.timezone(data.timezone);
data.stamp && this.stamp(data.stamp);
data.allDay && this.allDay(data.allDay);
data.floating && this.floating(data.floating);
data.repeating && this.repeating(data.repeating);
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);
data.categories && this.categories(data.categories);
data.status && this.status(data.status);
data.busystatus && this.busystatus(data.busystatus);
data.url && this.url(data.url);
data.transparency && this.transparency(data.transparency);
data.created && this.created(data.created);
data.lastModified && this.lastModified(data.lastModified);
data.x && this.x(data.x);
}

/**
Expand Down Expand Up @@ -513,69 +505,28 @@ export default class ICalEvent {

/**
* Set/Get the event's location
*
* @param {String} [location]
* @since 0.2.0
* @returns {ICalEvent|String}
*/
location(): string | null;
location(location: string | null): this;
location(location?: string | null): this | string | null {
location(): ICalLocation | null;
location(location: ICalLocation | string | null): this;
location(location?: ICalLocation | string | null): this | ICalLocation | null {
if (location === undefined) {
return this.data.location;
}
if (this.data.appleLocation && location) {
this.data.appleLocation = null;
}

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

/**
* Set/Get the Apple event's location
* @since 1.10.0
*/
appleLocation(): ICalLocation | null;
appleLocation(location: ICalLocation | null): this;
appleLocation(location?: ICalLocation | null): this | ICalLocation | null {
if (location === undefined) {
return this.data.appleLocation;
}
if (location === null) {
this.data.appleLocation = null;
return this;
}

if (!location.title || !location.address || !location.radius || !location.geo || !location.geo.lat || !location.geo.lon) {
throw new Error('`appleLocation` isn\'t formatted correctly. See https://github.com/sebbo2002/ical-generator#applelocationobject-applelocation');
}

this.data.appleLocation = location;
this.data.location = location.title + '\n' + location.address;
return this;
}

/**
* Set/Get the event's geo
* @since 1.5.0
*/
geo(): ICalGeo | null;
geo(geo: ICalGeo | null): this;
geo(geo?: ICalGeo | null): this | ICalGeo | null {
if (geo === undefined) {
return this.data.geo;
}
if (geo === null) {
this.data.geo = null;
if (typeof location === 'string') {
this.data.location = {
title: location
};
return this;
}

if ((!geo || !isFinite(geo.lat) || !isFinite(geo.lon))) {
throw new Error('`geo` isn\'t formated correctly. See https://github.com/sebbo2002/ical-generator#geostringobject-geo');
if (
(location && !location.title) ||
(location?.geo && (!isFinite(location.geo.lat) || !isFinite(location.geo.lon)))
) {
throw new Error('`location` isn\'t formatted correctly. See https://github.com/sebbo2002/ical-generator#locationobject-location');
}

this.data.geo = geo;
this.data.location = location || null;
return this;
}

Expand Down Expand Up @@ -1000,21 +951,21 @@ export default class ICalEvent {
}

// LOCATION
if (this.data.location) {
g += 'LOCATION:' + escape(this.data.location) + '\r\n';
}

// APPLE LOCATION
if (this.data.appleLocation) {
g += 'X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-ADDRESS=' + escape(this.data.appleLocation.address) + ';' +
'X-APPLE-RADIUS=' + escape(this.data.appleLocation.radius) + ';' +
'X-TITLE=' + escape(this.data.appleLocation.title) +
':geo:' + escape(this.data.appleLocation.geo.lat) + ',' + escape(this.data.appleLocation.geo.lon) + '\r\n';
}
if (this.data.location?.title) {
if (this.data.location.address && this.data.location.radius && this.data.location.geo) {
g += 'LOCATION:' + escape(this.data.location.title + '\n' + this.data.location.address) + '\r\n';

g += 'X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-ADDRESS=' + escape(this.data.location.address) + ';' +
'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 {
g += 'LOCATION:' + escape(this.data.location.title) + '\r\n';
}

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

// DESCRIPTION
Expand Down
4 changes: 2 additions & 2 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateo
) : '');
}
else if(isLuxonDate(d)) {
let m = timezone ? d.setZone(timezone) : (floating ? d : d.setZone('utc'));
const m = timezone ? d.setZone(timezone) : (floating ? d : d.setZone('utc'));
if(!m.isValid) {
m = d;
throw new Error('Unable to format DateTime: value is not valid!');
}

return m.toFormat('yyyyLLdd') + (!dateonly ? (
Expand Down
8 changes: 3 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export interface ICalRepeatingOptions {

export interface ICalLocation {
title: string;
address: string;
radius: number;
geo: ICalGeo;
address?: string;
radius?: number;
geo?: ICalGeo;
}

export interface ICalGeo {
Expand Down Expand Up @@ -56,5 +56,3 @@ export enum ICalWeekday {
FR = 'FR',
SA = 'SA'
}


6 changes: 6 additions & 0 deletions test/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ describe('ical-generator Cases', function () {
allDay: true,
stamp: new Date('Fr Oct 04 2013 23:34:53 UTC'),
summary: 'Sample Event',
location: {
title: 'Apple Store Kurfürstendamm',
address: 'Kurfürstendamm 26, 10719 Berlin, Deutschland',
radius: 141.1751386318387,
geo: {lat: 52.503630, lon: 13.328650}
},
organizer: 'Sebastian Pekarek <mail@sebbo.net>',
status: ICalEventStatus.CONFIRMED,
categories: [{name: 'WORK'}],
Expand Down
Loading

0 comments on commit 62c1516

Please sign in to comment.