Skip to content

Commit

Permalink
feat(Event): Made ICalEvent.location.title optional to allow settin…
Browse files Browse the repository at this point in the history
…g `GEO` without title

close #578

BREAKING CHANGE: [ICalEvent.location()](https://sebbo2002.github.io/ical-generator/develop/reference/classes/ICalEvent.html#location)'s `title` field can now be undefined
  • Loading branch information
sebbo2002 committed Feb 20, 2024
1 parent a66adf1 commit 42be230
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
31 changes: 24 additions & 7 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ export default class ICalEvent {

/**
* Set the event's location by passing a string (minimum) or
* an {@link ICalLocation} object which will also fill the iCal
* an {@link ICalLocationWithTitle} object which will also fill the iCal
* `GEO` attribute and Apple's `X-APPLE-STRUCTURED-LOCATION`.
*
* ```javascript
Expand All @@ -904,6 +904,22 @@ export default class ICalEvent {
* GEO:52.50363;13.32865
* ```
*
* Since v6.1.0 you can also pass a {@link ICalLocationWithoutTitle} object to pass
* the geolocation only. This will only fill the iCal `GEO` attribute.
*
* ```javascript
* event.location({
* geo: {
* lat: 52.503630,
* lon: 13.328650
* }
* });
* ```
*
* ```text
* GEO:52.50363;13.32865
* ```
*
* @since 0.2.0
*/
location(location: ICalLocation | string | null): this;
Expand All @@ -917,10 +933,11 @@ export default class ICalEvent {
};
return this;
}
if (
(!location?.title && !location?.geo) ||
(location?.geo && (!isFinite(location.geo.lat) || !isFinite(location.geo.lon)))
) {
if (location && (
('title' in location && !location.title) ||
(location?.geo && (!isFinite(location.geo.lat) || !isFinite(location.geo.lon))) ||
(!('title' in location) && !location?.geo)
)) {
throw new Error(
'`location` isn\'t formatted correctly. See https://sebbo2002.github.io/ical-generator/'+
'develop/reference/classes/ICalEvent.html#location'
Expand Down Expand Up @@ -1751,7 +1768,7 @@ export default class ICalEvent {
}

// LOCATION
if (this.data.location?.title) {
if (this.data.location && 'title' in this.data.location && this.data.location.title) {
g += 'LOCATION:' + escape(
this.data.location.title +
(this.data.location.address ? '\n' + this.data.location.address : ''),
Expand All @@ -1769,7 +1786,7 @@ export default class ICalEvent {
}

// GEO
if (this.data.location.geo) {
if (this.data.location && 'geo' in this.data.location && this.data.location.geo) {
g += 'GEO:' + escape(this.data.location.geo?.lat, false) + ';' +
escape(this.data.location.geo?.lon, false) + '\r\n';
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export {
ICalDateTimeValue,
ICalRepeatingOptions,
ICalLocation,
ICalLocationWithTitle,
ICalLocationWithoutTitle,
ICalGeo,
ICalOrganizer,
ICalDescription,
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ export interface ICalRepeatingOptions {
startOfWeek?: ICalWeekday;
}

export interface ICalLocation {
export type ICalLocation = ICalLocationWithTitle | ICalLocationWithoutTitle;

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

export interface ICalLocationWithoutTitle {
geo: ICalGeo;
}

export interface ICalGeo {
lat: number;
lon: number;
Expand Down
14 changes: 11 additions & 3 deletions test/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,12 @@ describe('ical-generator Event', function () {
title: 'Foo',
geo: {lat: 44.5, lon: -3.4}
});
assert.deepStrictEqual(e.location()?.title, 'Foo');
assert.deepStrictEqual(e.location()?.geo, {lat: 44.5, lon: -3.4});

const location = e.location();
assert.ok(location);
assert.ok('title' in location);
assert.deepStrictEqual(location?.title, 'Foo');
assert.deepStrictEqual(location?.geo, {lat: 44.5, lon: -3.4});

e.location(null);
assert.strictEqual(e.location(), null);
Expand All @@ -1245,7 +1249,11 @@ describe('ical-generator Event', function () {
}, new ICalCalendar());

event.location('Europa-Park');
assert.strictEqual(event.location()?.title, 'Europa-Park');

const location = event.location();
assert.ok(location);
assert.ok('title' in location);
assert.strictEqual(location?.title, 'Europa-Park');
});

it('should throw error when location is not valid', function () {
Expand Down
32 changes: 32 additions & 0 deletions test/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,36 @@ describe('Issues', function () {
].join('\r\n'));
});
});

describe('Issue #569 / 570', function () {
it('event.location should work with `geo` only', function () {
const event = ical().createEvent({
id: '12345',
summary: 'Hello',
start: new Date('2020-06-15T00:00:00Z'),
end: new Date('2020-06-15T01:00:00Z'),
stamp: new Date('2020-06-15T00:00:00Z')
});

event.location({
geo: {
lat: 52.51147570081018,
lon: 13.342200696373846
}
});

assert.strictEqual(event.toString(), [
'BEGIN:VEVENT',
'UID:12345',
'SEQUENCE:0',
'DTSTAMP:20200615T000000Z',
'DTSTART:20200615T000000Z',
'DTEND:20200615T010000Z',
'SUMMARY:Hello',
'GEO:52.51147570081018;13.342200696373846',
'END:VEVENT',
''
].join('\r\n'));
});
});
});

0 comments on commit 42be230

Please sign in to comment.