-
Notifications
You must be signed in to change notification settings - Fork 0
/
icy.js
57 lines (52 loc) · 1.7 KB
/
icy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
let fs = require('fs')
/**
* Write ICS to a file in the current directory called event.ics
* @param {Date} start - Starting time of the event.
* @param {Date} end - Ending time of the event.
* @param {String} title - Title of the event.
* @param {String} location - Location of the event.
*/
function makeICS(start, end, title="", location="") {
fs.writeFile(__dirname + "/event.ics", makeString(startDate, endDate, title, location), function(err) {
if (err) {
return console.log(err)
}
});
}
/**
* Output ICS as a string
* @param {Date} start - Starting time of the event.
* @param {Date} end - Ending time of the event.
* @param {String} title - Title of the event.
* @param {String} location - Location of the event.
* @returns {String} String representation of the ICS event
*/
function makeString(start, end, title="", location="") {
return 'BEGIN:VCALENDAR\n' +
'VERSION:2.0\n' +
'PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n' +
'BEGIN:VEVENT\n' +
`UID:${generateUID()}\n` +
`DTSTAMP:${toDateTime(new Date())}\n` +
`DTSTART:${toDateTime(start)}\n` +
`DTEND:${toDateTime(end)}\n` +
`SUMMARY:${title}\n` +
`LOCATION:${location}\n` +
'END:VEVENT\n' +
'END:VCALENDAR'
}
// JS Date Format -> ICS Date Format
function toDateTime(date) {
const isoString = date.toISOString()
const formattedString = isoString.replace(/[-:.]/g,'')
return formattedString.substring(0, formattedString.length - 4) + 'Z'
}
// Not important unless sequencing events
function generateUID() {
const randomInt = Math.floor(1000 + Math.random() * 9000)
return 'uid' + randomInt + '@charliepeters.me'
}
module.exports = {
makeICS : makeICS,
makeString : makeString
}