Support cloning? #55
NightProgramming
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
I've just made #57 which will slightly change how the internals work so that cloning can simply be done like this: mergeOther: (values, utils, meta) => {
_.cloneDeep(utils.defaultMergeFunctions.mergeOthers(values)),
} Released in version 3.0.0-beta.7 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I understand that the actual cloning logic might be beyond the scope of this library, but please bare with me.
Consider we merge two objects:
So when doing things like
result.date.setHours()
thex.date
will be changed too (and vice-versa) which might be unexpected or unwanted. Assuming your library won't provide the actual cloning logic, something like the following could still be provided.deepmerge lib could then use this cloner whenever appropriate, e.g. when the value on one side is nullish and on the other side the value is an object which has an appropriate type for cloning. So deepmerge could make sure that this cloner is not called, when the type is not appropriate, e.g. having
const x = {str: null}; const y = {str: "myString"};
There doesn't need to be an attempt to call the cloner because the string literal is immutable anyway. Also when having deep mergable objects on bove sides the cloner doesn't need to be called like in the following case:const x = {obj: {str: null}; const y = {obj: {str: "mySring};
-> calling the cloner for the object that comes from the left or rightobj
property would not make any sense because all the properties inside that objects (onlystr
in this case) need to be merged.Of course we could do all that via custom merging, but it would require quite some code for the easiest use-case which is: Everything appropriate to be cloned should be cloned. All the things would have to be custom coded (=boilerplate) regarding the decision, if to clone, based on immutability and based on having only one value in deepmergeCustom values array that is a clone candidate (all others have to be nullish).
If you don't like this option based approached, maybe deepmergeCustom
meta
parameter could provide a function liketryGetCloneCandidate
. So something like:Because deepmerge probably has to know things about the values and their types anyway, I think an approach like above should perform better than simply cloning all of the end result:
Also when needing to merge into an existing object, the code would become a bit verbose:
compared to somethig like:
Beta Was this translation helpful? Give feedback.
All reactions