Skip to content

Commit

Permalink
Moving the actual string repeat function out of the IIFE, and using a…
Browse files Browse the repository at this point in the history
… "string max length" var instead of directly comparing to Infinity.
  • Loading branch information
ljharb committed Jan 28, 2015
1 parent dbd7685 commit 2bb77d0
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,27 +532,26 @@
defineProperty(String, 'fromCodePoint', function fromCodePoint(codePoints) { return originalFromCodePoint(this, arguments); }, true);
}

var StringShims = {
// Fast repeat, uses the `Exponentiation by squaring` algorithm.
// Perf: http://jsperf.com/string-repeat2/2
repeat: (function () {
var repeat = function (s, times) {
if (times < 1) { return ''; }
if (times % 2) { return repeat(s, times - 1) + s; }
var half = repeat(s, times / 2);
return half + half;
};
// Fast repeat, uses the `Exponentiation by squaring` algorithm.
// Perf: http://jsperf.com/string-repeat2/2
var stringRepeat = function repeat(s, times) {
if (times < 1) { return ''; }
if (times % 2) { return repeat(s, times - 1) + s; }
var half = repeat(s, times / 2);
return half + half;
};
var stringMaxLength = Infinity;

return function (times) {
ES.RequireObjectCoercible(this);
var thisStr = String(this);
times = ES.ToInteger(times);
if (times < 0 || times === Infinity) {
throw new RangeError('repeat count must be less than infinity and not overflow maximum string size');
}
return repeat(thisStr, times);
};
}()),
var StringShims = {
repeat: function repeat(times) {
ES.RequireObjectCoercible(this);
var thisStr = String(this);
times = ES.ToInteger(times);
if (times < 0 || times >= stringMaxLength) {
throw new RangeError('repeat count must be less than infinity and not overflow maximum string size');
}
return stringRepeat(thisStr, times);
},

startsWith: function (searchStr) {
ES.RequireObjectCoercible(this);
Expand Down

0 comments on commit 2bb77d0

Please sign in to comment.