Skip to content

Commit 67c2575

Browse files
committed
Move more of iCalendar event generation outside of constructor
1 parent 8567ec0 commit 67c2575

File tree

2 files changed

+56
-52
lines changed

2 files changed

+56
-52
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hebcal/icalendar",
3-
"version": "4.6.1",
3+
"version": "4.7.0",
44
"author": "Michael J. Radwin (https://github.com/mjradwin)",
55
"keywords": [
66
"ical",
@@ -24,7 +24,7 @@
2424
"url": "https://github.com/hebcal/hebcal-icalendar/issues"
2525
},
2626
"dependencies": {
27-
"@hebcal/core": "^3.17.5",
27+
"@hebcal/core": "^3.18.0",
2828
"@hebcal/rest-api": "^3.6.0"
2929
},
3030
"scripts": {
@@ -57,7 +57,7 @@
5757
"@ava/babel": "^1.0.1",
5858
"@babel/core": "^7.14.6",
5959
"@babel/polyfill": "^7.12.1",
60-
"@babel/preset-env": "^7.14.5",
60+
"@babel/preset-env": "^7.14.7",
6161
"@babel/register": "^7.14.5",
6262
"@rollup/plugin-babel": "^5.3.0",
6363
"@rollup/plugin-commonjs": "^19.0.0",

src/icalendar.js

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -56,49 +56,49 @@ export class IcalEvent {
5656
* @param {HebrewCalendar.Options} options
5757
*/
5858
constructor(ev, options={}) {
59-
const dtstamp = options.dtstamp || IcalEvent.makeDtstamp(new Date());
60-
const timed = Boolean(ev.eventTime);
59+
this.ev = ev;
60+
this.options = options;
61+
this.dtstamp = options.dtstamp || IcalEvent.makeDtstamp(new Date());
62+
const timed = this.timed = Boolean(ev.eventTime);
6163
let subj = timed || (ev.getFlags() & flags.DAF_YOMI) ? ev.renderBrief() : ev.render();
62-
const desc = ev.getDesc(); // original untranslated
63-
const mask = ev.getFlags();
64-
let location;
6564
if (timed && options.location && options.location.name) {
6665
const comma = options.location.name.indexOf(',');
67-
location = (comma == -1) ? options.location.name : options.location.name.substring(0, comma);
66+
this.locationName = (comma == -1) ? options.location.name : options.location.name.substring(0, comma);
6867
}
68+
const mask = ev.getFlags();
6969
if (mask & flags.DAF_YOMI) {
70-
location = Locale.gettext('Daf Yomi');
70+
this.locationName = Locale.gettext('Daf Yomi');
7171
}
7272

7373
const date = IcalEvent.formatYYYYMMDD(ev.getDate().greg());
74-
let startDate = date;
75-
let dtargs = '';
76-
let endDate;
77-
let transp = 'TRANSPARENT'; let busyStatus = 'FREE';
74+
this.startDate = date;
75+
this.dtargs = '';
76+
this.transp = 'TRANSPARENT';
77+
this.busyStatus = 'FREE';
7878
if (timed) {
7979
let [hour, minute] = ev.eventTimeStr.split(':');
8080
hour = +hour;
8181
minute = +minute;
82-
startDate += 'T' + pad2(hour) + pad2(minute) + '00';
83-
endDate = startDate;
82+
this.startDate += 'T' + pad2(hour) + pad2(minute) + '00';
83+
this.endDate = this.startDate;
8484
if (options.location && options.location.tzid) {
85-
dtargs = `;TZID=${options.location.tzid}`;
85+
this.dtargs = `;TZID=${options.location.tzid}`;
8686
}
8787
} else {
88-
endDate = IcalEvent.formatYYYYMMDD(ev.getDate().next().greg());
88+
this.endDate = IcalEvent.formatYYYYMMDD(ev.getDate().next().greg());
8989
// for all-day untimed, use DTEND;VALUE=DATE intsead of DURATION:P1D.
9090
// It's more compatible with everthing except ancient versions of
9191
// Lotus Notes circa 2004
92-
dtargs = ';VALUE=DATE';
92+
this.dtargs = ';VALUE=DATE';
9393
if (mask & flags.CHAG) {
94-
transp = 'OPAQUE';
95-
busyStatus = 'OOF';
94+
this.transp = 'OPAQUE';
95+
this.busyStatus = 'OOF';
9696
}
9797
}
9898

9999
let uid = ev.uid;
100100
if (!uid) {
101-
const digest = murmur3(desc).toString(16);
101+
const digest = murmur3(ev.getDesc()).toString(16);
102102
uid = `hebcal-${date}-${digest}`;
103103
if (timed && options.location) {
104104
if (options.location.geoid) {
@@ -108,6 +108,7 @@ export class IcalEvent {
108108
}
109109
}
110110
}
111+
this.uid = uid;
111112

112113
// make subject safe for iCalendar
113114
subj = IcalEvent.escape(subj);
@@ -118,57 +119,67 @@ export class IcalEvent {
118119
subj += ` / ${hebrew}`;
119120
}
120121
}
122+
this.subj = subj;
121123

122124
const isUserEvent = Boolean(mask & flags.USER_EVENT);
123-
const category = CATEGORY[getEventCategories(ev)[0]];
124-
const categoryLine = category ? `CATEGORIES:${category}` : [];
125+
if (mask & flags.OMER_COUNT) {
126+
this.alarm = '0DT3H30M0S'; // 8:30pm Omer alarm evening before
127+
} else if (isUserEvent) {
128+
this.alarm = '0DT12H0M0S'; // noon the day before
129+
} else if (timed && ev.getDesc().startsWith('Candle lighting')) {
130+
this.alarm = '0DT0H10M0S'; // ten minutes
131+
}
132+
133+
this.category = CATEGORY[getEventCategories(ev)[0]];
134+
}
135+
136+
/**
137+
* @return {string[]}
138+
*/
139+
getLongLines() {
140+
const categoryLine = this.category ? `CATEGORIES:${this.category}` : [];
125141
const arr = [
126142
'BEGIN:VEVENT',
127-
`DTSTAMP:${dtstamp}`,
143+
`DTSTAMP:${this.dtstamp}`,
128144
].concat(categoryLine).concat([
129-
`SUMMARY:${subj}`,
130-
`DTSTART${dtargs}:${startDate}`,
131-
`DTEND${dtargs}:${endDate}`,
132-
`TRANSP:${transp}`,
133-
`X-MICROSOFT-CDO-BUSYSTATUS:${busyStatus}`,
134-
`UID:${uid}`,
145+
`SUMMARY:${this.subj}`,
146+
`DTSTART${this.dtargs}:${this.startDate}`,
147+
`DTEND${this.dtargs}:${this.endDate}`,
148+
`TRANSP:${this.transp}`,
149+
`X-MICROSOFT-CDO-BUSYSTATUS:${this.busyStatus}`,
150+
`UID:${this.uid}`,
135151
]);
136152

153+
const ev = this.ev;
154+
const mask = ev.getFlags();
155+
const isUserEvent = Boolean(mask & flags.USER_EVENT);
137156
if (!isUserEvent) {
138157
arr.push('CLASS:PUBLIC');
139158
}
140159

160+
const options = this.options;
141161
// create memo (holiday descr, Torah, etc)
142162
const memo = createMemo(ev, options.il);
143163
addOptional(arr, 'DESCRIPTION', memo);
144-
addOptional(arr, 'LOCATION', location);
145-
if (timed && options.location) {
164+
addOptional(arr, 'LOCATION', this.locationName);
165+
if (this.timed && options.location) {
146166
arr.push('GEO:' + options.location.latitude + ';' + options.location.longitude);
147167
}
148168

149-
let alarm;
150-
if (mask & flags.OMER_COUNT) {
151-
alarm = '0DT3H30M0S'; // 8:30pm Omer alarm evening before
152-
} else if (isUserEvent) {
153-
alarm = '0DT12H0M0S'; // noon the day before
154-
} else if (timed && desc.startsWith('Candle lighting')) {
155-
alarm = '0DT0H10M0S'; // ten minutes
156-
}
157-
if (alarm) {
169+
if (this.alarm) {
158170
arr.push(
159171
'BEGIN:VALARM',
160172
'ACTION:DISPLAY',
161173
'DESCRIPTION:This is an event reminder',
162-
`TRIGGER:-P${alarm}`,
174+
`TRIGGER:-P${this.alarm}`,
163175
'END:VALARM',
164176
);
165177
}
166178

167179
arr.push('END:VEVENT');
168180

169181
this.lines = arr;
170-
this.options = options;
171-
this.ev = ev;
182+
return this.lines;
172183
}
173184

174185
/**
@@ -186,13 +197,6 @@ export class IcalEvent {
186197
return this.getLongLines().map(IcalEvent.fold);
187198
}
188199

189-
/**
190-
* @return {string[]}
191-
*/
192-
getLongLines() {
193-
return this.lines;
194-
}
195-
196200
/**
197201
* fold line to 75 characters
198202
* @param {string} line

0 commit comments

Comments
 (0)