fix(compat/meanBy): skip undefined values when averaging to match lodash - #1978
Open
kdelay wants to merge 1 commit into
Open
fix(compat/meanBy): skip undefined values when averaging to match lodash#1978kdelay wants to merge 1 commit into
kdelay wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
`es-toolkit/compat`'s `meanBy` delegated to the main library's `meanBy`,
which sums every value as-is. A single `undefined` from the iteratee
therefore poisoned the whole sum and the result became `NaN`.
lodash skips `undefined` values while summing and still divides by the
full length of the input, so `meanBy([{ a: 1 }, {}], 'a')` returns `0.5`
in lodash but returned `NaN` here. `compat`'s own `mean` already matches
lodash because it builds on `compat`'s `sum`, so the two were also
inconsistent with each other.
Compute the sum with `compat`'s `sumBy`, which already implements
lodash's undefined-skipping behavior, and divide by the input length.
This also avoids the intermediate `Array.from` copy for array-likes.
kdelay
force-pushed
the
fix/compat-meanby-undefined
branch
from
August 8, 2026 12:59
de8c307 to
767bfb3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
es-toolkit/compat'smeanByreturnsNaNwhenever the iteratee producesundefinedfor any element, while lodash skips those values and still divides by the full length.More cases measured against
lodash@4.18.1onmain(81af489):meanBy([{ a: 1 }, {}], 'a')NaN0.5meanBy([{ a: 1 }, { a: undefined }, { a: 3 }], 'a')NaN1.333...meanBy([1, undefined, 2])NaN1meanBy({ 0: { a: 1 }, 1: {}, length: 2 }, 'a')NaN0.5meanBy([{ a: { b: 1 } }, {}], 'a.b')NaN0.5This is also inconsistent inside
compatitself:mean([1, undefined, 2])already returns1like lodash, becausecompat/meanbuilds oncompat/sum, which skipsundefined.Cause
src/compat/math/meanBy.tsdelegated to the main library'smeanBy, which sums with the main library'ssumBy. That one adds every value as-is, so oneundefinedturns the whole sum intoNaN. lodash'sbaseMeandividesbaseSum— which ignoresundefined— byarray.length.Changes
compat's ownsumBy, which already implements lodash's undefined-skipping behavior, and divide by the length ofitems. When every value isundefinedthe result staysNaN, matching lodash.Array.fromcopy, sincecompat/sumByaccepts array-likes directly.undefined, deep paths, array-likes, and the all-undefinedcase.compatreference docs for all four languages.Only
es-toolkit/compatis changed;es-toolkit's ownmeanBykeeps its strictergetValue: (element: T) => numbercontract.Test results
yarn vitest run src/compat/math src/math— 35 files, 288 tests passyarn vitest run— 659 files pass (tests/check-dist.spec.tsfails to build locally on Node 24.10 with an unrelated tsdown/PnP loader error; it also fails on unmodifiedmainhere)yarn typecheck,yarn workspace type-tests test— no errorsyarn eslinton the changed files — no errorslodash@4.18.1in thebenchmarksworkspace: all cases match after the change