-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
@@ -25,7 +20,7 @@ export default function FontFamilyControl( { | |||
fontFamilies = blockLevelFontFamilies; | |||
} | |||
|
|||
if ( isEmpty( fontFamilies ) ) { | |||
if ( ! fontFamilies || fontFamilies.length === 0 ) { |
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.
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.
@@ -39,7 +34,7 @@ export default function ImageSizeControl( { | |||
|
|||
return ( | |||
<> | |||
{ ! isEmpty( imageSizeOptions ) && ( | |||
{ imageSizeOptions && imageSizeOptions.length > 0 && ( |
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.
SelectControl
's options is always an array. However, we're just making sure it's not nullish before actually checking the array length.
@@ -135,7 +130,7 @@ function InserterSearchResults( { | |||
); | |||
|
|||
const hasItems = | |||
! isEmpty( filteredBlockTypes ) || ! isEmpty( filteredBlockPatterns ); | |||
filteredBlockTypes.length > 0 || filteredBlockPatterns.length > 0; |
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.
Already used as arrays in this component, so checking the length is enough.
@@ -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; |
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.
Object.values()
will always return an array, so checking .length
here will do the job.
@@ -41,7 +36,7 @@ export const settings = { | |||
} | |||
|
|||
if ( context === 'accessibility' ) { | |||
return isEmpty( content ) | |||
return ! content || content.length === 0 |
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.
content
will be a string, but we're still making sure it's not nullish for some weird reason.
@@ -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; |
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.
Same as above for heading's content
attribute.
Size Change: -2 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
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.
LGTM! Thank you for the great work and for the extensive commenting, as always 🙂
There seems to lots more instance of |
Thanks for chiming in @spacedmonkey - we're intentionally doing the refactoring in small pieces, in order to be able to:
Also, this PR was focused on addressing simpler use cases of Stay tuned for more of the Lodash removal PRs 💪 |
What?
This PR refactors some of the simpler existing Lodash
_.isEmpty()
usages.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're using simpler native terms of checking whether the item is empty in the current context. I'm going to post a self-review that adds more information about each change.
Testing Instructions