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