shim.js 711 B

123456789101112131415161718192021222324
  1. // Thanks
  2. // @rauchma http://www.2ality.com/2014/01/efficient-string-repeat.html
  3. // @mathiasbynens https://github.com/mathiasbynens/String.prototype.repeat/blob/4a4b567def/repeat.js
  4. "use strict";
  5. var value = require("../../../object/valid-value")
  6. , toInteger = require("../../../number/to-integer");
  7. module.exports = function (count) {
  8. var str = String(value(this)), result;
  9. count = toInteger(count);
  10. if (count < 0) throw new RangeError("Count must be >= 0");
  11. if (!isFinite(count)) throw new RangeError("Count must be < ∞");
  12. result = "";
  13. while (count) {
  14. if (count % 2) result += str;
  15. if (count > 1) str += str;
  16. // eslint-disable-next-line no-bitwise
  17. count >>= 1;
  18. }
  19. return result;
  20. };