diff --git a/README.md b/README.md index f7664e37..a7bdec1d 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/es6-shim.js b/es6-shim.js index 19bb7fa4..943a71f0 100644 --- a/es6-shim.js +++ b/es6-shim.js @@ -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); @@ -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. diff --git a/test/array.js b/test/array.js index fc57f0f1..b8c2a4ef 100644 --- a/test/array.js +++ b/test/array.js @@ -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]; @@ -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); + }); + }); }); };