fix(compat/merge): use isArrayLikeObject to match lodash merge behavior#1542
Merged
fix(compat/merge): use isArrayLikeObject to match lodash merge behavior#1542
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1542 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 474 474
Lines 4492 4498 +6
Branches 1313 1314 +1
=======================================
+ Hits 4491 4497 +6
Misses 1 1 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a compatibility issue in merge() and mergeWith() where plain object properties were incorrectly preserved when the object was replaced by an array from the source, diverging from Lodash's behavior.
Key Changes:
- Modified
mergeWithDeep()to useisArrayLikeObjectto distinguish between arrays, array-like objects, and plain objects when the source value is an array - Added test cases to verify plain object properties are not preserved when replaced by arrays
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/compat/object/mergeWith.ts | Implements three-case logic for handling arrays, array-like objects, and plain objects when merging with array sources, using isArrayLikeObject to match Lodash behavior |
| src/compat/object/mergeWith.spec.ts | Adds test case verifying plain object properties don't persist when replaced by an array |
| src/compat/object/merge.spec.ts | Adds test case verifying plain object properties don't persist when replaced by an array |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
raon0211
approved these changes
Dec 5, 2025
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.
PR Description
Problem
es-toolkit/compat's merge() produces different results than Lodash when a nested object property is replaced by an array from the source.
Root Cause
In mergeWith.ts, when source is an array, the code treated all objects the same - copying every property to the new array:
Lodash (_baseMergeDeep.js lines 53-70) distinguishes three cases
using isArrayLikeObject:
Solution
Use isArrayLikeObject to match Lodash's logic:
Test
Added test cases in merge.spec.ts and mergeWith.spec.ts: