min.js 954 B

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