From 569af9b372bdea3f72df6953a99cec78db7ab5f6 Mon Sep 17 00:00:00 2001 From: Denis Demchenko Date: Tue, 24 May 2016 20:59:52 +0300 Subject: [PATCH 01/16] add issue temlate --- ISSUE_TEMPLATE.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ISSUE_TEMPLATE.md diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..165469f --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,5 @@ +Template to report about browser detection issue + +`window.navigator.userAgent` of the browser is: ... + And it's detected like a ... + But real name of the browser is ... From 6715bb2b08ae487eb703aac2a180b3d1b63feb47 Mon Sep 17 00:00:00 2001 From: kerbyfc Date: Thu, 16 Jun 2016 12:06:15 +0300 Subject: [PATCH 02/16] Implement isUnsupportedBrowser method See http://jsbin.com/vohahaciku/1/edit?js,console --- src/bowser.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/bowser.js b/src/bowser.js index d966269..2e88be1 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -406,6 +406,86 @@ } return false; } + + /** + * Get version precisions count + * @example + * getVersionPrecision("1.10.3") // 3 + * @param {string} version + * @return {number} + */ + function getVersionPrecision(version) { + return version.split(".").length; + } + + /** + * Calculate browser version weight + * @see http://jsbin.com/vohahaciku/1/edit?js,console + * + * @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 + */ + 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 = versions.map(function (version) { + var delta = precision - getVersionPrecision(version); + return (version + new Array(delta + 1).join(".0")) // 2) "9" -> "9.0" (for precision = 2) + .split(".") + .map(function (chunk) { return new Array(10 - chunk.length).join("0") + chunk; }) + .reverse(); + }); + // iterate in reverse order by reversed chunks array + while (--precision >= 0) { + // 5) 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.check({ + * msie: "10", + * firefox: "23", + * chrome: "29", + * safari: "5.1", + * opera: "16", + * phantom: "534" + * }); + */ + function isUnsupportedBrowser(minVersions, strictMode) { + if (strictMode === void 0) { strictMode = false; } + 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 true && !strictMode; // not found + } + + bowser.isUnsupportedBrowser = isUnsupportedBrowser; + bowser.compareVersions = compareVersions; /* * Set our detect method to the main bowser object so we can From 609f3d27bf3a5b0c0b0e1c1bdd34720e7e48174a Mon Sep 17 00:00:00 2001 From: kerbyfc Date: Thu, 16 Jun 2016 12:18:35 +0300 Subject: [PATCH 03/16] Add test for bowser.compareVersions --- test/test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test.js b/test/test.js index c11e035..c78dcb2 100644 --- a/test/test.js +++ b/test/test.js @@ -73,3 +73,25 @@ for (g in allUserAgents) { (function(group, userAgents) { }) })(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], +]; + +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]); + }) + } +}); From 57a45d1dee433ffbf775c2a53e9b200ec0a59e2c Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:04:01 +0300 Subject: [PATCH 04/16] fix comments --- src/bowser.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bowser.js b/src/bowser.js index 2e88be1..e338617 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -406,7 +406,7 @@ } return false; } - + /** * Get version precisions count * @example @@ -427,6 +427,9 @@ * 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} versions versions to compare + * @return {Number} comparision result */ function compareVersions(versions) { // 1) get common precision for both versions, for example for "10.0" and "9" it should be 2 @@ -467,6 +470,10 @@ * opera: "16", * phantom: "534" * }); + * + * @param {Object} minVersions minVersions map of minimal version to browser + * @param {Boolean} strictMode flag to return false if browser wasn't found in map + * @return {Boolean} */ function isUnsupportedBrowser(minVersions, strictMode) { if (strictMode === void 0) { strictMode = false; } From c9eed902e3b4b4142660b87648f62ae61761e05e Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:04:57 +0300 Subject: [PATCH 05/16] add check method --- src/bowser.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/bowser.js b/src/bowser.js index e338617..59f1666 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -461,8 +461,9 @@ /** * Check if browser is unsupported + * * @example - * bowser.check({ + * bowser.isUnsupportedBrowser({ * msie: "10", * firefox: "23", * chrome: "29", @@ -491,6 +492,17 @@ return true && !strictMode; // not found } + /** + * Check if browser is supported + * + * @param {Object} minVersions map of minimal version to browser + * @param {Boolean} strictMode 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; From ceb98fd099c858d885332a4034707d40be7a7e0c Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:20:30 +0300 Subject: [PATCH 06/16] add tests for alpha, beta, rc --- test/test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test.js b/test/test.js index c78dcb2..13336c8 100644 --- a/test/test.js +++ b/test/test.js @@ -81,6 +81,12 @@ var comparisionsTasks = [ ['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() { From b51a35e0065cfbe95596454125a84fed67e55af9 Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:38:25 +0300 Subject: [PATCH 07/16] add Array::map polyfill to compatible with es4 --- src/bowser.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/bowser.js b/src/bowser.js index 59f1666..81d1c6c 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -418,6 +418,23 @@ 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 * @see http://jsbin.com/vohahaciku/1/edit?js,console From c77cf0ae1b124b79de2059ac3b9781a67692283d Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:39:05 +0300 Subject: [PATCH 08/16] refactoring: use map instead of direct Array::map --- src/bowser.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bowser.js b/src/bowser.js index 81d1c6c..8d2dee4 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -451,16 +451,21 @@ 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 = versions.map(function (version) { - var delta = precision - getVersionPrecision(version); - return (version + new Array(delta + 1).join(".0")) // 2) "9" -> "9.0" (for precision = 2) - .split(".") - .map(function (chunk) { return new Array(10 - chunk.length).join("0") + chunk; }) - .reverse(); + 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) { - // 5) compare: "000000009" > "000000010" = false (but "9" > "10" = true) + // 4) compare: "000000009" > "000000010" = false (but "9" > "10" = true) if (chunks[0][precision] > chunks[1][precision]) { return 1; } From 24b91a4dab87ce031274a3bbead2d2a3ea27f26c Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:41:21 +0300 Subject: [PATCH 09/16] fix comments --- src/bowser.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bowser.js b/src/bowser.js index 8d2dee4..1bb343d 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -409,8 +409,10 @@ /** * Get version precisions count + * * @example * getVersionPrecision("1.10.3") // 3 + * * @param {string} version * @return {number} */ @@ -420,6 +422,7 @@ /** * Array::map polyfill + * * @param {Array} arr * @param {Function} iterator * @return {Array} @@ -457,7 +460,7 @@ // 2) "9" -> "9.0" (for precision = 2) version = version + new Array(delta + 1).join(".0"); - // 3) "9.0" -> [""000000000"", "000000009"] + // 3) "9.0" -> ["000000000"", "000000009"] return map(version.split("."), function (chunk) { return new Array(20 - chunk.length).join("0") + chunk; }).reverse(); From a5306640fa91411443760592a7d041791c5fd777 Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:41:53 +0300 Subject: [PATCH 10/16] small fixes --- test/test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 13336c8..e8cb04a 100644 --- a/test/test.js +++ b/test/test.js @@ -90,7 +90,6 @@ var comparisionsTasks = [ ]; describe('Browser versions comparision', function() { - for(g in comparisionsTasks) { var task = comparisionsTasks[g], version = task[0], @@ -98,6 +97,6 @@ describe('Browser versions comparision', function() { matching = task[2] === 0 ? ' == ' : (task[2] > 0) ? ' > ' : ' < '; it('version ' + version + ' should be' + matching + 'version ' + version2, function(){ assert.equal(browser.compareVersions([version, version2]), task[2]); - }) + }); } }); From ec7531e8cb31475e2f13be896138a20ab1d0d064 Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:43:48 +0300 Subject: [PATCH 11/16] remove @see jsbin comment --- src/bowser.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bowser.js b/src/bowser.js index 1bb343d..9c689bd 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -440,7 +440,6 @@ /** * Calculate browser version weight - * @see http://jsbin.com/vohahaciku/1/edit?js,console * * @example * compareVersions(['1.10.2.1', '1.8.2.1.90']) // 1 From 43ac868d84750c175a86e4c66362b89fe7d960b8 Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:45:24 +0300 Subject: [PATCH 12/16] export bowser.check --- src/bowser.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bowser.js b/src/bowser.js index 9c689bd..83e6396 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -529,6 +529,7 @@ bowser.isUnsupportedBrowser = isUnsupportedBrowser; bowser.compareVersions = compareVersions; + bowser.check = check; /* * Set our detect method to the main bowser object so we can From c30f6d41d98c987eaead133d1362be2c0dc29f3d Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 13:49:18 +0300 Subject: [PATCH 13/16] fix isUnsupportedBrowser documentation --- src/bowser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bowser.js b/src/bowser.js index 83e6396..ab558fc 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -496,7 +496,7 @@ * phantom: "534" * }); * - * @param {Object} minVersions minVersions map of minimal version to browser + * @param {Object} minVersions map of minimal version to browser * @param {Boolean} strictMode flag to return false if browser wasn't found in map * @return {Boolean} */ From 134aeede535187e83d614d6e409dad04e7b17c5c Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 16:01:45 +0300 Subject: [PATCH 14/16] add support for optional ua parameter --- src/bowser.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/bowser.js b/src/bowser.js index ab558fc..1f96a1d 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -497,15 +497,30 @@ * }); * * @param {Object} minVersions map of minimal version to browser - * @param {Boolean} strictMode flag to return false if browser wasn't found in map + * @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) { - if (strictMode === void 0) { strictMode = false; } - var version = "" + bowser.version; + 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]) { + if (_bowser[browser]) { // browser version and min supported version. if (compareVersions([version, minVersions[browser]]) < 0) { return true; // unsupported @@ -513,14 +528,14 @@ } } } - return true && !strictMode; // not found + return strictMode; // not found } /** * Check if browser is supported * * @param {Object} minVersions map of minimal version to browser - * @param {Boolean} strictMode flag to return false if browser wasn't found in map + * @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map * @return {Boolean} */ function check(minVersions, strictMode) { From 93c381f7e8ae270d3330f0444a288f337680251a Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 16:02:07 +0300 Subject: [PATCH 15/16] add tests for isUnsupportedBrowser & check methods --- test/test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/test.js b/test/test.js index e8cb04a..780e044 100644 --- a/test/test.js +++ b/test/test.js @@ -100,3 +100,31 @@ describe('Browser versions comparision', function() { }); } }); + +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 be NOT 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); + }); + +}) From c029b5113e491acbe43ff17a7d1bf16e03c22dde Mon Sep 17 00:00:00 2001 From: Alexander Lukin Date: Thu, 16 Jun 2016 16:04:18 +0300 Subject: [PATCH 16/16] fix test name --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 780e044..cce4198 100644 --- a/test/test.js +++ b/test/test.js @@ -122,7 +122,7 @@ describe('Unsupported browser check', function() { assert.equal(supported, true); }); - it('should be NOT passed by #check for IE10.6 when version was not specified in strict mode', function() { + 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); });