shim.js 595 B

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