From 4d5db699bd1caf386995e5577e69bf99131a3251 Mon Sep 17 00:00:00 2001 From: Hannes Diercks Date: Fri, 13 Sep 2013 15:21:31 +0200 Subject: [PATCH] Move the versionNumber matcher to the matcher function to improve minification. --- bowser.js | 33 +++++++++++++++++---------------- bowser.min.js | 2 +- src/bowser.js | 33 +++++++++++++++++---------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/bowser.js b/bowser.js index 980781e..9edefc1 100644 --- a/bowser.js +++ b/bowser.js @@ -68,42 +68,43 @@ } /** - * Get version numbers from our user agent. + * Append version number matcher to our starting string + * and get version number from our user agent. * - * @param {RegExp} regex + * @param {String} start * @return {Number} */ - function versionTest(regex, i) { - var match = ua.match(regex); + function versionTest(start, i) { + var match = ua.match(new RegExp(start + '(\\d+(\\.\\d+)?)', 'i')); return match ? match[i || 1] : 0; } var iphone = uaTest(/iphone/i) , ipad = uaTest(/ipad/i) - , webkitVersion = /version\/(\d+(\.\d+)?)/i - , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i + , webkitVersion = versionTest(STR_VERSION + '\/') + , firefoxVersion = versionTest(STR_FIREFOX + '[\/ ]') , o = {} , version = 0; if (uaTest(/(msie|trident)/i)) { o[STR_MSIE] = TRUE; - o[STR_VERSION] = versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2); + o[STR_VERSION] = versionTest('(msie |rv:)', 2); } else if (uaTest(/opera/i) || uaTest(/opr/i)) { o[STR_OPERA] = TRUE; - o[STR_VERSION] = versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = webkitVersion || versionTest('opr\/'); } else if (uaTest(/chrome/i)) { o[STR_CHROME] = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(/chrome\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest(STR_CHROME + '\/'); } else if (uaTest(/phantom/i)) { o.phantom = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(/phantomjs\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest('phantomjs\/'); } else if (uaTest(/touchpad/i)) { o.touchpad = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(/touchpad\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest('touchpad\/'); } else if (iphone || ipad) { o.ios = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; // WTF: version is not part of user agent in web apps - o[STR_VERSION] = versionTest(webkitVersion); + o[STR_VERSION] = webkitVersion; if (iphone) { o.iphone = TRUE; } else if (ipad) { @@ -111,19 +112,19 @@ } } else if (uaTest(/android/i)) { o.android = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(webkitVersion) || versionTest(firefoxVersion); + o[STR_VERSION] = webkitVersion || firefoxVersion; } else if (uaTest(/safari/i)) { o[STR_SAFARI] = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(webkitVersion); + o[STR_VERSION] = webkitVersion; } else if (uaTest(/gecko\//i)) { o.gecko = o.mozilla = TRUE; - o[STR_VERSION] = versionTest(firefoxVersion); + o[STR_VERSION] = firefoxVersion; if (uaTest(/firefox/i)) { o.firefox = TRUE; } } else if (uaTest(/seamonkey\//i)) { o.seamonkey = TRUE; - o[STR_VERSION] = versionTest(/seamonkey\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest('seamonkey\/'); } else { o[STR_VERSION] = 0; } diff --git a/bowser.min.js b/bowser.min.js index 8546b85..a7f0b78 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 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 +!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(new RegExp(e+"(\\d+(\\.\\d+)?)","i"));return n?n[t||1]:0}var h=l(/iphone/i),p=l(/ipad/i),d=c(t+"/"),v=c(a+"[/ ]"),m={},g=0;return l(/(msie|trident)/i)?(m[r]=e,m[t]=c("(msie |rv:)",2)):l(/opera/i)||l(/opr/i)?(m[i]=e,m[t]=d||c("opr/")):l(/chrome/i)?(m[s]=m[o]=e,m[t]=c(s+"/")):l(/phantom/i)?(m.phantom=m[o]=e,m[t]=c("phantomjs/")):l(/touchpad/i)?(m.touchpad=m[o]=e,m[t]=c("touchpad/")):h||p?(m.ios=m[n]=m[o]=e,m[t]=d,h?m.iphone=e:p&&(m.ipad=e)):l(/android/i)?(m.android=m[n]=m[o]=e,m[t]=d||v):l(/safari/i)?(m[u]=m[o]=e,m[t]=d):l(/gecko\//i)?(m.gecko=m.mozilla=e,m[t]=v,l(/firefox/i)&&(m.firefox=e)):l(/seamonkey\//i)?(m.seamonkey=e,m[t]=c("seamonkey/")):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 8b1506e..721da88 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -62,42 +62,43 @@ } /** - * Get version numbers from our user agent. + * Append version number matcher to our starting string + * and get version number from our user agent. * - * @param {RegExp} regex + * @param {String} start * @return {Number} */ - function versionTest(regex, i) { - var match = ua.match(regex); + function versionTest(start, i) { + var match = ua.match(new RegExp(start + '(\\d+(\\.\\d+)?)', 'i')); return match ? match[i || 1] : 0; } var iphone = uaTest(/iphone/i) , ipad = uaTest(/ipad/i) - , webkitVersion = /version\/(\d+(\.\d+)?)/i - , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i + , webkitVersion = versionTest(STR_VERSION + '\/') + , firefoxVersion = versionTest(STR_FIREFOX + '[\/ ]') , o = {} , version = 0; if (uaTest(/(msie|trident)/i)) { o[STR_MSIE] = TRUE; - o[STR_VERSION] = versionTest(/(msie |rv:)(\d+(\.\d+)?)/i, 2); + o[STR_VERSION] = versionTest('(msie |rv:)', 2); } else if (uaTest(/opera/i) || uaTest(/opr/i)) { o[STR_OPERA] = TRUE; - o[STR_VERSION] = versionTest(webkitVersion) || versionTest(/opr\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = webkitVersion || versionTest('opr\/'); } else if (uaTest(/chrome/i)) { o[STR_CHROME] = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(/chrome\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest(STR_CHROME + '\/'); } else if (uaTest(/phantom/i)) { o.phantom = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(/phantomjs\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest('phantomjs\/'); } else if (uaTest(/touchpad/i)) { o.touchpad = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(/touchpad\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest('touchpad\/'); } else if (iphone || ipad) { o.ios = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; // WTF: version is not part of user agent in web apps - o[STR_VERSION] = versionTest(webkitVersion); + o[STR_VERSION] = webkitVersion; if (iphone) { o.iphone = TRUE; } else if (ipad) { @@ -105,19 +106,19 @@ } } else if (uaTest(/android/i)) { o.android = o[STR_MOBILE] = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(webkitVersion) || versionTest(firefoxVersion); + o[STR_VERSION] = webkitVersion || firefoxVersion; } else if (uaTest(/safari/i)) { o[STR_SAFARI] = o[STR_WEBKIT] = TRUE; - o[STR_VERSION] = versionTest(webkitVersion); + o[STR_VERSION] = webkitVersion; } else if (uaTest(/gecko\//i)) { o.gecko = o.mozilla = TRUE; - o[STR_VERSION] = versionTest(firefoxVersion); + o[STR_VERSION] = firefoxVersion; if (uaTest(/firefox/i)) { o.firefox = TRUE; } } else if (uaTest(/seamonkey\//i)) { o.seamonkey = TRUE; - o[STR_VERSION] = versionTest(/seamonkey\/(\d+(\.\d+)?)/i); + o[STR_VERSION] = versionTest('seamonkey\/'); } else { o[STR_VERSION] = 0; }