Skip to content

Commit

Permalink
Ensure that wrapped ES5 Array.prototype methods return the proper value.
Browse files Browse the repository at this point in the history
Fixes #341.
  • Loading branch information
ljharb committed May 9, 2015
1 parent 9b5c93b commit 08818b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
14 changes: 7 additions & 7 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,43 +907,43 @@
if (!toLengthsCorrectly(Array.prototype.forEach)) {
var originalForEach = Array.prototype.forEach;
overrideNative(Array.prototype, 'forEach', function forEach(callbackFn) {
if (this.length >= 0) { return originalForEach.apply(this, arguments); }
return originalForEach.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.map)) {
var originalMap = Array.prototype.map;
overrideNative(Array.prototype, 'map', function map(callbackFn) {
if (this.length >= 0) { return originalMap.apply(this, arguments); }
return originalMap.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.filter)) {
var originalFilter = Array.prototype.filter;
overrideNative(Array.prototype, 'filter', function filter(callbackFn) {
if (this.length >= 0) { return originalFilter.apply(this, arguments); }
return originalFilter.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.some)) {
var originalSome = Array.prototype.some;
overrideNative(Array.prototype, 'some', function some(callbackFn) {
if (this.length >= 0) { return originalSome.apply(this, arguments); }
return originalSome.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.every)) {
var originalEvery = Array.prototype.every;
overrideNative(Array.prototype, 'every', function every(callbackFn) {
if (this.length >= 0) { return originalEvery.apply(this, arguments); }
return originalEvery.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.reduce)) {
var originalReduce = Array.prototype.reduce;
overrideNative(Array.prototype, 'reduce', function reduce(callbackFn) {
if (this.length >= 0) { return originalReduce.apply(this, arguments); }
return originalReduce.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.reduceRight, true)) {
var originalReduceRight = Array.prototype.reduceRight;
overrideNative(Array.prototype, 'reduceRight', function reduceRight(callbackFn) {
if (this.length >= 0) { return originalReduceRight.apply(this, arguments); }
return originalReduceRight.apply(this.length >= 0 ? this : [], arguments);
}, true);
}

Expand Down
26 changes: 20 additions & 6 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,41 +855,52 @@ var runArrayTests = function () {

describe('#map()', function () {
it('uses ToLength to clamp negative values to zero', function () {
var mapped;
expect(function () {
Array.prototype.map.call(negativeLength, throwRangeError);
mapped = Array.prototype.map.call(negativeLength, throwRangeError);
}).not.to['throw'](RangeError);
expect(mapped).to.eql([]);
});
});

describe('#filter()', function () {
it('uses ToLength to clamp negative values to zero', function () {
var filtered;
expect(function () {
Array.prototype.filter.call(negativeLength, throwRangeError);
filtered = Array.prototype.filter.call(negativeLength, throwRangeError);
}).not.to['throw'](RangeError);
expect(filtered).to.eql([]);
});
});

describe('#some()', function () {
it('uses ToLength to clamp negative values to zero', function () {
var result;
expect(function () {
Array.prototype.some.call(negativeLength, throwRangeError);
result = Array.prototype.some.call(negativeLength, throwRangeError);
}).not.to['throw'](RangeError);
expect(result).to.equal([].some(Object));
});
});

describe('#every()', function () {
it('uses ToLength to clamp negative values to zero', function () {
var result;
expect(function () {
Array.prototype.every.call(negativeLength, throwRangeError);
result = Array.prototype.every.call(negativeLength, throwRangeError);
}).not.to['throw'](RangeError);
expect(result).to.equal([].every(Object));
});
});

describe('#reduce()', function () {
it('uses ToLength to clamp negative values to zero', function () {
var accumulator = {};
var reduced;
expect(function () {
Array.prototype.reduce.call(negativeLength, throwRangeError, {});
reduced = Array.prototype.reduce.call(negativeLength, throwRangeError, accumulator);
}).not.to['throw'](RangeError);
expect(reduced).to.equal(accumulator);
});
});

Expand All @@ -898,9 +909,12 @@ var runArrayTests = function () {
var negativeOneToUint32minusOne = (-1 >>> 0) - 1;
var obj = { length: -1 };
obj[negativeOneToUint32minusOne] = true;
var accumulator = {};
var reduced;
expect(function () {
Array.prototype.reduceRight.call(obj, throwRangeError, {});
reduced = Array.prototype.reduceRight.call(obj, throwRangeError, accumulator);
}).not.to['throw'](RangeError);
expect(reduced).to.equal(accumulator);
});
});
});
Expand Down

0 comments on commit 08818b7

Please sign in to comment.