Skip to content
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

[Utils] mergedeep deeply clones source key if it's an object #35364

Merged
merged 1 commit into from Dec 20, 2022

Conversation

yurisldk
Copy link
Contributor

@yurisldk yurisldk commented Dec 5, 2022

I'm not sure about lodash but we definetly have to use deep copy for objects otherwise value assign by reference.
Test case has been added.

Fix #35363

@mui-bot
Copy link

mui-bot commented Dec 5, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-35364--material-ui.netlify.app/

Details of bundle changes

Generated by 🚫 dangerJS against 34dd04d

@@ -1,3 +1,5 @@
import cloneDeep from 'lodash/cloneDeep';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lodash is a no go.

Suggested change
import cloneDeep from 'lodash/cloneDeep';

Copy link
Contributor Author

@yurisldk yurisldk Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliviertassinari I'm agree, it has been added to illustrate an solution and check that all tests will be passed. Should we write own implementation of clonedeep or do we have other ways?

Comment on lines 9 to 22
function clonedeep<T>(source: T): T | Record<keyof any, unknown> {
if (!isPlainObject(source)) {
return source;
}

const output: Record<keyof any, unknown> = {};

Object.keys(source).forEach((key) => {
output[key] = clonedeep(source[key]);
});

return output;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliviertassinari Seems that this implementation fits well, all tests passed and code coverage is happy.

@zannager zannager added the package: system Specific to @mui/system label Dec 6, 2022
Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be working well. I've got a small remark.

@@ -6,6 +6,20 @@ export interface DeepmergeOptions {
clone?: boolean;
}

function clonedeep<T>(source: T): T | Record<keyof any, unknown> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you call this function deepClone? I know that deepmerge doesn't follow conventions (should be deepMerge), but let's ensure new functions are named consistently with most of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yurisldk yurisldk force-pushed the fix/deepmerge branch 2 times, most recently from 6b041aa to a9a5287 Compare December 20, 2022 09:20
@yurisldk
Copy link
Contributor Author

@michaldudak @oliviertassinari branch is updated and ready for review. Thank you.

@yurisldk yurisldk requested review from michaldudak and oliviertassinari and removed request for oliviertassinari and michaldudak December 20, 2022 10:28
Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good! Thanks for your contribution!

@michaldudak michaldudak changed the title [Utils] mergedeep function clonedeep source key if its an object [Utils] mergedeep deep clones source key if it's an object Dec 20, 2022
@michaldudak michaldudak changed the title [Utils] mergedeep deep clones source key if it's an object [Utils] mergedeep deeply clones source key if it's an object Dec 20, 2022
@michaldudak michaldudak merged commit ba0ba65 into mui:master Dec 20, 2022
@yurisldk yurisldk deleted the fix/deepmerge branch December 21, 2022 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: system Specific to @mui/system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Utils/deepmerge] target key assigns a reference to the source key object if the target key does not exist.
5 participants