@hebcal/icalendar 5.0.6 → 5.1.0

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,487 @@
1
- /*! @hebcal/icalendar v5.0.6 */
1
+ /*! @hebcal/icalendar v5.1.0 */
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.0';
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
+ const trigger = this.getAlarm();
222
+ if (trigger) {
223
+ arr.push('BEGIN:VALARM', 'ACTION:DISPLAY', 'DESCRIPTION:Event reminder', `${trigger}`, 'END:VALARM');
224
+ }
225
+ arr.push('END:VEVENT');
226
+ return arr;
229
227
  }
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
- );
228
+ /**
229
+ * @return {string}
230
+ */
231
+ toString() {
232
+ return this.getLines().join('\r\n');
240
233
  }
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
- }
234
+ /**
235
+ * fold lines to 75 characters
236
+ * @return {string[]}
237
+ */
238
+ getLines() {
239
+ return this.getLongLines().map(IcalEvent.fold);
274
240
  }
275
- if (isASCII) {
276
- return line.length <= 74 ? line : line.match(char74re).join('\r\n ');
241
+ /**
242
+ * fold line to 75 characters
243
+ * @param {string} line
244
+ * @return {string}
245
+ */
246
+ static fold(line) {
247
+ let isASCII = true;
248
+ for (let i = 0; i < line.length; i++) {
249
+ if (line.charCodeAt(i) > 255) {
250
+ isASCII = false;
251
+ break;
252
+ }
253
+ }
254
+ if (isASCII) {
255
+ if (line.length <= 74) {
256
+ return line;
257
+ }
258
+ const matches = line.match(char74re);
259
+ return matches.join('\r\n ');
260
+ }
261
+ if (encoder.encode(line).length <= 74) {
262
+ return line;
263
+ }
264
+ // iterate unicode character by character, making sure
265
+ // that adding a new character would keep the line <= 75 octets
266
+ let result = '';
267
+ let current = '';
268
+ let len = 0;
269
+ for (let i = 0; i < line.length; i++) {
270
+ const char = line[i];
271
+ const octets = char.charCodeAt(0) < 256 ? 1 : encoder.encode(char).length;
272
+ const newlen = len + octets;
273
+ if (newlen < 75) {
274
+ current += char;
275
+ len = newlen;
276
+ }
277
+ else {
278
+ result += current + '\r\n ';
279
+ line = line.substring(i);
280
+ current = '';
281
+ len = 0;
282
+ i = -1;
283
+ }
284
+ }
285
+ return result + current;
277
286
  }
278
- if (encoder.encode(line).length <= 74) {
279
- return line;
287
+ /**
288
+ * @param {string} str
289
+ * @return {string}
290
+ */
291
+ static escape(str) {
292
+ if (str.indexOf(',') !== -1) {
293
+ str = str.replace(/,/g, '\\,');
294
+ }
295
+ if (str.indexOf(';') !== -1) {
296
+ str = str.replace(/;/g, '\\;');
297
+ }
298
+ return str;
280
299
  }
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
- }
300
+ /**
301
+ * @param {Date} dt
302
+ * @return {string}
303
+ */
304
+ static formatYYYYMMDD(dt) {
305
+ return pad4(dt.getFullYear()) +
306
+ pad2(dt.getMonth() + 1) + pad2(dt.getDate());
300
307
  }
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, '\\,');
308
+ /**
309
+ * Returns UTC string for iCalendar
310
+ * @param {Date} dt
311
+ * @return {string}
312
+ */
313
+ static makeDtstamp(dt) {
314
+ const s = dt.toISOString();
315
+ return s.slice(0, 4) + s.slice(5, 7) + s.slice(8, 13) +
316
+ s.slice(14, 16) + s.slice(17, 19) + 'Z';
311
317
  }
312
- if (str.indexOf(';') !== -1) {
313
- str = str.replace(/;/g, '\\;');
318
+ /** @return {string} */
319
+ static version() {
320
+ return version;
314
321
  }
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
322
  }
343
-
344
323
  /**
345
324
  * Transforms a single Event into a VEVENT string
346
- * @param {Event} ev
347
- * @param {CalOptions} options
348
325
  * @return {string} multi-line result, delimited by \r\n
349
326
  */
