Skip to content

Commit

Permalink
Remove coercion method use.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Mar 14, 2017
1 parent 2f281c6 commit bb7c959
Show file tree
Hide file tree
Showing 33 changed files with 59 additions and 194 deletions.
35 changes: 0 additions & 35 deletions .internal/baseRepeat.js

This file was deleted.

11 changes: 3 additions & 8 deletions .internal/createRound.js
@@ -1,7 +1,3 @@
import toInteger from '../toInteger.js'
import toNumber from '../toNumber.js'
import toString from '../toString.js'

/**
* Creates a function like `round`.
*
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions after.js
@@ -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.
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions before.js
@@ -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
Expand All @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions chunk.js
@@ -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`.
Expand All @@ -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))
Expand Down
9 changes: 3 additions & 6 deletions clamp.js
@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'

/**
* Clamps `number` within the inclusive `lower` and `upper` bounds.
*
Expand All @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions debounce.js
@@ -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`
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions delay.js
@@ -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.
Expand All @@ -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
9 changes: 3 additions & 6 deletions drop.js
@@ -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.
Expand All @@ -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
2 changes: 0 additions & 2 deletions dropRight.js
@@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 3 additions & 10 deletions endsWith.js
@@ -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.
*
Expand All @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions findLastIndex.js
@@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions flatMapDepth.js
@@ -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
Expand All @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions flattenDepth.js
@@ -1,5 +1,4 @@
import baseFlatten from './.internal/baseFlatten.js'
import toInteger from './toInteger.js'

/**
* Recursively flatten `array` up to `depth` times.
Expand All @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions gt.js
@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'

/**
* Checks if `value` is greater than `other`.
*
Expand All @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions gte.js
@@ -1,5 +1,3 @@
import toNumber from './toNumber.js'

/**
* Checks if `value` is greater than or equal to `other`.
*
Expand All @@ -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
}
Expand Down
8 changes: 1 addition & 7 deletions inRange.js
@@ -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
Expand Down Expand Up @@ -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
8 changes: 2 additions & 6 deletions indexOf.js
@@ -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`
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit bb7c959

Please sign in to comment.