-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.js
65 lines (57 loc) · 1.95 KB
/
google.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
58
59
60
61
62
63
64
65
const formatDate = date => date.toISOString().replaceAll(/[^\dZT]/g, '');
export function createGoogleCalendarEventLink({ title, description, start, end, location }) {
if (typeof title !== 'string') {
throw new TypeError('Title must be a string.');
} else if (typeof description !== 'string') {
throw new TypeError('Description must be a string.');
} else if (typeof start === 'string') {
return createGoogleCalendarEventLink({
title, description, start: new Date(start), end, location,
});
} else if (typeof end === 'string') {
return createGoogleCalendarEventLink({
title, description, start, end: new Date(end), location,
});
} else if (! (start instanceof Date)) {
throw new TypeError('Start must be a Date.');
} else if (! (end instanceof Date)) {
throw new TypeError('End must be a Date.');
} else {
const url = new URL('https://calendar.google.com/calendar/render');
url.searchParams.set('action', 'TEMPLATE');
url.searchParams.set('text', title);
url.searchParams.set('details', description);
url.searchParams.set('dates', `${formatDate(start)}/${formatDate(end)}`);
if (typeof location === 'string') {
url.searchParams.set('location', location);
}
return url.toString();
}
}
export function getGoogleMapsURL({ latitude, longitude }) {
return new URL(`https://google.com/maps/place/${latitude}/${longitude}/`);
}
export function openInGoogleMaps(coords, target = '_blank', {
noreferrer = true,
noopener = true,
popup = false,
height = NaN,
width = NaN,
top = NaN,
left = NaN,
} = {}) {
const url = getGoogleMapsURL(coords);
const flags = Object.entries({ popup, noreferrer, noopener, height, width, top, left })
.filter(([, v]) => v !== false && v !== null && ! Number.isNaN(v))
.map(([k, v]) => {
switch (typeof v) {
case 'boolean':
return k;
case 'object':
return Array.isArray(v) ? v.join(' ') : v.toString();
default:
return `${k}=${v}`;
}
}).join(',');
open(url, target, flags);
}