Skip to content

Commit

Permalink
Fix Firefox 25-31+ for Array#find and Array#findIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 3, 2014
1 parent f4c3c18 commit 3e0d222
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,21 @@
};
defineProperties(Array.prototype, arrayPrototypeShims);

var handlesSparseArrays = function (method) {
var count = 0;
method.call([1, ,], function (item) { count += 1; });
return count === 2;
};

// Firefox >= 25 (up to 31 at least) does not handle sparse arrays properly
// for Array#find and Array#findIndex
if (!handlesSparseArrays(Array.prototype.find)) {
defineProperty(Array.prototype, 'find', arrayPrototypeShims.find, true);
}
if (!handlesSparseArrays(Array.prototype.findIndex)) {
defineProperty(Array.prototype, 'findIndex', arrayPrototypeShims.findIndex, true);
}

addIterator(Array.prototype, function() { return this.values(); });
// Chrome defines keys/values/entries on Array, but doesn't give us
// any way to identify its iterator. So add our own shimmed field.
Expand Down

2 comments on commit 3e0d222

@cscott
Copy link
Collaborator

@cscott cscott commented on 3e0d222 Sep 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break on many browsers, because the Array#find implementations use ToLength, which uses methods on Number which haven't been defined yet. See adb8528.

@ljharb
Copy link
Collaborator Author

@ljharb ljharb commented on 3e0d222 Sep 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, thanks - fixed in cc7f9d6

Please sign in to comment.