New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reapply #7089 and handle arrays of primitives/arrays #8408
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a3b280
Reapply #7089
af7261b
handle simple arrays and arrays of arrays
c9d1aff
remove unecessary check and reorder for positive checks instead of ne…
4e29634
use isObject
a65fd9e
Merge branch 'master' of https://github.com/facebook/jest
9dbc98f
PR comments - remove frivolous comments, use Array.from(target)
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,13 +178,37 @@ export const saveSnapshotFile = ( | |
); | ||
}; | ||
|
||
const deepMergeArray = (target: Array<any>, source: Array<any>) => { | ||
// Clone target | ||
const mergedOutput = target.slice(); | ||
|
||
source.forEach((sourceElement, index) => { | ||
const targetElement = mergedOutput[index]; | ||
|
||
if (Array.isArray(target[index])) { | ||
// Target is an array | ||
mergedOutput[index] = deepMergeArray(target[index], sourceElement); | ||
} else if (isObject(targetElement)) { | ||
// Target is a (non-array) object - recursively merge | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need these comments (line 192 & 188), can you remove these? |
||
mergedOutput[index] = deepMerge(target[index], sourceElement); | ||
} else { | ||
// Source does not exist in target or target is primitive and cannot be deep merged | ||
mergedOutput[index] = sourceElement; | ||
} | ||
}); | ||
|
||
return mergedOutput; | ||
}; | ||
|
||
export const deepMerge = (target: any, source: any) => { | ||
const mergedOutput = {...target}; | ||
if (isObject(target) && isObject(source)) { | ||
Object.keys(source).forEach(key => { | ||
if (isObject(source[key]) && !source[key].$$typeof) { | ||
if (!(key in target)) Object.assign(mergedOutput, {[key]: source[key]}); | ||
else mergedOutput[key] = deepMerge(target[key], source[key]); | ||
} else if (Array.isArray(source[key])) { | ||
mergedOutput[key] = deepMergeArray(target[key], source[key]); | ||
} else { | ||
Object.assign(mergedOutput, {[key]: source[key]}); | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Array.from(target)
would be more obvious and wouldn't require a comment