arrayExtremum.js 822 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * A specialized version of `baseExtremum` for arrays which invokes `iteratee`
  3. * with one argument: (value).
  4. *
  5. * @private
  6. * @param {Array} array The array to iterate over.
  7. * @param {Function} iteratee The function invoked per iteration.
  8. * @param {Function} comparator The function used to compare values.
  9. * @param {*} exValue The initial extremum value.
  10. * @returns {*} Returns the extremum value.
  11. */
  12. function arrayExtremum(array, iteratee, comparator, exValue) {
  13. var index = -1,
  14. length = array.length,
  15. computed = exValue,
  16. result = computed;
  17. while (++index < length) {
  18. var value = array[index],
  19. current = +iteratee(value);
  20. if (comparator(current, computed)) {
  21. computed = current;
  22. result = value;
  23. }
  24. }
  25. return result;
  26. }
  27. module.exports = arrayExtremum;