-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathextremumBy.js
35 lines (31 loc) · 1.27 KB
/
extremumBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
define(['./baseEach'], function(baseEach) {
/** Used as references for `-Infinity` and `Infinity`. */
var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY,
POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
/**
* Gets the extremum value of `collection` invoking `iteratee` for each value
* in `collection` to generate the criterion by which the value is ranked.
* The `iteratee` is invoked with three arguments; (value, index, collection).
*
* @private
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @param {boolean} [isMin] Specify returning the minimum, instead of the
* maximum, extremum value.
* @returns {*} Returns the extremum value.
*/
function extremumBy(collection, iteratee, isMin) {
var exValue = isMin ? POSITIVE_INFINITY : NEGATIVE_INFINITY,
computed = exValue,
result = computed;
baseEach(collection, function(value, index, collection) {
var current = iteratee(value, index, collection);
if ((isMin ? current < computed : current > computed) || (current === exValue && current === result)) {
computed = current;
result = value;
}
});
return result;
}
return extremumBy;
});