get-device.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { getWindow } from 'ssr-window';
  2. import { getSupport } from './get-support.js';
  3. let deviceCached;
  4. function calcDevice(_temp) {
  5. let {
  6. userAgent
  7. } = _temp === void 0 ? {} : _temp;
  8. const support = getSupport();
  9. const window = getWindow();
  10. const platform = window.navigator.platform;
  11. const ua = userAgent || window.navigator.userAgent;
  12. const device = {
  13. ios: false,
  14. android: false
  15. };
  16. const screenWidth = window.screen.width;
  17. const screenHeight = window.screen.height;
  18. const android = ua.match(/(Android);?[\s\/]+([\d.]+)?/); // eslint-disable-line
  19. let ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
  20. const ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
  21. const iphone = !ipad && ua.match(/(iPhone\sOS|iOS)\s([\d_]+)/);
  22. const windows = platform === 'Win32';
  23. let macos = platform === 'MacIntel'; // iPadOs 13 fix
  24. const iPadScreens = ['1024x1366', '1366x1024', '834x1194', '1194x834', '834x1112', '1112x834', '768x1024', '1024x768', '820x1180', '1180x820', '810x1080', '1080x810'];
  25. if (!ipad && macos && support.touch && iPadScreens.indexOf(`${screenWidth}x${screenHeight}`) >= 0) {
  26. ipad = ua.match(/(Version)\/([\d.]+)/);
  27. if (!ipad) ipad = [0, 1, '13_0_0'];
  28. macos = false;
  29. } // Android
  30. if (android && !windows) {
  31. device.os = 'android';
  32. device.android = true;
  33. }
  34. if (ipad || iphone || ipod) {
  35. device.os = 'ios';
  36. device.ios = true;
  37. } // Export object
  38. return device;
  39. }
  40. function getDevice(overrides) {
  41. if (overrides === void 0) {
  42. overrides = {};
  43. }
  44. if (!deviceCached) {
  45. deviceCached = calcDevice(overrides);
  46. }
  47. return deviceCached;
  48. }
  49. export { getDevice };