diff --git a/.internal/baseExtremum.js b/.internal/baseExtremum.js deleted file mode 100644 index 7455bf561..000000000 --- a/.internal/baseExtremum.js +++ /dev/null @@ -1,34 +0,0 @@ -import isSymbol from './isSymbol.js'; - -/** - * The base implementation of methods like `max` and `min` which accepts a - * `comparator` to determine the extremum value. - * - * @private - * @param {Array} array The array to iterate over. - * @param {Function} iteratee The iteratee invoked per iteration. - * @param {Function} comparator The comparator used to compare values. - * @returns {*} Returns the extremum value. - */ -function baseExtremum(array, iteratee, comparator) { - let result; - let index = -1; - const length = array.length; - - while (++index < length) { - let computed; - const value = array[index]; - const current = iteratee(value); - - if (current != null && (computed === undefined - ? (current === current && !isSymbol(current)) - : comparator(current, computed) - )) { - computed = current; - result = value; - } - } - return result; -} - -export default baseExtremum; diff --git a/max.js b/max.js index 2b77e9ff7..1b1f074cf 100644 --- a/max.js +++ b/max.js @@ -1,6 +1,4 @@ -import baseExtremum from './.internal/baseExtremum.js'; -import baseGt from './.internal/baseGt.js'; -import identity from './identity.js'; +import isSymbol from './isSymbol.js'; /** * Computes the maximum value of `array`. If `array` is empty or falsey, @@ -19,9 +17,23 @@ import identity from './identity.js'; * // => undefined */ function max(array) { - return (array && array.length) - ? baseExtremum(array, identity, baseGt) - : undefined; + let result; + let index = -1; + const length = array ? array.length : 0; + + while (++index < length) { + let computed; + const value = array[index]; + + if (value != null && (computed === undefined + ? (value === value && !isSymbol(value)) + : (value > computed) + )) { + computed = value; + result = value; + } + } + return result; } export default max; diff --git a/maxBy.js b/maxBy.js index 9c3737095..22cfb8a5b 100644 --- a/maxBy.js +++ b/maxBy.js @@ -1,5 +1,4 @@ -import baseExtremum from './.internal/baseExtremum.js'; -import baseGt from './.internal/baseGt.js'; +import isSymbol from './isSymbol.js'; /** * This method is like `max` except that it accepts `iteratee` which is @@ -19,9 +18,24 @@ import baseGt from './.internal/baseGt.js'; * // => { 'n': 2 } */ function maxBy(array, iteratee) { - return (array && array.length) - ? baseExtremum(array, iteratee, baseGt) - : undefined; + let result; + let index = -1; + const length = array ? array.length : 0; + + while (++index < length) { + let computed; + const value = array[index]; + const current = iteratee(value); + + if (current != null && (computed === undefined + ? (current === current && !isSymbol(current)) + : (current > computed) + )) { + computed = current; + result = value; + } + } + return result; } export default maxBy; diff --git a/min.js b/min.js index c7b44e651..22c10c76a 100644 --- a/min.js +++ b/min.js @@ -1,6 +1,4 @@ -import baseExtremum from './.internal/baseExtremum.js'; -import baseLt from './.internal/baseLt.js'; -import identity from './identity.js'; +import isSymbol from './isSymbol.js'; /** * Computes the minimum value of `array`. If `array` is empty or falsey, @@ -19,9 +17,23 @@ import identity from './identity.js'; * // => undefined */ function min(array) { - return (array && array.length) - ? baseExtremum(array, identity, baseLt) - : undefined; + let result; + let index = -1; + const length = array ? array.length : 0; + + while (++index < length) { + let computed; + const value = array[index]; + + if (value != null && (computed === undefined + ? (value === value && !isSymbol(value)) + : (value < computed) + )) { + computed = value; + result = value; + } + } + return result; } export default min; diff --git a/minBy.js b/minBy.js index a76cf3da6..743642b7a 100644 --- a/minBy.js +++ b/minBy.js @@ -1,5 +1,4 @@ -import baseExtremum from './.internal/baseExtremum.js'; -import baseLt from './.internal/baseLt.js'; +import isSymbol from './isSymbol.js'; /** * This method is like `min` except that it accepts `iteratee` which is @@ -19,9 +18,24 @@ import baseLt from './.internal/baseLt.js'; * // => { 'n': 1 } */ function minBy(array, iteratee) { - return (array && array.length) - ? baseExtremum(array, iteratee, baseLt) - : undefined; + let result; + let index = -1; + const length = array ? array.length : 0; + + while (++index < length) { + let computed; + const value = array[index]; + const current = iteratee(value); + + if (current != null && (computed === undefined + ? (current === current && !isSymbol(current)) + : (current < computed) + )) { + computed = current; + result = value; + } + } + return result; } export default minBy;