Skip to content

Instantly share code, notes, and snippets.

@sudarshann
Created July 20, 2021 07:34
Show Gist options
  • Save sudarshann/744624d02f6445814439ed7bc2b1e583 to your computer and use it in GitHub Desktop.
Save sudarshann/744624d02f6445814439ed7bc2b1e583 to your computer and use it in GitHub Desktop.
const detectOs = {
getUserAgent: () => {
return navigator.userAgent;
},
getPlatform: () => {
return navigator.platform;
},
isIos: () => {
return /iPhone|iPad|iPod/.test(detectOs.getPlatform());
},
isAndroid: () => {
return /Android/.test(detectOs.getUserAgent());
},
isBlackBerry: () => {
return /BlackBerry/.test(detectOs.getPlatform());
},
isMac: () => {
return /Mac/.test(detectOs.getPlatform());
},
isWindows: () => {
return /Win/.test(detectOs.getPlatform());
},
isLinux: () => {
return /Linux/.test(detectOs.getPlatform()) && !detectOs.isAndroid();
},
get: () => {
if (detectOs.isIos()) return 'iOS';
if (detectOs.isAndroid()) return 'Android';
if (detectOs.isBlackBerry()) return 'BlackBerry';
if (detectOs.isMac()) return 'Mac';
if (detectOs.isWindows()) return 'Windows';
if (detectOs.isLinux()) return 'Linux';
return 'Unknown';
}
}
module.exports = detectOs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment