artdaq_node_server  v1_00_08
 All Classes Namespaces Files Variables Pages
_baseRepeat.js
1 
2 var MAX_SAFE_INTEGER = 9007199254740991;
3 
4 /* Built-in method references for those with the same name as other `lodash` methods. */
5 var nativeFloor = Math.floor;
6 
15 function baseRepeat(string, n) {
16  var result = '';
17  if (!string || n < 1 || n > MAX_SAFE_INTEGER) {
18  return result;
19  }
20  // Leverage the exponentiation by squaring algorithm for a faster repeat.
21  // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
22  do {
23  if (n % 2) {
24  result += string;
25  }
26  n = nativeFloor(n / 2);
27  if (n) {
28  string += string;
29  }
30  } while (n);
31 
32  return result;
33 }
34 
35 module.exports = baseRepeat;