fix(replaceOrAppend): treat only null/undefined as a missing new item - #477
Open
mahirhir wants to merge 1 commit into
Open
fix(replaceOrAppend): treat only null/undefined as a missing new item#477mahirhir wants to merge 1 commit into
mahirhir wants to merge 1 commit into
Conversation
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.
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
replaceOrAppenduses a truthiness check to decide whether a new item was passed:Because
!newItemis true for any falsy value, a falsynewItemsuch as0,'',false, orNaNis treated as "no item" and silently dropped instead of being replaced or appended:The sibling
replacealready handles this correctly because it checksnewItem === undefined, andtogglechecksitem === undefined, so both treat falsy values as valid items.replaceOrAppendis the only one of the three that uses a truthiness check.The fix narrows the guard to
newItem == null. This still coversnullandundefined(the existing "missing item" tests passnull, so that behavior is preserved) while no longer swallowing falsy values.== nullis the same null/undefined idiom already used elsewhere in the codebase, for example inselect,clamp, andconcat.Related issue, if any:
None.
For any code change,
Does this PR introduce a breaking change?
No