Skip to content

Commit

Permalink
Lodash: Remove _.get() from various blocks (#48491)
Browse files Browse the repository at this point in the history
* Lodash: Remove _.get() from various blocks

* Remove unnecessary optional chaining
  • Loading branch information
tyxla authored Mar 9, 2023
1 parent 621c255 commit 3d768cc
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 64 deletions.
5 changes: 2 additions & 3 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -260,8 +259,8 @@ function Placeholder( { clientId, name, setAttributes } ) {
return (
<div { ...blockProps }>
<__experimentalBlockVariationPicker
icon={ get( blockType, [ 'icon', 'src' ] ) }
label={ get( blockType, [ 'title' ] ) }
icon={ blockType?.icon?.src }
label={ blockType?.title }
variations={ variations }
onSelect={ ( nextVariation = defaultVariation ) => {
if ( nextVariation.attributes ) {
Expand Down
19 changes: 7 additions & 12 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ const USERS_LIST_QUERY = {
};

function getFeaturedImageDetails( post, size ) {
const image = get( post, [ '_embedded', 'wp:featuredmedia', '0' ] );
const image = post._embedded?.[ 'wp:featuredmedia' ]?.[ '0' ];

return {
url:
Expand Down Expand Up @@ -116,16 +115,12 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
);

return {
defaultImageWidth: get(
settings.imageDimensions,
[ featuredImageSizeSlug, 'width' ],
0
),
defaultImageHeight: get(
settings.imageDimensions,
[ featuredImageSizeSlug, 'height' ],
0
),
defaultImageWidth:
settings.imageDimensions?.[ featuredImageSizeSlug ]
?.width ?? 0,
defaultImageHeight:
settings.imageDimensions?.[ featuredImageSizeSlug ]
?.height ?? 0,
imageSizes: settings.imageSizes,
latestPosts: getEntityRecords(
'postType',
Expand Down
10 changes: 2 additions & 8 deletions packages/block-library/src/media-text/edit.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import { View } from 'react-native';

/**
Expand Down Expand Up @@ -99,13 +98,8 @@ class MediaTextEdit extends Component {
if ( mediaType === 'image' && media.sizes ) {
// Try the "large" size URL, falling back to the "full" size URL below.
src =
get( media, [ 'sizes', 'large', 'url' ] ) ||
get( media, [
'media_details',
'sizes',
'large',
'source_url',
] );
media.sizes.large?.url ||
media?.media_details?.sizes?.large?.source_url;
}

setAttributes( {
Expand Down
8 changes: 2 additions & 6 deletions packages/block-library/src/pullquote/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -435,11 +434,8 @@ const v2 = {
// Is normal style and a named color is being used, we need to retrieve the color value to set the style,
// as there is no expectation that themes create classes that set border colors.
} else if ( mainColor ) {
const colors = get(
select( blockEditorStore ).getSettings(),
[ 'colors' ],
[]
);
const colors =
select( blockEditorStore ).getSettings().colors ?? [];
const colorObject = getColorObjectByAttributeValues(
colors,
mainColor
Expand Down
25 changes: 19 additions & 6 deletions packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -62,6 +57,24 @@ export const getEntitiesInfo = ( entities ) => {
};
};

/**
* Helper util to return a value from a certain path of the object.
* Path is specified as a string of properties, separated by dots,
* for example: "parent.child".
*
* @param {Object} object Input object.
* @param {string} path Path to the object property.
* @return {*} Value of the object property at the specified path.
*/
const getValueFromObjectPath = ( object, path ) => {
const normalizedPath = path.split( '.' );
let value = object;
normalizedPath.forEach( ( fieldName ) => {
value = value?.[ fieldName ];
} );
return value;
};

/**
* Helper util to map records to add a `name` prop from a
* provided path, in order to handle all entities in the same
Expand All @@ -74,7 +87,7 @@ export const getEntitiesInfo = ( entities ) => {
export const mapToIHasNameAndId = ( entities, path ) => {
return ( entities || [] ).map( ( entity ) => ( {
...entity,
name: decodeEntities( get( entity, path ) ),
name: decodeEntities( getValueFromObjectPath( entity, path ) ),
} ) );
};

Expand Down
25 changes: 8 additions & 17 deletions packages/block-library/src/table/state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, mapValues } from 'lodash';
import { mapValues } from 'lodash';

const INHERITED_COLUMN_ATTRIBUTES = [ 'align' ];

Expand Down Expand Up @@ -55,13 +55,9 @@ export function getFirstRow( state ) {
*/
export function getCellAttribute( state, cellLocation, attributeName ) {
const { sectionName, rowIndex, columnIndex } = cellLocation;
return get( state, [
sectionName,
rowIndex,
'cells',
columnIndex,
attributeName,
] );
return state[ sectionName ]?.[ rowIndex ]?.cells?.[ columnIndex ]?.[
attributeName
];
}

/**
Expand Down Expand Up @@ -158,9 +154,7 @@ export function isCellSelected( cellLocation, selection ) {
export function insertRow( state, { sectionName, rowIndex, columnCount } ) {
const firstRow = getFirstRow( state );
const cellCount =
columnCount === undefined
? get( firstRow, [ 'cells', 'length' ] )
: columnCount;
columnCount === undefined ? firstRow?.cells?.length : columnCount;

// Bail early if the function cannot determine how many cells to add.
if ( ! cellCount ) {
Expand All @@ -173,11 +167,8 @@ export function insertRow( state, { sectionName, rowIndex, columnCount } ) {
{
cells: Array.from( { length: cellCount } ).map(
( _, index ) => {
const firstCellInColumn = get(
firstRow,
[ 'cells', index ],
{}
);
const firstCellInColumn =
firstRow?.cells?.[ index ] ?? {};

const inheritedAttributes = Object.fromEntries(
Object.entries( firstCellInColumn ).filter(
Expand Down Expand Up @@ -310,7 +301,7 @@ export function toggleSection( state, sectionName ) {
}

// Get the length of the first row of the body to use when creating the header.
const columnCount = get( state, [ 'body', 0, 'cells', 'length' ], 1 );
const columnCount = state.body?.[ 0 ]?.cells?.length ?? 1;

// Section doesn't exist, insert an empty row to create the section.
return insertRow( state, { sectionName, rowIndex: 0, columnCount } );
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/text-columns/edit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -64,7 +59,7 @@ export default function TextColumnsEdit( { attributes, setAttributes } ) {
>
<RichText
tagName="p"
value={ get( content, [ index, 'children' ] ) }
value={ content?.[ index ]?.children }
onChange={ ( nextContent ) => {
setAttributes( {
content: [
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/text-columns/save.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -20,7 +15,7 @@ export default function save( { attributes } ) {
<div className="wp-block-column" key={ `column-${ index }` }>
<RichText.Content
tagName="p"
value={ get( content, [ index, 'children' ] ) }
value={ content?.[ index ]?.children }
/>
</div>
) ) }
Expand Down

0 comments on commit 3d768cc

Please sign in to comment.