UNPKG

1.58 kBJavaScriptView Raw
1import { getWindow } from 'ssr-window';
2import { getSupport } from './get-support';
3var device;
4
5function calcDevice(_temp) {
6 var _ref = _temp === void 0 ? {} : _temp,
7 userAgent = _ref.userAgent;
8
9 var support = getSupport();
10 var window = getWindow();
11 var platform = window.navigator.platform;
12 var ua = userAgent || window.navigator.userAgent;
13 var device = {
14 ios: false,
15 android: false
16 };
17 var screenWidth = window.screen.width;
18 var screenHeight = window.screen.height;
19 var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/); // eslint-disable-line
20
21 var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
22 var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
23 var iphone = !ipad && ua.match(/(iPhone\sOS|iOS)\s([\d_]+)/);
24 var windows = platform === 'Win32';
25 var macos = platform === 'MacIntel'; // iPadOs 13 fix
26
27 var iPadScreens = ['1024x1366', '1366x1024', '834x1194', '1194x834', '834x1112', '1112x834', '768x1024', '1024x768'];
28
29 if (!ipad && macos && support.touch && iPadScreens.indexOf(screenWidth + "x" + screenHeight) >= 0) {
30 ipad = ua.match(/(Version)\/([\d.]+)/);
31 if (!ipad) ipad = [0, 1, '13_0_0'];
32 macos = false;
33 } // Android
34
35
36 if (android && !windows) {
37 device.os = 'android';
38 device.android = true;
39 }
40
41 if (ipad || iphone || ipod) {
42 device.os = 'ios';
43 device.ios = true;
44 } // Export object
45
46
47 return device;
48}
49
50function getDevice(overrides) {
51 if (overrides === void 0) {
52 overrides = {};
53 }
54
55 if (!device) {
56 device = calcDevice(overrides);
57 }
58
59 return device;
60}
61
62export { getDevice };
\No newline at end of file