`String#repeat` called on `undefined` or `null` should throw
Closed this issue · 2 comments
mathiasbynens commented
This implementation fails the following tests taken from https://github.com/mathiasbynens/String.prototype.repeat/blob/master/tests/tests.js:
assertThrows(function() { String.prototype.repeat.call(undefined); }, TypeError);
assertThrows(function() { String.prototype.repeat.call(undefined, 4); }, TypeError);
assertThrows(function() { String.prototype.repeat.call(null); }, TypeError);
assertThrows(function() { String.prototype.repeat.call(null, 4); }, TypeError);
assertThrows(function() { String.prototype.repeat.apply(undefined); }, TypeError);
assertThrows(function() { String.prototype.repeat.apply(undefined, [4]); }, TypeError);
assertThrows(function() { String.prototype.repeat.apply(null); }, TypeError);
assertThrows(function() { String.prototype.repeat.apply(null, [4]); }, TypeError);
paulmillr commented
we need ES.checkObjectCoercible
: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-checkobjectcoercible
mathiasbynens commented
function checkObjectCoercible(value) {
if (value == null) { // `null` or `undefined`
throw TypeError();
}
return value;
}