-
Notifications
You must be signed in to change notification settings - Fork 82
/
utils.ts
83 lines (70 loc) · 2.43 KB
/
utils.ts
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
import { UAParser } from 'ua-parser-js'
/*
* Sometimes tests need to know what browser they run in to make proper assertions.
* Karma doesn't provide this information.
* The browser detect functions in the `src` directory can't be used because they are objects of testing.
* Therefore a popular third party library is used to detect browser.
* The library isn't used in the main code because some visitors tamper user agent while the test environments don't.
*
* We should find a way to pass browser settings from the Karma configuration to the tests,
* otherwise we can't distinguish incognito browsers from regular browsers, Brave from Chrome, etc.
*/
export function isChromium(): boolean {
return new UAParser().getEngine().name === 'Blink'
}
export function isHeadlessChrome(): boolean {
return navigator.userAgent.includes('HeadlessChrome')
}
export function isGecko(): boolean {
return new UAParser().getEngine().name === 'Gecko'
}
export function isWebKit(): boolean {
return new UAParser().getEngine().name === 'WebKit'
}
export function isMobile(): boolean {
return new UAParser().getDevice().type === 'mobile'
}
export function isMacOS(): boolean {
return new UAParser().getOS().name === 'Mac OS'
}
export function getOsMajorVersion(): number | undefined {
const version = new UAParser().getOS().version
if (version === undefined) {
return undefined
}
return parseInt(version.split('.')[0])
}
export function getBrowserMajorVersion(): number | undefined {
const version = new UAParser().getBrowser().version
if (version === undefined) {
return undefined
}
return parseInt(version.split('.')[0])
}
export function getBrowserEngineMajorVersion(): number | undefined {
const version = new UAParser().getEngine().version
if (version === undefined) {
return undefined
}
return parseInt(version.split('.')[0])
}
export function getBrowserVersion(): { major: number; minor: number } | undefined {
const version = new UAParser().getBrowser().version
if (version === undefined) {
return undefined
}
return {
major: parseInt(version.split('.')[0]),
minor: parseInt(version.split('.')[1]),
}
}
export function getBrowserEngineVersion(): { major: number; minor: number } | undefined {
const version = new UAParser().getEngine().version
if (version === undefined) {
return undefined
}
return {
major: parseInt(version.split('.')[0]),
minor: parseInt(version.split('.')[1]),
}
}