Skip to content

fix(replaceOrAppend): treat only null/undefined as a missing new item - #477

Open
mahirhir wants to merge 1 commit into
radashi-org:mainfrom
mahirhir:fix/replace-or-append-falsy-item
Open

fix(replaceOrAppend): treat only null/undefined as a missing new item#477
mahirhir wants to merge 1 commit into
radashi-org:mainfrom
mahirhir:fix/replace-or-append-falsy-item

Conversation

@mahirhir

Copy link
Copy Markdown

Summary

replaceOrAppend uses a truthiness check to decide whether a new item was passed:

if (!array && !newItem) return []
if (!newItem) return [...array]

Because !newItem is true for any falsy value, a falsy newItem such as 0, '', false, or NaN is treated as "no item" and silently dropped instead of being replaced or appended:

replaceOrAppend([1, 2, 3], 0, n => n === 2)      // got [1, 2, 3], expected [1, 0, 3]
replaceOrAppend([1, 2, 3], 0, n => n > 100)      // got [1, 2, 3], expected [1, 2, 3, 0]
replaceOrAppend([true], false, x => x === true)  // got [true],    expected [false]

The sibling replace already handles this correctly because it checks newItem === undefined, and toggle checks item === undefined, so both treat falsy values as valid items. replaceOrAppend is the only one of the three that uses a truthiness check.

The fix narrows the guard to newItem == null. This still covers null and undefined (the existing "missing item" tests pass null, so that behavior is preserved) while no longer swallowing falsy values. == null is the same null/undefined idiom already used elsewhere in the codebase, for example in select, clamp, and concat.

Related issue, if any:

None.

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed
  • Release notes in next-minor.md or next-major.md have been added, if needed

Does this PR introduce a breaking change?

No

replaceOrAppend used a truthiness check (!newItem) to decide whether a new item was supplied, so falsy items like 0, '', false, and NaN were treated as missing and silently dropped. Its sibling replace uses newItem === undefined and toggle uses item === undefined, so both already handle falsy items as valid. Narrow the guard to newItem == null, which keeps the existing null/undefined behavior while replacing or appending falsy items correctly.
@mahirhir
mahirhir changed the base branch from master to main June 29, 2026 19:51
@mahirhir
mahirhir requested a review from aleclarson as a code owner June 29, 2026 19:51
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.

1 participant