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

Lodash: Refactor some _.isEmpty() instances #47353

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/block-editor/src/components/font-family/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -25,7 +20,7 @@ export default function FontFamilyControl( {
fontFamilies = blockLevelFontFamilies;
}

if ( isEmpty( fontFamilies ) ) {
if ( ! fontFamilies || fontFamilies.length === 0 ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

fontFamilies is being used as an array a few lines below already, so making sure it's not nullish and then checking for the array length is a safe bet.

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -39,7 +34,7 @@ export default function ImageSizeControl( {

return (
<>
{ ! isEmpty( imageSizeOptions ) && (
{ imageSizeOptions && imageSizeOptions.length > 0 && (
Copy link
Member Author

Choose a reason for hiding this comment

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

SelectControl's options is always an array. However, we're just making sure it's not nullish before actually checking the array length.

<SelectControl
__nextHasNoMarginBottom
label={ __( 'Image size' ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -135,7 +130,7 @@ function InserterSearchResults( {
);

const hasItems =
! isEmpty( filteredBlockTypes ) || ! isEmpty( filteredBlockPatterns );
filteredBlockTypes.length > 0 || filteredBlockPatterns.length > 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

Already used as arrays in this component, so checking the length is enough.


const blocksUI = !! filteredBlockTypes.length && (
<InserterPanel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -211,5 +206,5 @@ export function isValuesDefined( values ) {
if ( values === undefined || values === null ) {
return false;
}
return ! isEmpty( Object.values( values ).filter( ( value ) => !! value ) );
return Object.values( values ).filter( ( value ) => !! value ).length > 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

Object.values() will always return an array, so checking .length here will do the job.

}
7 changes: 1 addition & 6 deletions packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -41,7 +36,7 @@ export const settings = {
}

if ( context === 'accessibility' ) {
return isEmpty( content )
return ! content || content.length === 0
Copy link
Member Author

Choose a reason for hiding this comment

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

content will be a string, but we're still making sure it's not nullish for some weird reason.

? sprintf(
/* translators: accessibility text. %s: heading level. */
__( 'Level %s. Empty.' ),
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -35,7 +30,7 @@ export const settings = {
__experimentalLabel( attributes, { context } ) {
if ( context === 'accessibility' ) {
const { content } = attributes;
return isEmpty( content ) ? __( 'Empty' ) : content;
return ! content || content.length === 0 ? __( 'Empty' ) : content;
Copy link
Member Author

Choose a reason for hiding this comment

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

Same as above for heading's content attribute.

}
},
transforms,
Expand Down