diff --git a/bowser.js b/bowser.js index 082cd47..980781e 100644 --- a/bowser.js +++ b/bowser.js @@ -28,6 +28,34 @@ * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" */ + /** @const */ + var TRUE = true; + + /** @const */ + var STR_VERSION = 'version'; + + /** @const */ + var STR_MOBILE = 'mobile'; + + /** @const */ + var STR_MSIE = 'msie'; + + /** @const */ + var STR_OPERA = 'opera'; + + /** @const */ + var STR_CHROME = 'chrome'; + + /** @const */ + var STR_WEBKIT = 'webkit'; + + /** @const */ + var STR_SAFARI = 'safari'; + + /** @const */ + var STR_FIREFOX = 'firefox'; + + function detect(ua) { /** * Test a given regex against our user agent @@ -50,100 +78,76 @@ return match ? match[i || 1] : 0; } - var t = true - , iphone = uaTest(/iphone/i) + var iphone = uaTest(/iphone/i) , ipad = uaTest(/ipad/i) , webkitVersion = /version\/(\d+(\.\d+)?)/i , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i - , o; + , o = {} + , version = 0; if (uaTest(/(msie|trident)/i)) { - o = { - msie: t - , version: versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2) - }; + o[STR_MSIE] = TRUE; + o[STR_VERSION] = versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2); } else if (uaTest(/opera/i) || uaTest(/opr/i)) { - o = { - opera: t - , version: versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i) - }; + o[STR_OPERA] = TRUE; + o[STR_VERSION] = versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i); } else if (uaTest(/chrome/i)) { - o = { - webkit: t - , chrome: t - , version: versionTest(/chrome\/(\d+(\.\d+)?)/i) - }; + o[STR_CHROME] = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(/chrome\/(\d+(\.\d+)?)/i); } else if (uaTest(/phantom/i)) { - o = { - webkit: t - , phantom: t - , version: versionTest(/phantomjs\/(\d+(\.\d+)+)/i) - }; + o.phantom = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(/phantomjs\/(\d+(\.\d+)?)/i); } else if (uaTest(/touchpad/i)) { - o = { - webkit: t - , touchpad: t - , version : versionTest(/touchpad\/(\d+(\.\d+)?)/i) - }; + o.touchpad = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(/touchpad\/(\d+(\.\d+)?)/i); } else if (iphone || ipad) { - o = { - webkit: t - , mobile: t - , ios: t - , iphone: iphone - , ipad: ipad + o.ios = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; // WTF: version is not part of user agent in web apps - , version: versionTest(webkitVersion) - }; + o[STR_VERSION] = versionTest(webkitVersion); + if (iphone) { + o.iphone = TRUE; + } else if (ipad) { + o.ipad = TRUE; + } } else if (uaTest(/android/i)) { - o = { - webkit: t - , android: t - , mobile: t - , version: versionTest(webkitVersion) || versionTest(firefoxVersion) - }; + o.android = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(webkitVersion) || versionTest(firefoxVersion); } else if (uaTest(/safari/i)) { - o = { - webkit: t - , safari: t - , version: versionTest(webkitVersion) - }; + o[STR_SAFARI] = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(webkitVersion); } else if (uaTest(/gecko\//i)) { - o = { - gecko: t - , mozilla: t - , version: versionTest(firefoxVersion) - }; + o.gecko = o.mozilla = TRUE; + o[STR_VERSION] = versionTest(firefoxVersion); if (uaTest(/firefox/i)) { - o.firefox = t; + o.firefox = TRUE; } } else if (uaTest(/seamonkey\//i)) { - o = { - seamonkey: t - , version: versionTest(/seamonkey\/(\d+(\.\d+)?)/i) - }; + o.seamonkey = TRUE; + o[STR_VERSION] = versionTest(/seamonkey\/(\d+(\.\d+)?)/i); } else { - o = {}; + o[STR_VERSION] = 0; } + version = o[STR_VERSION]; + // Graded Browser Support // http://developer.yahoo.com/yui/articles/gbs - if ((o.msie && o.version >= 8) || - (o.chrome && o.version >= 10) || - (o.firefox && o.version >= 4.0) || - (o.safari && o.version >= 5) || - (o.opera && o.version >= 10.0)) { - o.a = t; + if ((o[STR_MSIE] && version >= 8) || + (o[STR_CHROME] && version >= 10) || + (o[STR_FIREFOX] && version >= 4.0) || + (o[STR_SAFARI] && version >= 5) || + (o[STR_OPERA] && version >= 10.0)) { + o.a = TRUE; } - else if ((o.msie && o.version < 8) || - (o.chrome && o.version < 10) || - (o.firefox && o.version < 4.0) || - (o.safari && o.version < 5) || - (o.opera && o.version < 10.0)) { - o.c = t; + else if ((o[STR_MSIE] && version < 8) || + (o[STR_CHROME] && version < 10) || + (o[STR_FIREFOX] && version < 4.0) || + (o[STR_SAFARI] && version < 5) || + (o[STR_OPERA] && version < 10.0)) { + o.c = TRUE; } else { - o.x = t; + o.x = TRUE; } return o; diff --git a/bowser.min.js b/bowser.min.js index ed48146..8546b85 100644 --- a/bowser.min.js +++ b/bowser.min.js @@ -3,4 +3,4 @@ * https://github.com/ded/bowser * MIT License | (c) Dustin Diaz 2013 */ -!function(e,t){typeof define=="function"?define(t):typeof module!="undefined"&&module.exports?module.exports[e]=t():this[e]=t()}("bowser",function(){function e(e){function t(t){return t.test(e)}function n(t,n){var r=e.match(t);return r?r[n||1]:0}var r=!0,i=t(/iphone/i),s=t(/ipad/i),o=/version\/(\d+(\.\d+)?)/i,u=/firefox\/(\d+(\.\d+)?)/i,a;if(t(/(msie|trident)/i))a={msie:r,version:n(/(msie |rv:)(\d+(\.\d+)?)/i,2)};else if(t(/opera/i)||t(/opr/i))a={opera:r,version:n(o)||n(/opr\/(\d+(\.\d+)?)/i)};else if(t(/chrome/i))a={webkit:r,chrome:r,version:n(/chrome\/(\d+(\.\d+)?)/i)};else if(t(/phantom/i))a={webkit:r,phantom:r,version:n(/phantomjs\/(\d+(\.\d+)+)/i)};else{if(t(/touchpad/i))return{webkit:r,touchpad:r,version:n(/touchpad\/(\d+(\.\d+)?)/i)};i||s?a={webkit:r,mobile:r,ios:r,iphone:i,ipad:s,version:n(o)}:t(/android/i)?a={webkit:r,android:r,mobile:r,version:n(o)||n(u)}:t(/safari/i)?a={webkit:r,safari:r,version:n(o)}:t(/gecko\//i)?(a={gecko:r,mozilla:r,version:n(u)},t(/firefox/i)&&(a.firefox=r)):t(/seamonkey\//i)?a={seamonkey:r,version:n(/seamonkey\/(\d+(\.\d+)?)/i)}:a={}}return a.msie&&a.version>=8||a.chrome&&a.version>=10||a.firefox&&a.version>=4||a.safari&&a.version>=5||a.opera&&a.version>=10?a.a=r:a.msie&&a.version<8||a.chrome&&a.version<10||a.firefox&&a.version<4||a.safari&&a.version<5||a.opera&&a.version<10?a.c=r:a.x=r,a}var t=e(typeof navigator!="undefined"?navigator.userAgent:"");return t._detect=e,t}) +!function(e,t){typeof define=="function"?define(t):typeof module!="undefined"&&module.exports?module.exports[e]=t():this[e]=t()}("bowser",function(){function f(f){function l(e){return e.test(f)}function c(e,t){var n=f.match(e);return n?n[t||1]:0}var h=l(/iphone/i),p=l(/ipad/i),d=/version\/(\d+(\.\d+)?)/i,v=/firefox\/(\d+(\.\d+)?)/i,m={},g=0;return l(/(msie|trident)/i)?(m[r]=e,m[t]=c(/(msie |rv:)(\d+(\.\d+)?)/i,2)):l(/opera/i)||l(/opr/i)?(m[i]=e,m[t]=c(d)||c(/opr\/(\d+(\.\d+)?)/i)):l(/chrome/i)?(m[s]=m[o]=e,m[t]=c(/chrome\/(\d+(\.\d+)?)/i)):l(/phantom/i)?(m.phantom=m[o]=e,m[t]=c(/phantomjs\/(\d+(\.\d+)?)/i)):l(/touchpad/i)?(m.touchpad=m[o]=e,m[t]=c(/touchpad\/(\d+(\.\d+)?)/i)):h||p?(m.ios=m[n]=m[o]=e,m[t]=c(d),h?m.iphone=e:p&&(m.ipad=e)):l(/android/i)?(m.android=m[n]=m[o]=e,m[t]=c(d)||c(v)):l(/safari/i)?(m[u]=m[o]=e,m[t]=c(d)):l(/gecko\//i)?(m.gecko=m.mozilla=e,m[t]=c(v),l(/firefox/i)&&(m.firefox=e)):l(/seamonkey\//i)?(m.seamonkey=e,m[t]=c(/seamonkey\/(\d+(\.\d+)?)/i)):m[t]=0,g=m[t],m[r]&&g>=8||m[s]&&g>=10||m[a]&&g>=4||m[u]&&g>=5||m[i]&&g>=10?m.a=e:m[r]&&g<8||m[s]&&g<10||m[a]&&g<4||m[u]&&g<5||m[i]&&g<10?m.c=e:m.x=e,m}var e=!0,t="version",n="mobile",r="msie",i="opera",s="chrome",o="webkit",u="safari",a="firefox",l=f(typeof navigator!="undefined"?navigator.userAgent:"");return l._detect=f,l}) \ No newline at end of file diff --git a/src/bowser.js b/src/bowser.js index 8c211fd..8b1506e 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -22,6 +22,34 @@ * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" */ + /** @const */ + var TRUE = true; + + /** @const */ + var STR_VERSION = 'version'; + + /** @const */ + var STR_MOBILE = 'mobile'; + + /** @const */ + var STR_MSIE = 'msie'; + + /** @const */ + var STR_OPERA = 'opera'; + + /** @const */ + var STR_CHROME = 'chrome'; + + /** @const */ + var STR_WEBKIT = 'webkit'; + + /** @const */ + var STR_SAFARI = 'safari'; + + /** @const */ + var STR_FIREFOX = 'firefox'; + + function detect(ua) { /** * Test a given regex against our user agent @@ -44,103 +72,76 @@ return match ? match[i || 1] : 0; } - var t = true - , iphone = uaTest(/iphone/i) + var iphone = uaTest(/iphone/i) , ipad = uaTest(/ipad/i) , webkitVersion = /version\/(\d+(\.\d+)?)/i , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i - , o; + , o = {} + , version = 0; if (uaTest(/(msie|trident)/i)) { - o = { - msie: t - , version: versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2) - }; + o[STR_MSIE] = TRUE; + o[STR_VERSION] = versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2); } else if (uaTest(/opera/i) || uaTest(/opr/i)) { - o = { - opera: t - , version: versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i) - }; + o[STR_OPERA] = TRUE; + o[STR_VERSION] = versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i); } else if (uaTest(/chrome/i)) { - o = { - webkit: t - , chrome: t - , version: versionTest(/chrome\/(\d+(\.\d+)?)/i) - }; + o[STR_CHROME] = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(/chrome\/(\d+(\.\d+)?)/i); } else if (uaTest(/phantom/i)) { - o = { - webkit: t - , phantom: t - , version: versionTest(/phantomjs\/(\d+(\.\d+)?)/i) - }; + o.phantom = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(/phantomjs\/(\d+(\.\d+)?)/i); } else if (uaTest(/touchpad/i)) { - o = { - webkit: t - , touchpad: t - , version : versionTest(/touchpad\/(\d+(\.\d+)?)/i) - }; + o.touchpad = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(/touchpad\/(\d+(\.\d+)?)/i); } else if (iphone || ipad) { - o = { - webkit: t - , mobile: t - , ios: t + o.ios = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; // WTF: version is not part of user agent in web apps - , version: versionTest(webkitVersion) - }; + o[STR_VERSION] = versionTest(webkitVersion); if (iphone) { - o.iphone = t; + o.iphone = TRUE; } else if (ipad) { - o.ipad = t; + o.ipad = TRUE; } } else if (uaTest(/android/i)) { - o = { - webkit: t - , android: t - , mobile: t - , version: versionTest(webkitVersion) || versionTest(firefoxVersion) - }; + o.android = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(webkitVersion) || versionTest(firefoxVersion); } else if (uaTest(/safari/i)) { - o = { - webkit: t - , safari: t - , version: versionTest(webkitVersion) - }; + o[STR_SAFARI] = o[STR_WEBKIT] = TRUE; + o[STR_VERSION] = versionTest(webkitVersion); } else if (uaTest(/gecko\//i)) { - o = { - gecko: t - , mozilla: t - , version: versionTest(firefoxVersion) - }; + o.gecko = o.mozilla = TRUE; + o[STR_VERSION] = versionTest(firefoxVersion); if (uaTest(/firefox/i)) { - o.firefox = t; + o.firefox = TRUE; } } else if (uaTest(/seamonkey\//i)) { - o = { - seamonkey: t - , version: versionTest(/seamonkey\/(\d+(\.\d+)?)/i) - }; + o.seamonkey = TRUE; + o[STR_VERSION] = versionTest(/seamonkey\/(\d+(\.\d+)?)/i); } else { - o = {}; + o[STR_VERSION] = 0; } + version = o[STR_VERSION]; + // Graded Browser Support // http://developer.yahoo.com/yui/articles/gbs - if ((o.msie && o.version >= 8) || - (o.chrome && o.version >= 10) || - (o.firefox && o.version >= 4.0) || - (o.safari && o.version >= 5) || - (o.opera && o.version >= 10.0)) { - o.a = t; + if ((o[STR_MSIE] && version >= 8) || + (o[STR_CHROME] && version >= 10) || + (o[STR_FIREFOX] && version >= 4.0) || + (o[STR_SAFARI] && version >= 5) || + (o[STR_OPERA] && version >= 10.0)) { + o.a = TRUE; } - else if ((o.msie && o.version < 8) || - (o.chrome && o.version < 10) || - (o.firefox && o.version < 4.0) || - (o.safari && o.version < 5) || - (o.opera && o.version < 10.0)) { - o.c = t; + else if ((o[STR_MSIE] && version < 8) || + (o[STR_CHROME] && version < 10) || + (o[STR_FIREFOX] && version < 4.0) || + (o[STR_SAFARI] && version < 5) || + (o[STR_OPERA] && version < 10.0)) { + o.c = TRUE; } else { - o.x = t; + o.x = TRUE; } return o;