forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelperFunctions.ts
More file actions
35 lines (30 loc) · 1.13 KB
/
Copy pathhelperFunctions.ts
File metadata and controls
35 lines (30 loc) · 1.13 KB
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
export function isElementFullScreen(mediaId: string): boolean {
if (document.fullscreenElement) {
return document.fullscreenElement.id === mediaId;
}
return false;
}
// URLSearchParams is not supported in IE
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
export function addUrlParam(url: string, key: string, value: string) {
const urlParams = parseUrlParams(url);
urlParams[key] = value;
return `${url.split('?').shift()!}?${urlParamsToString(urlParams)}`;
}
export function parseUrlParams(url: string): Record<string, string> {
const params: Record<string, string> = {};
const urlParams = url.split('?').slice(1).join('?');
if (!urlParams) return params;
urlParams.split('&').forEach((p) => {
const param = p.split('=');
const key = decodeURIComponent(param.shift()!);
params[key] = decodeURIComponent(param.join('='));
});
return params;
}
function urlParamsToString(urlParams: Record<string, string>) {
// convert an object of url parameters to a string
return Object.keys(urlParams)
.map((p) => `${encodeURIComponent(p)}=${encodeURIComponent(urlParams[p])}`)
.join('&');
}