Skip to content

Commit

Permalink
feat(Calendar): Add support for external VTimezone generator
Browse files Browse the repository at this point in the history
Closes  #122
  • Loading branch information
sebbo2002 committed Apr 14, 2021
1 parent 6bda01a commit f4bc8e0
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 25 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jobs:
steps:
- name: ☁️ Checkout Project
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: ☁️ Checkout ReleaseBot
uses: actions/checkout@v2
with:
Expand Down
193 changes: 191 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@semantic-release/github": "^7.2.0",
"@semantic-release/npm": "^7.1.0",
"@semantic-release/release-notes-generator": "^9.0.2",
"@touch4it/ical-timezones": "^1.6.0",
"@types/luxon": "^1.26.3",
"@types/mocha": "^8.2.2",
"@types/node": "^14.14.35",
Expand All @@ -60,6 +61,7 @@
"typescript": "^4.2.3"
},
"peerDependencies": {
"@touch4it/ical-timezones": "^1.6.0",
"@types/luxon": "^1.26.0",
"@types/mocha": "^8.2.1",
"@types/node": "^14.14.31",
Expand Down
48 changes: 39 additions & 9 deletions src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import ICalEvent, {ICalEventData, ICalEventJSONData} from './event';
import {writeFile, writeFileSync} from 'fs';
import {promises as fsPromises} from 'fs';
import {ServerResponse} from 'http';
import {ICalTimezone} from './types';


export interface ICalCalendarData {
prodId?: ICalCalendarProdIdData | string;
method?: ICalCalendarMethod | null;
name?: string | null;
description?: string | null;
timezone?: string | null;
timezone?: ICalTimezone | string | null;
url?: string | null;
scale?: string | null;
ttl?: number | Duration | null;
Expand All @@ -33,7 +34,7 @@ interface ICalCalendarInternalData {
method: ICalCalendarMethod | null;
name: string | null;
description: string | null;
timezone: string | null;
timezone: ICalTimezone | null;
url: string | null;
scale: string | null;
ttl: number | null;
Expand Down Expand Up @@ -289,13 +290,22 @@ export default class ICalCalendar {
*
* @since 0.2.0
*/
timezone(timezone: string | null): this;
timezone(timezone?: string | null): this | string | null {
timezone(timezone: ICalTimezone | string | null): this;
timezone(timezone?: ICalTimezone | string | null): this | string | null {
if (timezone === undefined) {
return this.data.timezone;
return this.data.timezone?.name || null;
}

if(typeof timezone === 'string') {
this.data.timezone = {name: timezone};
}
else if(timezone === null) {
this.data.timezone = null;
}
else {
this.data.timezone = timezone;
}

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

Expand Down Expand Up @@ -665,6 +675,7 @@ export default class ICalCalendar {
*/
toJSON(): ICalCalendarJSONData {
return Object.assign({}, this.data, {
timezone: this.timezone(),
events: this.data.events.map(event => event.toJSON()),
x: this.x()
});
Expand Down Expand Up @@ -723,9 +734,28 @@ export default class ICalCalendar {
}

// Timezone
if (this.data.timezone) {
g += 'TIMEZONE-ID:' + this.data.timezone + '\r\n';
g += 'X-WR-TIMEZONE:' + this.data.timezone + '\r\n';
if(this.data.timezone?.generator) {
const timezones = [...new Set([
this.timezone(),
...this.data.events.map(event => event.timezone())
])].filter(tz => tz !== null && !tz.startsWith('/')) as string[];

timezones.forEach(tz => {
if(!this.data.timezone?.generator) {
return;
}

const s = this.data.timezone.generator(tz);
if(!s) {
return;
}

g += s.replace(/\n/g, '\r\n');
});
}
if (this.data.timezone?.name) {
g += 'TIMEZONE-ID:' + this.data.timezone.name + '\r\n';
g += 'X-WR-TIMEZONE:' + this.data.timezone.name + '\r\n';
}

// TTL
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ export {
ICalOrganizer,
ICalDescription,
ICalEventRepeatingFreq,
ICalWeekday
ICalWeekday,
ICalTimezone
} from './types';
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface ICalDescription {
html?: string;
}

export interface ICalTimezone {
name: string | null;
generator?: (timezone: string) => string|null;
}

export enum ICalEventRepeatingFreq {
SECONDLY = 'SECONDLY',
MINUTELY = 'MINUTELY',
Expand Down
Loading

0 comments on commit f4bc8e0

Please sign in to comment.