@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/icalendar.d.ts +98 -0
- package/dist/index.cjs +412 -450
- package/dist/index.mjs +412 -450
- package/dist/pkgVersion.d.ts +1 -0
- package/package.json +25 -17
- package/icalendar.d.ts +0 -36
package/dist/index.mjs
CHANGED
|
@@ -1,525 +1,487 @@
|
|
|
1
|
-
/*! @hebcal/icalendar v5.0
|
|
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
|
|
8
|
+
const version = '5.1.0';
|
|
9
9
|
|
|
10
10
|
const vtimezoneCache = new Map();
|
|
11
11
|
const CATEGORY = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
|
|
276
|
-
|
|
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
|
-
|
|
279
|
-
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
|
|
313
|
-
|
|
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
|
-
|
|
352
|
-
|
|
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
|
-
|
|
359
|
-
|
|
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
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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(
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
if (
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
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
|
-
|
|
415
|
-
|
|
416
|
-
|
|
383
|
+
if (torahMemo) {
|
|
384
|
+
if (memo.length) {
|
|
385
|
+
memo += '\\n\\n';
|
|
386
|
+
}
|
|
387
|
+
memo += torahMemo;
|
|
417
388
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
389
|
+
if (url) {
|
|
390
|
+
if (memo.length) {
|
|
391
|
+
memo += '\\n\\n';
|
|
392
|
+
}
|
|
393
|
+
memo += url;
|
|
423
394
|
}
|
|
424
|
-
memo
|
|
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
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
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
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
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
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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
|
-
|
|
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 };
|