mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Consolidate max and min modules.
This commit is contained in:
@@ -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;
|
|
||||||
24
max.js
24
max.js
@@ -1,6 +1,4 @@
|
|||||||
import baseExtremum from './.internal/baseExtremum.js';
|
import isSymbol from './isSymbol.js';
|
||||||
import baseGt from './.internal/baseGt.js';
|
|
||||||
import identity from './identity.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the maximum value of `array`. If `array` is empty or falsey,
|
* Computes the maximum value of `array`. If `array` is empty or falsey,
|
||||||
@@ -19,9 +17,23 @@ import identity from './identity.js';
|
|||||||
* // => undefined
|
* // => undefined
|
||||||
*/
|
*/
|
||||||
function max(array) {
|
function max(array) {
|
||||||
return (array && array.length)
|
let result;
|
||||||
? baseExtremum(array, identity, baseGt)
|
let index = -1;
|
||||||
: undefined;
|
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;
|
export default max;
|
||||||
|
|||||||
24
maxBy.js
24
maxBy.js
@@ -1,5 +1,4 @@
|
|||||||
import baseExtremum from './.internal/baseExtremum.js';
|
import isSymbol from './isSymbol.js';
|
||||||
import baseGt from './.internal/baseGt.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is like `max` except that it accepts `iteratee` which is
|
* This method is like `max` except that it accepts `iteratee` which is
|
||||||
@@ -19,9 +18,24 @@ import baseGt from './.internal/baseGt.js';
|
|||||||
* // => { 'n': 2 }
|
* // => { 'n': 2 }
|
||||||
*/
|
*/
|
||||||
function maxBy(array, iteratee) {
|
function maxBy(array, iteratee) {
|
||||||
return (array && array.length)
|
let result;
|
||||||
? baseExtremum(array, iteratee, baseGt)
|
let index = -1;
|
||||||
: undefined;
|
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;
|
export default maxBy;
|
||||||
|
|||||||
24
min.js
24
min.js
@@ -1,6 +1,4 @@
|
|||||||
import baseExtremum from './.internal/baseExtremum.js';
|
import isSymbol from './isSymbol.js';
|
||||||
import baseLt from './.internal/baseLt.js';
|
|
||||||
import identity from './identity.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the minimum value of `array`. If `array` is empty or falsey,
|
* Computes the minimum value of `array`. If `array` is empty or falsey,
|
||||||
@@ -19,9 +17,23 @@ import identity from './identity.js';
|
|||||||
* // => undefined
|
* // => undefined
|
||||||
*/
|
*/
|
||||||
function min(array) {
|
function min(array) {
|
||||||
return (array && array.length)
|
let result;
|
||||||
? baseExtremum(array, identity, baseLt)
|
let index = -1;
|
||||||
: undefined;
|
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;
|
export default min;
|
||||||
|
|||||||
24
minBy.js
24
minBy.js
@@ -1,5 +1,4 @@
|
|||||||
import baseExtremum from './.internal/baseExtremum.js';
|
import isSymbol from './isSymbol.js';
|
||||||
import baseLt from './.internal/baseLt.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is like `min` except that it accepts `iteratee` which is
|
* This method is like `min` except that it accepts `iteratee` which is
|
||||||
@@ -19,9 +18,24 @@ import baseLt from './.internal/baseLt.js';
|
|||||||
* // => { 'n': 1 }
|
* // => { 'n': 1 }
|
||||||
*/
|
*/
|
||||||
function minBy(array, iteratee) {
|
function minBy(array, iteratee) {
|
||||||
return (array && array.length)
|
let result;
|
||||||
? baseExtremum(array, iteratee, baseLt)
|
let index = -1;
|
||||||
: undefined;
|
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;
|
export default minBy;
|
||||||
|
|||||||
Reference in New Issue
Block a user