From 3fe73237e225a1c7d399097f036df96d3a3c121d Mon Sep 17 00:00:00 2001 From: Hannes Diercks Date: Fri, 13 Sep 2013 14:08:52 +0200 Subject: [PATCH] Make the detect function reusable. --- bowser.js | 108 ++++++++++++++++++++++++++------------------------ bowser.min.js | 2 +- src/bowser.js | 108 ++++++++++++++++++++++++++------------------------ 3 files changed, 115 insertions(+), 103 deletions(-) diff --git a/bowser.js b/bowser.js index 851a1a3..4e4e7b9 100644 --- a/bowser.js +++ b/bowser.js @@ -28,44 +28,42 @@ * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" */ - var ua = navigator.userAgent - , 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) - , webkitVersion = /version\/(\d+(\.\d+)?)/i - , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i - , o; - - function detect() { + function detect(ua) { + 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) + , webkitVersion = /version\/(\d+(\.\d+)?)/i + , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i + , o; if (ie) { - return { + o = { msie: t , version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2] }; } else if (opera) { - return { + o = { opera: t , version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i) }; } else if (chrome) { - return { + o = { webkit: t , chrome: t , version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1] }; } else if (phantom) { - return { + o = { webkit: t , phantom: t , version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1] @@ -88,16 +86,15 @@ if (webkitVersion.test(ua)) { o.version = ua.match(webkitVersion)[1]; } - return o; } else if (android) { - return { + o = { webkit: t , android: t , mobile: t , version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1] }; } else if (safari) { - return { + o = { webkit: t , safari: t , version: ua.match(webkitVersion)[1] @@ -109,38 +106,47 @@ , version: ua.match(firefoxVersion)[1] }; if (firefox) o.firefox = t; - return o; } else if (seamonkey) { - return { + o = { seamonkey: t , version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1] }; } else { - return {}; + o = {}; } + + // 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; + } + + 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 { + o.x = t; + } + + return o; } - var bowser = detect() - - // Graded Browser Support - // http://developer.yahoo.com/yui/articles/gbs - if ((bowser.msie && bowser.version >= 8) || - (bowser.chrome && bowser.version >= 10) || - (bowser.firefox && bowser.version >= 4.0) || - (bowser.safari && bowser.version >= 5) || - (bowser.opera && bowser.version >= 10.0)) { - bowser.a = t; - } - - else if ((bowser.msie && bowser.version < 8) || - (bowser.chrome && bowser.version < 10) || - (bowser.firefox && bowser.version < 4.0) || - (bowser.safari && bowser.version < 5) || - (bowser.opera && bowser.version < 10.0)) { - bowser.c = t; - } else { - bowser.x = t; - } + /* Get our main bowser object from navigators user agent if present. */ + var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent : ''); + + /* + * Set our detect method to the main bowser object so we can + * reuse it to test other user agents. + * This is needed to implement future tests. + */ + bowser._detect = detect; return bowser; -}) +}); diff --git a/bowser.min.js b/bowser.min.js index bceae58..63ab1c5 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 g(){return n?{msie:t,version:e.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]}:l?{opera:t,version:e.match(d)?e.match(d)[1]:e.match(/opr\/(\d+(\.\d+)?)/i)}:r?{webkit:t,chrome:t,version:e.match(/chrome\/(\d+(\.\d+)?)/i)[1]}:i?{webkit:t,phantom:t,version:e.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]}:a?{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]),m):f?{webkit:t,android:t,mobile:t,version:(e.match(d)||e.match(v))[1]}:s?{webkit:t,safari:t,version:e.match(d)[1]}:h?(m={gecko:t,mozilla:t,version:e.match(v)[1]},c&&(m.firefox=t),m):p?{seamonkey:t,version:e.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]}:{}}var e=navigator.userAgent,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,y=g();return 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=t: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=t:y.x=t,y}) \ 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 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}) \ No newline at end of file diff --git a/src/bowser.js b/src/bowser.js index 889be66..2b6ff82 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -22,44 +22,42 @@ * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" */ - var ua = navigator.userAgent - , 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) - , webkitVersion = /version\/(\d+(\.\d+)?)/i - , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i - , o; - - function detect() { + function detect(ua) { + 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) + , webkitVersion = /version\/(\d+(\.\d+)?)/i + , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i + , o; if (ie) { - return { + o = { msie: t , version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2] }; } else if (opera) { - return { + o = { opera: t , version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i) }; } else if (chrome) { - return { + o = { webkit: t , chrome: t , version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1] }; } else if (phantom) { - return { + o = { webkit: t , phantom: t , version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1] @@ -82,16 +80,15 @@ if (webkitVersion.test(ua)) { o.version = ua.match(webkitVersion)[1]; } - return o; } else if (android) { - return { + o = { webkit: t , android: t , mobile: t , version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1] }; } else if (safari) { - return { + o = { webkit: t , safari: t , version: ua.match(webkitVersion)[1] @@ -103,38 +100,47 @@ , version: ua.match(firefoxVersion)[1] }; if (firefox) o.firefox = t; - return o; } else if (seamonkey) { - return { + o = { seamonkey: t , version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1] }; } else { - return {}; + o = {}; } + + // 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; + } + + 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 { + o.x = t; + } + + return o; } - var bowser = detect() - - // Graded Browser Support - // http://developer.yahoo.com/yui/articles/gbs - if ((bowser.msie && bowser.version >= 8) || - (bowser.chrome && bowser.version >= 10) || - (bowser.firefox && bowser.version >= 4.0) || - (bowser.safari && bowser.version >= 5) || - (bowser.opera && bowser.version >= 10.0)) { - bowser.a = t; - } - - else if ((bowser.msie && bowser.version < 8) || - (bowser.chrome && bowser.version < 10) || - (bowser.firefox && bowser.version < 4.0) || - (bowser.safari && bowser.version < 5) || - (bowser.opera && bowser.version < 10.0)) { - bowser.c = t; - } else { - bowser.x = t; - } + /* Get our main bowser object from navigators user agent if present. */ + var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent : ''); + + /* + * Set our detect method to the main bowser object so we can + * reuse it to test other user agents. + * This is needed to implement future tests. + */ + bowser._detect = detect; return bowser; -}) +});