Skip to content

Commit

Permalink
Merge pull request #40 from duskload/MDD-39-Add-Edge-Chromium
Browse files Browse the repository at this point in the history
MDD-39 Add Edge Chromium. Update ios Types, add edge legacy type, add os types
  • Loading branch information
duskload authored Jun 15, 2020
2 parents 07100b2 + fc8a4b5 commit a1c84b5
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 217 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ For react example, you can look into separated package [react-device-detect](htt
| isSafari | bool | returns true if browser is `Safari` |
| isOpera | bool | returns true if browser is `Opera` |
| isIE | bool | returns true if browser is `Internet Explorer` |
| isEdge | bool | returns true if browser is `Edge` |
| isEdge | bool | returns true if browser is `Edge` or `Edge Chromium` |
| isYandex | bool | returns true if browser is `Yandex` |
| isChromium | bool | returns true if browser is `Chromium` |
| isMobileSafari | bool | returns true if browser is `Mobile Safari` |
Expand All @@ -78,6 +78,15 @@ For react example, you can look into separated package [react-device-detect](htt
| engineVersion | string | returns engine version |
| getUA | string | returns user agent |
| deviceType | string | returns device type (e.g `mobile` or `tablet`) |
| isIOS13 | boolean | returns true/false if device is running on iOS13 |
| isIPhone13 | boolean | returns true/false if device is iPhone and running on iOS13 |
| isIPad13 | boolean | returns true/false if device is iPad and running on iOS13 |
| isIPod13 | boolean | returns true/false if device is iPod and running on iOS13 |
| isElectron | boolean | returns true/false if running on `Electron` |
| isEdgeChromium | boolean | returns true/false if browser is `Edge Chromium` |
| isLegacyEdge | boolean | returns true if browser is `Edge` |
| isWindows | boolean | returns true/false if os is `Windows` |
| isMacOs | boolean | returns true/false if os is `Mac OS` |
| deviceDetect | function | return data object which includes all data about device (e.g version, engine, os etc.) |

## License
Expand Down
355 changes: 232 additions & 123 deletions dist/index.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ declare module "mobile-device-detect" {
export const isIE: boolean;
export const isEdge: boolean;
export const isYandex: boolean;
export const isIOS13: boolean;
export const isIPad13: boolean;
export const isIPhone13: boolean;
export const isIPod13: boolean;
export const isElectron: boolean;
export const isEdgeChromium: boolean;
export const isLegacyEdge: boolean;
export const isWindows: boolean;
export const isMacOs: boolean;
export const osVersion: string;
export const osName: string;
export const fullBrowserVersion: string;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobile-device-detect",
"version": "0.3.3",
"version": "0.4.3",
"description": "Helpers for handling mobile devices",
"main": "dist/index.js",
"typings": "./index.d.ts",
Expand Down
7 changes: 5 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ const BROWSER_TYPES = {
EDGE: "Edge",
CHROMIUM: "Chromium",
IE: "IE",
MOBILE_SAFARI: "Mobile Safari"
MOBILE_SAFARI: "Mobile Safari",
EDGE_CHROMIUM: "Edge Chromium"
};

const OS_TYPES = {
IOS: "iOS",
ANDROID: "Android",
WINDOWS_PHONE: "Windows Phone"
WINDOWS_PHONE: "Windows Phone",
WINDOWS: "Windows",
MAC_OS: "Mac OS"
};

const defaultData = {
Expand Down
156 changes: 66 additions & 90 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const UAParser = require("../node_modules/ua-parser-js/dist/ua-parser.min");
const { BROWSER_TYPES, OS_TYPES, DEVICE_TYPES } = require("./constants");
const {
BROWSER_TYPES,
OS_TYPES,
DEVICE_TYPES,
defaultData
} = require("./constants");
checkType,
broPayload,
mobilePayload,
wearPayload,
consolePayload,
stvPayload,
getNavigatorInstance,
isIOS13Check
} = require("./utils")

const UA = new UAParser();

Expand All @@ -27,84 +32,7 @@ const {
MOBILE_SAFARI
} = BROWSER_TYPES;
const { MOBILE, TABLET, SMART_TV, BROWSER, WEARABLE, CONSOLE } = DEVICE_TYPES;
const { ANDROID, WINDOWS_PHONE, IOS } = OS_TYPES;

const checkType = type => {
switch (type) {
case MOBILE:
return { isMobile: true };
case TABLET:
return { isTablet: true };
case SMART_TV:
return { isSmartTV: true };
case CONSOLE:
return { isConsole: true };
case WEARABLE:
return { isWearable: true };
case BROWSER:
return { isBrowser: true };
default:
return defaultData;
}
};

const broPayload = (isBrowser, browser, engine, os, ua) => {
return {
isBrowser,
browserMajorVersion: browser.major,
browserFullVersion: browser.version,
browserName: browser.name,
engineName: engine.name || false,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};

const mobilePayload = (type, device, os, ua) => {
return {
...type,
vendor: device.vendor,
model: device.model,
os: os.name,
osVersion: os.version,
ua: ua
};
};

const stvPayload = (isSmartTV, engine, os, ua) => {
return {
isSmartTV,
engineName: engine.name,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};

const consolePayload = (isConsole, engine, os, ua) => {
return {
isConsole,
engineName: engine.name,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};

const wearPayload = (isWearable, engine, os, ua) => {
return {
isWearable,
engineName: engine.name,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};
const { ANDROID, WINDOWS_PHONE, IOS, WINDOWS, MAC_OS } = OS_TYPES;

const isMobileType = () => device.type === MOBILE;
const isTabletType = () => device.type === TABLET;
Expand All @@ -119,11 +47,21 @@ const isMobileAndTabletType = () => {
}
};

const isEdgeChromiumType = () => {
if (os.name === OS_TYPES.WINDOWS && os.version === '10') {
return typeof ua === 'string' && ua.indexOf('Edg/') !== -1;
}

return false;
};

const isSmartTVType = () => device.type === SMART_TV;
const isBrowserType = () => device.type === BROWSER;
const isWearableType = () => device.type === WEARABLE;
const isConsoleType = () => device.type === CONSOLE;
const isAndroidType = () => os.name === ANDROID;
const isWindowsType = () => os.name === WINDOWS;
const isMacOsType = () => os.name === MAC_OS;
const isWinPhoneType = () => os.name === WINDOWS_PHONE;
const isIOSType = () => os.name === IOS;
const isChromeType = () => browser.name === CHROME;
Expand All @@ -139,6 +77,26 @@ const isOperaType = () => browser.name === OPERA;
const isIEType = () =>
browser.name === INTERNET_EXPLORER || browser.name === IE;

const isElectronType = () => {
const nav = getNavigatorInstance();
const ua = nav && nav.userAgent.toLowerCase();

return typeof ua === 'string' ? /electron/.test(ua) : false;
};

const getIOS13 = () => {
const nav = getNavigatorInstance();
return (
nav &&
(/iPad|iPhone|iPod/.test(nav.platform) || (nav.platform === 'MacIntel' && nav.maxTouchPoints > 1)) &&
!window.MSStream
);
};

const getIPad13 = () => isIOS13Check('iPad');
const getIphone13 = () => isIOS13Check('iPhone');
const getIPod13 = () => isIOS13Check('iPod');

const getBrowserFullVersion = () => browser.major;
const getBrowserVersion = () => browser.version;
const getOsVersion = () => (os.version ? os.version : "none");
Expand All @@ -154,15 +112,15 @@ const getDeviceType = () => device.type;
const isSmartTV = isSmartTVType();
const isConsole = isConsoleType();
const isWearable = isWearableType();
const isMobileSafari = isMobileSafariType();
const isMobileSafari = isMobileSafariType() || getIPad13();
const isChromium = isChromiumType();
const isMobile = isMobileAndTabletType();
const isMobile = isMobileAndTabletType() || getIPad13();
const isMobileOnly = isMobileType();
const isTablet = isTabletType();
const isTablet = isTabletType() || getIPad13();
const isBrowser = isBrowserType();
const isAndroid = isAndroidType();
const isWinPhone = isWinPhoneType();
const isIOS = isIOSType();
const isIOS = isIOSType() || getIPad13();
const isChrome = isChromeType();
const isFirefox = isFirefoxType();
const isSafari = isSafariType();
Expand All @@ -178,9 +136,18 @@ const mobileModel = getMobileModel();
const engineName = getEngineName();
const engineVersion = getEngineVersion();
const getUA = getUseragent();
const isEdge = isEdgeType();
const isEdge = isEdgeType() || isEdgeChromiumType();
const isYandex = isYandexType();
const deviceType = getDeviceType()
const deviceType = getDeviceType();
const isIOS13 = getIOS13();
const isIPad13 = getIPad13();
const isIPhone13 = getIphone13();
const isIPod13 = getIPod13();
const isElectron = isElectronType();
const isEdgeChromium = isEdgeChromiumType();
const isLegacyEdge = isEdgeType();
const isWindows = isWindowsType();
const isMacOs = isMacOsType();

const type = checkType(device.type);

Expand Down Expand Up @@ -249,5 +216,14 @@ module.exports = {
getUA,
isEdge,
isYandex,
deviceType
deviceType,
isIOS13,
isIPad13,
isIPhone13,
isIPod13,
isElectron,
isEdgeChromium,
isLegacyEdge,
isWindows,
isMacOs
};
107 changes: 107 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const { DEVICE_TYPES, defaultData } = require("./constants");

const checkType = type => {
switch (type) {
case DEVICE_TYPES.MOBILE:
return { isMobile: true };
case DEVICE_TYPES.TABLET:
return { isTablet: true };
case DEVICE_TYPES.SMART_TV:
return { isSmartTV: true };
case DEVICE_TYPES.CONSOLE:
return { isConsole: true };
case DEVICE_TYPES.WEARABLE:
return { isWearable: true };
case DEVICE_TYPES.BROWSER:
return { isBrowser: true };
default:
return defaultData;
}
};

const broPayload = (isBrowser, browser, engine, os, ua) => {
return {
isBrowser,
browserMajorVersion: browser.major,
browserFullVersion: browser.version,
browserName: browser.name,
engineName: engine.name || false,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};

const mobilePayload = (type, device, os, ua) => {
return {
...type,
vendor: device.vendor,
model: device.model,
os: os.name,
osVersion: os.version,
ua: ua
};
};

const stvPayload = (isSmartTV, engine, os, ua) => {
return {
isSmartTV,
engineName: engine.name,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};

const consolePayload = (isConsole, engine, os, ua) => {
return {
isConsole,
engineName: engine.name,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};

const wearPayload = (isWearable, engine, os, ua) => {
return {
isWearable,
engineName: engine.name,
engineVersion: engine.version,
osName: os.name,
osVersion: os.version,
userAgent: ua
};
};


export const getNavigatorInstance = () => {
if (typeof window !== 'undefined') {
if (window.navigator || navigator) {
return window.navigator || navigator;
}
}

return false;
};

export const isIOS13Check = type => {
const nav = getNavigatorInstance();
return (
nav && nav.platform && (nav.platform.indexOf(type) !== -1 || (nav.platform === 'MacIntel' && nav.maxTouchPoints > 1 && !window.MSStream))
);
};

module.exports = {
checkType,
broPayload,
mobilePayload,
stvPayload,
consolePayload,
wearPayload,
getNavigatorInstance,
isIOS13Check,
}

0 comments on commit a1c84b5

Please sign in to comment.