Proposal

Add a repeat method to String.prototype taking an integer argument and returning a string that is repeated according to the argument. So “*”.repeat(3) produces “***”.

Currently, the way this is commonly done is with new Array(4).join(’*’). There should be a better built-in alternative.

(function () {
    var ToInteger = Number.toInteger;
 
    Object.defineProperty(String.prototype, 'repeat', {
        value: function (count) {
            var string = '' + this;
            count = ToInteger(count);
            var result = '';
            while (--count >= 0) {
                result += string;
            }
            return result;
        }
        configurable: true,
        enumerable: false,
        writable: true
    });
}());

This assumes Number.toInteger.

 
harmony/string.prototype.repeat.txt · Last modified: 2011/04/28 17:11 by brendan
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki