Skip to content

Commit

Permalink
Use @jdalton's implementation of _.max/_.min
Browse files Browse the repository at this point in the history
  • Loading branch information
megawac committed Mar 3, 2014
1 parent 4074324 commit 2e685d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
6 changes: 6 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@
equal(_.max({'a': 'a'}), -Infinity, 'Maximum value of a non-numeric collection');

equal(299999, _.max(_.range(1,300000)), 'Maximum value of a too-big array');

equal(3, _.max([1, 2, 3, 'test']), 'Finds correct max in array starting with num and containing a NaN');
equal(3, _.max(['test', 1, 2, 3]), 'Finds correct max in array starting with NaN');
});

test('min', function() {
Expand All @@ -312,6 +315,9 @@
equal(_.min([now, then]), then);

equal(1, _.min(_.range(1,300000)), 'Minimum value of a too-big array');

equal(1, _.min([1, 2, 3, 'test']), 'Finds correct min in array starting with num and containing a NaN');
equal(1, _.min(['test', 1, 2, 3]), 'Finds correct min in array starting with NaN');
});

test('sortBy', function() {
Expand Down
48 changes: 34 additions & 14 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,27 +250,47 @@

// Return the maximum element or (element-based computation).
_.max = function(obj, iterator, context) {
var result = -Infinity, lastComputed = -Infinity;
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
if (computed > lastComputed) {
result = value;
lastComputed = computed;
var result = -Infinity, lastComputed = -Infinity,
value, computed;
if (!iterator && _.isArray(obj)) {
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value > result) {
result = value;
}
}
});
} else {
each(obj, function(value, index, list) {
computed = iterator ? iterator.call(context, value, index, list) : value;
if (computed > lastComputed) {
result = value;
lastComputed = computed;
}
});
}
return result;
};

// Return the minimum element (or element-based computation).
_.min = function(obj, iterator, context) {
var result = Infinity, lastComputed = Infinity;
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
if (computed < lastComputed) {
result = value;
lastComputed = computed;
var result = Infinity, lastComputed = Infinity,
value, computed;
if (!iterator && _.isArray(obj)) {
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value < result) {
result = value;
}
}
});
} else {
each(obj, function(value, index, list) {
computed = iterator ? iterator.call(context, value, index, list) : value;
if (computed < lastComputed) {
result = value;
lastComputed = computed;
}
});
}
return result;
};

Expand Down

0 comments on commit 2e685d6

Please sign in to comment.