Repository navigation
fix(map/set): treat nullish initialValue as a real accumulator - #2196
Merged
raon0211 merged 2 commits intoNov 8, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Map.reduce, Set.reduce, and reduceAsync treated null and undefined as a missing initial value. An explicit null seed was dropped, and a callback that returned null skipped the next Map or Set entry. Co-authored-by: Rahul Kashyap <itisrmk@users.noreply.github.com>
raon0211
force-pushed
the
cursor/fix-reduce-nullish-accumulator-de9f
branch
from
November 8, 2026 01:12
99e724b to
d12d2ac
Compare
reduceAsync was already fixed in toss#2176, so only the Map and Set versions remain. Take the first entry from the iterator when no initial value is given and let the loop run the callback unconditionally. Rewrite the tests to check the callback arguments.
raon0211
force-pushed
the
cursor/fix-reduce-nullish-accumulator-de9f
branch
from
November 8, 2026 01:32
d12d2ac to
8d0abbb
Compare
raon0211
approved these changes
Nov 8, 2026
This branch was successfully deployed
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
Map.reduceandSet.reducetreatednull/undefinedas a missing initial value. Passingnullas the seed dropped it, and a callback that returnednullskipped the next entry. (reduceAsynchad the same bug and was already fixed in #2176.)Fix
Only omit the initial value when the argument count says it was not passed, like
es-toolkit/compat'sreduce(#1742). When no initial value is given, the first entry is taken from the iterator and the loop runs the callback unconditionally. Explicitnull/undefinedseeds and nullish callback results are kept.Test plan
yarn vitest run src/map/reduce src/set/reduce(43 passed; the 12 new tests fail on the previous implementation)yarn lintandtsc --noEmit