-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
setImmutably: don't clone all objects #56612
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,27 +4,6 @@ | |
import { paramCase } from 'change-case'; | ||
import memoize from 'memize'; | ||
|
||
/** | ||
* Converts a path to an array of its fragments. | ||
* Supports strings, numbers and arrays: | ||
* | ||
* 'foo' => [ 'foo' ] | ||
* 2 => [ '2' ] | ||
* [ 'foo', 'bar' ] => [ 'foo', 'bar' ] | ||
* | ||
* @param {string|number|Array} path Path | ||
* @return {Array} Normalized path. | ||
*/ | ||
function normalizePath( path ) { | ||
if ( Array.isArray( path ) ) { | ||
return path; | ||
} else if ( typeof path === 'number' ) { | ||
return [ path.toString() ]; | ||
} | ||
|
||
return [ path ]; | ||
} | ||
|
||
/** | ||
* Converts any string to kebab case. | ||
* Backwards compatible with Lodash's `_.kebabCase()`. | ||
|
@@ -55,33 +34,6 @@ export function kebabCase( str ) { | |
} ); | ||
} | ||
|
||
/** | ||
* Clones an object. | ||
* Arrays are also cloned as arrays. | ||
* Non-object values are returned unchanged. | ||
* | ||
* @param {*} object Object to clone. | ||
* @return {*} Cloned object, or original literal non-object value. | ||
*/ | ||
function cloneObject( object ) { | ||
if ( Array.isArray( object ) ) { | ||
return object.map( cloneObject ); | ||
} | ||
|
||
if ( object && typeof object === 'object' ) { | ||
return { | ||
...Object.fromEntries( | ||
Object.entries( object ).map( ( [ key, value ] ) => [ | ||
key, | ||
cloneObject( value ), | ||
] ) | ||
), | ||
}; | ||
} | ||
|
||
return object; | ||
} | ||
|
||
/** | ||
* Immutably sets a value inside an object. Like `lodash#set`, but returning a | ||
* new object. Treats nullish initial values as empty objects. Clones any | ||
|
@@ -93,24 +45,27 @@ function cloneObject( object ) { | |
* @return {Object} Cloned object with the new value set. | ||
*/ | ||
export function setImmutably( object, path, value ) { | ||
const normalizedPath = normalizePath( path ); | ||
const newObject = object ? cloneObject( object ) : {}; | ||
// Normalize path | ||
path = Array.isArray( path ) ? path : [ path ]; | ||
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. Furthermore, I see we're ditching the numeric path support, and I confirm that this is fine. I can't think of a use case where it won't work properly without that specific handling that we used to do. |
||
|
||
normalizedPath.reduce( ( acc, key, i ) => { | ||
if ( acc[ key ] === undefined ) { | ||
if ( Number.isInteger( path[ i + 1 ] ) ) { | ||
acc[ key ] = []; | ||
} else { | ||
acc[ key ] = {}; | ||
} | ||
} | ||
if ( i === normalizedPath.length - 1 ) { | ||
acc[ key ] = value; | ||
// Shallowly clone the base of the object | ||
object = Array.isArray( object ) ? [ ...object ] : { ...object }; | ||
|
||
// Traverse object from root to leaf, shallowly cloning at each level | ||
let prev = object; | ||
for ( const i in path ) { | ||
const branch = path[ i ]; | ||
if ( i < path.length - 1 ) { | ||
prev[ branch ] = Array.isArray( prev[ branch ] ) | ||
? [ ...prev[ branch ] ] | ||
: { ...prev[ branch ] }; | ||
} else { | ||
prev[ branch ] = value; | ||
} | ||
return acc[ key ]; | ||
}, newObject ); | ||
prev = prev[ branch ]; | ||
} | ||
|
||
return newObject; | ||
return object; | ||
} | ||
|
||
const stringToPath = memoize( ( path ) => path.split( '.' ) ); | ||
|
Oops, something went wrong.
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.
Should we use a new constant here? Mutation can introduce potential issues as we refactor in the future, and storing in a new constant is cheap.
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.
It's not mutatating in this case, just reassigning