-
Notifications
You must be signed in to change notification settings - Fork 216
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
Apply clone option to target object #209
Open
creativeux
wants to merge
10
commits into
TehShrike:master
Choose a base branch
from
creativeux:clone-target-false
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
97833ed
Apply clone option to target object
b9020e0
Apply target mutation to merge.all first array entry
10deb35
Missing index on return
38d54b2
Index madness. Oops.
cf8ef25
Add cloneWithTarget flag
971f5e6
merge with target, not clone with target...
c5688b7
Remove unnecessary boolean cast
3b66e4c
Handle undefined target
dadd37e
Improve undefined source / target testing
4c0d549
Incomplete assertion definitions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,10 @@ function emptyTarget(val) { | |
return Array.isArray(val) ? [] : {} | ||
} | ||
|
||
function firstArrayEntry(arr) { | ||
return arr && arr.length ? arr[0] : [] | ||
} | ||
|
||
function cloneUnlessOtherwiseSpecified(value, options) { | ||
return (options.clone !== false && options.isMergeableObject(value)) | ||
? deepmerge(emptyTarget(value), value, options) | ||
|
@@ -51,8 +55,21 @@ function propertyIsUnsafe(target, key) { | |
&& Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable. | ||
} | ||
|
||
// Retrieves either a new object or the appropriate target object to mutate. | ||
function getDestinationObject(target, source, options) { | ||
const targetDefined = typeof target !== 'undefined' | ||
const isArray = Array.isArray(target) || Array.isArray(source) | ||
const doMerge = options && (options.mergeWithTarget || options.clone === false) | ||
|
||
if (targetDefined && doMerge) { | ||
return Array.isArray(target) ? firstArrayEntry(target) : target | ||
} | ||
|
||
return isArray ? [] : {}; | ||
} | ||
|
||
function mergeObject(target, source, options) { | ||
var destination = {} | ||
var destination = getDestinationObject(target, source, options) | ||
if (options.isMergeableObject(target)) { | ||
getKeys(target).forEach(function(key) { | ||
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options) | ||
|
@@ -100,7 +117,7 @@ deepmerge.all = function deepmergeAll(array, options) { | |
|
||
return array.reduce(function(prev, next) { | ||
return deepmerge(prev, next, options) | ||
}, {}) | ||
}, getDestinationObject(array, undefined, options)) | ||
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. Following on from my other comment, this is how const target = array[0];
const destination = getDestinationObject(target, options)
const source = destination === target ? array.slice(1) : array
return source.reduce(function(prev, next) {
return deepmerge(prev, next, options)
}, destination) |
||
} | ||
|
||
module.exports = deepmerge |
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
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.
The destination object should never be an array.
This is how I would define this function:
Call from
deepmergeAll
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.
For me, the array check was required because during recursion it's possible that there is a property being handled whose value is
undefined
, but we know that the source value is an array, so we need to instantiate an empty object that can be merged into. If I remove this, nested properties with array types will fail to merge because an array source will try to be merged into an object target.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.
Yeah sorry, I was thrown off by the functionmergeObject
's name. I assumed that it's parameters were always non-array objects but in actual fact it merges any 2 values (or returns the latter one if they can't be merged).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.
Wait no, that's not right.
mergeObject
does only ever receive non-array objects.target
andsource
should never be arrays inmergeObject
- arrays should be handled byoptions.arrayMerge
.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.
@creativeux I suspect you'll need to update the logic in
deepmerge
itself to handle the case oftarget
beingundefined
.https://github.com/TehShrike/deepmerge/blob/master/index.js#L87-L93