mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 17:37:50 +00:00
Remove coercion method use.
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
/** Used as references for various `Number` constants. */
|
||||
const MAX_SAFE_INTEGER = 9007199254740991
|
||||
|
||||
/* Built-in method references for those with the same name as other `lodash` methods. */
|
||||
const nativeFloor = Math.floor
|
||||
|
||||
/**
|
||||
* The base implementation of `repeat` which doesn't coerce arguments.
|
||||
*
|
||||
* @private
|
||||
* @param {string} string The string to repeat.
|
||||
* @param {number} n The number of times to repeat the string.
|
||||
* @returns {string} Returns the repeated string.
|
||||
*/
|
||||
function baseRepeat(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 baseRepeat
|
||||
@@ -1,7 +1,3 @@
|
||||
import toInteger from '../toInteger.js'
|
||||
import toNumber from '../toNumber.js'
|
||||
import toString from '../toString.js'
|
||||
|
||||
/**
|
||||
* Creates a function like `round`.
|
||||
*
|
||||
@@ -12,15 +8,14 @@ import toString from '../toString.js'
|
||||
function createRound(methodName) {
|
||||
const func = Math[methodName]
|
||||
return (number, precision) => {
|
||||
number = toNumber(number)
|
||||
precision = precision == null ? 0 : Math.min(toInteger(precision), 292)
|
||||
precision = precision == null ? 0 : Math.min(precision, 292)
|
||||
if (precision) {
|
||||
// Shift with exponential notation to avoid floating-point issues.
|
||||
// See [MDN](https://mdn.io/round#Examples) for more details.
|
||||
let pair = `${ toString(number) }e`.split('e')
|
||||
let pair = `${ number }e`.split('e')
|
||||
const value = func(`${ pair[0] }e${ +pair[1] + precision }`)
|
||||
|
||||
pair = `${ toString(value) }e`.split('e')
|
||||
pair = `${ value }e`.split('e')
|
||||
return +`${ pair[0] }e${ +pair[1] - precision }`
|
||||
}
|
||||
return func(number)
|
||||
|
||||
Reference in New Issue
Block a user