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
92 lines (77 loc) · 2.58 KB
/
helperFunctions.ts
File metadata and controls
92 lines (77 loc) · 2.58 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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Config } from './config';
export function isFullScreen(el: HTMLMediaElement): boolean {
return document.fullscreenElement === el || false;
}
export function isHtmlMediaElement(element?: Node): element is HTMLMediaElement {
return element instanceof HTMLMediaElement;
}
export function isHtmlAudioElement(element?: Node): element is HTMLAudioElement {
return element instanceof HTMLAudioElement;
}
export function isHtmlVideoElement(element?: Node): element is HTMLVideoElement {
return element instanceof HTMLVideoElement;
}
export function getUriFileExtension(uri: string): string | null {
// greedily match until the final '.' is found with trailing characters, capture those as extension
// only capture until finding URI metacharacters
const pattern = /.+\.([^\.?&#]+)/;
let uriPath = uri;
try {
uriPath = new URL(uri).pathname;
} catch (e) {}
// try to match against the pathname only first, if unsuccessful try against the full URI
const match = pattern.exec(uriPath) || pattern.exec(uri);
return match && match[1];
}
export function getDuration(el: HTMLAudioElement | HTMLVideoElement): number | null {
const duration = el.duration;
// A NaN value is returned if duration is not available, or Infinity if the media resource is streaming.
if (isNaN(duration) || duration === Infinity) {
return null;
}
return duration;
}
// Checks if a url is a data_url, so we don't send a (potentially large) payload
export function dataUrlHandler(url: string): string {
if (url.indexOf('data:') !== -1) {
return 'DATA_URL';
}
return url;
}
type TimeRange = { start: number; end: number };
export function timeRangesToObjectArray(t: TimeRanges): TimeRange[] {
const out: TimeRange[] = [];
for (let i = 0; i < t.length; i++) {
const start = t.start(i);
const end = t.end(i);
if (isFinite(start) && isFinite(end)) {
out.push({ start: start || 0, end: end || 0 });
}
}
return out;
}
type TextTrack = {
label: string;
language: string;
kind: string;
mode: string;
};
export function textTrackListToJson(textTrackList: TextTrackList): TextTrack[] {
return Object.keys(textTrackList).map((_, i) => {
return {
label: textTrackList[i].label,
language: textTrackList[i].language,
kind: textTrackList[i].kind,
mode: textTrackList[i].mode,
};
});
}
export function setConfigDefaults(config: Config): Config {
const defaults = {
boundaries: [10, 25, 50, 75],
};
return { ...defaults, ...config };
}
export function parseVolume(volume: number): number {
return parseInt((volume * 100).toString());
}