Skip to content

fix(map/set): treat nullish initialValue as a real accumulator - #2196

Merged
raon0211 merged 2 commits into
toss:mainfrom
itisrmk:cursor/fix-reduce-nullish-accumulator-de9f
Nov 8, 2026
Merged

raon0211 merged 2 commits into
toss:mainfrom
itisrmk:cursor/fix-reduce-nullish-accumulator-de9f

Conversation

@itisrmk

@itisrmk itisrmk commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Map.reduce and Set.reduce treated null/undefined as a missing initial value. Passing null as the seed dropped it, and a callback that returned null skipped the next entry. (reduceAsync had 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's reduce (#1742). When no initial value is given, the first entry is taken from the iterator and the loop runs the callback unconditionally. Explicit null/undefined seeds 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 lint and tsc --noEmit

@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
es-toolkit Ready Ready Preview Nov 8, 2026 1:36am UTC

Request Review

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
raon0211 force-pushed the cursor/fix-reduce-nullish-accumulator-de9f branch from 99e724b to d12d2ac Compare November 8, 2026 01:12
@raon0211 raon0211 changed the title fix(map/set/reduceAsync): treat nullish initialValue as a real accumulator fix(map/set): treat nullish initialValue as a real accumulator Nov 8, 2026
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
raon0211 merged commit cb812b2 into toss:main Nov 8, 2026
13 checks passed

This branch was successfully deployed

1 active deployment
Preview — 8d0abbba Deployed Nov 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants