string-repeat.js 612 B

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