Skip to content

Commit

Permalink
Fix mergeDeepRight function not properly merging objects if key exist…
Browse files Browse the repository at this point in the history
…s in target with value undefined
  • Loading branch information
acelaya committed Dec 18, 2023
1 parent 454abd9 commit 8ccc9b0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).

## [1.0.3] - 2023-12-18
### Added
* *Nothing*

### Changed
* *Nothing*

### Deprecated
* *Nothing*

### Removed
* *Nothing*

### Fixed
* Fix `mergeDeepRight` function not properly merging objects if key exists in target with value `undefined`.


## [1.0.2] - 2023-11-26
### Added
* *Nothing*
Expand Down
4 changes: 3 additions & 1 deletion src/merge-deep-right/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const mergeDeepRight = <T extends Record<string, any>>(target: T, source:

Object.keys(source).forEach((key: keyof T) => {
if (isObject(source[key])) {
output[key] = key in target ? mergeDeepRight(target[key], source[key]) : { ...source[key] };
output[key] = key in target && target[key] !== undefined
? mergeDeepRight(target[key], source[key])
: { ...source[key] };
} else {
output[key] = source[key];
}
Expand Down
14 changes: 14 additions & 0 deletions src/merge-deep-right/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ describe('mergeDeepRight', () => {
something: 'hi!',
},
],
[
{ foo: undefined },
{
foo: { one: 1, two: 2 },
},
{
foo: { one: 1, two: 2 },
},
],
[
{ foo: undefined },
{ foo: 'something' },
{ foo: 'something' },
],
])('returns expected result', (target, source, expected) => {
expect(mergeDeepRight(target, source)).toEqual(expected);
});
Expand Down

0 comments on commit 8ccc9b0

Please sign in to comment.