mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 08:07:50 +00:00
Bump to v4.7.0.
This commit is contained in:
28
repeat.js
28
repeat.js
@@ -1,16 +1,11 @@
|
||||
define(['./toInteger', './toString'], function(toInteger, toString) {
|
||||
|
||||
/** Used as references for various `Number` constants. */
|
||||
var MAX_SAFE_INTEGER = 9007199254740991;
|
||||
|
||||
/* Built-in method references for those with the same name as other `lodash` methods. */
|
||||
var nativeFloor = Math.floor;
|
||||
define(['./_baseRepeat', './toInteger', './toString'], function(baseRepeat, toInteger, toString) {
|
||||
|
||||
/**
|
||||
* Repeats the given string `n` times.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @since 3.0.0
|
||||
* @category String
|
||||
* @param {string} [string=''] The string to repeat.
|
||||
* @param {number} [n=0] The number of times to repeat the string.
|
||||
@@ -27,24 +22,7 @@ define(['./toInteger', './toString'], function(toInteger, toString) {
|
||||
* // => ''
|
||||
*/
|
||||
function repeat(string, n) {
|
||||
string = toString(string);
|
||||
n = toInteger(n);
|
||||
|
||||
var result = '';
|
||||
if (!string || n < 1 || n > MAX_SAFE_INTEGER) {
|
||||
return result;
|
||||
}
|
||||
// Leverage the exponentiation by squaring algorithm for a faster repeat.
|
||||
// See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
|
||||
do {
|
||||
if (n % 2) {
|
||||
result += string;
|
||||
}
|
||||
n = nativeFloor(n / 2);
|
||||
string += string;
|
||||
} while (n);
|
||||
|
||||
return result;
|
||||
return baseRepeat(toString(string), toInteger(n));
|
||||
}
|
||||
|
||||
return repeat;
|
||||
|
||||
Reference in New Issue
Block a user