max.js 976 B

123456789101112131415161718192021222324252627282930
  1. define(['./_isArrayLike', './values', './_cb', './each'], function (_isArrayLike, values, _cb, each) {
  2. // Return the maximum element (or element-based computation).
  3. function max(obj, iteratee, context) {
  4. var result = -Infinity, lastComputed = -Infinity,
  5. value, computed;
  6. if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) {
  7. obj = _isArrayLike(obj) ? obj : values(obj);
  8. for (var i = 0, length = obj.length; i < length; i++) {
  9. value = obj[i];
  10. if (value != null && value > result) {
  11. result = value;
  12. }
  13. }
  14. } else {
  15. iteratee = _cb(iteratee, context);
  16. each(obj, function(v, index, list) {
  17. computed = iteratee(v, index, list);
  18. if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
  19. result = v;
  20. lastComputed = computed;
  21. }
  22. });
  23. }
  24. return result;
  25. }
  26. return max;
  27. });