Skip to content

Commit

Permalink
fixes #359 IE11 Added Array.prototype.find and Object.assign polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster committed Dec 19, 2019
1 parent ea8d9c5 commit f5a9085
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ class Parser {
* @return {ParsedResult}
*/
getResult() {
return Object.assign({}, this.parsedResult);
return Utils.assign({}, this.parsedResult);
}

/**
Expand Down Expand Up @@ -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]);
Expand All @@ -380,7 +380,7 @@ 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]);

Expand All @@ -392,7 +392,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]);
Expand Down
38 changes: 38 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,44 @@ 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;
for (i = 0, l = arr.length; i < l; i++ ) {
const value = arr[i];
if (!predicate(value, i)) continue;
return value;
}
}

/**
* Object::assign polyfill
*
* @param {Object} obj
* @param {Object} ...objs
* @return {Object}
*/
static assign(obj) {
let i;
let l;
let k;
for (i = 1, l = arguments.length; i < l; i++) {
const assigner = arguments[i];
if (!(typeof assigner === "object")) continue;
for (k in assigner) {
obj[k] = assigner[k]
}
}
return obj;
}

/**
* Get short version/alias for a browser name
*
Expand Down

0 comments on commit f5a9085

Please sign in to comment.