baseExtremum.js 1.0 KB

1234567891011121314151617181920212223242526272829
  1. var baseEach = require('./baseEach');
  2. /**
  3. * Gets the extremum value of `collection` invoking `iteratee` for each value
  4. * in `collection` to generate the criterion by which the value is ranked.
  5. * The `iteratee` is invoked with three arguments: (value, index|key, collection).
  6. *
  7. * @private
  8. * @param {Array|Object|string} collection The collection to iterate over.
  9. * @param {Function} iteratee The function invoked per iteration.
  10. * @param {Function} comparator The function used to compare values.
  11. * @param {*} exValue The initial extremum value.
  12. * @returns {*} Returns the extremum value.
  13. */
  14. function baseExtremum(collection, iteratee, comparator, exValue) {
  15. var computed = exValue,
  16. result = computed;
  17. baseEach(collection, function(value, index, collection) {
  18. var current = +iteratee(value, index, collection);
  19. if (comparator(current, computed) || (current === exValue && current === result)) {
  20. computed = current;
  21. result = value;
  22. }
  23. });
  24. return result;
  25. }
  26. module.exports = baseExtremum;