@hebcal/icalendar 5.0.6 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,525 +1,501 @@
1
- /*! @hebcal/icalendar v5.0.6 */
1
+ /*! @hebcal/icalendar v5.1.1 */
2
2
  import { flags, Locale, greg } from '@hebcal/core';
3
3
  import { murmur32HexSync } from 'murmurhash3';
4
4
  import { shouldRenderBrief, pad2, getEventCategories, makeAnchor, pad4, getHolidayDescription, getCalendarTitle, appendIsraelAndTracking, makeTorahMemoText } from '@hebcal/rest-api';
5
5
  import { promises } from 'fs';
6
6
 
7
7
  // DO NOT EDIT THIS AUTO-GENERATED FILE!
8
- const version = '5.0.6';
8
+ const version = '5.1.1';
9
9
 
10
10
  const vtimezoneCache = new Map();
11
11
  const CATEGORY = {
12
- candles: 'Holiday',
13
- dafyomi: 'Daf Yomi',
14
- mishnayomi: 'Mishna Yomi',
15
- nachyomi: 'Nach Yomi',
16
- yerushalmi: 'Yerushalmi Yomi',
17
- havdalah: 'Holiday',
18
- hebdate: null,
19
- holiday: 'Holiday',
20
- mevarchim: null,
21
- molad: null,
22
- omer: null,
23
- parashat: 'Parsha',
24
- roshchodesh: 'Holiday',
25
- user: 'Personal',
26
- zmanim: null,
12
+ candles: 'Holiday',
13
+ dafyomi: 'Daf Yomi',
14
+ mishnayomi: 'Mishna Yomi',
15
+ nachyomi: 'Nach Yomi',
16
+ yerushalmi: 'Yerushalmi Yomi',
17
+ havdalah: 'Holiday',
18
+ hebdate: null,
19
+ holiday: 'Holiday',
20
+ mevarchim: null,
21
+ molad: null,
22
+ omer: null,
23
+ parashat: 'Parsha',
24
+ roshchodesh: 'Holiday',
25
+ user: 'Personal',
26
+ zmanim: null,
27
27
  };
28
-
29
28
  /**
30
29
  * @private
31
- * @param {string[]} arr
32
- * @param {string} key
33
- * @param {string} val
34
30
  */
35
31
  function addOptional(arr, key, val) {
36
- if (val) {
37
- const str = IcalEvent.escape(val);
38
- arr.push(key + ':' + str);
39
- }
32
+ if (val) {
33
+ const str = IcalEvent.escape(val);
34
+ arr.push(key + ':' + str);
35
+ }
40
36
  }
41
-
42
37
  /**
43
38
  * @private
44
- * @param {string} url
45
- * @param {CalOptions} options
46
- * @return {string}
47
39
  */
48
40
  function appendTrackingToUrl(url, options) {
49
- if (!url) {
50
- return null;
51
- }
52
- const utmSource = options.utmSource || 'js';
53
- const utmMedium = options.utmMedium || 'icalendar';
54
- const utmCampaign = options.utmCampaign;
55
- return appendIsraelAndTracking(url,
56
- options.il, utmSource, utmMedium, utmCampaign);
41
+ if (!url) {
42
+ return null;
43
+ }
44
+ const utmSource = options.utmSource || 'js';
45
+ const utmMedium = options.utmMedium || 'icalendar';
46
+ const utmCampaign = options.utmCampaign;
47
+ return appendIsraelAndTracking(url, options.il, utmSource, utmMedium, utmCampaign);
57
48
  }
58
-
59
49
  const encoder = new TextEncoder();
60
50
  const char74re = /(.{1,74})/g;
61
-
62
51
  const DAILY_LEARNING = flags.DAILY_LEARNING | flags.DAF_YOMI |
63
- flags.MISHNA_YOMI | flags.YERUSHALMI_YOMI | flags.NACH_YOMI;
64
-
52
+ flags.MISHNA_YOMI | flags.YERUSHALMI_YOMI | flags.NACH_YOMI;
65
53
  /**
66
54
  * Represents an RFC 2445 iCalendar VEVENT
67
55
  */
