mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 09:47:48 +00:00
Add arrayMin and arrayMax helpers.
This commit is contained in:
144
lodash.js
144
lodash.js
@@ -1335,6 +1335,48 @@
|
|||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A specialized version of `_.max` for arrays without support for iteratees.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Array} array The array to iterate over.
|
||||||
|
* @returns {*} Returns the maximum value.
|
||||||
|
*/
|
||||||
|
function arrayMax(array) {
|
||||||
|
var index = -1,
|
||||||
|
length = array.length,
|
||||||
|
result = NEGATIVE_INFINITY;
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
var value = array[index];
|
||||||
|
if (value > result) {
|
||||||
|
result = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A specialized version of `_.min` for arrays without support for iteratees.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Array} array The array to iterate over.
|
||||||
|
* @returns {*} Returns the minimum value.
|
||||||
|
*/
|
||||||
|
function arrayMin(array) {
|
||||||
|
var index = -1,
|
||||||
|
length = array.length,
|
||||||
|
result = POSITIVE_INFINITY;
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
var value = array[index];
|
||||||
|
if (value < result) {
|
||||||
|
result = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by `_.defaults` to customize its `_.assign` use.
|
* Used by `_.defaults` to customize its `_.assign` use.
|
||||||
*
|
*
|
||||||
@@ -5433,38 +5475,31 @@
|
|||||||
* // => { 'user': 'fred', 'age': 40 };
|
* // => { 'user': 'fred', 'age': 40 };
|
||||||
*/
|
*/
|
||||||
function max(collection, iteratee, thisArg) {
|
function max(collection, iteratee, thisArg) {
|
||||||
iteratee = isIterateeCall(collection, iteratee, thisArg) ? null : iteratee;
|
var noIteratee = iteratee == null;
|
||||||
|
if (!noIteratee && isIterateeCall(collection, iteratee, thisArg)) {
|
||||||
var computed = NEGATIVE_INFINITY,
|
iteratee = null;
|
||||||
noIteratee = iteratee == null,
|
noIteratee = true;
|
||||||
isArr = noIteratee && isArray(collection),
|
}
|
||||||
isStr = !isArr && isString(collection),
|
var isArr = noIteratee && isArray(collection),
|
||||||
result = computed;
|
isStr = !isArr && isString(collection);
|
||||||
|
|
||||||
if (noIteratee && !isStr) {
|
if (noIteratee && !isStr) {
|
||||||
var index = -1,
|
return arrayMax(isArr ? collection : toIterable(collection));
|
||||||
iterable = toIterable(collection),
|
|
||||||
length = iterable.length;
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
var value = iterable[index];
|
|
||||||
if (value > result) {
|
|
||||||
result = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
iteratee = (noIteratee && isStr)
|
|
||||||
? charAtCallback
|
|
||||||
: getCallback(iteratee, thisArg, 3);
|
|
||||||
|
|
||||||
baseEach(collection, function(value, index, collection) {
|
|
||||||
var current = iteratee(value, index, collection);
|
|
||||||
if (current > computed || (current === NEGATIVE_INFINITY && current === result)) {
|
|
||||||
computed = current;
|
|
||||||
result = value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
var computed = NEGATIVE_INFINITY,
|
||||||
|
result = computed;
|
||||||
|
|
||||||
|
iteratee = (noIteratee && isStr)
|
||||||
|
? charAtCallback
|
||||||
|
: getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
|
baseEach(collection, function(value, index, collection) {
|
||||||
|
var current = iteratee(value, index, collection);
|
||||||
|
if (current > computed || (current === NEGATIVE_INFINITY && current === result)) {
|
||||||
|
computed = current;
|
||||||
|
result = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5512,38 +5547,31 @@
|
|||||||
* // => { 'user': 'barney', 'age': 36 };
|
* // => { 'user': 'barney', 'age': 36 };
|
||||||
*/
|
*/
|
||||||
function min(collection, iteratee, thisArg) {
|
function min(collection, iteratee, thisArg) {
|
||||||
iteratee = isIterateeCall(collection, iteratee, thisArg) ? null : iteratee;
|
var noIteratee = iteratee == null;
|
||||||
|
if (!noIteratee && isIterateeCall(collection, iteratee, thisArg)) {
|
||||||
var computed = POSITIVE_INFINITY,
|
iteratee = null;
|
||||||
noIteratee = iteratee == null,
|
noIteratee = true;
|
||||||
isArr = noIteratee && isArray(collection),
|
}
|
||||||
isStr = !isArr && isString(collection),
|
var isArr = noIteratee && isArray(collection),
|
||||||
result = computed;
|
isStr = !isArr && isString(collection);
|
||||||
|
|
||||||
if (noIteratee && !isStr) {
|
if (noIteratee && !isStr) {
|
||||||
var index = -1,
|
return arrayMin(isArr ? collection : toIterable(collection));
|
||||||
iterable = toIterable(collection),
|
|
||||||
length = iterable.length;
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
var value = iterable[index];
|
|
||||||
if (value < result) {
|
|
||||||
result = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
iteratee = (noIteratee && isStr)
|
|
||||||
? charAtCallback
|
|
||||||
: getCallback(iteratee, thisArg, 3);
|
|
||||||
|
|
||||||
baseEach(collection, function(value, index, collection) {
|
|
||||||
var current = iteratee(value, index, collection);
|
|
||||||
if (current < computed || (current === POSITIVE_INFINITY && current === result)) {
|
|
||||||
computed = current;
|
|
||||||
result = value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
var computed = POSITIVE_INFINITY,
|
||||||
|
result = computed;
|
||||||
|
|
||||||
|
iteratee = (noIteratee && isStr)
|
||||||
|
? charAtCallback
|
||||||
|
: getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
|
baseEach(collection, function(value, index, collection) {
|
||||||
|
var current = iteratee(value, index, collection);
|
||||||
|
if (current < computed || (current === POSITIVE_INFINITY && current === result)) {
|
||||||
|
computed = current;
|
||||||
|
result = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user