UNPKG

681 BJavaScriptView Raw
1'use strict';
2var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
3var toString = require('../internals/to-string');
4var requireObjectCoercible = require('../internals/require-object-coercible');
5
6var $RangeError = RangeError;
7
8// `String.prototype.repeat` method implementation
9// https://tc39.es/ecma262/#sec-string.prototype.repeat
10module.exports = function repeat(count) {
11 var str = toString(requireObjectCoercible(this));
12 var result = '';
13 var n = toIntegerOrInfinity(count);
14 if (n < 0 || n === Infinity) throw new $RangeError('Wrong number of repetitions');
15 for (;n > 0; (n >>>= 1) && (str += str)) if (n & 1) result += str;
16 return result;
17};