1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00

Move user agent tests and version matches in callback to improve minification.

This commit is contained in:
Hannes Diercks 2013-09-13 14:25:31 +02:00
parent 3fe73237e2
commit 73ac715e1e
3 changed files with 91 additions and 53 deletions

View File

@ -29,19 +29,40 @@
*/
function detect(ua) {
/**
* Test a given regex against our user agent
*
* @param {RegExp} regex
* @return {Boolean}
*/
function uaTest(regex) {
return regex.test(ua);
}
/**
* Get version numbers from our user agent.
*
* @param {RegExp} regex
* @return {Number}
*/
function versionTest(regex, i) {
var match = ua.match(regex);
return match ? match[i || 1] : 0;
}
var t = true
, ie = /(msie|trident)/i.test(ua)
, chrome = /chrome/i.test(ua)
, phantom = /phantom/i.test(ua)
, safari = /safari/i.test(ua) && !chrome && !phantom
, iphone = /iphone/i.test(ua)
, ipad = /ipad/i.test(ua)
, touchpad = /touchpad/i.test(ua)
, android = /android/i.test(ua)
, opera = /opera/i.test(ua) || /opr/i.test(ua)
, firefox = /firefox/i.test(ua)
, gecko = /gecko\//i.test(ua)
, seamonkey = /seamonkey\//i.test(ua)
, ie = uaTest(/(msie|trident)/i)
, chrome = uaTest(/chrome/i)
, phantom = uaTest(/phantom/i)
, safari = uaTest(/safari/i) && !chrome && !phantom
, iphone = uaTest(/iphone/i)
, ipad = uaTest(/ipad/i)
, touchpad = uaTest(/touchpad/i)
, android = uaTest(/android/i)
, opera = uaTest(/opera/i) || uaTest(/opr/i)
, firefox = uaTest(/firefox/i)
, gecko = uaTest(/gecko\//i)
, seamonkey = uaTest(/seamonkey\//i)
, webkitVersion = /version\/(\d+(\.\d+)?)/i
, firefoxVersion = /firefox\/(\d+(\.\d+)?)/i
, o;
@ -49,30 +70,30 @@
if (ie) {
o = {
msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
, version: versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2)
};
} else if (opera) {
o = {
opera: t
, version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i)
, version: versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i)
};
} else if (chrome) {
o = {
webkit: t
, chrome: t
, version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1]
, version: versionTest(/chrome\/(\d+(\.\d+)?)/i)
};
} else if (phantom) {
o = {
webkit: t
, phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]
, version: versionTest(/phantomjs\/(\d+(\.\d+)+)/i)
};
} else if (touchpad) {
return {
o = {
webkit: t
, touchpad: t
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
, version : versionTest(/touchpad\/(\d+(\.\d+)?)/i)
};
} else if (iphone || ipad) {
o = {
@ -81,35 +102,33 @@
, ios: t
, iphone: iphone
, ipad: ipad
};
// WTF: version is not part of user agent in web apps
if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1];
}
, version: versionTest(webkitVersion)
};
} else if (android) {
o = {
webkit: t
, android: t
, mobile: t
, version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1]
, version: versionTest(webkitVersion) || versionTest(firefoxVersion)
};
} else if (safari) {
o = {
webkit: t
, safari: t
, version: ua.match(webkitVersion)[1]
, version: versionTest(webkitVersion)
};
} else if (gecko) {
o = {
gecko: t
, mozilla: t
, version: ua.match(firefoxVersion)[1]
, version: versionTest(firefoxVersion)
};
if (firefox) o.firefox = t;
} else if (seamonkey) {
o = {
seamonkey: t
, version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
, version: versionTest(/seamonkey\/(\d+(\.\d+)?)/i)
};
} else {
o = {};

2
bowser.min.js vendored
View File

@ -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){var t=!0,n=/(msie|trident)/i.test(e),r=/chrome/i.test(e),i=/phantom/i.test(e),s=/safari/i.test(e)&&!r&&!i,o=/iphone/i.test(e),u=/ipad/i.test(e),a=/touchpad/i.test(e),f=/android/i.test(e),l=/opera/i.test(e)||/opr/i.test(e),c=/firefox/i.test(e),h=/gecko\//i.test(e),p=/seamonkey\//i.test(e),d=/version\/(\d+(\.\d+)?)/i,v=/firefox\/(\d+(\.\d+)?)/i,m;if(n)m={msie:t,version:e.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]};else if(l)m={opera:t,version:e.match(d)?e.match(d)[1]:e.match(/opr\/(\d+(\.\d+)?)/i)};else if(r)m={webkit:t,chrome:t,version:e.match(/chrome\/(\d+(\.\d+)?)/i)[1]};else if(i)m={webkit:t,phantom:t,version:e.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]};else{if(a)return{webkit:t,touchpad:t,version:e.match(/touchpad\/(\d+(\.\d+)?)/i)[1]};o||u?(m={webkit:t,mobile:t,ios:t,iphone:o,ipad:u},d.test(e)&&(m.version=e.match(d)[1])):f?m={webkit:t,android:t,mobile:t,version:(e.match(d)||e.match(v))[1]}:s?m={webkit:t,safari:t,version:e.match(d)[1]}:h?(m={gecko:t,mozilla:t,version:e.match(v)[1]},c&&(m.firefox=t)):p?m={seamonkey:t,version:e.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]}:m={}}return m.msie&&m.version>=8||m.chrome&&m.version>=10||m.firefox&&m.version>=4||m.safari&&m.version>=5||m.opera&&m.version>=10?m.a=t:m.msie&&m.version<8||m.chrome&&m.version<10||m.firefox&&m.version<4||m.safari&&m.version<5||m.opera&&m.version<10?m.c=t:m.x=t,m}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 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(/(msie|trident)/i),s=t(/chrome/i),o=t(/phantom/i),u=t(/safari/i)&&!s&&!o,a=t(/iphone/i),f=t(/ipad/i),l=t(/touchpad/i),c=t(/android/i),h=t(/opera/i)||t(/opr/i),p=t(/firefox/i),d=t(/gecko\//i),v=t(/seamonkey\//i),m=/version\/(\d+(\.\d+)?)/i,g=/firefox\/(\d+(\.\d+)?)/i,y;return i?y={msie:r,version:n(/(msie |rv:)(\d+(\.\d+)?)/i,2)}:h?y={opera:r,version:n(m)||n(/opr\/(\d+(\.\d+)?)/i)}:s?y={webkit:r,chrome:r,version:n(/chrome\/(\d+(\.\d+)?)/i)}:o?y={webkit:r,phantom:r,version:n(/phantomjs\/(\d+(\.\d+)+)/i)}:l?y={webkit:r,touchpad:r,version:n(/touchpad\/(\d+(\.\d+)?)/i)}:a||f?y={webkit:r,mobile:r,ios:r,iphone:a,ipad:f,version:n(m)}:c?y={webkit:r,android:r,mobile:r,version:n(m)||n(g)}:u?y={webkit:r,safari:r,version:n(m)}:d?(y={gecko:r,mozilla:r,version:n(g)},p&&(y.firefox=r)):v?y={seamonkey:r,version:n(/seamonkey\/(\d+(\.\d+)?)/i)}:y={},y.msie&&y.version>=8||y.chrome&&y.version>=10||y.firefox&&y.version>=4||y.safari&&y.version>=5||y.opera&&y.version>=10?y.a=r:y.msie&&y.version<8||y.chrome&&y.version<10||y.firefox&&y.version<4||y.safari&&y.version<5||y.opera&&y.version<10?y.c=r:y.x=r,y}var t=e(typeof navigator!="undefined"?navigator.userAgent:"");return t._detect=e,t})

View File

@ -23,19 +23,40 @@
*/
function detect(ua) {
/**
* Test a given regex against our user agent
*
* @param {RegExp} regex
* @return {Boolean}
*/
function uaTest(regex) {
return regex.test(ua);
}
/**
* Get version numbers from our user agent.
*
* @param {RegExp} regex
* @return {Number}
*/
function versionTest(regex, i) {
var match = ua.match(regex);
return match ? match[i || 1] : 0;
}
var t = true
, ie = /(msie|trident)/i.test(ua)
, chrome = /chrome/i.test(ua)
, phantom = /phantom/i.test(ua)
, safari = /safari/i.test(ua) && !chrome && !phantom
, iphone = /iphone/i.test(ua)
, ipad = /ipad/i.test(ua)
, touchpad = /touchpad/i.test(ua)
, android = /android/i.test(ua)
, opera = /opera/i.test(ua) || /opr/i.test(ua)
, firefox = /firefox/i.test(ua)
, gecko = /gecko\//i.test(ua)
, seamonkey = /seamonkey\//i.test(ua)
, ie = uaTest(/(msie|trident)/i)
, chrome = uaTest(/chrome/i)
, phantom = uaTest(/phantom/i)
, safari = uaTest(/safari/i) && !chrome && !phantom
, iphone = uaTest(/iphone/i)
, ipad = uaTest(/ipad/i)
, touchpad = uaTest(/touchpad/i)
, android = uaTest(/android/i)
, opera = uaTest(/opera/i) || uaTest(/opr/i)
, firefox = uaTest(/firefox/i)
, gecko = uaTest(/gecko\//i)
, seamonkey = uaTest(/seamonkey\//i)
, webkitVersion = /version\/(\d+(\.\d+)?)/i
, firefoxVersion = /firefox\/(\d+(\.\d+)?)/i
, o;
@ -43,30 +64,30 @@
if (ie) {
o = {
msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
, version: versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2)
};
} else if (opera) {
o = {
opera: t
, version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i)
, version: versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i)
};
} else if (chrome) {
o = {
webkit: t
, chrome: t
, version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1]
, version: versionTest(/chrome\/(\d+(\.\d+)?)/i)
};
} else if (phantom) {
o = {
webkit: t
, phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]
, version: versionTest(/phantomjs\/(\d+(\.\d+)+)/i)
};
} else if (touchpad) {
return {
o = {
webkit: t
, touchpad: t
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
, version : versionTest(/touchpad\/(\d+(\.\d+)?)/i)
};
} else if (iphone || ipad) {
o = {
@ -75,35 +96,33 @@
, ios: t
, iphone: iphone
, ipad: ipad
};
// WTF: version is not part of user agent in web apps
if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1];
}
, version: versionTest(webkitVersion)
};
} else if (android) {
o = {
webkit: t
, android: t
, mobile: t
, version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1]
, version: versionTest(webkitVersion) || versionTest(firefoxVersion)
};
} else if (safari) {
o = {
webkit: t
, safari: t
, version: ua.match(webkitVersion)[1]
, version: versionTest(webkitVersion)
};
} else if (gecko) {
o = {
gecko: t
, mozilla: t
, version: ua.match(firefoxVersion)[1]
, version: versionTest(firefoxVersion)
};
if (firefox) o.firefox = t;
} else if (seamonkey) {
o = {
seamonkey: t
, version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
, version: versionTest(/seamonkey\/(\d+(\.\d+)?)/i)
};
} else {
o = {};