350
327
  function eventToIcal(ev, options) {
351
- const ical = new IcalEvent(ev, options);
352
- return ical.toString();
328
+ const ical = new IcalEvent(ev, options);
329
+ return ical.toString();
353
330
  }
354
-
355
331
  const torahMemoCache = new Map();
356
-
357
332
  const HOLIDAY_IGNORE_MASK = DAILY_LEARNING | flags.OMER_COUNT |
358
- flags.SHABBAT_MEVARCHIM | flags.MOLAD | flags.USER_EVENT |
359
- flags.HEBREW_DATE;
360
-
333
+ flags.SHABBAT_MEVARCHIM | flags.MOLAD | flags.USER_EVENT |
334
+ flags.HEBREW_DATE;
361
335
  /**
362
336
  * @private
363
- * @param {Event} ev
364
- * @param {boolean} il
365
- * @return {string}
366
337
  */
367
338
  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') {
339
+ if ((ev.getFlags() & HOLIDAY_IGNORE_MASK) || ev.eventTime) {
340
+ return '';
341
+ }
342
+ const hd = ev.getDate();
343
+ const yy = hd.getFullYear();
344
+ const mm = hd.getMonth();
345
+ const dd = hd.getDate();
346
+ const key = [yy, mm, dd, il ? '1' : '0', ev.getDesc()].join('-');
347
+ let memo = torahMemoCache.get(key);
348
+ if (typeof memo === 'string') {
349
+ return memo;
350
+ }
351
+ memo = makeTorahMemoText(ev, il).replace(/\n/g, '\\n');
352
+ torahMemoCache.set(key, memo);
378
353
  return memo;
379
- }
380
- memo = makeTorahMemoText(ev, il).replace(/\n/g, '\\n');
381
- torahMemoCache.set(key, memo);
382
- return memo;
383
354
  }
384
-
385
355
  /**
386
356
  * @private
387
- * @param {Event} e
388
- * @param {CalOptions} options
389
- * @return {string}
390
357
  */
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);
358
+ function createMemo(ev, options) {
359
+ let memo = ev.memo;
360
+ if (typeof memo === 'string' && memo.length && memo.indexOf('\n') !== -1) {
361
+ memo = memo.replace(/\n/g, '\\n');
362
+ }
363
+ const desc = ev.getDesc();
364
+ if (desc === 'Havdalah' || desc === 'Candle lighting') {
365
+ return memo || '';
366
+ }
367
+ const mask = ev.getFlags();
368
+ if (mask & flags.OMER_COUNT) {
369
+ const omerEv = ev;
370
+ const sefira = [omerEv.sefira('en'), omerEv.sefira('he'), omerEv.sefira('translit')].join('\\n');
371
+ return omerEv.getTodayIs('en') + '\\n\\n' + omerEv.getTodayIs('he') + '\\n\\n' + sefira;
372
+ }
373
+ const url = appendTrackingToUrl(ev.url(), options);
374
+ const torahMemo = makeTorahMemo(ev, options.il);
375
+ if (!memo) {
376
+ if (typeof ev.linkedEvent !== 'undefined') {
377
+ memo = ev.linkedEvent.render(options.locale);
378
+ }
379
+ else {
380
+ memo = getHolidayDescription(ev);
381
+ }
412
382
  }
413
- }
414
- if (torahMemo) {
415
- if (memo.length) {
416
- memo += '\\n\\n';
383
+ if (torahMemo) {
384
+ if (memo.length) {
385
+ memo += '\\n\\n';
386
+ }
387
+ memo += torahMemo;
417
388
  }
418
- memo += torahMemo;
419
- }
420
- if (url) {
421
- if (memo.length) {
422
- memo += '\\n\\n';
389
+ if (url) {
390
+ if (memo.length) {
391
+ memo += '\\n\\n';
392
+ }
393
+ memo += url;
423
394
  }
424
- memo += url;
425
- }
426
- return memo;
395
+ return memo;
427
396
  }
428
-
429
397
  /**
430
398
  * Generates an RFC 2445 iCalendar string from an array of events
431
- * @param {Event[]} events
432
- * @param {CalOptions} options
433
- * @return {Promise<string>}
434
399
  */
435
400
  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);
401
+ if (!options)
402
+ throw new TypeError('Invalid options object');
403
+ const opts = Object.assign({}, options);
404
+ opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
405
+ if (!opts.title) {
406
+ opts.title = getCalendarTitle(events, opts);
407
+ }
408
+ const icals = events.map((ev) => new IcalEvent(ev, opts));
409
+ return icalEventsToString(icals, opts);
445
410
  }
