Skip to content

Commit

Permalink
Allow VALARM with a VALUE=DATE-TIME trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Apr 26, 2022
1 parent e4642db commit 9304a8a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/icalendar",
"version": "4.16.0",
"version": "4.17.0",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"ical",
Expand All @@ -24,7 +24,7 @@
"url": "https://github.com/hebcal/hebcal-icalendar/issues"
},
"dependencies": {
"@hebcal/core": "^3.35.0",
"@hebcal/core": "^3.36.0",
"@hebcal/rest-api": "^3.13.0",
"murmurhash3": "^0.5.0"
},
Expand Down
40 changes: 26 additions & 14 deletions src/icalendar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {flags, Locale} from '@hebcal/core';
import {flags, Locale, greg} from '@hebcal/core';
import {murmur32HexSync} from 'murmurhash3';
import {pad2, pad4, getCalendarTitle, makeAnchor, getEventCategories,
getHolidayDescription, makeTorahMemoText, appendIsraelAndTracking,
Expand Down Expand Up @@ -126,19 +126,30 @@ export class IcalEvent {
}
}
this.subj = subj;
this.category = ev.category || CATEGORY[getEventCategories(ev)[0]];
}

const isUserEvent = Boolean(mask & flags.USER_EVENT);
if (ev.alarm) {
this.alarm = ev.alarm;

/**
* @return {string}
*/
getAlarm() {
const ev = this.ev;
const mask = ev.getFlags();
const evAlarm = ev.alarm;
if (typeof evAlarm === 'string') {
return 'TRIGGER:' + evAlarm;
} else if (greg.isDate(evAlarm)) {
evAlarm.setSeconds(0);
return 'TRIGGER;VALUE=DATE-TIME:' + IcalEvent.makeDtstamp(evAlarm);
} else if (mask & flags.OMER_COUNT) {
this.alarm = '-P0DT3H30M0S'; // 8:30pm Omer alarm evening before
} else if (isUserEvent) {
this.alarm = '-P0DT12H0M0S'; // noon the day before
} else if (timed && ev.getDesc().startsWith('Candle lighting')) {
this.alarm = '-P0DT0H10M0S'; // ten minutes
return 'TRIGGER:-P0DT3H30M0S'; // 8:30pm Omer alarm evening before
} else if (mask & flags.USER_EVENT) {
return 'TRIGGER:-P0DT12H0M0S'; // noon the day before
} else if (this.timed && ev.getDesc().startsWith('Candle lighting')) {
return 'TRIGGER:-P0DT0H10M0S';
}

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

/**
Expand Down Expand Up @@ -197,12 +208,13 @@ export class IcalEvent {
arr.push('GEO:' + options.location.latitude + ';' + options.location.longitude);
}

if (this.alarm) {
const trigger = this.getAlarm();
if (trigger) {
arr.push(
'BEGIN:VALARM',
'ACTION:DISPLAY',
'DESCRIPTION:This is an event reminder',
`TRIGGER:${this.alarm}`,
'DESCRIPTION:Event reminder',
`${trigger}`,
'END:VALARM',
);
}
Expand Down
31 changes: 28 additions & 3 deletions src/icalendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ test('ical-candles', (t) => {
'GEO:41.85003;-87.65005',
'BEGIN:VALARM',
'ACTION:DISPLAY',
'DESCRIPTION:This is an event reminder',
'DESCRIPTION:Event reminder',
'TRIGGER:-P0DT0H10M0S',
'END:VALARM',
'END:VEVENT',
Expand Down Expand Up @@ -404,7 +404,7 @@ test('userEvent', (t) => {
'X-MICROSOFT-CDO-ALLDAYEVENT:TRUE',
'BEGIN:VALARM',
'ACTION:DISPLAY',
'DESCRIPTION:This is an event reminder',
'DESCRIPTION:Event reminder',
'TRIGGER:-P0DT12H0M0S',
'END:VALARM',
'END:VEVENT',
Expand Down Expand Up @@ -509,14 +509,39 @@ test('OmerEvent', (t) => {
' rah shebiYesod',
'BEGIN:VALARM',
'ACTION:DISPLAY',
'DESCRIPTION:This is an event reminder',
'DESCRIPTION:Event reminder',
'TRIGGER:-P0DT3H30M0S',
'END:VALARM',
'END:VEVENT',
];
t.deepEqual(lines, expected);
});

test('omer-alarm', (t) => {
const dt = new Date(2022, 3, 26);
const options = {
start: dt,
end: dt,
noHolidays: true,
omer: true,
candlelighting: true,
location: Location.lookup('Vancouver'),
};
const ev = HebrewCalendar.calendar(options)[0];
const ical = new IcalEvent(ev, options);
const lines = ical.toString().split('\r\n');
const alarm = lines.slice(lines.length - 6);
const expected = [
'BEGIN:VALARM',
'ACTION:DISPLAY',
'DESCRIPTION:Event reminder',
'TRIGGER;VALUE=DATE-TIME:20220426T040300Z',
'END:VALARM',
'END:VEVENT',
];
t.deepEqual(alarm, expected);
});

/** @private */
class TestEvent extends Event {
/** @param {HDate} date */
Expand Down

0 comments on commit 9304a8a

Please sign in to comment.