8000 Remove coercion method use. · lodash/lodash@bb7c959 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb7c959

Browse files
committed
Remove coercion method use.
1 parent 2f281c6 commit bb7c959

33 files changed

+59
-194
lines changed

.internal/baseRepeat.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

.internal/createRound.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import toInteger from '../toInteger.js'
2-
import toNumber from '../toNumber.js'
3-
import toString from '../toString.js'
4-
51
/**
62
* Creates a function like `round`.
73
*
@@ -12,15 +8,14 @@ import toString from '../toString.js'
128
function createRound(methodName) {
139
const func = Math[methodName]
1410
return (number, precision) => {
15-
number = toNumber(number)
16-
precision = precision == null ? 0 : Math.min(toInteger(precision), 292)
11+
precision = precision == null ? 0 : Math.min(precision, 292)
1712
if (precision) {
1813
// Shift with exponential notation to avoid floating-point issues.
1914
// See [MDN](https://mdn.io/round#Examples) for more details.
20-
let pair = `${ toString(number) }e`.split('e')
15+
let pair = `${ number }e`.split('e')
2116
const value = func(`${ pair[0] }e${ +pair[1] + precision }`)
2217

23-
pair = `${ toString(value) }e`.split('e')
18+
pair = `${ value }e`.split('e')
2419
return +`${ pair[0] }e${ +pair[1] - precision }`
2520
}
2621
return func(number)

after.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import toInteger from './toInteger.js'
2-
31
/**
42
* The opposite of `before`his method creates a function that invokes
53
* `func` once it's called `n` or more times.
@@ -21,7 +19,6 @@ function after(n, func) {
2119
if (typeof func != 'function') {
2220
throw new TypeError('Expected a function')
2321
}
24-
n = toInteger(n)
2522
return function(...args) {
2623
if (--n < 1) {
2724
return func.apply(this, args)

before.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import toInteger from './toInteger.js'
2-
31
/**
42
* Creates a function that invokes `func`, with the `this` binding and arguments
53
* of the created function, while it's called less than `n` times. Subsequent
@@ -20,7 +18,6 @@ function before(n, func) {
2018
if (typeof func != 'function') {
2119
throw new TypeError('Expected a function')
2220
}
23-
n = toInteger(n)
2421
return function(...args) {
2522
if (--n > 0) {
2623
result = func.apply(this, args)

chunk.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
import baseSlice from './.internal/baseSlice.js'
2-
import toInteger from './toInteger.js'
3-
4-
/* Built-in method references for those with the same name as other `lodash` methods. */
5-
const nativeCeil = Math.ceil
6-
const nativeMax = Math.max
72

83
/**
94
* Creates an array of elements split into groups the length of `size`.
@@ -24,14 +19,14 @@ const nativeMax = Math.max
2419
* // => [['a', 'b', 'c'], ['d']]
2520
*/
2621
function chunk(array, size) {
27-
size = nativeMax(toInteger(size), 0)
22+
size = Math.max(size, 0)
2823
const length = array == null ? 0 : array.length
2924
if (!length || size < 1) {
3025
return []
3126
}
3227
let index = 0
3328
let resIndex = 0
34-
const result = new Array(nativeCeil(length / size))
29+
const result = new Array(Math.ceil(length / size))
3530

3631
while (index < length) {
3732
result[resIndex++] = baseSlice(array, index, (index += size))

clamp.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import toNumber from './toNumber.js'
2-
31
/**
42
* Clamps `number` within the inclusive `lower` and `upper` bounds.
53
*
@@ -18,10 +16,9 @@ import toNumber from './toNumber.js'
1816
* // => 5
1917
*/
2018
function clamp(number, lower, upper) {
21-
number = toNumber(number)
22-
lower = toNumber(lower)
23-
upper = toNumber(upper)
24-
19+
number = +number
20+
lower = +lower
21+
upper = +upper
2522
lower = lower === lower ? lower : 0
2623
upper = upper === upper ? upper : 0
2724
if (number === number) {

debounce.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
import isObject from './isObject.js'
2-
import toNumber from './toNumber.js'
3-
4-
/* Built-in method references for those with the same name as other `lodash` methods. */
5-
const nativeMax = Math.max
6-
const nativeMin = Math.min
72

83
/**
94
* Creates a debounced function that delays invoking `func` until after `wait`
@@ -73,11 +68,11 @@ function debounce(func, wait, options) {
7368
if (typeof func != 'function') {
7469
throw new TypeError('Expected a function')
7570
}
76-
wait = toNumber(wait) || 0
71+
wait = +wait || 0
7772
if (isObject(options)) {
7873
leading = !!options.leading
7974
maxing = 'maxWait' in options
80-
maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait
75+
maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : maxWait
8176
trailing = 'trailing' in options ? !!options.trailing : trailing
8277
}
8378

@@ -105,7 +100,7 @@ function debounce(func, wait, options) {
105100
const timeSinceLastInvoke = time - lastInvokeTime
106101
const result = wait - timeSinceLastCall
107102

108-
return maxing ? nativeMin(result, maxWait - timeSinceLastInvoke) : result
103+
return maxing ? Math.min(result, maxWait - timeSinceLastInvoke) : result
109104
}
110105

111106
function shouldInvoke(time) {

delay.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import toNumber from './toNumber.js'
2-
31
/**
42
* Invokes `func` after `wait` milliseconds. Any additional arguments are
53
* provided to `func` when it's invoked.
@@ -19,7 +17,7 @@ function delay(func, wait, ...args) {
1917
if (typeof func != 'function') {
2018
throw new TypeError(' F438 ;Expected a function')
2119
}
22-
return setTimeout(func, toNumber(wait) || 0, ...args)
20+
return setTimeout(func, +wait || 0, ...args)
2321
}
2422

2523
export default delay

drop.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import baseSlice from './.internal/baseSlice.js'
2-
import toInteger from './toInteger.js'
32

43
/**
54
* Creates a slice of `array` with `n` elements dropped from the beginning.
@@ -25,11 +24,9 @@ import toInteger from './toInteger.js'
2524
*/
2625
function drop(array, n=1) {
2726
const length = array == null ? 0 : array.length
28-
if (!length) {
29-
return []
30-
}
31-
n = toInteger(n)
32-
return baseSlice(array, n < 0 ? 0 : n, length)
27+
return length
28+
? baseSlice(array, n < 0 ? 0 : n, length)
29+
: []
3330
}
3431

3532
export default drop

dropRight.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import baseSlice from './.internal/baseSlice.js'
2-
import toInteger from './toInteger.js'
32

43
/**
54
* Creates a slice of `array` with `n` elements dropped from the end.
@@ -28,7 +27,6 @@ function dropRight(array, n=1) {
2827
if (!length) {
2928
return []
3029
}
31-
n = toInteger(n)
3230
n = length - n
3331
return baseSlice(array, 0, n < 0 ? 0 : n)
3432
}

endsWith.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import baseToString from './.internal/baseToString.js'
2-
import toInteger from './toInteger.js'
3-
import toString from './toString.js'
4-
51
/**
62
* Checks if `string` ends with the given target string.
73
*
@@ -24,13 +20,10 @@ import toString from './toString.js'
2420
* endsWith('abc', 'b', 2)
2521
* // => true
2622
*/
27-
function endsWith(string, target, position) {
28-
string = toString(string)
29-
target = baseToString(target)
30-
23+
function endsWith(string, target, position) {)
3124
const { length } = string
32-
position = position === undefined ? length : toInteger(position)
33-
if (position < 0) {
25+
position = position === undefined ? length : +position
26+
if (position < 0 || position != position) {
3427
position = 0
3528
}
3629
else if (position > length) {

findLastIndex.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
import baseFindIndex from './.internal/baseFindIndex.js'
2-
import toInteger from './toInteger.js'
3-
4-
/* Built-in method ref 10000 erences for those with the same name as other `lodash` methods. */
5-
const nativeMax = Math.max
6-
const nativeMin = Math.min
72

83
/**
94
* This method is like `findIndex` except that it iterates over elements
@@ -34,10 +29,9 @@ function findLastIndex(array, predicate, fromIndex) {
3429
}
3530
let index = length - 1
3631
if (fromIndex !== undefined) {
37-
index = toInteger(fromIndex)
3832
index = fromIndex < 0
39-
? nativeMax(length + index, 0)
40-
: nativeMin(index, length - 1)
33+
? Math.max(length + fromIndex, 0)
34+
: Math.min(fromIndex, length - 1)
4135
}
4236
return baseFindIndex(array, predicate, index, true)
4337
}

flatMapDepth.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import baseFlatten from './.internal/baseFlatten.js'
22
import map from './map.js'
3-
import toInteger from './toInteger.js'
43

54
/**
65
* This method is like `flatMap` except that it recursively flattens the
@@ -23,7 +22,7 @@ import toInteger from './toInteger.js'
2322
* // => [[1, 1], [2, 2]]
2423
*/
2524
function flatMapDepth(collection, iteratee, depth) {
26-
depth = depth === undefined ? 1 : toInteger(depth)
25+
depth = depth === undefined ? 1 : +depth
2726
return baseFlatten(map(collection, iteratee), depth)
2827
}
2928

flattenDepth.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import baseFlatten from './.internal/baseFlatten.js'
2-
import toInteger from './toInteger.js'
32

43
/**
54
* Recursively flatten `array` up to `depth` times.
@@ -25,7 +24,7 @@ function flattenDepth(array, depth) {
2524
if (!length) {
2625
return []
2726
}
28-
depth = depth === undefined ? 1 : toInteger(depth)
27+
depth = depth === undefined ? 1 : +depth
2928
return baseFlatten(array, depth)
3029
}
3130

gt.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import toNumber from './toNumber.js'
2-
31
/**
42
* Checks if `value` is greater than `other`.
53
*
@@ -23,8 +21,8 @@ import toNumber from './toNumber.js'
2321
*/
2422
function gt(value, other) {
2523
if (!(typeof value == 'string' && typeof other == 'string')) {
26-
value = toNumber(value)
27-
other = toNumber(other)
24+
value = +value
25+
other = +other
2826
}
2927
return value > other
3028
}

gte.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import toNumber from './toNumber.js'
2-
31
/**
42
* Checks if `value` is greater than or equal to `other`.
53
*
@@ -23,8 +21,8 @@ import toNumber from './toNumber.js'
2321
*/
2422
function gte(value, other) {
2523
if (!(typeof value == 'string' && typeof other == 'string')) {
26-
value = toNumber(value)
27-
other = toNumber(other)
24+
value = +value
25+
other = +other
2826
}
2927
return value >= other
3028
}

inRange.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import baseInRange from './.internal/baseInRange.js'
2-
import toFinite from './toFinite.js'
3-
import toNumber from './toNumber.js'
42

53
/**
64
* Checks if `n` is between `start` and up to, but not including, `end`. If
@@ -39,15 +37,11 @@ import toNumber from './toNumber.js'
3937
* // => true
4038
*/
4139
function inRange(number, start, end) {
42-
start = toFinite(start)
4340
if (end === undefined) {
4441
end = start
4542
start = 0
46-
} else {
47-
end = toFinite(end)
4843
}
49-
number = toNumber(number)
50-
return baseInRange(number, start, end)
44+
return baseInRange(+number, +start, +end)
5145
}
5246

5347
export default inRange

indexOf.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import baseIndexOf from './.internal/baseIndexOf.js'
2-
import toInteger from './toInteger.js'
3-
4-
/* Built-in method references for those with the same name as other `lodash` methods. */
5-
const nativeMax = Math.max
62

73
/**
84
* Gets the index at which the first occurrence of `value` is found in `array`
@@ -30,9 +26,9 @@ function indexOf(array, value, fromIndex) {
3026
if (!length) {
3127
return -1
3228
}
33-
let index = fromIndex == null ? 0 : toInteger(fromIndex)
29+
let index = fromIndex == null ? 0 : +fromIndex
3430
if (index < 0) {
35-
index = nativeMax(length + index, 0)
31+
index = Math.max(length + index, 0)
3632
}
3733
return baseIndexOf(array, value, index)
3834
}

0 commit comments

Comments
 (0)
0