UNPKG

1.66 kBJavaScriptView Raw
1import { getWindow } from 'ssr-window';
2import { getSupport } from './get-support.js';
3let deviceCached;
4
5function calcDevice(_temp) {
6 let {
7 userAgent
8 } = _temp === void 0 ? {} : _temp;
9 const support = getSupport();
10 const window = getWindow();
11 const platform = window.navigator.platform;
12 const ua = userAgent || window.navigator.userAgent;
13 const device = {
14 ios: false,
15 android: false
16 };
17 const screenWidth = window.screen.width;
18 const screenHeight = window.screen.height;
19 const android = ua.match(/(Android);?[\s\/]+([\d.]+)?/); // eslint-disable-line
20
21 let ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
22 const ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
23 const iphone = !ipad && ua.match(/(iPhone\sOS|iOS)\s([\d_]+)/);
24 const windows = platform === 'Win32';
25 let macos = platform === 'MacIntel'; // iPadOs 13 fix
26
27 const iPadScreens = ['1024x1366', '1366x1024', '834x1194', '1194x834', '834x1112', '1112x834', '768x1024', '1024x768', '820x1180', '1180x820', '810x1080', '1080x810'];
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 (!deviceCached) {
56 deviceCached = calcDevice(overrides);
57 }
58
59 return deviceCached;
60}
61
62export { getDevice };
\No newline at end of file