Skip to content

Commit

Permalink
Array.from: add (and fix) tests ensuring a falsy thisArg is accepted.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 6, 2014
1 parent 22e01c3 commit 14df918
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@
throw new TypeError('Array.from: when provided, the second argument must be a function');
}

var thisArg = arguments.length > 2 ? arguments[2] : undefined;
var hasThisArg = arguments.length > 2;
var thisArg = hasThisArg ? arguments[2] : undefined;

var usingIterator = ES.IsIterable(list);
// does the spec really mean that Arrays should use ArrayIterator?
Expand All @@ -595,7 +596,7 @@
value = list[i];
}
if (mapFn) {
result[i] = thisArg ? mapFn.call(thisArg, value, i) : mapFn(value, i);
result[i] = hasThisArg ? mapFn.call(thisArg, value, i) : mapFn(value, i);
} else {
result[i] = value;
}
Expand Down
7 changes: 7 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ var runArrayTests = function() {
expect(Object.prototype.toString.call(this)).to.equal('[object Number]');
}, 42);
});

it('accepts a falsy thisArg', function () {
Array.from([1, 2, 3], function (value, index) {
expect(this.valueOf()).to.equal(false);
expect(Object.prototype.toString.call(this)).to.equal('[object Boolean]');
}, false);
});
});

it('throws when provided a nonfunction second arg', function() {
Expand Down

0 comments on commit 14df918

Please sign in to comment.