bowser
Advanced tools
Comparing version 1.3.0 to 1.4.0
141
bowser.js
@@ -380,2 +380,3 @@ /*! | ||
(result.blackberry && result.version >= 10.1) | ||
|| (result.chromium && result.version >= 20) | ||
) { | ||
@@ -390,2 +391,3 @@ result.a = t; | ||
(result.ios && result.osversion && result.osversion.split(".")[0] < 6) | ||
|| (result.chromium && result.version < 20) | ||
) { | ||
@@ -412,2 +414,141 @@ result.c = t | ||
/** | ||
* Get version precisions count | ||
* | ||
* @example | ||
* getVersionPrecision("1.10.3") // 3 | ||
* | ||
* @param {string} version | ||
* @return {number} | ||
*/ | ||
function getVersionPrecision(version) { | ||
return version.split(".").length; | ||
} | ||
/** | ||
* Array::map polyfill | ||
* | ||
* @param {Array} arr | ||
* @param {Function} iterator | ||
* @return {Array} | ||
*/ | ||
function map(arr, iterator) { | ||
var result = [], i; | ||
if (Array.prototype.map) { | ||
return Array.prototype.map.call(arr, iterator); | ||
} | ||
for (i = 0; i < arr.length; i++) { | ||
result = iterator(arr[i]); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Calculate browser version weight | ||
* | ||
* @example | ||
* compareVersions(['1.10.2.1', '1.8.2.1.90']) // 1 | ||
* compareVersions(['1.010.2.1', '1.09.2.1.90']); // 1 | ||
* compareVersions(['1.10.2.1', '1.10.2.1']); // 0 | ||
* compareVersions(['1.10.2.1', '1.0800.2']); // -1 | ||
* | ||
* @param {Array<String>} versions versions to compare | ||
* @return {Number} comparison result | ||
*/ | ||
function compareVersions(versions) { | ||
// 1) get common precision for both versions, for example for "10.0" and "9" it should be 2 | ||
var precision = Math.max(getVersionPrecision(versions[0]), getVersionPrecision(versions[1])); | ||
var chunks = map(versions, function (version) { | ||
var delta = precision - getVersionPrecision(version); | ||
// 2) "9" -> "9.0" (for precision = 2) | ||
version = version + new Array(delta + 1).join(".0"); | ||
// 3) "9.0" -> ["000000000"", "000000009"] | ||
return map(version.split("."), function (chunk) { | ||
return new Array(20 - chunk.length).join("0") + chunk; | ||
}).reverse(); | ||
}); | ||
// iterate in reverse order by reversed chunks array | ||
while (--precision >= 0) { | ||
// 4) compare: "000000009" > "000000010" = false (but "9" > "10" = true) | ||
if (chunks[0][precision] > chunks[1][precision]) { | ||
return 1; | ||
} | ||
else if (chunks[0][precision] === chunks[1][precision]) { | ||
if (precision === 0) { | ||
// all version chunks are same | ||
return 0; | ||
} | ||
} | ||
else { | ||
return -1; | ||
} | ||
} | ||
} | ||
/** | ||
* Check if browser is unsupported | ||
* | ||
* @example | ||
* bowser.isUnsupportedBrowser({ | ||
* msie: "10", | ||
* firefox: "23", | ||
* chrome: "29", | ||
* safari: "5.1", | ||
* opera: "16", | ||
* phantom: "534" | ||
* }); | ||
* | ||
* @param {Object} minVersions map of minimal version to browser | ||
* @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map | ||
* @param {String} [ua] user agent string | ||
* @return {Boolean} | ||
*/ | ||
function isUnsupportedBrowser(minVersions, strictMode, ua) { | ||
var _bowser = bowser; | ||
// make strictMode param optional with ua param usage | ||
if (typeof strictMode === 'string') { | ||
ua = strictMode; | ||
strictMode = void(0); | ||
} | ||
if (strictMode === void(0)) { | ||
strictMode = false; | ||
} | ||
if (ua) { | ||
_bowser = detect(ua); | ||
} | ||
var version = "" + _bowser.version; | ||
for (var browser in minVersions) { | ||
if (minVersions.hasOwnProperty(browser)) { | ||
if (_bowser[browser]) { | ||
// browser version and min supported version. | ||
if (compareVersions([version, minVersions[browser]]) < 0) { | ||
return true; // unsupported | ||
} | ||
} | ||
} | ||
} | ||
return strictMode; // not found | ||
} | ||
/** | ||
* Check if browser is supported | ||
* | ||
* @param {Object} minVersions map of minimal version to browser | ||
* @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map | ||
* @return {Boolean} | ||
*/ | ||
function check(minVersions, strictMode) { | ||
return !isUnsupportedBrowser(minVersions, strictMode); | ||
} | ||
bowser.isUnsupportedBrowser = isUnsupportedBrowser; | ||
bowser.compareVersions = compareVersions; | ||
bowser.check = check; | ||
/* | ||
@@ -414,0 +555,0 @@ * Set our detect method to the main bowser object so we can |
@@ -6,2 +6,2 @@ /*! | ||
*/ | ||
!function(e,t){typeof module!="undefined"&&module.exports?module.exports=t():typeof define=="function"&&define.amd?define(t):this[e]=t()}("bowser",function(){function t(t){function n(e){var n=t.match(e);return n&&n.length>1&&n[1]||""}function r(e){var n=t.match(e);return n&&n.length>1&&n[2]||""}var i=n(/(ipod|iphone|ipad)/i).toLowerCase(),s=/like android/i.test(t),o=!s&&/android/i.test(t),u=/nexus\s*[0-6]\s*/i.test(t),a=!u&&/nexus\s*[0-9]+/i.test(t),f=/CrOS/.test(t),l=/silk/i.test(t),c=/sailfish/i.test(t),h=/tizen/i.test(t),p=/(web|hpw)os/i.test(t),d=/windows phone/i.test(t),v=!d&&/windows/i.test(t),m=!i&&!l&&/macintosh/i.test(t),g=!o&&!c&&!h&&!p&&/linux/i.test(t),y=n(/edge\/(\d+(\.\d+)?)/i),b=n(/version\/(\d+(\.\d+)?)/i),w=/tablet/i.test(t),E=!w&&/[^-]mobi/i.test(t),S=/xbox/i.test(t),x;/opera|opr|opios/i.test(t)?x={name:"Opera",opera:e,version:b||n(/(?:opera|opr|opios)[\s\/](\d+(\.\d+)?)/i)}:/coast/i.test(t)?x={name:"Opera Coast",coast:e,version:b||n(/(?:coast)[\s\/](\d+(\.\d+)?)/i)}:/yabrowser/i.test(t)?x={name:"Yandex Browser",yandexbrowser:e,version:b||n(/(?:yabrowser)[\s\/](\d+(\.\d+)?)/i)}:/ucbrowser/i.test(t)?x={name:"UC Browser",ucbrowser:e,version:n(/(?:ucbrowser)[\s\/](\d+(?:\.\d+)+)/i)}:/mxios/i.test(t)?x={name:"Maxthon",maxthon:e,version:n(/(?:mxios)[\s\/](\d+(?:\.\d+)+)/i)}:/epiphany/i.test(t)?x={name:"Epiphany",epiphany:e,version:n(/(?:epiphany)[\s\/](\d+(?:\.\d+)+)/i)}:/puffin/i.test(t)?x={name:"Puffin",puffin:e,version:n(/(?:puffin)[\s\/](\d+(?:\.\d+)?)/i)}:/sleipnir/i.test(t)?x={name:"Sleipnir",sleipnir:e,version:n(/(?:sleipnir)[\s\/](\d+(?:\.\d+)+)/i)}:/k-meleon/i.test(t)?x={name:"K-Meleon",kMeleon:e,version:n(/(?:k-meleon)[\s\/](\d+(?:\.\d+)+)/i)}:d?(x={name:"Windows Phone",windowsphone:e},y?(x.msedge=e,x.version=y):(x.msie=e,x.version=n(/iemobile\/(\d+(\.\d+)?)/i))):/msie|trident/i.test(t)?x={name:"Internet Explorer",msie:e,version:n(/(?:msie |rv:)(\d+(\.\d+)?)/i)}:f?x={name:"Chrome",chromeos:e,chromeBook:e,chrome:e,version:n(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)}:/chrome.+? edge/i.test(t)?x={name:"Microsoft Edge",msedge:e,version:y}:/vivaldi/i.test(t)?x={name:"Vivaldi",vivaldi:e,version:n(/vivaldi\/(\d+(\.\d+)?)/i)||b}:c?x={name:"Sailfish",sailfish:e,version:n(/sailfish\s?browser\/(\d+(\.\d+)?)/i)}:/seamonkey\//i.test(t)?x={name:"SeaMonkey",seamonkey:e,version:n(/seamonkey\/(\d+(\.\d+)?)/i)}:/firefox|iceweasel|fxios/i.test(t)?(x={name:"Firefox",firefox:e,version:n(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.\d+)?)/i)},/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(t)&&(x.firefoxos=e)):l?x={name:"Amazon Silk",silk:e,version:n(/silk\/(\d+(\.\d+)?)/i)}:/phantom/i.test(t)?x={name:"PhantomJS",phantom:e,version:n(/phantomjs\/(\d+(\.\d+)?)/i)}:/slimerjs/i.test(t)?x={name:"SlimerJS",slimer:e,version:n(/slimerjs\/(\d+(\.\d+)?)/i)}:/blackberry|\bbb\d+/i.test(t)||/rim\stablet/i.test(t)?x={name:"BlackBerry",blackberry:e,version:b||n(/blackberry[\d]+\/(\d+(\.\d+)?)/i)}:p?(x={name:"WebOS",webos:e,version:b||n(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i)},/touchpad\//i.test(t)&&(x.touchpad=e)):/bada/i.test(t)?x={name:"Bada",bada:e,version:n(/dolfin\/(\d+(\.\d+)?)/i)}:h?x={name:"Tizen",tizen:e,version:n(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i)||b}:/qupzilla/i.test(t)?x={name:"QupZilla",qupzilla:e,version:n(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i)||b}:/chromium/i.test(t)?x={name:"Chromium",chromium:e,version:n(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i)||b}:/chrome|crios|crmo/i.test(t)?x={name:"Chrome",chrome:e,version:n(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)}:o?x={name:"Android",version:b}:/safari|applewebkit/i.test(t)?(x={name:"Safari",safari:e},b&&(x.version=b)):i?(x={name:i=="iphone"?"iPhone":i=="ipad"?"iPad":"iPod"},b&&(x.version=b)):/googlebot/i.test(t)?x={name:"Googlebot",googlebot:e,version:n(/googlebot\/(\d+(\.\d+))/i)||b}:x={name:n(/^(.*)\/(.*) /),version:r(/^(.*)\/(.*) /)},!x.msedge&&/(apple)?webkit/i.test(t)?(/(apple)?webkit\/537\.36/i.test(t)?(x.name=x.name||"Blink",x.blink=e):(x.name=x.name||"Webkit",x.webkit=e),!x.version&&b&&(x.version=b)):!x.opera&&/gecko\//i.test(t)&&(x.name=x.name||"Gecko",x.gecko=e,x.version=x.version||n(/gecko\/(\d+(\.\d+)?)/i)),!x.msedge&&(o||x.silk)?x.android=e:i?(x[i]=e,x.ios=e):m?x.mac=e:S?x.xbox=e:v?x.windows=e:g&&(x.linux=e);var T="";x.windowsphone?T=n(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i):i?(T=n(/os (\d+([_\s]\d+)*) like mac os x/i),T=T.replace(/[_\s]/g,".")):o?T=n(/android[ \/-](\d+(\.\d+)*)/i):x.webos?T=n(/(?:web|hpw)os\/(\d+(\.\d+)*)/i):x.blackberry?T=n(/rim\stablet\sos\s(\d+(\.\d+)*)/i):x.bada?T=n(/bada\/(\d+(\.\d+)*)/i):x.tizen&&(T=n(/tizen[\/\s](\d+(\.\d+)*)/i)),T&&(x.osversion=T);var N=T.split(".")[0];if(w||a||i=="ipad"||o&&(N==3||N>=4&&!E)||x.silk)x.tablet=e;else if(E||i=="iphone"||i=="ipod"||o||u||x.blackberry||x.webos||x.bada)x.mobile=e;return x.msedge||x.msie&&x.version>=10||x.yandexbrowser&&x.version>=15||x.vivaldi&&x.version>=1||x.chrome&&x.version>=20||x.firefox&&x.version>=20||x.safari&&x.version>=6||x.opera&&x.version>=10||x.ios&&x.osversion&&x.osversion.split(".")[0]>=6||x.blackberry&&x.version>=10.1?x.a=e:x.msie&&x.version<10||x.chrome&&x.version<20||x.firefox&&x.version<20||x.safari&&x.version<6||x.opera&&x.version<10||x.ios&&x.osversion&&x.osversion.split(".")[0]<6?x.c=e:x.x=e,x}var e=!0,n=t(typeof navigator!="undefined"?navigator.userAgent:"");return n.test=function(e){for(var t=0;t<e.length;++t){var r=e[t];if(typeof r=="string"&&r in n)return!0}return!1},n._detect=t,n}) | ||
!function(e,t){typeof module!="undefined"&&module.exports?module.exports=t():typeof define=="function"&&define.amd?define(t):this[e]=t()}("bowser",function(){function t(t){function n(e){var n=t.match(e);return n&&n.length>1&&n[1]||""}function r(e){var n=t.match(e);return n&&n.length>1&&n[2]||""}var i=n(/(ipod|iphone|ipad)/i).toLowerCase(),s=/like android/i.test(t),o=!s&&/android/i.test(t),u=/nexus\s*[0-6]\s*/i.test(t),a=!u&&/nexus\s*[0-9]+/i.test(t),f=/CrOS/.test(t),l=/silk/i.test(t),c=/sailfish/i.test(t),h=/tizen/i.test(t),p=/(web|hpw)os/i.test(t),d=/windows phone/i.test(t),v=!d&&/windows/i.test(t),m=!i&&!l&&/macintosh/i.test(t),g=!o&&!c&&!h&&!p&&/linux/i.test(t),y=n(/edge\/(\d+(\.\d+)?)/i),b=n(/version\/(\d+(\.\d+)?)/i),w=/tablet/i.test(t),E=!w&&/[^-]mobi/i.test(t),S=/xbox/i.test(t),x;/opera|opr|opios/i.test(t)?x={name:"Opera",opera:e,version:b||n(/(?:opera|opr|opios)[\s\/](\d+(\.\d+)?)/i)}:/coast/i.test(t)?x={name:"Opera Coast",coast:e,version:b||n(/(?:coast)[\s\/](\d+(\.\d+)?)/i)}:/yabrowser/i.test(t)?x={name:"Yandex Browser",yandexbrowser:e,version:b||n(/(?:yabrowser)[\s\/](\d+(\.\d+)?)/i)}:/ucbrowser/i.test(t)?x={name:"UC Browser",ucbrowser:e,version:n(/(?:ucbrowser)[\s\/](\d+(?:\.\d+)+)/i)}:/mxios/i.test(t)?x={name:"Maxthon",maxthon:e,version:n(/(?:mxios)[\s\/](\d+(?:\.\d+)+)/i)}:/epiphany/i.test(t)?x={name:"Epiphany",epiphany:e,version:n(/(?:epiphany)[\s\/](\d+(?:\.\d+)+)/i)}:/puffin/i.test(t)?x={name:"Puffin",puffin:e,version:n(/(?:puffin)[\s\/](\d+(?:\.\d+)?)/i)}:/sleipnir/i.test(t)?x={name:"Sleipnir",sleipnir:e,version:n(/(?:sleipnir)[\s\/](\d+(?:\.\d+)+)/i)}:/k-meleon/i.test(t)?x={name:"K-Meleon",kMeleon:e,version:n(/(?:k-meleon)[\s\/](\d+(?:\.\d+)+)/i)}:d?(x={name:"Windows Phone",windowsphone:e},y?(x.msedge=e,x.version=y):(x.msie=e,x.version=n(/iemobile\/(\d+(\.\d+)?)/i))):/msie|trident/i.test(t)?x={name:"Internet Explorer",msie:e,version:n(/(?:msie |rv:)(\d+(\.\d+)?)/i)}:f?x={name:"Chrome",chromeos:e,chromeBook:e,chrome:e,version:n(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)}:/chrome.+? edge/i.test(t)?x={name:"Microsoft Edge",msedge:e,version:y}:/vivaldi/i.test(t)?x={name:"Vivaldi",vivaldi:e,version:n(/vivaldi\/(\d+(\.\d+)?)/i)||b}:c?x={name:"Sailfish",sailfish:e,version:n(/sailfish\s?browser\/(\d+(\.\d+)?)/i)}:/seamonkey\//i.test(t)?x={name:"SeaMonkey",seamonkey:e,version:n(/seamonkey\/(\d+(\.\d+)?)/i)}:/firefox|iceweasel|fxios/i.test(t)?(x={name:"Firefox",firefox:e,version:n(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.\d+)?)/i)},/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(t)&&(x.firefoxos=e)):l?x={name:"Amazon Silk",silk:e,version:n(/silk\/(\d+(\.\d+)?)/i)}:/phantom/i.test(t)?x={name:"PhantomJS",phantom:e,version:n(/phantomjs\/(\d+(\.\d+)?)/i)}:/slimerjs/i.test(t)?x={name:"SlimerJS",slimer:e,version:n(/slimerjs\/(\d+(\.\d+)?)/i)}:/blackberry|\bbb\d+/i.test(t)||/rim\stablet/i.test(t)?x={name:"BlackBerry",blackberry:e,version:b||n(/blackberry[\d]+\/(\d+(\.\d+)?)/i)}:p?(x={name:"WebOS",webos:e,version:b||n(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i)},/touchpad\//i.test(t)&&(x.touchpad=e)):/bada/i.test(t)?x={name:"Bada",bada:e,version:n(/dolfin\/(\d+(\.\d+)?)/i)}:h?x={name:"Tizen",tizen:e,version:n(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i)||b}:/qupzilla/i.test(t)?x={name:"QupZilla",qupzilla:e,version:n(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i)||b}:/chromium/i.test(t)?x={name:"Chromium",chromium:e,version:n(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i)||b}:/chrome|crios|crmo/i.test(t)?x={name:"Chrome",chrome:e,version:n(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)}:o?x={name:"Android",version:b}:/safari|applewebkit/i.test(t)?(x={name:"Safari",safari:e},b&&(x.version=b)):i?(x={name:i=="iphone"?"iPhone":i=="ipad"?"iPad":"iPod"},b&&(x.version=b)):/googlebot/i.test(t)?x={name:"Googlebot",googlebot:e,version:n(/googlebot\/(\d+(\.\d+))/i)||b}:x={name:n(/^(.*)\/(.*) /),version:r(/^(.*)\/(.*) /)},!x.msedge&&/(apple)?webkit/i.test(t)?(/(apple)?webkit\/537\.36/i.test(t)?(x.name=x.name||"Blink",x.blink=e):(x.name=x.name||"Webkit",x.webkit=e),!x.version&&b&&(x.version=b)):!x.opera&&/gecko\//i.test(t)&&(x.name=x.name||"Gecko",x.gecko=e,x.version=x.version||n(/gecko\/(\d+(\.\d+)?)/i)),!x.msedge&&(o||x.silk)?x.android=e:i?(x[i]=e,x.ios=e):m?x.mac=e:S?x.xbox=e:v?x.windows=e:g&&(x.linux=e);var T="";x.windowsphone?T=n(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i):i?(T=n(/os (\d+([_\s]\d+)*) like mac os x/i),T=T.replace(/[_\s]/g,".")):o?T=n(/android[ \/-](\d+(\.\d+)*)/i):x.webos?T=n(/(?:web|hpw)os\/(\d+(\.\d+)*)/i):x.blackberry?T=n(/rim\stablet\sos\s(\d+(\.\d+)*)/i):x.bada?T=n(/bada\/(\d+(\.\d+)*)/i):x.tizen&&(T=n(/tizen[\/\s](\d+(\.\d+)*)/i)),T&&(x.osversion=T);var N=T.split(".")[0];if(w||a||i=="ipad"||o&&(N==3||N>=4&&!E)||x.silk)x.tablet=e;else if(E||i=="iphone"||i=="ipod"||o||u||x.blackberry||x.webos||x.bada)x.mobile=e;return x.msedge||x.msie&&x.version>=10||x.yandexbrowser&&x.version>=15||x.vivaldi&&x.version>=1||x.chrome&&x.version>=20||x.firefox&&x.version>=20||x.safari&&x.version>=6||x.opera&&x.version>=10||x.ios&&x.osversion&&x.osversion.split(".")[0]>=6||x.blackberry&&x.version>=10.1||x.chromium&&x.version>=20?x.a=e:x.msie&&x.version<10||x.chrome&&x.version<20||x.firefox&&x.version<20||x.safari&&x.version<6||x.opera&&x.version<10||x.ios&&x.osversion&&x.osversion.split(".")[0]<6||x.chromium&&x.version<20?x.c=e:x.x=e,x}function r(e){return e.split(".").length}function i(e,t){var n=[],r;if(Array.prototype.map)return Array.prototype.map.call(e,t);for(r=0;r<e.length;r++)n=t(e[r]);return n}function s(e){var t=Math.max(r(e[0]),r(e[1])),n=i(e,function(e){var n=t-r(e);return e+=(new Array(n+1)).join(".0"),i(e.split("."),function(e){return(new Array(20-e.length)).join("0")+e}).reverse()});while(--t>=0){if(n[0][t]>n[1][t])return 1;if(n[0][t]!==n[1][t])return-1;if(t===0)return 0}}function o(e,r,i){var o=n;typeof r=="string"&&(i=r,r=void 0),r===void 0&&(r=!1),i&&(o=t(i));var u=""+o.version;for(var a in e)if(e.hasOwnProperty(a)&&o[a]&&s([u,e[a]])<0)return!0;return r}function u(e,t){return!o(e,t)}var e=!0,n=t(typeof navigator!="undefined"?navigator.userAgent:"");return n.test=function(e){for(var t=0;t<e.length;++t){var r=e[t];if(typeof r=="string"&&r in n)return!0}return!1},n.isUnsupportedBrowser=o,n.compareVersions=s,n.check=u,n._detect=t,n}) |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"homepage": "https://github.com/ded/bowser", | ||
@@ -31,5 +31,6 @@ "author": "Dustin Diaz <dustin@dustindiaz.com> (http://dustindiaz.com)", | ||
"scripts": { | ||
"test": "make test" | ||
"test": "make test", | ||
"prepublish": "make boosh" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -21,2 +21,40 @@ ## Bowser | ||
--- | ||
## API | ||
### bowser()`:Object` | ||
Use it to get object with detected flags of your current browser. | ||
### bowser._detect(ua `:String`)`:Object` | ||
Use it to get object with detected flags from User Agent string. | ||
### bowser.check(minVersions`:Object`, strictMode`:Boolean`)`:Boolean` | ||
Use it to check if browser supported. | ||
``` | ||
browser.check({msie: "11"}, window.navigator.userAgent); | ||
// true / false | ||
``` | ||
### bowser.compareVersions(versions`:Array<String>`)`:Number` | ||
Use it to compare two versions. | ||
``` | ||
browser.compareVersions(['9.0', '10']); | ||
// -1 | ||
``` | ||
### bowser.isUnsupportedBrowser(minVersions`:Object`, [strictMode]`:Boolean`, [ua]`:string`)`:Boolean` | ||
Use it to check if browser is unsupported. | ||
``` | ||
browser.isUnsupportedBrowser({msie: "10"}, window.navigator.userAgent); | ||
// true / false | ||
``` | ||
See more examples in [tests](test/test.js). | ||
--- | ||
## Bowser Flags | ||
@@ -23,0 +61,0 @@ Your mileage may vary, but these flags should be set. See Contributing below. |
@@ -380,2 +380,3 @@ /*! | ||
(result.blackberry && result.version >= 10.1) | ||
|| (result.chromium && result.version >= 20) | ||
) { | ||
@@ -390,2 +391,3 @@ result.a = t; | ||
(result.ios && result.osversion && result.osversion.split(".")[0] < 6) | ||
|| (result.chromium && result.version < 20) | ||
) { | ||
@@ -412,2 +414,141 @@ result.c = t | ||
/** | ||
* Get version precisions count | ||
* | ||
* @example | ||
* getVersionPrecision("1.10.3") // 3 | ||
* | ||
* @param {string} version | ||
* @return {number} | ||
*/ | ||
function getVersionPrecision(version) { | ||
return version.split(".").length; | ||
} | ||
/** | ||
* Array::map polyfill | ||
* | ||
* @param {Array} arr | ||
* @param {Function} iterator | ||
* @return {Array} | ||
*/ | ||
function map(arr, iterator) { | ||
var result = [], i; | ||
if (Array.prototype.map) { | ||
return Array.prototype.map.call(arr, iterator); | ||
} | ||
for (i = 0; i < arr.length; i++) { | ||
result = iterator(arr[i]); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Calculate browser version weight | ||
* | ||
* @example | ||
* compareVersions(['1.10.2.1', '1.8.2.1.90']) // 1 | ||
* compareVersions(['1.010.2.1', '1.09.2.1.90']); // 1 | ||
* compareVersions(['1.10.2.1', '1.10.2.1']); // 0 | ||
* compareVersions(['1.10.2.1', '1.0800.2']); // -1 | ||
* | ||
* @param {Array<String>} versions versions to compare | ||
* @return {Number} comparison result | ||
*/ | ||
function compareVersions(versions) { | ||
// 1) get common precision for both versions, for example for "10.0" and "9" it should be 2 | ||
var precision = Math.max(getVersionPrecision(versions[0]), getVersionPrecision(versions[1])); | ||
var chunks = map(versions, function (version) { | ||
var delta = precision - getVersionPrecision(version); | ||
// 2) "9" -> "9.0" (for precision = 2) | ||
version = version + new Array(delta + 1).join(".0"); | ||
// 3) "9.0" -> ["000000000"", "000000009"] | ||
return map(version.split("."), function (chunk) { | ||
return new Array(20 - chunk.length).join("0") + chunk; | ||
}).reverse(); | ||
}); | ||
// iterate in reverse order by reversed chunks array | ||
while (--precision >= 0) { | ||
// 4) compare: "000000009" > "000000010" = false (but "9" > "10" = true) | ||
if (chunks[0][precision] > chunks[1][precision]) { | ||
return 1; | ||
} | ||
else if (chunks[0][precision] === chunks[1][precision]) { | ||
if (precision === 0) { | ||
// all version chunks are same | ||
return 0; | ||
} | ||
} | ||
else { | ||
return -1; | ||
} | ||
} | ||
} | ||
/** | ||
* Check if browser is unsupported | ||
* | ||
* @example | ||
* bowser.isUnsupportedBrowser({ | ||
* msie: "10", | ||
* firefox: "23", | ||
* chrome: "29", | ||
* safari: "5.1", | ||
* opera: "16", | ||
* phantom: "534" | ||
* }); | ||
* | ||
* @param {Object} minVersions map of minimal version to browser | ||
* @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map | ||
* @param {String} [ua] user agent string | ||
* @return {Boolean} | ||
*/ | ||
function isUnsupportedBrowser(minVersions, strictMode, ua) { | ||
var _bowser = bowser; | ||
// make strictMode param optional with ua param usage | ||
if (typeof strictMode === 'string') { | ||
ua = strictMode; | ||
strictMode = void(0); | ||
} | ||
if (strictMode === void(0)) { | ||
strictMode = false; | ||
} | ||
if (ua) { | ||
_bowser = detect(ua); | ||
} | ||
var version = "" + _bowser.version; | ||
for (var browser in minVersions) { | ||
if (minVersions.hasOwnProperty(browser)) { | ||
if (_bowser[browser]) { | ||
// browser version and min supported version. | ||
if (compareVersions([version, minVersions[browser]]) < 0) { | ||
return true; // unsupported | ||
} | ||
} | ||
} | ||
} | ||
return strictMode; // not found | ||
} | ||
/** | ||
* Check if browser is supported | ||
* | ||
* @param {Object} minVersions map of minimal version to browser | ||
* @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map | ||
* @return {Boolean} | ||
*/ | ||
function check(minVersions, strictMode) { | ||
return !isUnsupportedBrowser(minVersions, strictMode); | ||
} | ||
bowser.isUnsupportedBrowser = isUnsupportedBrowser; | ||
bowser.compareVersions = compareVersions; | ||
bowser.check = check; | ||
/* | ||
@@ -414,0 +555,0 @@ * Set our detect method to the main bowser object so we can |
@@ -76,1 +76,56 @@ /** | ||
})(g, allUserAgents[g])} | ||
var comparisionsTasks = [ | ||
['9.0', '10', -1], | ||
['11', '10', 1], | ||
['1.10.2.1', '1.8.2.1.90', 1], | ||
['1.010.2.1', '1.08.2.1.90', 1], | ||
['1.10.2.1', '1.10.2.1', 0], | ||
['1.10.2.1', '1.0800.2', -1], | ||
['1.0.0-alpha', '1.0.0-alpha.1', -1], | ||
['1.0.0-alpha.1', '1.0.0-alpha.beta', -1], | ||
['1.0.0-alpha.beta', '1.0.0-beta', -1], | ||
['1.0.0-beta', '1.0.0-beta.2', -1], | ||
['1.0.0-beta.11', '1.0.0-rc.1', -1], | ||
['1.0.0-rc.1', '1.0.0', -1] | ||
]; | ||
describe('Browser versions comparision', function() { | ||
for(g in comparisionsTasks) { | ||
var task = comparisionsTasks[g], | ||
version = task[0], | ||
version2 = task[1], | ||
matching = task[2] === 0 ? ' == ' : (task[2] > 0) ? ' > ' : ' < '; | ||
it('version ' + version + ' should be' + matching + 'version ' + version2, function(){ | ||
assert.equal(browser.compareVersions([version, version2]), task[2]); | ||
}); | ||
} | ||
}); | ||
describe('Unsupported browser check', function() { | ||
before(function() { | ||
this.ie10_6 = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0"; | ||
}); | ||
it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified', function() { | ||
var unsupported = browser.isUnsupportedBrowser({msie: "10"}, this.ie10_6); | ||
assert.equal(unsupported, false); | ||
}); | ||
it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified', function() { | ||
var supported = browser.check({msie: "11"}, this.ie10_6); | ||
assert.equal(supported, false); | ||
}); | ||
it('should be passed by #check for IE10.6 when version was not specified', function() { | ||
var supported = browser.check({}, this.ie10_6); | ||
assert.equal(supported, true); | ||
}); | ||
it('should NOT be passed by #check for IE10.6 when version was not specified in strict mode', function() { | ||
var supported = browser.check({}, true, this.ie10_6); | ||
assert.equal(supported, false); | ||
}); | ||
}) |
Sorry, the diff of this file is not supported yet
85339
23
1246
192