Skip to content

Commit

Permalink
[New] Add Array#indexOf from post-ES6 errata.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 16, 2016
1 parent f2de7b2 commit 9f9ca09
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ In both browser and node you may also want to include `unorm`; see the [`String.
* `findIndex()` ([a standalone shim is also available](https://github.com/paulmillr/Array.prototype.findIndex))
* `keys()` (note: keys/values/entries return an `ArrayIterator` object)
* `values()`
* `indexOf()` (ES6 errata)
* `Object`:
* `assign()` ([a standalone shim is also available](https://github.com/ljharb/object.assign))
* `is()` ([a standalone shim is also available](https://github.com/ljharb/object-is))
Expand Down
13 changes: 13 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
var globals = getGlobal();
var globalIsFinite = globals.isFinite;
var _indexOf = Function.call.bind(String.prototype.indexOf);
var _arrayIndexOfApply = Function.apply.bind(Array.prototype.indexOf);
var _concat = Function.call.bind(Array.prototype.concat);
var _sort = Function.call.bind(Array.prototype.sort);
var _strSlice = Function.call.bind(String.prototype.slice);
Expand Down Expand Up @@ -1200,6 +1201,18 @@
}
defineProperties(Array.prototype, ArrayPrototypeShims);

if (1 / [true].indexOf(true, -0) < 0) {
// indexOf when given a position arg of -0 should return +0.
// https://github.com/tc39/ecma262/pull/316
defineProperty(Array.prototype, 'indexOf', function indexOf(searchElement) {
var value = _arrayIndexOfApply(this, arguments);
if (value === 0 && (1 / value) < 0) {
return 0;
}
return value;
}, 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
10 changes: 10 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ var runArrayTests = function (it) {
var ifSymbolUnscopablesIt = isSymbol(Sym.unscopables) ? it : xit;
var ifShimIt = (typeof process !== 'undefined' && process.env.NO_ES6_SHIM) ? it.skip : it;

var isNegativeZero = function (x) {
return (1 / x) < 0;
};

describe('Array', function () {
var list = [5, 10, 15, 20];

Expand Down Expand Up @@ -975,6 +979,12 @@ var runArrayTests = function (it) {
expect(reduced).to.equal(accumulator);
});
});

describe('#indexOf()', function () {
it('converts second argument from -0 to +0', function () {
expect(isNegativeZero([true].indexOf(true, -0))).to.equal(false);
});
});
});
};

Expand Down

0 comments on commit 9f9ca09

Please sign in to comment.