-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-google-calendar-url.js
55 lines (49 loc) · 1.72 KB
/
generate-google-calendar-url.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
(function() {
var moment = typeof module === 'object' ? require('moment') : window.moment,
BASE_URL = 'http://www.google.com/calendar/event?action=TEMPLATE',
MAX_LENGTH = 512,
toMoment = function(options) {
return moment(moment(options.date, 'YYYY/MM/DD'));
},
toAllDay = function(options) {
if (!options.date) return '';
var moment = toMoment(options);
return moment.isValid() ?
'&dates=' + moment.format('YYYYMMDD') + '/' + moment.add(1, 'd').format('YYYYMMDD') :
'';
},
toIsoHour = function(date) {
return moment(date).utc().format('YYYYMMDDTHHmmss') + 'Z';
},
toHour = function(options) {
if (!(options.start instanceof Date) || !(options.end instanceof Date)) return '';
return '&dates=' + toIsoHour(options.start) + '/' + toIsoHour(options.end);
},
toDatesParameter = function(options) {
return options.start && options.end ? toHour(options) :
options.date ? toAllDay(options) :
'';
},
toStringParameter = function(options, propertyName, alternativeName) {
if (!options[propertyName]) return '';
return '&' +
(alternativeName || propertyName) +
'=' +
encodeURIComponent(options[propertyName].substr(0, MAX_LENGTH - 1));
},
generateUrl = function(options) {
options = options || {};
return BASE_URL +
toStringParameter(options, 'title', 'text') +
toStringParameter(options, 'location') +
toStringParameter(options, 'details') +
toDatesParameter(options);
};
if (typeof module === 'object') {
// CommonJS
module.exports = generateUrl;
} else {
// Browser global.
window.generateUrl = generateUrl;
}
})();