Skip to content

_.max and _.min may behave incorrectly if iteratee returns infinite number #2688

Open
@jpli

Description

@jpli

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions