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

View File

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

View File

@@ -1,5 +1,3 @@
import toInteger from './toInteger.js'
/**
* The opposite of `before`his method creates a function that invokes
* `func` once it's called `n` or more times.
@@ -21,7 +19,6 @@ function after(n, func) {
if (typeof func != 'function') {
throw new TypeError('Expected a function')
}
n = toInteger(n)
return function(...args) {
if (--n < 1) {
return func.apply(this, args)

View File

@@ -1,5 +1,3 @@
import toInteger from './toInteger.js'
/**
* Creates a function that invokes `func`, with the `this` binding and arguments
* of the created function, while it's called less than `n` times. Subsequent
@@ -20,7 +18,6 @@ function before(n, func) {
if (typeof func != 'function') {
throw new TypeError('Expected a function')
}
n = toInteger(n)
return function(...args) {
if (--n > 0) {
result = func.apply(this, args)

View File

@@ -1,9 +1,4 @@
import baseSlice from './.internal/baseSlice.js'
import toInteger from './toInteger.js'
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeCeil = Math.ceil
const nativeMax = Math.max
/**
* Creates an array of elements split into groups the length of `size`.
@@ -24,14 +19,14 @@ const nativeMax = Math.max
* // => [['a', 'b', 'c'], ['d']]
*/
function chunk(array, size) {
size = nativeMax(toInteger(size), 0)
size = Math.max(size, 0)
const length = array == null ? 0 : array.length
if (!length || size < 1) {
return []
}
let index = 0
let resIndex = 0
const result = new Array(nativeCeil(length / size))
const result = new Array(Math.ceil(length / size))
while (index < length) {
result[resIndex++] = baseSlice(array, index, (index += size))

View File

@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'
/**
* Clamps `number` within the inclusive `lower` and `upper` bounds.
*
@@ -18,10 +16,9 @@ import toNumber from './toNumber.js'
* // => 5
*/
function clamp(number, lower, upper) {
number = toNumber(number)
lower = toNumber(lower)
upper = toNumber(upper)
number = +number
lower = +lower
upper = +upper
lower = lower === lower ? lower : 0
upper = upper === upper ? upper : 0
if (number === number) {

View File

@@ -1,9 +1,4 @@
import isObject from './isObject.js'
import toNumber from './toNumber.js'
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeMax = Math.max
const nativeMin = Math.min
/**
* Creates a debounced function that delays invoking `func` until after `wait`
@@ -73,11 +68,11 @@ function debounce(func, wait, options) {
if (typeof func != 'function') {
throw new TypeError('Expected a function')
}
wait = toNumber(wait) || 0
wait = +wait || 0
if (isObject(options)) {
leading = !!options.leading
maxing = 'maxWait' in options
maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait
maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : maxWait
trailing = 'trailing' in options ? !!options.trailing : trailing
}
@@ -105,7 +100,7 @@ function debounce(func, wait, options) {
const timeSinceLastInvoke = time - lastInvokeTime
const result = wait - timeSinceLastCall
return maxing ? nativeMin(result, maxWait - timeSinceLastInvoke) : result
return maxing ? Math.min(result, maxWait - timeSinceLastInvoke) : result
}
function shouldInvoke(time) {

View File

@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'
/**
* Invokes `func` after `wait` milliseconds. Any additional arguments are
* provided to `func` when it's invoked.
@@ -19,7 +17,7 @@ function delay(func, wait, ...args) {
if (typeof func != 'function') {
throw new TypeError('Expected a function')
}
return setTimeout(func, toNumber(wait) || 0, ...args)
return setTimeout(func, +wait || 0, ...args)
}
export default delay

View File

@@ -1,5 +1,4 @@
import baseSlice from './.internal/baseSlice.js'
import toInteger from './toInteger.js'
/**
* Creates a slice of `array` with `n` elements dropped from the beginning.
@@ -25,11 +24,9 @@ import toInteger from './toInteger.js'
*/
function drop(array, n=1) {
const length = array == null ? 0 : array.length
if (!length) {
return []
}
n = toInteger(n)
return baseSlice(array, n < 0 ? 0 : n, length)
return length
? baseSlice(array, n < 0 ? 0 : n, length)
: []
}
export default drop

View File

@@ -1,5 +1,4 @@
import baseSlice from './.internal/baseSlice.js'
import toInteger from './toInteger.js'
/**
* Creates a slice of `array` with `n` elements dropped from the end.
@@ -28,7 +27,6 @@ function dropRight(array, n=1) {
if (!length) {
return []
}
n = toInteger(n)
n = length - n
return baseSlice(array, 0, n < 0 ? 0 : n)
}

View File

@@ -1,7 +1,3 @@
import baseToString from './.internal/baseToString.js'
import toInteger from './toInteger.js'
import toString from './toString.js'
/**
* Checks if `string` ends with the given target string.
*
@@ -24,13 +20,10 @@ import toString from './toString.js'
* endsWith('abc', 'b', 2)
* // => true
*/
function endsWith(string, target, position) {
string = toString(string)
target = baseToString(target)
function endsWith(string, target, position) {)
const { length } = string
position = position === undefined ? length : toInteger(position)
if (position < 0) {
position = position === undefined ? length : +position
if (position < 0 || position != position) {
position = 0
}
else if (position > length) {

View File

@@ -1,9 +1,4 @@
import baseFindIndex from './.internal/baseFindIndex.js'
import toInteger from './toInteger.js'
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeMax = Math.max
const nativeMin = Math.min
/**
* This method is like `findIndex` except that it iterates over elements
@@ -34,10 +29,9 @@ function findLastIndex(array, predicate, fromIndex) {
}
let index = length - 1
if (fromIndex !== undefined) {
index = toInteger(fromIndex)
index = fromIndex < 0
? nativeMax(length + index, 0)
: nativeMin(index, length - 1)
? Math.max(length + fromIndex, 0)
: Math.min(fromIndex, length - 1)
}
return baseFindIndex(array, predicate, index, true)
}

View File

@@ -1,6 +1,5 @@
import baseFlatten from './.internal/baseFlatten.js'
import map from './map.js'
import toInteger from './toInteger.js'
/**
* This method is like `flatMap` except that it recursively flattens the
@@ -23,7 +22,7 @@ import toInteger from './toInteger.js'
* // => [[1, 1], [2, 2]]
*/
function flatMapDepth(collection, iteratee, depth) {
depth = depth === undefined ? 1 : toInteger(depth)
depth = depth === undefined ? 1 : +depth
return baseFlatten(map(collection, iteratee), depth)
}

View File

@@ -1,5 +1,4 @@
import baseFlatten from './.internal/baseFlatten.js'
import toInteger from './toInteger.js'
/**
* Recursively flatten `array` up to `depth` times.
@@ -25,7 +24,7 @@ function flattenDepth(array, depth) {
if (!length) {
return []
}
depth = depth === undefined ? 1 : toInteger(depth)
depth = depth === undefined ? 1 : +depth
return baseFlatten(array, depth)
}

6
gt.js
View File

@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'
/**
* Checks if `value` is greater than `other`.
*
@@ -23,8 +21,8 @@ import toNumber from './toNumber.js'
*/
function gt(value, other) {
if (!(typeof value == 'string' && typeof other == 'string')) {
value = toNumber(value)
other = toNumber(other)
value = +value
other = +other
}
return value > other
}

6
gte.js
View File

@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'
/**
* Checks if `value` is greater than or equal to `other`.
*
@@ -23,8 +21,8 @@ import toNumber from './toNumber.js'
*/
function gte(value, other) {
if (!(typeof value == 'string' && typeof other == 'string')) {
value = toNumber(value)
other = toNumber(other)
value = +value
other = +other
}
return value >= other
}

View File

@@ -1,6 +1,4 @@
import baseInRange from './.internal/baseInRange.js'
import toFinite from './toFinite.js'
import toNumber from './toNumber.js'
/**
* Checks if `n` is between `start` and up to, but not including, `end`. If
@@ -39,15 +37,11 @@ import toNumber from './toNumber.js'
* // => true
*/
function inRange(number, start, end) {
start = toFinite(start)
if (end === undefined) {
end = start
start = 0
} else {
end = toFinite(end)
}
number = toNumber(number)
return baseInRange(number, start, end)
return baseInRange(+number, +start, +end)
}
export default inRange

View File

@@ -1,8 +1,4 @@
import baseIndexOf from './.internal/baseIndexOf.js'
import toInteger from './toInteger.js'
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeMax = Math.max
/**
* Gets the index at which the first occurrence of `value` is found in `array`
@@ -30,9 +26,9 @@ function indexOf(array, value, fromIndex) {
if (!length) {
return -1
}
let index = fromIndex == null ? 0 : toInteger(fromIndex)
let index = fromIndex == null ? 0 : +fromIndex
if (index < 0) {
index = nativeMax(length + index, 0)
index = Math.max(length + index, 0)
}
return baseIndexOf(array, value, index)
}

View File

@@ -1,11 +1,6 @@
import baseFindIndex from './.internal/baseFindIndex.js'
import baseIsNaN from './.internal/baseIsNaN.js'
import strictLastIndexOf from './.internal/strictLastIndexOf.js'
import toInteger from './toInteger.js'
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeMax = Math.max
const nativeMin = Math.min
/**
* This method is like `indexOf` except that it iterates over elements of
@@ -33,8 +28,7 @@ function lastIndexOf(array, value, fromIndex) {
}
let index = length
if (fromIndex !== undefined) {
index = toInteger(fromIndex)
index = index < 0 ? nativeMax(length + index, 0) : nativeMin(index, length - 1)
index = index < 0 ? Math.max(length + index, 0) : Math.min(index, length - 1)
}
return value === value
? strictLastIndexOf(array, value, index)

6
lt.js
View File

@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'
/**
* Checks if `value` is less than `other`.
*
@@ -23,8 +21,8 @@ import toNumber from './toNumber.js'
*/
function lt(value, other) {
if (!(typeof value == 'string' && typeof other == 'string')) {
value = toNumber(value)
other = toNumber(other)
value = +value
other = +other
}
return value < other
}

6
lte.js
View File

@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'
/**
* Checks if `value` is less than or equal to `other`.
*
@@ -23,8 +21,8 @@ import toNumber from './toNumber.js'
*/
function lte(value, other) {
if (!(typeof value == 'string' && typeof other == 'string')) {
value = toNumber(value)
other = toNumber(other)
value = +value
other = +other
}
return value <= other
}

2
nth.js
View File

@@ -1,5 +1,4 @@
import isIndex from './.internal/isIndex.js'
import toInteger from './toInteger.js'
/**
* Gets the element at index `n` of `array`. If `n` is negative, the nth
@@ -25,7 +24,6 @@ function nth(array, n) {
if (!length) {
return
}
n = toInteger(n)
n += n < 0 ? length : 0
return isIndex(n, length) ? array[n] : undefined
}

13
pad.js
View File

@@ -1,11 +1,5 @@
import createPadding from './.internal/createPadding.js'
import stringSize from './.internal/stringSize.js'
import toInteger from './toInteger.js'
import toString from './toString.js'
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeCeil = Math.ceil
const nativeFloor = Math.floor
/**
* Pads `string` on the left and right sides if it's shorter than `length`.
@@ -29,18 +23,15 @@ const nativeFloor = Math.floor
* // => 'abc'
*/
function pad(string, length, chars) {
string = toString(string)
length = toInteger(length)
const strLength = length ? stringSize(string) : 0
if (!length || strLength >= length) {
return string
}
const mid = (length - strLength) / 2
return (
createPadding(nativeFloor(mid), chars) +
createPadding(Math.floor(mid), chars) +
string +
createPadding(nativeCeil(mid), chars)
createPadding(Math.ceil(mid), chars)
)
}

View File

@@ -1,7 +1,5 @@
import createPadding from './.internal/createPadding.js'
import stringSize from './.internal/stringSize.js'
import toInteger from './toInteger.js'
import toString from './toString.js'
/**
* Pads `string` on the right side if it's shorter than `length`. Padding
@@ -25,9 +23,6 @@ import toString from './toString.js'
* // => 'abc'
*/
function padEnd(string, length, chars) {
string = toString(string)
length = toInteger(length)
const strLength = length ? stringSize(string) : 0
return (length && strLength < length)
? (string + createPadding(length - strLength, chars))

View File

@@ -1,7 +1,5 @@
import createPadding from './.internal/createPadding.js'
import stringSize from './.internal/stringSize.js'
import toInteger from './toInteger.js'
import toString from './toString.js'
/**
* Pads `string` on the left side if it's shorter than `length`. Padding
@@ -25,9 +23,6 @@ import toString from './toString.js'
* // => 'abc'
*/
function padStart(string, length, chars) {
string = toString(string)
length = toInteger(length)
const strLength = length ? stringSize(string) : 0
return (length && strLength < length)
? (createPadding(length - strLength, chars) + string)

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

View File

@@ -1,5 +1,4 @@
import copyArray from './.internal/copyArray.js'
import toInteger from './toInteger.js'
/**
* Gets `n` random elements at unique keys from `array` up to the
@@ -19,7 +18,6 @@ import toInteger from './toInteger.js'
* // => [2, 3, 1]
*/
function sampleSize(array, n) {
n = toInteger(n)
const length = array == null ? 0 : array.length
if (!length || n < 1) {
return []

View File

@@ -1,5 +1,4 @@
import baseSlice from './.internal/baseSlice.js'
import toInteger from './toInteger.js'
/**
* Creates a slice of `array` from `start` up to, but not including, `end`.
@@ -20,8 +19,8 @@ function slice(array, start, end) {
if (!length) {
return []
}
start = start == null ? 0 : toInteger(start)
end = end === undefined ? length : toInteger(end)
start = start == null ? 0 : start
end = end === undefined ? length : end
return baseSlice(array, start, end)
}

View File

@@ -1,7 +1,3 @@
import baseToString from './.internal/baseToString.js'
import toInteger from './toInteger.js'
import toString from './toString.js'
/**
* Checks if `string` starts with the given target string.
*
@@ -25,17 +21,15 @@ import toString from './toString.js'
* // => true
*/
function startsWith(string, target, position) {
string = toString(string)
const { length } = string
position = position == null ? 0 : toInteger(position)
position = position == null ? 0 : position
if (position < 0) {
position = 0
}
else if (position > length) {
position = length
}
target = baseToString(target)
target = (target + '')
return string.slice(position, position + target.length) == target
}

View File

@@ -1,5 +1,4 @@
import baseSlice from './.internal/baseSlice.js'
import toInteger from './toInteger.js'
/**
* Creates a slice of `array` with `n` elements taken from the beginning.
@@ -27,7 +26,6 @@ function take(array, n=1) {
if (!(array != null && array.length)) {
return []
}
n = toInteger(n)
return baseSlice(array, 0, n < 0 ? 0 : n)
}

View File

@@ -1,5 +1,4 @@
import baseSlice from './.internal/baseSlice.js'
import toInteger from './toInteger.js'
/**
* Creates a slice of `array` with `n` elements taken from the end.
@@ -28,7 +27,6 @@ function takeRight(array, n=1) {
if (!length) {
return []
}
n = toInteger(n)
n = length - n
return baseSlice(array, n < 0 ? 0 : n, length)
}

View File

@@ -1,5 +1,3 @@
import toInteger from './toInteger.js'
/** Used as references for various `Number` constants. */
const MAX_SAFE_INTEGER = 9007199254740991
@@ -24,7 +22,6 @@ const MAX_ARRAY_LENGTH = 4294967295
* // => [0, 0, 0, 0]
*/
function times(n, iteratee) {
n = toInteger(n)
if (n < 1 || n > MAX_SAFE_INTEGER) {
return []
}

View File

@@ -5,8 +5,6 @@ import isObject from './isObject.js'
import isRegExp from './isRegExp.js'
import stringSize from './.internal/stringSize.js'
import stringToArray from './.internal/stringToArray.js'
import toInteger from './toInteger.js'
import toString from './toString.js'
/** Used as default options for `truncate`. */
const DEFAULT_TRUNC_LENGTH = 30
@@ -58,11 +56,9 @@ function truncate(string, options) {
if (isObject(options)) {
separator = 'separator' in options ? options.separator : separator
length = 'length' in options ? toInteger(options.length) : length
length = 'length' in options ? options.length : length
omission = 'omission' in options ? baseToString(options.omission) : omission
}
string = toString(string)
let strSymbols
let strLength = string.length
if (hasUnicode(string)) {
@@ -93,7 +89,7 @@ function truncate(string, options) {
const substring = result
if (!separator.global) {
separator = RegExp(separator.source, `${ toString(reFlags.exec(separator)) }g`)
separator = RegExp(separator.source, `${ reFlags.exec(separator) || '' }g`)
}
separator.lastIndex = 0
while ((match = separator.exec(substring))) {