Skip to content

Instantly share code, notes, and snippets.

@peponi
Last active May 6, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peponi/e1de61c81b64510b388c0c4529afb58a to your computer and use it in GitHub Desktop.
Save peponi/e1de61c81b64510b388c0c4529afb58a to your computer and use it in GitHub Desktop.
is.js - es6 - just browser detection, nothing elese
// based on https://github.com/arasatasaygin/is.js/blob/master/is.js
// this is a cut off minimal version
const userAgent = (navigator && navigator.userAgent.toLowerCase()) || '';
const vendor = (navigator && navigator.vendor || '').toLowerCase();
// build a 'comparator' object for various comparison checks
const comparator = {
'<': (a, b) => a < b,
'<=': (a, b) => a <= b,
'>': (a, b) => a > b,
'>=': (a, b) => a >= b
};
const compareVersion = (version, range) => {
const string = (range + '');
const n = +(string.match(/\d+/) || NaN);
const op = string.match(/^[<>]=?|/)[0];
return comparator[op] ? comparator[op](version, n) : (version == n || n !== n);
};
const ie = range => {
const match = userAgent.match(/(?:msie |trident.+?; rv:)(\d+)/);
return match !== null && compareVersion(match[1], range);
};
const edge = range => {
const match = userAgent.match(/edge\/(\d+)/);
return match !== null && compareVersion(match[1], range);
};
const firefox = range => {
const match = userAgent.match(/(?:firefox|fxios)\/(\d+)/);
return match !== null && compareVersion(match[1], range);
};
const opera = range => {
const match = userAgent.match(/(?:^opera.+?version|opr)\/(\d+)/);
return match !== null && compareVersion(match[1], range);
};
const chrome = range => {
const match = /google inc/.test(vendor) ? userAgent.match(/(?:chrome|crios)\/(\d+)/) : null;
return match !== null && !opera() && compareVersion(match[1], range);
};
const safari = range => {
const match = userAgent.match(/version\/(\d+).+?safari/);
return match !== null && compareVersion(match[1], range);
};
const is = {
ie,
edge,
firefox,
opera,
chrome,
safari
};
export default is;
export {
ie as isIe,
edge as isEdge,
firefox as isFirefox,
opera as isOpera,
chrome as isChrome,
safari as isSafari,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment