Last active
August 18, 2017 08:56
-
-
Save jokemmy/b02875e0619706a14cc74484f09d74da to your computer and use it in GitHub Desktop.
simple browser detect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const browser = ( function() { | |
try { | |
const userAgent = navigator.userAgent.toLowerCase(); | |
const makeArray = ( some ) => { | |
return !Array.isArray( some ) ? [some] : some; | |
}; | |
const regexps = { | |
ie: [ /rv:([\d.]+)\) like gecko/, /msie ([\d.]+)/ ], | |
firefox: /firefox\/([\d.]+)/, | |
chrome: /chrome\/([\d.]+)/, | |
opera: /opera.([\d.]+)/, | |
safari: /version\/([\d.]+).*safari/ | |
}; | |
return Object.entries( regexps ).reduce(( env, [ key, regexps ]) => { | |
makeArray( regexps ) | |
.map(( regexp ) => userAgent.match( regexp )) | |
.filter(( s ) => s ) | |
.forEach(( s ) => { | |
env[key] = s[1]; | |
}); | |
return env; | |
}, {}); | |
} catch ( e ) { | |
return {}; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment