min.js 974 B

12345678910111213141516171819202122232425262728293031
  1. var _isArrayLike = require('./_isArrayLike.js');
  2. var values = require('./values.js');
  3. var _cb = require('./_cb.js');
  4. var each = require('./each.js');
  5. // Return the minimum element (or element-based computation).
  6. 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. }
  29. module.exports = min;