I was looking at
http://wiki.ecmascript.org/doku.php?id=strawman:string_dup and I
thought I would come up with an implementation that would be suitable.
Something that I think would be appropriate IMO.
String.prototype.repeat = function(times) {
// Let `str be the result of calling ToString passing the
`this value as the argument.
var str = ""+this;
// Let `amount be the result of calling ToNumber passing
`times as the argument.
amount = +times;
// If `amount is NaN, return `str.
if (isNaN(amount)) return str;
// If ToUint32(`amount) is not equal to `amount, throw a RangeError.
if (amount >>> 0 !== amount) throw new RangeError("invalid
repeat argument");
// If `amount is zero, return the empty String.
if (amount === 0) return "";
// Let `k be 1.
var k = 1;
// Let `R equal `str.
var R = str;
// Repeat, while `k is less than `amount.
while (k < amount) {
// Let R be a String value produced by concatenating R and str.
// (Using arrays might be faster, but it is written
like this for clarity)
R += str; // (Throw some internal error if `R is too large)
// Increase `k by 1.
k++;
}
// Return `R.
return R;
};
I might have changes to my idea here:
http://eboyjr.homelinux.org:8080/testing/js/?pp=repeat.js
--
Regards,
Devin Samarin <[email protected]>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss