From 250ca36f057647606d7c5a6f72e20b4461e3bb9c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 25 Apr 2014 07:21:10 -0700 Subject: [PATCH] Ensure `_.max` and `_.min` return the correct value when `callback` computes +/-Infinity. --- lodash.js | 4 ++-- test/test.js | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index fc39586b6..72a548232 100644 --- a/lodash.js +++ b/lodash.js @@ -4239,7 +4239,7 @@ baseEach(collection, function(value, index, collection) { var current = callback(value, index, collection); - if (current > computed) { + if (current > computed || (current === -Infinity && current === result)) { computed = current; result = value; } @@ -4314,7 +4314,7 @@ baseEach(collection, function(value, index, collection) { var current = callback(value, index, collection); - if (current < computed) { + if (current < computed || (current === Infinity && current === result)) { computed = current; result = value; } diff --git a/test/test.js b/test/test.js index 251cb57c9..210b571c1 100644 --- a/test/test.js +++ b/test/test.js @@ -2576,7 +2576,7 @@ }); test('should return `' + expected[1] + '` if value is not found', 1, function() { - strictEqual(func(objects, function(object) { return object.a == 3; }), expected[1]); + strictEqual(func(objects, function(object) { return object.a === 3; }), expected[1]); }); test('should work with an object for `callback`', 1, function() { @@ -6297,7 +6297,17 @@ }); }); - test('`_.' + methodName + '` should resolve the correct value when provided an array containing only one value', 1, function() { + test('`_.' + methodName + '` should work when `callback` returns +/-Infinity', 1, function() { + var object = { 'a': (isMax ? -Infinity : Infinity) }; + + var actual = func([object, { 'a': object.a }], function(object) { + return object.a; + }); + + strictEqual(actual, object); + }); + + test('`_.' + methodName + '` should work when chaining on an array with only one value', 1, function() { if (!isNpm) { var actual = _([40])[methodName]().value(); strictEqual(actual, 40);