-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckDevice.js
60 lines (50 loc) · 1.68 KB
/
checkDevice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { isArr, isNodeJS, isObj } from './utils'
export const DEVICE_TYPE = {
desktop: 'desktop',
mobile: 'mobile',
nodejs: 'nodejs',
tablet: 'tablet',
}
// Source https://attacomsian.com/blog/javascript-detect-mobile-device
export const MOBILE_REGEX = /Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/
export const TABLET_REGEX = /(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i
/**
* @name checkDevice
* @summary check if device is of specific type
*
* @param {String|Array} type Valid types: desktop, mobile, table or nodejs
*
* @returns {Boolean}
*/
export const checkDevice = type => (isArr(type) ? type : [type]).includes(getDeviceType())
/**
* @name checkSafari
* @summary check if browser is Apple Safari
*
* @param {Boolean} desktop check Safari desktop
* Default: `false`
*
* @returns {Boolean}
*/
export const checkSafari = (desktop = false) => {
const { vendor = '', userAgent = '' } = navigator || {}
const isSafari = vendor.indexOf('Apple') > -1
&& userAgent.indexOf('CriOS') == -1
&& userAgent.indexOf('FxiOS') == -1
return !desktop
? isSafari
: isSafari && isObj(window.safari)
}
/**
* @name getDeviceType
* @summary get the type of the device
*
* @returns {String} Device type. One of the following: desktop, mobile, table or nodejs
*/
export const getDeviceType = () => {
if (isNodeJS()) return DEVICE_TYPE.nodejs
const { userAgent } = navigator
if (TABLET_REGEX.test(userAgent)) return DEVICE_TYPE.tablet
if (MOBILE_REGEX.test(userAgent)) return DEVICE_TYPE.mobile
return DEVICE_TYPE.desktop
}