UNPKG

2.27 kBPlain TextView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13function 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
23function 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
29function 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
43export const isMac = cached(function () {
44 return testPlatform(/^Mac/i);
45});
46
47export const isIPhone = cached(function () {
48 return testPlatform(/^iPhone/i);
49});
50
51export const isIPad = cached(function () {
52 return testPlatform(/^iPad/i) ||
53 // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
54 (isMac() && navigator.maxTouchPoints > 1);
55});
56
57export const isIOS = cached(function () {
58 return isIPhone() || isIPad();
59});
60
61export const isAppleDevice = cached(function () {
62 return isMac() || isIOS();
63});
64
65export const isWebKit = cached(function () {
66 return testUserAgent(/AppleWebKit/i) && !isChrome();
67});
68
69export const isChrome = cached(function () {
70 return testUserAgent(/Chrome/i);
71});
72
73export const isAndroid = cached(function () {
74 return testUserAgent(/Android/i);
75});
76
77export const isFirefox = cached(function () {
78 return testUserAgent(/Firefox/i);
79});