| 1 |
/** Used as references for various `Number` constants. */ |
| 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 |
|
| 7 |
/** |
| 8 |
* The base implementation of `_.repeat` which doesn't coerce arguments. |
| 9 |
* |
| 10 |
* @private |
| 11 |
* @param {string} string The string to repeat. |
| 12 |
* @param {number} n The number of times to repeat the string. |
| 13 |
* @returns {string} Returns the repeated string. |
| 14 |
*/ |
| 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; |