Skip to content

Commit d7ca1c1

Browse files
committed
1 parent 483fc0f commit d7ca1c1

File tree

1 file changed

+20
-43
lines changed

1 file changed

+20
-43
lines changed

underscore.js

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -277,56 +277,33 @@
277277
return _.find(obj, _.matcher(attrs));
278278
};
279279

280-
// Return the maximum element (or element-based computation).
281-
_.max = function(obj, iteratee, context) {
282-
var result = -Infinity, lastComputed = -Infinity,
283-
value, computed;
284-
if (iteratee == null && obj != null) {
285-
obj = isArrayLike(obj) ? obj : _.values(obj);
286-
for (var i = 0, length = obj.length; i < length; i++) {
287-
value = obj[i];
288-
if (value > result) {
289-
result = value;
290-
}
291-
}
292-
} else {
280+
// Generator function to create the max and min functions
281+
var createExtremumFinder = function(max) {
282+
return function(obj, iteratee, context) {
283+
var keys = !isArrayLike(obj) && _.keys(obj),
284+
length = (keys || obj).length,
285+
result = max ? -Infinity : Infinity,
286+
lastComputed = result,
287+
found = false;
293288
iteratee = cb(iteratee, context);
294-
_.each(obj, function(value, index, list) {
295-
computed = iteratee(value, index, list);
296-
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
289+
for (var index = 0; index < length; index++) {
290+
var currentKey = keys ? keys[index] : index;
291+
var value = obj[currentKey];
292+
var computed = iteratee ? iteratee(value, currentKey, obj) : value;
293+
if ((max ? computed > lastComputed : computed < lastComputed) || (!found && computed === result)) {
294+
found = true;
297295
result = value;
298296
lastComputed = computed;
299297
}
300-
});
301-
}
302-
return result;
303-
};
304-
305-
// Return the minimum element (or element-based computation).
306-
_.min = function(obj, iteratee, context) {
307-
var result = Infinity, lastComputed = Infinity,
308-
value, computed;
309-
if (iteratee == null && obj != null) {
310-
obj = isArrayLike(obj) ? obj : _.values(obj);
311-
for (var i = 0, length = obj.length; i < length; i++) {
312-
value = obj[i];
313-
if (value < result) {
314-
result = value;
315-
}
316298
}
317-
} else {
318-
iteratee = cb(iteratee, context);
319-
_.each(obj, function(value, index, list) {
320-
computed = iteratee(value, index, list);
321-
if (computed < lastComputed || computed === Infinity && result === Infinity) {
322-
result = value;
323-
lastComputed = computed;
324-
}
325-
});
326-
}
327-
return result;
299+
return result;
300+
};
328301
};
329302

303+
// Return the extremum element (or element-based computation).
304+
_.max = createExtremumFinder(true);
305+
_.min = createExtremumFinder(false);
306+
330307
// Shuffle a collection, using the modern version of the
331308
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
332309
_.shuffle = function(obj) {

0 commit comments

Comments
 (0)