mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 08:37:49 +00:00
Remove coercion method use.
This commit is contained in:
23
repeat.js
23
repeat.js
@@ -1,7 +1,3 @@
|
||||
import baseRepeat from './.internal/baseRepeat.js'
|
||||
import toInteger from './toInteger.js'
|
||||
import toString from './toString.js'
|
||||
|
||||
/**
|
||||
* Repeats the given string `n` times.
|
||||
*
|
||||
@@ -22,8 +18,23 @@ import toString from './toString.js'
|
||||
* // => ''
|
||||
*/
|
||||
function repeat(string, n) {
|
||||
n = toInteger(n)
|
||||
return baseRepeat(toString(string), n)
|
||||
let 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)
|
||||
if (n) {
|
||||
string += string
|
||||
}
|
||||
} while (n)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export default repeat
|
||||
|
||||
Reference in New Issue
Block a user