68
56
  class IcalEvent {
69
- /**
70
- * Builds an IcalEvent object from a Hebcal Event
71
- * @param {Event} ev
72
- * @param {CalOptions} options
73
- */
74
- constructor(ev, options={}) {
75
- this.ev = ev;
76
- this.options = options;
77
- this.dtstamp = options.dtstamp || IcalEvent.makeDtstamp(new Date());
78
- if (typeof ev.sequence === 'number') {
79
- this.sequence = ev.sequence;
80
- } else if (typeof options.sequence === 'number') {
81
- this.sequence = options.sequence;
82
- }
83
- const timed = this.timed = Boolean(ev.eventTime);
84
- const locale = options.locale;
85
- const location = options.location;
86
- let subj = shouldRenderBrief(ev) ? ev.renderBrief(locale) : ev.render(locale);
87
- const mask = ev.getFlags();
88
- if (ev.locationName) {
89
- this.locationName = ev.locationName;
90
- } else if (timed && location) {
91
- this.locationName = location.getShortName();
92
- } else if ((mask & DAILY_LEARNING) && ev.category) {
93
- this.locationName = Locale.gettext(ev.category, locale);
94
- }
95
- const date = IcalEvent.formatYYYYMMDD(ev.getDate().greg());
96
- this.startDate = this.isoDateOnly = date;
97
- this.dtargs = '';
98
- this.transp = 'TRANSPARENT';
99
- this.busyStatus = 'FREE';
100
- if (timed) {
101
- let [hour, minute] = ev.eventTimeStr.split(':');
102
- hour = +hour;
103
- minute = +minute;
104
- this.startDate += 'T' + pad2(hour) + pad2(minute) + '00';
105
- this.endDate = this.startDate;
106
- if (location?.getTzid()) {
107
- this.dtargs = `;TZID=${location.getTzid()}`;
108
- }
109
- } else {
110
- this.endDate = IcalEvent.formatYYYYMMDD(ev.getDate().next().greg());
111
- // for all-day untimed, use DTEND;VALUE=DATE intsead of DURATION:P1D.
112
- // It's more compatible with everthing except ancient versions of
113
- // Lotus Notes circa 2004
114
- this.dtargs = ';VALUE=DATE';
115
- if (mask & flags.CHAG) {
116
- this.transp = 'OPAQUE';
117
- this.busyStatus = 'OOF';
118
- }
119
- }
120
-
121
- if (options.emoji) {
122
- const prefix = ev.getEmoji();
123
- if (prefix) {
124
- if (mask & flags.OMER_COUNT) {
125
- subj = subj + ' ' + prefix;
126
- } else {
127
- subj = prefix + ' ' + subj;
128
- }
129
- }
130
- }
131
-
132
- // make subject safe for iCalendar
133
- subj = IcalEvent.escape(subj);
134
-
135
- if (options.appendHebrewToSubject) {
136
- const hebrew = ev.renderBrief('he');
137
- if (hebrew) {
138
- subj += ` / ${hebrew}`;
139
- }
140
- }
141
- this.subj = subj;
142
- this.category = ev.category || CATEGORY[getEventCategories(ev)?.[0]];
143
- }
144
-
145
-
146
- /**
147
- * @return {string}
148
- */
149
- getAlarm() {
150
- const ev = this.ev;
151
- const mask = ev.getFlags();
152
- const evAlarm = ev.alarm;
153
- if (typeof evAlarm === 'string') {
154
- return 'TRIGGER:' + evAlarm;
155
- } else if (typeof evAlarm === 'boolean' && !evAlarm) {
156
- return null;
157
- } else if (greg.isDate(evAlarm)) {
158
- evAlarm.setSeconds(0);
159
- return 'TRIGGER;VALUE=DATE-TIME:' + IcalEvent.makeDtstamp(evAlarm);
160
- } else if (mask & flags.OMER_COUNT) {
161
- return 'TRIGGER:-P0DT3H30M0S'; // 8:30pm Omer alarm evening before
162
- } else if (mask & flags.USER_EVENT) {
163
- return 'TRIGGER:-P0DT12H0M0S'; // noon the day before
164
- } else if (this.timed && ev.getDesc().startsWith('Candle lighting')) {
165
- return 'TRIGGER:-P0DT0H10M0S';
166
- }
167
- return null;
168
- }
169
-
170
- /**
171
- * @return {string}
172
- */
173
- getUid() {
174
- const options = this.options;
175
- const digest = murmur32HexSync(this.ev.getDesc());
176
- let uid = `hebcal-${this.isoDateOnly}-${digest}`;
177
- const loc = options.location;
178
- if (this.timed && loc) {
179
- if (loc.getGeoId()) {
180
- uid += `-${loc.getGeoId()}`;
181
- } else if (loc.getName()) {
182
- uid += '-' + makeAnchor(loc.getName());
183
- }
184
- }
185
- return uid;
186
- }
187
-
188
- /**
189
- * @return {string[]}
190
- */
191
- getLongLines() {
192
- if (this.lines) return this.lines;
193
- const categoryLine = this.category ? [`CATEGORIES:${this.category}`] : [];
194
- const uid = this.ev.uid || this.getUid();
195
- if (this.sequence) {
196
- categoryLine.unshift(`SEQUENCE:${this.sequence}`);
57
+ /**
58
+ * Builds an IcalEvent object from a Hebcal Event
59
+ */
60
+ constructor(ev, options = {}) {
61
+ var _a;
62
+ this.ev = ev;
63
+ const opts = Object.assign({}, options);
64
+ this.options = opts;
65
+ this.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
66
+ if (typeof ev.sequence === 'number') {
67
+ this.sequence = ev.sequence;
68
+ }
69
+ else if (typeof opts.sequence === 'number') {
70
+ this.sequence = opts.sequence;
71
+ }
72
+ const timed = this.timed = Boolean(ev.eventTime);
73
+ const locale = opts.locale;
74
+ const location = opts.location;
75
+ let subj = shouldRenderBrief(ev) ? ev.renderBrief(locale) : ev.render(locale);
76
+ const mask = ev.getFlags();
77
+ if (ev.locationName) {
78
+ this.locationName = ev.locationName;
79
+ }
80
+ else if (timed && location) {
81
+ this.locationName = location.getShortName();
82
+ }
83
+ else if ((mask & DAILY_LEARNING) && ev.category) {
84
+ this.locationName = Locale.gettext(ev.category, locale);
85
+ }
86
+ const date = IcalEvent.formatYYYYMMDD(ev.getDate().greg());
87
+ this.startDate = this.isoDateOnly = date;
88
+ this.dtargs = '';
89
+ this.transp = 'TRANSPARENT';
90
+ this.busyStatus = 'FREE';
91
+ if (timed) {
92
+ let [hour, minute] = ev.eventTimeStr.split(':');
93
+ hour = +hour;
94
+ minute = +minute;
95
+ this.startDate += 'T' + pad2(hour) + pad2(minute) + '00';
96
+ this.endDate = this.startDate;
97
+ if (location === null || location === void 0 ? void 0 : location.getTzid()) {
98
+ this.dtargs = `;TZID=${location.getTzid()}`;
99
+ }
100
+ }
101
+ else {
102
+ this.endDate = IcalEvent.formatYYYYMMDD(ev.getDate().next().greg());
103
+ // for all-day untimed, use DTEND;VALUE=DATE intsead of DURATION:P1D.
104
+ // It's more compatible with everthing except ancient versions of
105
+ // Lotus Notes circa 2004
106
+ this.dtargs = ';VALUE=DATE';
107
+ if (mask & flags.CHAG) {
108
+ this.transp = 'OPAQUE';
109
+ this.busyStatus = 'OOF';
110
+ }
111
+ }
112
+ if (opts.emoji) {
113
+ const prefix = ev.getEmoji();
114
+ if (prefix) {
115
+ if (mask & flags.OMER_COUNT) {
116
+ subj = subj + ' ' + prefix;
117
+ }
118
+ else {
119
+ subj = prefix + ' ' + subj;
120
+ }
121
+ }
122
+ }
123
+ // make subject safe for iCalendar
124
+ subj = IcalEvent.escape(subj);
125
+ if (opts.appendHebrewToSubject) {
126
+ const hebrew = ev.renderBrief('he');
127
+ if (hebrew) {
128
+ subj += ` / ${hebrew}`;
129
+ }
130
+ }
131
+ this.subj = subj;
132
+ this.category = ev.category || CATEGORY[(_a = getEventCategories(ev)) === null || _a === void 0 ? void 0 : _a[0]];
197
133
  }
198
- const arr = this.lines = [
199
- 'BEGIN:VEVENT',
200
- `DTSTAMP:${this.dtstamp}`,
201
- ].concat(categoryLine).concat([
202
- `SUMMARY:${this.subj}`,
203
- `DTSTART${this.dtargs}:${this.startDate}`,
204
- `DTEND${this.dtargs}:${this.endDate}`,
205
- `UID:${uid}`,
206
- `TRANSP:${this.transp}`,
207
- `X-MICROSOFT-CDO-BUSYSTATUS:${this.busyStatus}`,
208
- ]);
209
-
210
- if (!this.timed) {
211
- arr.push('X-MICROSOFT-CDO-ALLDAYEVENT:TRUE');
134
+ getAlarm() {
135
+ const ev = this.ev;
136
+ const mask = ev.getFlags();
137
+ const evAlarm = ev.alarm;
138
+ if (typeof evAlarm === 'string') {
139
+ return 'TRIGGER:' + evAlarm;
140
+ }
141
+ else if (typeof evAlarm === 'boolean' && !evAlarm) {
142
+ return null;
143
+ }
144
+ else if (greg.isDate(evAlarm)) {
145
+ const alarmDt = evAlarm;
146
+ alarmDt.setSeconds(0);
147
+ return 'TRIGGER;VALUE=DATE-TIME:' + IcalEvent.makeDtstamp(alarmDt);
148
+ }
149
+ else if (mask & flags.OMER_COUNT) {
150
+ return 'TRIGGER:-P0DT3H30M0S'; // 8:30pm Omer alarm evening before
151
+ }
152
+ else if (mask & flags.USER_EVENT) {
153
+ return 'TRIGGER:-P0DT12H0M0S'; // noon the day before
154
+ }
155
+ else if (this.timed && ev.getDesc().startsWith('Candle lighting')) {
156
+ return 'TRIGGER:-P0DT0H10M0S';
157
+ }
158
+ return null;
212
159
  }
213
-
214
- const ev = this.ev;
215
- const mask = ev.getFlags();
216
- const isUserEvent = Boolean(mask & flags.USER_EVENT);
217
- if (!isUserEvent) {
218
- arr.push('CLASS:PUBLIC');
160
+ /**
161
+ * @return {string}
162
+ */
163
+ getUid() {
164
+ if (this.ev.uid) {
165
+ return this.ev.uid;
166
+ }
167
+ const options = this.options;
168
+ const digest = murmur32HexSync(this.ev.getDesc());
169
+ let uid = `hebcal-${this.isoDateOnly}-${digest}`;
170
+ const loc = options.location;
171
+ if (this.timed && loc) {
172
+ if (loc.getGeoId()) {
173
+ uid += `-${loc.getGeoId()}`;
174
+ }
175
+ else if (loc.getName()) {
176
+ uid += '-' + makeAnchor(loc.getName());
177
+ }
178
+ }
179
+ return uid;
219
180
  }
220
-
221
- const options = this.options;
222
- // create memo (holiday descr, Torah, etc)
223
- const memo = createMemo(ev, options);
224
- addOptional(arr, 'DESCRIPTION', memo);
225
- addOptional(arr, 'LOCATION', this.locationName);
226
- const loc = options.location;
227
- if (this.timed && loc) {
228
- arr.push('GEO:' + loc.getLatitude() + ';' + loc.getLongitude());
181
+ /**
182
+ * @return {string[]}
183
+ */
184
+ getLongLines() {
185
+ if (this.lines)
186
+ return this.lines;
187
+ const categoryLine = this.category ? [`CATEGORIES:${this.category}`] : [];
188
+ const uid = this.getUid();
189
+ if (this.sequence) {
190
+ categoryLine.unshift(`SEQUENCE:${this.sequence}`);
191
+ }
192
+ const arr = this.lines = [
193
+ 'BEGIN:VEVENT',
194
+ `DTSTAMP:${this.dtstamp}`,
195
+ ].concat(categoryLine).concat([
196
+ `SUMMARY:${this.subj}`,
197
+ `DTSTART${this.dtargs}:${this.startDate}`,
198
+ `DTEND${this.dtargs}:${this.endDate}`,
199
+ `UID:${uid}`,
200
+ `TRANSP:${this.transp}`,
201
+ `X-MICROSOFT-CDO-BUSYSTATUS:${this.busyStatus}`,
202
+ ]);
203
+ if (!this.timed) {
204
+ arr.push('X-MICROSOFT-CDO-ALLDAYEVENT:TRUE');
205
+ }
206
+ const ev = this.ev;
207
+ const mask = ev.getFlags();
208
+ const isUserEvent = Boolean(mask & flags.USER_EVENT);
209
+ if (!isUserEvent) {
210
+ arr.push('CLASS:PUBLIC');
211
+ }
212
+ const options = this.options;
213
+ // create memo (holiday descr, Torah, etc)
214
+ const memo = createMemo(ev, options);
215
+ addOptional(arr, 'DESCRIPTION', memo);
216
+ addOptional(arr, 'LOCATION', this.locationName);
217
+ const loc = options.location;
218
+ if (this.timed && loc) {
219
+ arr.push('GEO:' + loc.getLatitude() + ';' + loc.getLongitude());
220
+ }
221
+ // In addition to the URL being part of the DESCRIPTION field,
222
+ // should we also generate an RFC 5545 URL property?
223
+ if (options.url) {
224
+ const url = ev.url();
225
+ if (url) {
226
+ arr.push('URL:' + appendTrackingToUrl(url, options));
227
+ }
228
+ }
229
+ const trigger = this.getAlarm();
230
+ if (trigger) {
231
+ arr.push('BEGIN:VALARM', 'ACTION:DISPLAY', 'DESCRIPTION:Event reminder', `${trigger}`, 'END:VALARM');
232
+ }
233
+ arr.push('END:VEVENT');
234
+ return arr;
229
235
  }
230
-
231
- const trigger = this.getAlarm();
232
- if (trigger) {
233
- arr.push(
234
- 'BEGIN:VALARM',
235
- 'ACTION:DISPLAY',
236
- 'DESCRIPTION:Event reminder',
237
- `${trigger}`,
238
- 'END:VALARM',
239
- );
236
+ /**
237
+ * @return {string}
238
+ */
239
+ toString() {
240
+ return this.getLines().join('\r\n');
240
241
  }
241
-
242
- arr.push('END:VEVENT');
243
-
244
- return arr;
245
- }
246
-
247
- /**
248
- * @return {string}
249
- */
250
- toString() {
251
- return this.getLines().join('\r\n');
252
- }
253
-
254
- /**
255
- * fold lines to 75 characters
256
- * @return {string[]}
257
- */
258
- getLines() {
259
- return this.getLongLines().map(IcalEvent.fold);
260
- }
261
-
262
- /**
263
- * fold line to 75 characters
264
- * @param {string} line
265
- * @return {string}
266
- */
267
- static fold(line) {
268
- let isASCII = true;
269
- for (let i = 0; i < line.length; i++) {
270
- if (line.charCodeAt(i) > 255) {
271
- isASCII = false;
272
- break;
273
- }
242
+ /**
243
+ * fold lines to 75 characters
244
+ * @return {string[]}
245
+ */
246
+ getLines() {
247
+ return this.getLongLines().map(IcalEvent.fold);
274
248
  }
275
- if (isASCII) {
276
- return line.length <= 74 ? line : line.match(char74re).join('\r\n ');
249
+ /**
250
+ * fold line to 75 characters
251
+ * @param {string} line
252
+ * @return {string}
253
+ */
254
+ static fold(line) {
255
+ let isASCII = true;
256
+ for (let i = 0; i < line.length; i++) {
257
+ if (line.charCodeAt(i) > 255) {
258
+ isASCII = false;
259
+ break;
260
+ }
261
+ }
262
+ if (isASCII) {
263
+ if (line.length <= 74) {
264
+ return line;
265
+ }
266
+ const matches = line.match(char74re);
267
+ return matches.join('\r\n ');
268
+ }
269
+ if (encoder.encode(line).length <= 74) {
270
+ return line;
271
+ }
272
+ // iterate unicode character by character, making sure
273
+ // that adding a new character would keep the line <= 75 octets
274
+ let result = '';
275
+ let current = '';
276
+ let len = 0;
277
+ for (let i = 0; i < line.length; i++) {
278
+ const char = line[i];
279
+ const octets = char.charCodeAt(0) < 256 ? 1 : encoder.encode(char).length;
280
+ const newlen = len + octets;
281
+ if (newlen < 75) {
282
+ current += char;
283
+ len = newlen;
284
+ }
285
+ else {
286
+ result += current + '\r\n ';
287
+ line = line.substring(i);
288
+ current = '';
289
+ len = 0;
290
+ i = -1;
291
+ }
292
+ }
293
+ return result + current;
277
294
  }
278
- if (encoder.encode(line).length <= 74) {
279
- return line;
295
+ /**
296
+ * @param {string} str
297
+ * @return {string}
298
+ */
299
+ static escape(str) {
300
+ if (str.indexOf(',') !== -1) {
301
+ str = str.replace(/,/g, '\\,');
302
+ }
303
+ if (str.indexOf(';') !== -1) {
304
+ str = str.replace(/;/g, '\\;');
305
+ }
306
+ return str;
280
307
  }
281
- // iterate unicode character by character, making sure
282
- // that adding a new character would keep the line <= 75 octets
283
- let result = '';
284
- let current = '';
285
- let len = 0;
286
- for (let i = 0; i < line.length; i++) {
287
- const char = line[i];
288
- const octets = char.charCodeAt(0) < 256 ? 1 : encoder.encode(char).length;
289
- const newlen = len + octets;
290
- if (newlen < 75) {
291
- current += char;
292
- len = newlen;
293
- } else {
294
- result += current + '\r\n ';
295
- line = line.substring(i);
296
- current = '';
297
- len = 0;
298
- i = -1;
299
- }
308
+ /**
309
+ * @param {Date} dt
310
+ * @return {string}
311
+ */
312
+ static formatYYYYMMDD(dt) {
313
+ return pad4(dt.getFullYear()) +
314
+ pad2(dt.getMonth() + 1) + pad2(dt.getDate());
300
315
  }
301
- return result + current;
302
- }
303
-
304
- /**
305
- * @param {string} str
306
- * @return {string}
307
- */
308
- static escape(str) {
309
- if (str.indexOf(',') !== -1) {
310
- str = str.replace(/,/g, '\\,');
316
+ /**
317
+ * Returns UTC string for iCalendar
318
+ * @param {Date} dt
319
+ * @return {string}
320
+ */
321
+ static makeDtstamp(dt) {
322
+ const s = dt.toISOString();
323
+ return s.slice(0, 4) + s.slice(5, 7) + s.slice(8, 13) +
324
+ s.slice(14, 16) + s.slice(17, 19) + 'Z';
311
325
  }
312
- if (str.indexOf(';') !== -1) {
313
- str = str.replace(/;/g, '\\;');
326
+ /** @return {string} */
327
+ static version() {
328
+ return version;
314
329
  }
315
- return str;
316
- }
317
-
318
- /**
319
- * @param {Date} dt
320
- * @return {string}
321
- */
322
- static formatYYYYMMDD(dt) {
323
- return pad4(dt.getFullYear()) +
324
- pad2(dt.getMonth() + 1) + pad2(dt.getDate());
325
- }
326
-
327
- /**
328
- * Returns UTC string for iCalendar
329
- * @param {Date} dt
330
- * @return {string}
331
- */
332
- static makeDtstamp(dt) {
333
- const s = dt.toISOString();
334
- return s.slice(0, 4) + s.slice(5, 7) + s.slice(8, 13) +
335
- s.slice(14, 16) + s.slice(17, 19) + 'Z';
336
- }
337
-
338
- /** @return {string} */
339
- static version() {
340
- return version;
341
- }
342
330
  }
343
-
344
331
  /**
345
332
  * Transforms a single Event into a VEVENT string
346
- * @param {Event} ev
347
- * @param {CalOptions} options
348
333
  * @return {string} multi-line result, delimited by \r\n
349
334
  */
350
335
  function eventToIcal(ev, options) {
351
- const ical = new IcalEvent(ev, options);
352
- return ical.toString();
336
+ const ical = new IcalEvent(ev, options);
337
+ return ical.toString();
353
338
  }
354
-
355
339
  const torahMemoCache = new Map();
356
-
357
340
  const HOLIDAY_IGNORE_MASK = DAILY_LEARNING | flags.OMER_COUNT |
358
- flags.SHABBAT_MEVARCHIM | flags.MOLAD | flags.USER_EVENT |
359
- flags.HEBREW_DATE;
360
-
341
+ flags.SHABBAT_MEVARCHIM | flags.MOLAD | flags.USER_EVENT |
342
+ flags.HEBREW_DATE;
361
343
  /**
362
344
  * @private
363
- * @param {Event} ev
364
- * @param {boolean} il
365
- * @return {string}
366
345
  */
367
346
  function makeTorahMemo(ev, il) {
368
- if ((ev.getFlags() & HOLIDAY_IGNORE_MASK) || ev.eventTime) {
369
- return '';
370
- }
371
- const hd = ev.getDate();
372
- const yy = hd.getFullYear();
373
- const mm = hd.getMonth();
374
- const dd = hd.getDate();
375
- const key = [yy, mm, dd, il ? '1' : '0', ev.getDesc()].join('-');
376
- let memo = torahMemoCache.get(key);
377
- if (typeof memo === 'string') {
347
+ if ((ev.getFlags() & HOLIDAY_IGNORE_MASK) || ev.eventTime) {
348
+ return '';
349
+ }
350
+ const hd = ev.getDate();
351
+ const yy = hd.getFullYear();
352
+ const mm = hd.getMonth();
353
+ const dd = hd.getDate();
354
+ const key = [yy, mm, dd, il ? '1' : '0', ev.getDesc()].join('-');
355
+ let memo = torahMemoCache.get(key);
356
+ if (typeof memo === 'string') {
357
+ return memo;
358
+ }
359
+ memo = makeTorahMemoText(ev, il).replace(/\n/g, '\\n');
360
+ torahMemoCache.set(key, memo);
378
361
  return memo;
379
- }
380
- memo = makeTorahMemoText(ev, il).replace(/\n/g, '\\n');
381
- torahMemoCache.set(key, memo);
382
- return memo;
383
362
  }
384
-
385
363
  /**
386
364
  * @private
387
- * @param {Event} e
388
- * @param {CalOptions} options
389
- * @return {string}
390
365
  */
391
- function createMemo(e, options) {
392
- let memo = e.memo;
393
- if (typeof memo === 'string' && memo.length && memo.indexOf('\n') !== -1) {
394
- memo = memo.replace(/\n/g, '\\n');
395
- }
396
- const desc = e.getDesc();
397
- if (desc === 'Havdalah' || desc === 'Candle lighting') {
398
- return memo || '';
399
- }
400
- const mask = e.getFlags();
401
- if (mask & flags.OMER_COUNT) {
402
- const sefira = [e.sefira('en'), e.sefira('he'), e.sefira('translit')].join('\\n');
403
- return e.getTodayIs('en') + '\\n\\n' + e.getTodayIs('he') + '\\n\\n' + sefira;
404
- }
405
- const url = appendTrackingToUrl(e.url(), options);
406
- const torahMemo = makeTorahMemo(e, options.il);
407
- if (!memo) {
408
- if (typeof e.linkedEvent !== 'undefined') {
409
- memo = e.linkedEvent.render(options.locale);
410
- } else {
411
- memo = getHolidayDescription(e);
366
+ function createMemo(ev, options) {
367
+ let memo = ev.memo;
368
+ if (typeof memo === 'string' && memo.length && memo.indexOf('\n') !== -1) {
369
+ memo = memo.replace(/\n/g, '\\n');
370
+ }
371
+ const desc = ev.getDesc();
372
+ if (desc === 'Havdalah' || desc === 'Candle lighting') {
373
+ return memo || '';
374
+ }
375
+ const mask = ev.getFlags();
376
+ if (mask & flags.OMER_COUNT) {
377
+ const omerEv = ev;
378
+ const sefira = [omerEv.sefira('en'), omerEv.sefira('he'), omerEv.sefira('translit')].join('\\n');
379
+ return omerEv.getTodayIs('en') + '\\n\\n' + omerEv.getTodayIs('he') + '\\n\\n' + sefira;
380
+ }
381
+ const url = appendTrackingToUrl(ev.url(), options);
382
+ const torahMemo = makeTorahMemo(ev, options.il);
383
+ if (!memo) {
384
+ if (typeof ev.linkedEvent !== 'undefined') {
385
+ memo = ev.linkedEvent.render(options.locale);
386
+ }
387
+ else {
388
+ memo = getHolidayDescription(ev);
389
+ }
412
390
  }
413
- }
414
- if (torahMemo) {
415
- if (memo.length) {
416
- memo += '\\n\\n';
391
+ if (torahMemo) {
392
+ if (memo.length) {
393
+ memo += '\\n\\n';
394
+ }
395
+ memo += torahMemo;
417
396
  }
418
- memo += torahMemo;
419
- }
420
- if (url) {
421
- if (memo.length) {
422
- memo += '\\n\\n';
397
+ if (url) {
398
+ if (memo.length) {
399
+ memo += '\\n\\n';
400
+ }
401
+ memo += url;
423
402
  }
424
- memo += url;
425
- }
426
- return memo;
403
+ return memo;
427
404
  }
428
-
429
405
  /**
430
406
  * Generates an RFC 2445 iCalendar string from an array of events
431
- * @param {Event[]} events
432
- * @param {CalOptions} options
433
- * @return {Promise<string>}
434
407
  */
435
408
  async function eventsToIcalendar(events, options) {
436
- if (!events.length) throw new RangeError('Events can not be empty');
437
- if (!options) throw new TypeError('Invalid options object');
438
- const opts = Object.assign({}, options);
439
- opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
440
- if (!opts.title) {
441
- opts.title = getCalendarTitle(events, opts);
442
- }
443
- const icals = events.map((ev) => new IcalEvent(ev, opts));
444
- return icalEventsToString(icals, opts);
409
+ if (!events.length)
410
+ throw new RangeError('Events can not be empty');
411
+ if (!options)
412
+ throw new TypeError('Invalid options object');
413
+ const opts = Object.assign({}, options);
414
+ opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
415
+ if (!opts.title) {
416
+ opts.title = getCalendarTitle(events, opts);
417
+ }
418
+ const icals = events.map((ev) => new IcalEvent(ev, opts));
419
+ return icalEventsToString(icals, opts);
445
420
  }
446
-
447
421
  /**
448
422
  * Generates an RFC 2445 iCalendar string from an array of IcalEvents
449
- * @param {IcalEvent[]} icals
450
- * @param {CalOptions} options
451
- * @return {Promise<string>}
452
423
  */
453
424
  async function icalEventsToString(icals, options) {
454
- const stream = [];
455
- const locale = options.locale || Locale.getLocaleName();
456
- const uclang = locale.toUpperCase();
457
- const opts = Object.assign({}, options);
458
- opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
459
- const title = opts.title ? IcalEvent.escape(opts.title) : 'Untitled';
460
- const caldesc = opts.caldesc ? IcalEvent.escape(opts.caldesc) :
461
- opts.yahrzeit ?
462
- 'Yahrzeits + Anniversaries from www.hebcal.com' :
463
- 'Jewish Holidays from www.hebcal.com';
464
- const prodid = opts.prodid || `-//hebcal.com/NONSGML Hebcal Calendar v1${version}//${uclang}`;
465
- const preamble = [
466
- 'BEGIN:VCALENDAR',
467
- 'VERSION:2.0',
468
- `PRODID:${prodid}`,
469
- 'CALSCALE:GREGORIAN',
470
- 'METHOD:PUBLISH',
471
- 'X-LOTUS-CHARSET:UTF-8',
472
- ];
473
- if (opts.publishedTTL !== false) {
474
- const publishedTTL = opts.publishedTTL || 'PT7D';
475
- preamble.push(`X-PUBLISHED-TTL:${publishedTTL}`);
476
- }
477
- preamble.push(`X-WR-CALNAME:${title}`);
478
- preamble.push(`X-WR-CALDESC:${caldesc}`);
479
- for (const line of preamble.map(IcalEvent.fold)) {
480
- stream.push(line);
481
- stream.push('\r\n');
482
- }
483
- if (opts.relcalid) {
484
- stream.push(IcalEvent.fold(`X-WR-RELCALID:${opts.relcalid}`));
485
- stream.push('\r\n');
486
- }
487
- if (opts.calendarColor) {
488
- stream.push(`X-APPLE-CALENDAR-COLOR:${opts.calendarColor}\r\n`);
489
- }
490
- const location = opts.location;
491
- const tzid = location?.getTzid();
492
- if (tzid) {
493
- stream.push(`X-WR-TIMEZONE;VALUE=TEXT:${tzid}\r\n`);
494
- const vtz = vtimezoneCache.get(tzid);
495
- if (typeof vtz === 'string') {
496
- stream.push(vtz);
497
- stream.push('\r\n');
498
- } else {
499
- const vtimezoneFilename = `./zoneinfo/${tzid}.ics`;
500
- try {
501
- const vtimezoneIcs = await promises.readFile(vtimezoneFilename, 'utf-8');
502
- const lines = vtimezoneIcs.split('\r\n');
503
- // ignore first 3 and last 1 lines
504
- const str = lines.slice(3, lines.length - 2).join('\r\n');
505
- stream.push(str);
425
+ if (!icals.length)
426
+ throw new RangeError('Events can not be empty');
427
+ if (!options)
428
+ throw new TypeError('Invalid options object');
429
+ const stream = [];
430
+ const locale = options.locale || Locale.getLocaleName();
431
+ const uclang = locale.toUpperCase();
432
+ const opts = Object.assign({}, options);
433
+ opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
434
+ const title = opts.title ? IcalEvent.escape(opts.title) : 'Untitled';
435
+ const caldesc = opts.caldesc ? IcalEvent.escape(opts.caldesc) :
436
+ opts.yahrzeit ?
437
+ 'Yahrzeits + Anniversaries from www.hebcal.com' :
438
+ 'Jewish Holidays from www.hebcal.com';
439
+ const prodid = opts.prodid || `-//hebcal.com/NONSGML Hebcal Calendar v1${version}//${uclang}`;
440
+ const preamble = [
441
+ 'BEGIN:VCALENDAR',
442
+ 'VERSION:2.0',
443
+ `PRODID:${prodid}`,
444
+ 'CALSCALE:GREGORIAN',
445
+ 'METHOD:PUBLISH',
446
+ 'X-LOTUS-CHARSET:UTF-8',
447
+ ];
448
+ if (opts.publishedTTL !== false) {
449
+ const publishedTTL = opts.publishedTTL || 'PT7D';
450
+ preamble.push(`X-PUBLISHED-TTL:${publishedTTL}`);
451
+ }
452
+ preamble.push(`X-WR-CALNAME:${title}`);
453
+ preamble.push(`X-WR-CALDESC:${caldesc}`);
454
+ for (const line of preamble.map(IcalEvent.fold)) {
455
+ stream.push(line);
506
456
  stream.push('\r\n');
507
- vtimezoneCache.set(tzid, str);
508
- } catch (error) {
509
- // ignore failure when no timezone definition to read
510
- }
511
457
  }
512
- }
513
-
514
- for (const ical of icals) {
515
- const lines = ical.getLines();
516
- for (const line of lines) {
517
- stream.push(line);
518
- stream.push('\r\n');
458
+ if (opts.relcalid) {
459
+ stream.push(IcalEvent.fold(`X-WR-RELCALID:${opts.relcalid}`));
460
+ stream.push('\r\n');
461
+ }
462
+ if (opts.calendarColor) {
463
+ stream.push(`X-APPLE-CALENDAR-COLOR:${opts.calendarColor}\r\n`);
464
+ }
465
+ const location = opts.location;
466
+ const tzid = location === null || location === void 0 ? void 0 : location.getTzid();
467
+ if (tzid) {
468
+ stream.push(`X-WR-TIMEZONE;VALUE=TEXT:${tzid}\r\n`);
469
+ const vtz = vtimezoneCache.get(tzid);
470
+ if (typeof vtz === 'string') {
471
+ stream.push(vtz);
472
+ stream.push('\r\n');
473
+ }
474
+ else {
475
+ const vtimezoneFilename = `./zoneinfo/${tzid}.ics`;
476
+ try {
477
+ const vtimezoneIcs = await promises.readFile(vtimezoneFilename, 'utf-8');
478
+ const lines = vtimezoneIcs.split('\r\n');
479
+ // ignore first 3 and last 1 lines
480
+ const str = lines.slice(3, lines.length - 2).join('\r\n');
481
+ stream.push(str);
482
+ stream.push('\r\n');
483
+ vtimezoneCache.set(tzid, str);
484
+ }
485
+ catch (error) {
486
+ // ignore failure when no timezone definition to read
487
+ }
488
+ }
489
+ }
490
+ for (const ical of icals) {
491
+ const lines = ical.getLines();
492
+ for (const line of lines) {
493
+ stream.push(line);
494
+ stream.push('\r\n');
495
+ }
519
496
  }
520
- }
521
- stream.push('END:VCALENDAR\r\n');
522
- return stream.join('');
497
+ stream.push('END:VCALENDAR\r\n');
498
+ return stream.join('');
523
499
  }
524
500
 
525
501
  export { IcalEvent, eventToIcal, eventsToIcalendar, icalEventsToString };