string-repeat.js 676 B

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