Skip to content

Commit

Permalink
Lodash: Refactor away from _.set() in PushChangesToGlobalStylesControl
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jul 7, 2023
1 parent bb5c6d0 commit 5e5723b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/core-data/src/utils/set-nested-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*
* @see https://lodash.com/docs/4.17.15#set
*
* @todo Needs to be deduplicated with its copy in `@wordpress/edit-site`.
*
* @param {Object} object Object to modify
* @param {Array} path Path of the property to set.
* @param {*} value Value to set.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, set } from 'lodash';
import { get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -119,6 +119,46 @@ function useChangesToPush( name, attributes ) {
);
}

/**
* 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.
*/
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;
}

function cloneDeep( object ) {
return ! object ? {} : JSON.parse( JSON.stringify( object ) );
}
Expand Down Expand Up @@ -148,8 +188,12 @@ function PushChangesToGlobalStylesControl( {
const newUserConfig = cloneDeep( userConfig );

for ( const { path, value } of changes ) {
set( newBlockStyles, path, undefined );
set( newUserConfig, [ 'styles', 'blocks', name, ...path ], value );
setNestedValue( newBlockStyles, path, undefined );
setNestedValue(
newUserConfig,
[ 'styles', 'blocks', name, ...path ],
value
);
}

// @wordpress/core-data doesn't support editing multiple entity types in
Expand Down

0 comments on commit 5e5723b

Please sign in to comment.