Remove coercion method use.

This commit is contained in:
John-David Dalton
2017-03-13 20:49:45 -07:00
parent 2f281c68b0
commit bb7c959479
33 changed files with 59 additions and 194 deletions

View File

@@ -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