max.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var createExtremum = require('../internal/createExtremum'),
  2. gt = require('../lang/gt');
  3. /** Used as references for `-Infinity` and `Infinity`. */
  4. var NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY;
  5. /**
  6. * Gets the maximum value of `collection`. If `collection` is empty or falsey
  7. * `-Infinity` is returned. If an iteratee function is provided it's invoked
  8. * for each value in `collection` to generate the criterion by which the value
  9. * is ranked. The `iteratee` is bound to `thisArg` and invoked with three
  10. * arguments: (value, index, collection).
  11. *
  12. * If a property name is provided for `iteratee` the created `_.property`
  13. * style callback returns the property value of the given element.
  14. *
  15. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  16. * style callback returns `true` for elements that have a matching property
  17. * value, else `false`.
  18. *
  19. * If an object is provided for `iteratee` the created `_.matches` style
  20. * callback returns `true` for elements that have the properties of the given
  21. * object, else `false`.
  22. *
  23. * @static
  24. * @memberOf _
  25. * @category Math
  26. * @param {Array|Object|string} collection The collection to iterate over.
  27. * @param {Function|Object|string} [iteratee] The function invoked per iteration.
  28. * @param {*} [thisArg] The `this` binding of `iteratee`.
  29. * @returns {*} Returns the maximum value.
  30. * @example
  31. *
  32. * _.max([4, 2, 8, 6]);
  33. * // => 8
  34. *
  35. * _.max([]);
  36. * // => -Infinity
  37. *
  38. * var users = [
  39. * { 'user': 'barney', 'age': 36 },
  40. * { 'user': 'fred', 'age': 40 }
  41. * ];
  42. *
  43. * _.max(users, function(chr) {
  44. * return chr.age;
  45. * });
  46. * // => { 'user': 'fred', 'age': 40 }
  47. *
  48. * // using the `_.property` callback shorthand
  49. * _.max(users, 'age');
  50. * // => { 'user': 'fred', 'age': 40 }
  51. */
  52. var max = createExtremum(gt, NEGATIVE_INFINITY);
  53. module.exports = max;