-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid overriding custom settings on font library save (#60438)
Co-authored-by: matiasbenedetto <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: mikachan <[email protected]> Co-authored-by: creativecoder <[email protected]> Co-authored-by: desrosj <[email protected]> Co-authored-by: estelaris <[email protected]> Co-authored-by: YanCol <[email protected]>
- Loading branch information
1 parent
6841cfb
commit 9f46833
Showing
4 changed files
with
110 additions
and
82 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Sets the value at path of object. | ||
* If a portion of path doesn’t exist, it’s created. | ||
* Arrays are created for missing index properties while objects are created | ||
* for all other missing properties. | ||
* | ||
* This function intentionally mutates the input object. | ||
* | ||
* Inspired by _.set(). | ||
* | ||
* @see https://lodash.com/docs/4.17.15#set | ||
* | ||
* @todo Needs to be deduplicated with its copy in `@wordpress/core-data`. | ||
* | ||
* @param {Object} object Object to modify | ||
* @param {Array} path Path of the property to set. | ||
* @param {*} value Value to set. | ||
*/ | ||
export default function setNestedValue( object, path, value ) { | ||
if ( ! object || typeof object !== 'object' ) { | ||
return object; | ||
} | ||
|
||
path.reduce( ( acc, key, idx ) => { | ||
if ( acc[ key ] === undefined ) { | ||
if ( Number.isInteger( path[ idx + 1 ] ) ) { | ||
acc[ key ] = []; | ||
} else { | ||
acc[ key ] = {}; | ||
} | ||
} | ||
if ( idx === path.length - 1 ) { | ||
acc[ key ] = value; | ||
} | ||
return acc[ key ]; | ||
}, object ); | ||
|
||
return object; | ||
} |