Skip to content

fix(compat): read sparse array holes as undefined in toString - #2091

Merged
dayongkr merged 1 commit into
toss:mainfrom
mariazuheros:fix/compat-tostring-sparse
Sep 10, 2026
Merged

fix(compat): read sparse array holes as undefined in toString#2091
dayongkr merged 1 commit into
toss:mainfrom
mariazuheros:fix/compat-tostring-sparse

Conversation

@mariazuheros

Copy link
Copy Markdown
Contributor

Summary

compat/toString renders an array by mapping its elements with Array.prototype.map, which skips holes in a sparse array. lodash reads every index, so a hole is converted as undefined rather than dropped:

toString([1, , 3]);
// es-toolkit: "1,,3"
// lodash:     "1,undefined,3"

toString([, ,]);
// es-toolkit: ","
// lodash:     "undefined,undefined"

Changes

Iterate the array by index instead of using .map, so each hole is read as undefined and converted through the same path as any other element. Dense arrays are unaffected, and the -0, symbol, and nested-array handling stay the same.

Added tests for sparse inputs.

Verified

Ran the compat implementation against lodash at runtime across sparse, dense, nested, nullish, -0, and symbol arrays. All outputs match.

toString mapped array elements with Array.prototype.map, which skips holes
in a sparse array. lodash reads every index, so a hole is rendered as
'undefined' rather than dropped:

toString([1, , 3])
// es-toolkit: '1,,3'
// lodash:     '1,undefined,3'

Iterate by index so holes are converted the same way lodash does.
@vercel

vercel Bot commented Sep 8, 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 Sep 8, 2026 11:28am UTC

Request Review

@dayongkr
dayongkr merged commit 9807495 into toss:main Sep 10, 2026
11 of 12 checks passed
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.

3 participants