Skip to content

Commit

Permalink
Use package dependencies for ES6 Array shims
Browse files Browse the repository at this point in the history
The shims that are vendored in to the library break in IE; also, no
reason to reinvent the wheel. Instead, use the `array.prototype.find`
and `array-includes` modules to ensure compatibility.
  • Loading branch information
Mat Brown committed Jan 31, 2017
1 parent f271228 commit dc61d57
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 50 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@
"webpack-stream": "^3.1.0",
"yargs": "^1.3.1"
},
"dependencies": {}
"dependencies": {
"array-includes": "^3.0.2",
"array.prototype.find": "^2.0.3"
}
}
54 changes: 5 additions & 49 deletions src/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,15 @@
Misc polyfills
*/
/*jshint -W121 */
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, "find", {
value: function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
import shimArrayFind from 'array.prototype.find/shim';
import shimArrayIncludes from 'array-includes/shim';

for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
}
});
if (!Array.prototype.find) {
shimArrayFind();
}

if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, "includes", {
value: function(searchElement) {
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
}
});
shimArrayIncludes();
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
Expand Down

0 comments on commit dc61d57

Please sign in to comment.