shim.js 621 B

12345678910111213141516171819202122
  1. /* eslint no-bitwise: "off" */
  2. // Thanks: http://www.2ality.com/2014/01/efficient-string-repeat.html
  3. "use strict";
  4. var value = require("../../../object/valid-value")
  5. , toInteger = require("../../../number/to-integer");
  6. module.exports = function (count) {
  7. var str = String(value(this)), result;
  8. count = toInteger(count);
  9. if (count < 0) throw new RangeError("Count must be >= 0");
  10. if (!isFinite(count)) throw new RangeError("Count must be < ∞");
  11. if (!count) return "";
  12. if (count === 1) return str;
  13. result = "";
  14. if (count & 1) result += str;
  15. while ((count >>>= 1)) str += str;
  16. return result + str;
  17. };