createExtremum.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. var arrayExtremum = require('./arrayExtremum'),
  2. baseCallback = require('./baseCallback'),
  3. baseExtremum = require('./baseExtremum'),
  4. isArray = require('../lang/isArray'),
  5. isIterateeCall = require('./isIterateeCall'),
  6. toIterable = require('./toIterable');
  7. /**
  8. * Creates a `_.max` or `_.min` function.
  9. *
  10. * @private
  11. * @param {Function} comparator The function used to compare values.
  12. * @param {*} exValue The initial extremum value.
  13. * @returns {Function} Returns the new extremum function.
  14. */
  15. function createExtremum(comparator, exValue) {
  16. return function(collection, iteratee, thisArg) {
  17. if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
  18. iteratee = undefined;
  19. }
  20. iteratee = baseCallback(iteratee, thisArg, 3);
  21. if (iteratee.length == 1) {
  22. collection = isArray(collection) ? collection : toIterable(collection);
  23. var result = arrayExtremum(collection, iteratee, comparator, exValue);
  24. if (!(collection.length && result === exValue)) {
  25. return result;
  26. }
  27. }
  28. return baseExtremum(collection, iteratee, comparator, exValue);
  29. };
  30. }
  31. module.exports = createExtremum;