mirror of
https://github.com/lancedikson/bowser
synced 2024-10-27 20:34:22 +00:00
Merge pull request #383 from oliverfoster/issue/359
fixes #359 IE11 Added Array.prototype.find and Object.assign polyfills
This commit is contained in:
commit
d238da4906
@ -82,7 +82,7 @@ class Parser {
|
||||
parseBrowser() {
|
||||
this.parsedResult.browser = {};
|
||||
|
||||
const browserDescriptor = browserParsersList.find((_browser) => {
|
||||
const browserDescriptor = Utils.find(browserParsersList, (_browser) => {
|
||||
if (typeof _browser.test === 'function') {
|
||||
return _browser.test(this);
|
||||
}
|
||||
@ -165,7 +165,7 @@ class Parser {
|
||||
parseOS() {
|
||||
this.parsedResult.os = {};
|
||||
|
||||
const os = osParsersList.find((_os) => {
|
||||
const os = Utils.find(osParsersList, (_os) => {
|
||||
if (typeof _os.test === 'function') {
|
||||
return _os.test(this);
|
||||
}
|
||||
@ -241,7 +241,7 @@ class Parser {
|
||||
parsePlatform() {
|
||||
this.parsedResult.platform = {};
|
||||
|
||||
const platform = platformParsersList.find((_platform) => {
|
||||
const platform = Utils.find(platformParsersList, (_platform) => {
|
||||
if (typeof _platform.test === 'function') {
|
||||
return _platform.test(this);
|
||||
}
|
||||
@ -292,7 +292,7 @@ class Parser {
|
||||
parseEngine() {
|
||||
this.parsedResult.engine = {};
|
||||
|
||||
const engine = enginesParsersList.find((_engine) => {
|
||||
const engine = Utils.find(enginesParsersList, (_engine) => {
|
||||
if (typeof _engine.test === 'function') {
|
||||
return _engine.test(this);
|
||||
}
|
||||
@ -328,7 +328,7 @@ class Parser {
|
||||
* @return {ParsedResult}
|
||||
*/
|
||||
getResult() {
|
||||
return Object.assign({}, this.parsedResult);
|
||||
return Utils.assign({}, this.parsedResult);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -370,7 +370,7 @@ class Parser {
|
||||
|
||||
if (platformsAndOSCounter > 0) {
|
||||
const platformsAndOSNames = Object.keys(platformsAndOSes);
|
||||
const OSMatchingDefinition = platformsAndOSNames.find(name => (this.isOS(name)));
|
||||
const OSMatchingDefinition = Utils.find(platformsAndOSNames, name => (this.isOS(name)));
|
||||
|
||||
if (OSMatchingDefinition) {
|
||||
const osResult = this.satisfies(platformsAndOSes[OSMatchingDefinition]);
|
||||
@ -380,7 +380,10 @@ class Parser {
|
||||
}
|
||||
}
|
||||
|
||||
const platformMatchingDefinition = platformsAndOSNames.find(name => (this.isPlatform(name)));
|
||||
const platformMatchingDefinition = Utils.find(
|
||||
platformsAndOSNames,
|
||||
name => (this.isPlatform(name)),
|
||||
);
|
||||
if (platformMatchingDefinition) {
|
||||
const platformResult = this.satisfies(platformsAndOSes[platformMatchingDefinition]);
|
||||
|
||||
@ -392,7 +395,7 @@ class Parser {
|
||||
|
||||
if (browsersCounter > 0) {
|
||||
const browserNames = Object.keys(browsers);
|
||||
const matchingDefinition = browserNames.find(name => (this.isBrowser(name, true)));
|
||||
const matchingDefinition = Utils.find(browserNames, name => (this.isBrowser(name, true)));
|
||||
|
||||
if (matchingDefinition !== void 0) {
|
||||
return this.compareVersion(browsers[matchingDefinition]);
|
||||
|
56
src/utils.js
56
src/utils.js
@ -210,6 +210,8 @@ export default class Utils {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -231,6 +233,54 @@ export default class Utils {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array::find polyfill
|
||||
*
|
||||
* @param {Array} arr
|
||||
* @param {Function} predicate
|
||||
* @return {Array}
|
||||
*/
|
||||
static find(arr, predicate) {
|
||||
let i;
|
||||
let l;
|
||||
if (Array.prototype.find) {
|
||||
return Array.prototype.find.call(arr, predicate);
|
||||
}
|
||||
for (i = 0, l = arr.length; i < l; i += 1) {
|
||||
const value = arr[i];
|
||||
if (predicate(value, i)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Object::assign polyfill
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @param {Object} ...objs
|
||||
* @return {Object}
|
||||
*/
|
||||
static assign(obj, ...assigners) {
|
||||
const result = obj;
|
||||
let i;
|
||||
let l;
|
||||
if (Object.assign) {
|
||||
return Object.assign(obj, ...assigners);
|
||||
}
|
||||
for (i = 0, l = assigners.length; i < l; i += 1) {
|
||||
const assigner = assigners[i];
|
||||
if (typeof assigner === 'object' && assigner !== null) {
|
||||
const keys = Object.keys(assigner);
|
||||
keys.forEach((key) => {
|
||||
result[key] = assigner[key];
|
||||
});
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get short version/alias for a browser name
|
||||
*
|
||||
@ -250,10 +300,10 @@ export default class Utils {
|
||||
* @example
|
||||
* getBrowserAlias('edge') // Microsoft Edge
|
||||
*
|
||||
* @param {string} browserName
|
||||
* @param {string} browserAlias
|
||||
* @return {string}
|
||||
*/
|
||||
static getBrowserTypeByAlias(browserAlia) {
|
||||
return BROWSER_MAP[browserAlia] || '';
|
||||
static getBrowserTypeByAlias(browserAlias) {
|
||||
return BROWSER_MAP[browserAlias] || '';
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,18 @@
|
||||
import test from 'ava';
|
||||
import {
|
||||
getBrowserAlias,
|
||||
getFirstMatch,
|
||||
getSecondMatch,
|
||||
matchAndReturnConst,
|
||||
getWindowsVersionName,
|
||||
getMacOSVersionName,
|
||||
getAndroidVersionName,
|
||||
getVersionPrecision,
|
||||
compareVersions,
|
||||
map,
|
||||
find,
|
||||
assign,
|
||||
getBrowserAlias,
|
||||
getBrowserTypeByAlias
|
||||
} from '../../src/utils';
|
||||
|
||||
test('getFirstMatch', (t) => {
|
||||
@ -13,6 +20,16 @@ test('getFirstMatch', (t) => {
|
||||
t.is(matchedVersion, '11.11.11');
|
||||
});
|
||||
|
||||
test('getSecondMatch', (t) => {
|
||||
const matchedVersion = getSecondMatch(/version\/(\S+).*version\/(\S+)/i, 'Chrome Version/11.11.11 Chrome Version/22.22.22');
|
||||
t.is(matchedVersion, '22.22.22');
|
||||
});
|
||||
|
||||
test('matchAndReturnConst', (t) => {
|
||||
const _const = matchAndReturnConst(/version/i, 'version', "_const");
|
||||
t.is(_const, '_const');
|
||||
});
|
||||
|
||||
test('getWindowsVersionName', (t) => {
|
||||
t.is(getWindowsVersionName('NT 5.0'), '2000');
|
||||
t.is(getWindowsVersionName('XXX'), void 0);
|
||||
@ -32,6 +49,11 @@ test('getAndroidVersionName', (t) => {
|
||||
t.is(getAndroidVersionName('XXX'), void 0);
|
||||
});
|
||||
|
||||
test('getVersionPrecision', (t) => {
|
||||
const precision = getVersionPrecision("10.14.5");
|
||||
t.is(precision, 3);
|
||||
});
|
||||
|
||||
test('compareVersions', (t) => {
|
||||
const comparisionsTasks = [
|
||||
['9.0', '10', -1],
|
||||
@ -68,7 +90,51 @@ test('compareVersions', (t) => {
|
||||
});
|
||||
});
|
||||
|
||||
test('map', (t) => {
|
||||
const result = map([1,2], (value) => value+2);
|
||||
t.is(result[0], 3);
|
||||
t.is(result[1], 4);
|
||||
const original = Array.prototype.map;
|
||||
delete Array.prototype.map;
|
||||
const polyfillResult = map([1,2], (value) => value+2);
|
||||
Array.prototype.map = original;
|
||||
t.is(polyfillResult[0], 3);
|
||||
t.is(polyfillResult[1], 4);
|
||||
});
|
||||
|
||||
test('find', (t) => {
|
||||
const result = find([1,2], (value) => value==2);
|
||||
t.is(result, 2);
|
||||
const original = Array.prototype.find;
|
||||
delete Array.prototype.find;
|
||||
const polyfillResultFound = find([1,2], (value) => value==2);
|
||||
const polyfillResultNotFound = find([1,2], (value) => value==3);
|
||||
Array.prototype.find = original;
|
||||
t.is(polyfillResultFound, 2);
|
||||
t.is(polyfillResultNotFound, undefined);
|
||||
});
|
||||
|
||||
test('assign', (t) => {
|
||||
const result = assign({}, { a: 1 }, { b: 1 }, { b: 2, c: 3 });
|
||||
t.is(result['a'], 1);
|
||||
t.is(result['b'], 2);
|
||||
t.is(result['c'], 3);
|
||||
const original = Object.assign;
|
||||
delete Object.assign;
|
||||
const polyfillResult = assign({}, { a: 1 }, { b: 1 }, null, { b: 2, c: 3 });
|
||||
Object.assign = original;
|
||||
t.is(polyfillResult['a'], 1);
|
||||
t.is(polyfillResult['b'], 2);
|
||||
t.is(polyfillResult['c'], 3);
|
||||
});
|
||||
|
||||
test('getBrowserAlias', (t) => {
|
||||
t.is(getBrowserAlias('Microsoft Edge'), 'edge');
|
||||
t.is(getBrowserAlias('Unexisting Browser'), void 0);
|
||||
});
|
||||
|
||||
test('getBrowserTypeByAlias', (t) => {
|
||||
t.is(getBrowserTypeByAlias('edge'), 'Microsoft Edge');
|
||||
t.is(getBrowserTypeByAlias(void 0), '');
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user