jashkenas / jashkenas/underscore
_.max and _.min may behave incorrectly if iteratee returns infinite number
- Dominant language
- JavaScript
- Stars
- 27.3k
- Forks
- 5.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 1
Description
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, correct
When the smaller member is `-Infinity`:
_.max([-Infinity, 5], function (value) { return value === 5 ? -Infinity : 5; }); // 5, wrong
Similar 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, wrong
I 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.
Contributor guide
Research direction
Start with the _.max and _.min implementations, especially the conditional expression after `||` in the min logic described in the issue. Read the surrounding comparisons to determine the intended handling of Infinity and -Infinity, then verify the reported examples return the expected members for both functions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100