lodash/lodash

`meanBy` returning `NaN` on empty lists is error prone

Open

#5,901 opened on Jul 10, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)JavaScript (7,163 forks)batch import
documentationhelp wanted

Repository metrics

Stars
 (61,227 stars)
PR merge metrics
 (Avg merge 209d 9h) (5 merged PRs in 30d)

Description

_.meanBy([], 'test') will return NaN

This behavior is not currently documented, meaning that users might not know that it could return NaN.

Possible solutions:

  • Make meanBy return undefined instead of NaN, meaning that users using TypeScript will have to handle that case
  • Improve the documentation to warn users that they mustn't assume that the returned value is a valid number

Contributor guide