Skip to content

Commit c68768c

Browse files
authored
Merge pull request #62 from duskload/RDD-60-Validate-Window
RDD-60 Validate window
2 parents 33f3e4b + b514564 commit c68768c

File tree

5 files changed

+68
-50
lines changed

5 files changed

+68
-50
lines changed

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tabWidth": 2,
3+
"singleQuote": true,
4+
"endOfLine": "auto",
5+
"printWidth": 120
6+
}

main.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
77
var React = require('react');
88
var React__default = _interopDefault(React);
99

10-
var UAParser = require("ua-parser-js/dist/ua-parser.min");
10+
var UAParser = require('ua-parser-js/dist/ua-parser.min');
1111

1212
var UA = new UAParser();
1313
var browser = UA.getBrowser();
@@ -16,12 +16,23 @@ var device = UA.getDevice();
1616
var engine = UA.getEngine();
1717
var os = UA.getOS();
1818
var ua = UA.getUA();
19+
1920
var setDefaults = function setDefaults(p) {
20-
var d = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "none";
21+
var d = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'none';
2122
return p ? p : d;
2223
};
24+
var getNavigatorInstance = function getNavigatorInstance() {
25+
if (typeof window !== 'undefined') {
26+
if (window.navigator || navigator) {
27+
return window.navigator || navigator;
28+
}
29+
}
30+
31+
return false;
32+
};
2333
var isIOS13Check = function isIOS13Check(type) {
24-
return navigator && (navigator.platform.includes(type) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1 && !window.MSStream);
34+
var nav = getNavigatorInstance();
35+
return nav && (nav.platform.includes(type) || nav.platform === 'MacIntel' && nav.maxTouchPoints > 1 && !window.MSStream);
2536
};
2637

2738
function _typeof(obj) {
@@ -415,24 +426,26 @@ var isIEType = function isIEType() {
415426
};
416427

417428
var isElectronType = function isElectronType() {
418-
var ua = navigator && navigator.userAgent.toLowerCase();
429+
var nav = getNavigatorInstance();
430+
var ua = nav && nav.userAgent.toLowerCase();
419431
return typeof ua === 'string' ? ua.includes('electron') : false;
420432
};
421433

422434
var getIOS13 = function getIOS13() {
423-
return navigator && (/iPad|iPhone|iPod/.test(navigator.platform) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1) && !window.MSStream;
435+
var nav = getNavigatorInstance();
436+
return nav && (/iPad|iPhone|iPod/.test(nav.platform) || nav.platform === 'MacIntel' && nav.maxTouchPoints > 1) && !window.MSStream;
424437
};
425438

426439
var getIPad13 = function getIPad13() {
427-
return isIOS13Check("iPad");
440+
return isIOS13Check('iPad');
428441
};
429442

430443
var getIphone13 = function getIphone13() {
431-
return isIOS13Check("iPhone");
444+
return isIOS13Check('iPhone');
432445
};
433446

434447
var getIPod13 = function getIPod13() {
435-
return isIOS13Check("iPod");
448+
return isIOS13Check('iPod');
436449
};
437450

438451
var getBrowserFullVersion = function getBrowserFullVersion() {
@@ -476,7 +489,7 @@ var getUseragent = function getUseragent() {
476489
};
477490

478491
var getDeviceType = function getDeviceType() {
479-
return setDefaults(device.type, "browser");
492+
return setDefaults(device.type, 'browser');
480493
};
481494

482495
var isSmartTV = isSmartTVType();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-device-detect",
3-
"version": "1.11.13",
3+
"version": "1.11.14",
44
"description": "Detect device type and render your component according to it",
55
"main": "main.js",
66
"typings": "./index.d.ts",
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const UAParser = require("ua-parser-js/dist/ua-parser.min");
1+
const UAParser = require('ua-parser-js/dist/ua-parser.min');
22

33
export const UA = new UAParser();
44

@@ -11,16 +11,25 @@ export const ua = UA.getUA();
1111
export const setUA = uaStr => UA.setUA(uaStr);
1212

1313
export const mockUserAgent = userAgent => {
14-
window.navigator.__defineGetter__("userAgent", () => userAgent);
14+
window.navigator.__defineGetter__('userAgent', () => userAgent);
1515
};
1616

17-
export const setDefaults = (p, d = "none") => (p ? p : d);
17+
//TODO: Rework structure, move helper functions to utils
18+
export const setDefaults = (p, d = 'none') => (p ? p : d);
19+
20+
export const getNavigatorInstance = () => {
21+
if (typeof window !== 'undefined') {
22+
if (window.navigator || navigator) {
23+
return window.navigator || navigator;
24+
}
25+
}
26+
27+
return false;
28+
};
1829

1930
export const isIOS13Check = type => {
20-
return navigator && (
21-
navigator.platform.includes(type) ||
22-
(navigator.platform === "MacIntel" &&
23-
navigator.maxTouchPoints > 1 &&
24-
!window.MSStream)
31+
const nav = getNavigatorInstance();
32+
return (
33+
nav && (nav.platform.includes(type) || (nav.platform === 'MacIntel' && nav.maxTouchPoints > 1 && !window.MSStream))
2534
);
2635
};

src/components/helpers/selectors.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import {
2-
os,
3-
device,
4-
browser,
5-
ua,
6-
engine,
7-
setDefaults,
8-
isIOS13Check
9-
} from "./get-ua-data";
10-
import { BROWSER_TYPES, DEVICE_TYPES, OS_TYPES } from "./types";
1+
import { os, device, browser, ua, engine, setDefaults, isIOS13Check, getNavigatorInstance } from './get-ua-data';
2+
import { BROWSER_TYPES, DEVICE_TYPES, OS_TYPES } from './types';
113

124
const isMobileType = () => device.type === DEVICE_TYPES.MOBILE;
135
const isTabletType = () => device.type === DEVICE_TYPES.TABLET;
@@ -34,31 +26,29 @@ const isFirefoxType = () => browser.name === BROWSER_TYPES.FIREFOX;
3426
const isChromiumType = () => browser.name === BROWSER_TYPES.CHROMIUM;
3527
const isEdgeType = () => browser.name === BROWSER_TYPES.EDGE;
3628
const isYandexType = () => browser.name === BROWSER_TYPES.YANDEX;
37-
const isSafariType = () =>
38-
browser.name === BROWSER_TYPES.SAFARI ||
39-
browser.name === BROWSER_TYPES.MOBILE_SAFARI;
29+
const isSafariType = () => browser.name === BROWSER_TYPES.SAFARI || browser.name === BROWSER_TYPES.MOBILE_SAFARI;
4030
const isMobileSafariType = () => browser.name === BROWSER_TYPES.MOBILE_SAFARI;
4131
const isOperaType = () => browser.name === BROWSER_TYPES.OPERA;
42-
const isIEType = () =>
43-
browser.name === BROWSER_TYPES.INTERNET_EXPLORER ||
44-
browser.name === BROWSER_TYPES.IE;
32+
const isIEType = () => browser.name === BROWSER_TYPES.INTERNET_EXPLORER || browser.name === BROWSER_TYPES.IE;
4533
const isElectronType = () => {
46-
const ua = navigator && navigator.userAgent.toLowerCase()
34+
const nav = getNavigatorInstance();
35+
const ua = nav && nav.userAgent.toLowerCase();
4736

48-
return typeof ua === 'string' ? ua.includes('electron') : false
49-
}
37+
return typeof ua === 'string' ? ua.includes('electron') : false;
38+
};
5039

5140
const getIOS13 = () => {
52-
return navigator && (
53-
(/iPad|iPhone|iPod/.test(navigator.platform) ||
54-
(navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1)) &&
55-
!window.MSStream
41+
const nav = getNavigatorInstance();
42+
return (
43+
nav &&
44+
(/iPad|iPhone|iPod/.test(nav.platform) || (nav.platform === 'MacIntel' && nav.maxTouchPoints > 1)) &&
45+
!window.MSStream
5646
);
5747
};
5848

59-
const getIPad13 = () => isIOS13Check("iPad");
60-
const getIphone13 = () => isIOS13Check("iPhone");
61-
const getIPod13 = () => isIOS13Check("iPod");
49+
const getIPad13 = () => isIOS13Check('iPad');
50+
const getIphone13 = () => isIOS13Check('iPhone');
51+
const getIPod13 = () => isIOS13Check('iPod');
6252

6353
const getBrowserFullVersion = () => setDefaults(browser.version);
6454
const getBrowserVersion = () => setDefaults(browser.major);
@@ -70,7 +60,7 @@ const getMobileModel = () => setDefaults(device.model);
7060
const getEngineName = () => setDefaults(engine.name);
7161
const getEngineVersion = () => setDefaults(engine.version);
7262
const getUseragent = () => setDefaults(ua);
73-
const getDeviceType = () => setDefaults(device.type, "browser");
63+
const getDeviceType = () => setDefaults(device.type, 'browser');
7464

7565
export const isSmartTV = isSmartTVType();
7666
export const isConsole = isConsoleType();
@@ -102,8 +92,8 @@ export const getUA = getUseragent();
10292
export const isEdge = isEdgeType();
10393
export const isYandex = isYandexType();
10494
export const deviceType = getDeviceType();
105-
export const isIOS13 = getIOS13()
106-
export const isIPad13 = getIPad13()
107-
export const isIPhone13 = getIphone13()
108-
export const isIPod13 = getIPod13()
109-
export const isElectron = isElectronType()
95+
export const isIOS13 = getIOS13();
96+
export const isIPad13 = getIPad13();
97+
export const isIPhone13 = getIphone13();
98+
export const isIPod13 = getIPod13();
99+
export const isElectron = isElectronType();

0 commit comments

Comments
 (0)