Skip to content

Commit

Permalink
More flexibility for iCalendar clients
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Oct 21, 2021
1 parent bafe02b commit 09e8a4a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions icalendar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export function eventToIcal(e: Event, options: HebrewCalendar.Options): string;
* @returns multi-line result, delimited by \r\n
*/
export async function eventsToIcalendar(events: Event[], options: HebrewCalendar.Options): string;
/**
* Generates an RFC 2445 iCalendar string from an array of IcalEvents
* @returns multi-line result, delimited by \r\n
*/
export async function icalEventsToString(icals: IcalEvent[], options: HebrewCalendar.Options): string;

/**
* Represents an RFC 2445 iCalendar VEVENT
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/icalendar",
"version": "4.13.2",
"version": "4.14.0",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"ical",
Expand All @@ -24,8 +24,8 @@
"url": "https://github.com/hebcal/hebcal-icalendar/issues"
},
"dependencies": {
"@hebcal/core": "^3.26.5",
"@hebcal/rest-api": "^3.8.3"
"@hebcal/core": "^3.26.7",
"@hebcal/rest-api": "^3.8.4"
},
"scripts": {
"build": "rollup -c",
Expand Down
39 changes: 28 additions & 11 deletions src/icalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ export class IcalEvent {
const timed = this.timed = Boolean(ev.eventTime);
const locale = options.locale;
let subj = timed || (ev.getFlags() & BRIEF_FLAGS) ? ev.renderBrief(locale) : ev.render(locale);
if (timed && options.location && options.location.name) {
const comma = options.location.name.indexOf(',');
this.locationName = (comma == -1) ? options.location.name : options.location.name.substring(0, comma);
}
const mask = ev.getFlags();
if (mask & flags.DAF_YOMI) {
if (ev.locationName) {
this.locationName = ev.locationName;
} else if (mask & flags.DAF_YOMI) {
this.locationName = Locale.gettext('Daf Yomi');
} else if (timed && options.location && options.location.name) {
const comma = options.location.name.indexOf(',');
this.locationName = (comma == -1) ? options.location.name : options.location.name.substring(0, comma);
}

const date = IcalEvent.formatYYYYMMDD(ev.getDate().greg());
this.startDate = date;
this.dtargs = '';
Expand Down Expand Up @@ -124,15 +124,17 @@ export class IcalEvent {
this.subj = subj;

const isUserEvent = Boolean(mask & flags.USER_EVENT);
if (mask & flags.OMER_COUNT) {
if (ev.alarm) {
this.alarm = ev.alarm;
} else if (mask & flags.OMER_COUNT) {
this.alarm = '0DT3H30M0S'; // 8:30pm Omer alarm evening before
} else if (isUserEvent) {
this.alarm = '0DT12H0M0S'; // noon the day before
} else if (timed && ev.getDesc().startsWith('Candle lighting')) {
this.alarm = '0DT0H10M0S'; // ten minutes
}

this.category = CATEGORY[getEventCategories(ev)[0]];
this.category = ev.category || CATEGORY[getEventCategories(ev)[0]];
}

/**
Expand Down Expand Up @@ -348,11 +350,27 @@ function createMemo(e, options) {
export async function eventsToIcalendar(events, options) {
if (!events.length) throw new RangeError('Events can not be empty');
if (!options) throw new TypeError('Invalid options object');
const opts = Object.assign({}, options);
opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
if (!opts.title) {
opts.title = getCalendarTitle(events, opts);
}
const icals = events.map((ev) => new IcalEvent(ev, opts));
return icalEventsToString(icals, opts);
}

/**
* Generates an RFC 2445 iCalendar string from an array of IcalEvents
* @param {IcalEvent[]} icals
* @param {HebrewCalendar.Options} options
* @return {string}
*/
export async function icalEventsToString(icals, options) {
const stream = [];
const uclang = Locale.getLocaleName().toUpperCase();
const opts = Object.assign({}, options);
opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
const title = opts.title ? IcalEvent.escape(opts.title) : getCalendarTitle(events, opts);
const title = opts.title ? IcalEvent.escape(opts.title) : 'Untitled';
const caldesc = opts.caldesc ? IcalEvent.escape(opts.caldesc) :
opts.yahrzeit ?
'Yahrzeits + Anniversaries from www.hebcal.com' :
Expand Down Expand Up @@ -404,8 +422,7 @@ export async function eventsToIcalendar(events, options) {
}
}

for (const ev of events) {
const ical = new IcalEvent(ev, opts);
for (const ical of icals) {
const lines = ical.getLines();
for (const line of lines) {
stream.push(line);
Expand Down

0 comments on commit 09e8a4a

Please sign in to comment.