ddbfe0ca409ae1d67d607624cff79b8cfec01580
[platform/framework/web/crosswalk-tizen.git] /
1 var baseEach = require('./baseEach');
2
3 /** Used as references for `-Infinity` and `Infinity`. */
4 var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY,
5     POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
6
7 /**
8  * Gets the extremum value of `collection` invoking `iteratee` for each value
9  * in `collection` to generate the criterion by which the value is ranked.
10  * The `iteratee` is invoked with three arguments: (value, index, collection).
11  *
12  * @private
13  * @param {Array|Object|string} collection The collection to iterate over.
14  * @param {Function} iteratee The function invoked per iteration.
15  * @param {boolean} [isMin] Specify returning the minimum, instead of the
16  *  maximum, extremum value.
17  * @returns {*} Returns the extremum value.
18  */
19 function extremumBy(collection, iteratee, isMin) {
20   var exValue = isMin ? POSITIVE_INFINITY : NEGATIVE_INFINITY,
21       computed = exValue,
22       result = computed;
23
24   baseEach(collection, function(value, index, collection) {
25     var current = iteratee(value, index, collection);
26     if ((isMin ? (current < computed) : (current > computed)) ||
27         (current === exValue && current === result)) {
28       computed = current;
29       result = value;
30     }
31   });
32   return result;
33 }
34
35 module.exports = extremumBy;