446
-
447
411
  /**
448
412
  * Generates an RFC 2445 iCalendar string from an array of IcalEvents
449
- * @param {IcalEvent[]} icals
450
- * @param {CalOptions} options
451
- * @return {Promise<string>}
452
413
  */
453
414
  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);
415
+ const stream = [];
416
+ const locale = options.locale || Locale.getLocaleName();
417
+ const uclang = locale.toUpperCase();
418
+ const opts = Object.assign({}, options);
419
+ opts.dtstamp = opts.dtstamp || IcalEvent.makeDtstamp(new Date());
420
+ const title = opts.title ? IcalEvent.escape(opts.title) : 'Untitled';
421
+ const caldesc = opts.caldesc ? IcalEvent.escape(opts.caldesc) :
422
+ opts.yahrzeit ?
423
+ 'Yahrzeits + Anniversaries from www.hebcal.com' :
424
+ 'Jewish Holidays from www.hebcal.com';
425
+ const prodid = opts.prodid || `-//hebcal.com/NONSGML Hebcal Calendar v1${version}//${uclang}`;
426
+ const preamble = [
427
+ 'BEGIN:VCALENDAR',
428
+ 'VERSION:2.0',
429
+ `PRODID:${prodid}`,
430
+ 'CALSCALE:GREGORIAN',
431
+ 'METHOD:PUBLISH',
432
+ 'X-LOTUS-CHARSET:UTF-8',
433
+ ];
434
+ if (opts.publishedTTL !== false) {
435
+ const publishedTTL = opts.publishedTTL || 'PT7D';
436
+ preamble.push(`X-PUBLISHED-TTL:${publishedTTL}`);
437
+ }
438
+ preamble.push(`X-WR-CALNAME:${title}`);
439
+ preamble.push(`X-WR-CALDESC:${caldesc}`);
440
+ for (const line of preamble.map(IcalEvent.fold)) {
441
+ stream.push(line);
506
442
  stream.push('\r\n');
507
- vtimezoneCache.set(tzid, str);
508
- } catch (error) {
509
- // ignore failure when no timezone definition to read
510
- }
511
443
  }
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');
444
+ if (opts.relcalid) {
445
+ stream.push(IcalEvent.fold(`X-WR-RELCALID:${opts.relcalid}`));
446
+ stream.push('\r\n');
447
+ }
448
+ if (opts.calendarColor) {
449
+ stream.push(`X-APPLE-CALENDAR-COLOR:${opts.calendarColor}\r\n`);
450
+ }
451
+ const location = opts.location;
452
+ const tzid = location === null || location === void 0 ? void 0 : location.getTzid();
453
+ if (tzid) {
454
+ stream.push(`X-WR-TIMEZONE;VALUE=TEXT:${tzid}\r\n`);
455
+ const vtz = vtimezoneCache.get(tzid);
456
+ if (typeof vtz === 'string') {
457
+ stream.push(vtz);
458
+ stream.push('\r\n');
459
+ }
460
+ else {
461
+ const vtimezoneFilename = `./zoneinfo/${tzid}.ics`;
462
+ try {
463
+ const vtimezoneIcs = await promises.readFile(vtimezoneFilename, 'utf-8');
464
+ const lines = vtimezoneIcs.split('\r\n');
465
+ // ignore first 3 and last 1 lines
466
+ const str = lines.slice(3, lines.length - 2).join('\r\n');
467
+ stream.push(str);
468
+ stream.push('\r\n');
469
+ vtimezoneCache.set(tzid, str);
470
+ }
471
+ catch (error) {
472
+ // ignore failure when no timezone definition to read
473
+ }
474
+ }
475
+ }
476
+ for (const ical of icals) {
477
+ const lines = ical.getLines();
478
+ for (const line of lines) {
479
+ stream.push(line);
480
+ stream.push('\r\n');
481
+ }
519
482
  }
520
- }
521
- stream.push('END:VCALENDAR\r\n');
522
- return stream.join('');
483
+ stream.push('END:VCALENDAR\r\n');
484
+ return stream.join('');
523
485
  }
524
486
 
525
487
  export { IcalEvent, eventToIcal, eventsToIcalendar, icalEventsToString };