1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function testUserAgent(re: RegExp) {
|
14 | if (typeof window === 'undefined' || window.navigator == null) {
|
15 | return false;
|
16 | }
|
17 | return (
|
18 | window.navigator['userAgentData']?.brands.some((brand: {brand: string, version: string}) => re.test(brand.brand))
|
19 | ) ||
|
20 | re.test(window.navigator.userAgent);
|
21 | }
|
22 |
|
23 | function testPlatform(re: RegExp) {
|
24 | return typeof window !== 'undefined' && window.navigator != null
|
25 | ? re.test(window.navigator['userAgentData']?.platform || window.navigator.platform)
|
26 | : false;
|
27 | }
|
28 |
|
29 | function cached(fn: () => boolean) {
|
30 | if (process.env.NODE_ENV === 'test') {
|
31 | return fn;
|
32 | }
|
33 |
|
34 | let res: boolean | null = null;
|
35 | return () => {
|
36 | if (res == null) {
|
37 | res = fn();
|
38 | }
|
39 | return res;
|
40 | };
|
41 | }
|
42 |
|
43 | export const isMac = cached(function () {
|
44 | return testPlatform(/^Mac/i);
|
45 | });
|
46 |
|
47 | export const isIPhone = cached(function () {
|
48 | return testPlatform(/^iPhone/i);
|
49 | });
|
50 |
|
51 | export const isIPad = cached(function () {
|
52 | return testPlatform(/^iPad/i) ||
|
53 |
|
54 | (isMac() && navigator.maxTouchPoints > 1);
|
55 | });
|
56 |
|
57 | export const isIOS = cached(function () {
|
58 | return isIPhone() || isIPad();
|
59 | });
|
60 |
|
61 | export const isAppleDevice = cached(function () {
|
62 | return isMac() || isIOS();
|
63 | });
|
64 |
|
65 | export const isWebKit = cached(function () {
|
66 | return testUserAgent(/AppleWebKit/i) && !isChrome();
|
67 | });
|
68 |
|
69 | export const isChrome = cached(function () {
|
70 | return testUserAgent(/Chrome/i);
|
71 | });
|
72 |
|
73 | export const isAndroid = cached(function () {
|
74 | return testUserAgent(/Android/i);
|
75 | });
|
76 |
|
77 | export const isFirefox = cached(function () {
|
78 | return testUserAgent(/Firefox/i);
|
79 | });
|