_.max and _.min may behave incorrectly if iteratee returns infinite number
Author: jpliCreated Jul 9, 2017Updated Dec 19, 2020
Labelsfixedbug
The iteratee "swaps" the contents of the array, so _.max should return the smaller member:
_.max([2, 5], function (value) { return value === 5 ? 2 : 5; }); // 2, correctWhen the smaller member is -Infinity:
_.max([-Infinity, 5], function (value) { return value === 5 ? -Infinity : 5; }); // 5, wrongSimilar for _.min:
_.min([10, 5], function (value) { return value === 5 ? 10 : 5; }); // 10, correct
_.min([Infinity, 5], function (value) { return value === 5 ? Infinity : 5; }); // 5, wrongI know which part of the code causes the bug, for example in _.min, it's the conditional expression after ||:
if (computed < lastComputed || computed === Infinity && result === Infinity) {
...But I don't know how to fix it because I can't figure out the exact intent of that expression.
Source: jashkenas/underscore