2018-09-09 12:08:48 +00:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< title > utils.js - Documentation< / title >
< script src = "scripts/prettify/prettify.js" > < / script >
< script src = "scripts/prettify/lang-css.js" > < / script >
<!-- [if lt IE 9]>
< script src = "//html5shiv.googlecode.com/svn/trunk/html5.js" > < / script >
<![endif]-->
< link type = "text/css" rel = "stylesheet" href = "styles/prettify.css" >
< link type = "text/css" rel = "stylesheet" href = "styles/jsdoc.css" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< / head >
< body >
< input type = "checkbox" id = "nav-trigger" class = "nav-trigger" / >
< label for = "nav-trigger" class = "navicon-button x" >
< div class = "navicon" > < / div >
< / label >
< label for = "nav-trigger" class = "overlay" > < / label >
< nav >
< h2 > < a href = "index.html" > Home< / a > < / h2 > < h3 > Classes< / h3 > < ul > < li > < a href = "Bowser.html" > Bowser< / a > < ul class = 'methods' > < li data-type = 'method' > < a href = "Bowser.html#.getParser" > getParser< / a > < / li > < li data-type = 'method' > < a href = "Bowser.html#.parse" > parse< / a > < / li > < / ul > < / li > < li > < a href = "Parser.html" > Parser< / a > < ul class = 'methods' > < li data-type = 'method' > < a href = "Parser.html#getBrowser" > getBrowser< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getBrowserName" > getBrowserName< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getBrowserVersion" > getBrowserVersion< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getEngine" > getEngine< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getOS" > getOS< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getOSName" > getOSName< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getOSVersion" > getOSVersion< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getPlatform" > getPlatform< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getPlatformType" > getPlatformType< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getResult" > getResult< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#getUA" > getUA< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#is" > is< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#parse" > parse< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#parseBrowser" > parseBrowser< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#parseEngine" > parseEngine< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#parseOS" > parseOS< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#parsePlatform" > parsePlatform< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#satisfies" > satisfies< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#some" > some< / a > < / li > < li data-type = 'method' > < a href = "Parser.html#test" > test< / a > < / li > < / ul > < / li > < / ul > < h3 > < a href = "global.html" > Global< / a > < / h3 >
< / nav >
< div id = "main" >
< h1 class = "page-title" > utils.js< / h1 >
< section >
< article >
2018-07-05 19:44:43 +00:00
< pre class = "prettyprint source linenums" > < code > class Utils {
/**
* Get first matched item for a string
* @param {RegExp} regexp
* @param {String} ua
* @return {Array|{index: number, input: string}|*|boolean|string}
*/
static getFirstMatch(regexp, ua) {
const match = ua.match(regexp);
return (match & & match.length > 0 & & match[1]) || '';
}
/**
* Get second matched item for a string
* @param regexp
* @param {String} ua
* @return {Array|{index: number, input: string}|*|boolean|string}
*/
static getSecondMatch(regexp, ua) {
const match = ua.match(regexp);
return (match & & match.length > 1 & & match[2]) || '';
}
/**
* Match a regexp and return a constant or undefined
* @param {RegExp} regexp
* @param {String} ua
* @param {*} _const Any const that will be returned if regexp matches the string
* @return {*}
*/
static matchAndReturnConst(regexp, ua, _const) {
if (regexp.test(ua)) {
return _const;
}
return void (0);
}
static getWindowsVersionName(version) {
switch (version) {
case 'NT': return 'NT';
case 'XP': return 'XP';
case 'NT 5.0': return '2000';
case 'NT 5.1': return 'XP';
case 'NT 5.2': return '2003';
case 'NT 6.0': return 'Vista';
case 'NT 6.1': return '7';
case 'NT 6.2': return '8';
case 'NT 6.3': return '8.1';
case 'NT 10.0': return '10';
default: return undefined;
}
}
/**
* Get version precisions count
*
* @example
* getVersionPrecision("1.10.3") // 3
*
* @param {string} version
* @return {number}
*/
static getVersionPrecision(version) {
return version.split('.').length;
}
/**
* Calculate browser version weight
*
* @example
2018-08-18 11:14:56 +00:00
* 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
* compareVersions('1.10.2.1', '1.10', true); // 0
2018-07-05 19:44:43 +00:00
*
* @param {String} versionA versions versions to compare
* @param {String} versionB versions versions to compare
2018-08-18 11:14:56 +00:00
* @param {boolean} [isLoose] enable loose comparison
2018-07-05 19:44:43 +00:00
* @return {Number} comparison result: -1 when versionA is lower,
* 1 when versionA is bigger, 0 when both equal
*/
2018-07-08 09:09:27 +00:00
/* eslint consistent-return: 1 */
2018-08-18 11:14:56 +00:00
static compareVersions(versionA, versionB, isLoose = false) {
2018-07-05 19:44:43 +00:00
// 1) get common precision for both versions, for example for "10.0" and "9" it should be 2
2018-08-18 11:14:56 +00:00
const versionAPrecision = Utils.getVersionPrecision(versionA);
const versionBPrecision = Utils.getVersionPrecision(versionB);
let precision = Math.max(versionAPrecision, versionBPrecision);
let lastPrecision = 0;
2018-07-05 19:44:43 +00:00
const chunks = Utils.map([versionA, versionB], (version) => {
const delta = precision - Utils.getVersionPrecision(version);
// 2) "9" -> "9.0" (for precision = 2)
const _version = version + new Array(delta + 1).join('.0');
// 3) "9.0" -> ["000000000"", "000000009"]
return Utils.map(_version.split('.'), chunk => new Array(20 - chunk.length).join('0') + chunk).reverse();
});
2018-08-18 11:14:56 +00:00
// adjust precision for loose comparison
if (isLoose) {
lastPrecision = precision - Math.min(versionAPrecision, versionBPrecision);
}
2018-07-05 19:44:43 +00:00
// iterate in reverse order by reversed chunks array
precision -= 1;
2018-08-18 11:14:56 +00:00
while (precision >= lastPrecision) {
2018-07-05 19:44:43 +00:00
// 4) compare: "000000009" > "000000010" = false (but "9" > "10" = true)
if (chunks[0][precision] > chunks[1][precision]) {
return 1;
2018-09-09 12:08:48 +00:00
}
if (chunks[0][precision] === chunks[1][precision]) {
2018-08-18 11:14:56 +00:00
if (precision === lastPrecision) {
2018-07-05 19:44:43 +00:00
// all version chunks are same
return 0;
}
2018-09-09 12:08:48 +00:00
precision -= 1;
} else if (chunks[0][precision] < chunks[1][precision]) {
2018-07-05 19:44:43 +00:00
return -1;
}
}
}
/**
* Array::map polyfill
*
* @param {Array} arr
* @param {Function} iterator
* @return {Array}
*/
static map(arr, iterator) {
const result = [];
let i;
if (Array.prototype.map) {
return Array.prototype.map.call(arr, iterator);
}
for (i = 0; i < arr.length; i += 1) {
result.push(iterator(arr[i]));
}
return result;
}
}
module.exports = Utils;
2018-09-09 12:08:48 +00:00
< / code > < / pre >
< / article >
< / section >
< / div >
< br class = "clear" >
< footer >
Documentation generated by < a href = "https://github.com/jsdoc3/jsdoc" > JSDoc 3.5.5< / a > on Sun Sep 09 2018 15:08:13 GMT+0300 (EEST) using the < a href = "https://github.com/clenemt/docdash" > docdash< / a > theme.
< / footer >
< script > prettyPrint ( ) ; < / script >
< script src = "scripts/linenumber.js" > < / script >
< / body >
< / html >