-
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 block library away from _.map()
#47215
Conversation
Size Change: -10 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
@@ -668,7 +667,7 @@ const v4 = { | |||
|
|||
return { | |||
...attributes, | |||
ids: map( attributes.ids, ( id ) => { | |||
ids: ( attributes.ids ?? [] ).map( ( id ) => { |
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.
Adding extra safety here, since post content could contain any surprises.
@@ -942,7 +941,7 @@ const v2 = { | |||
} | |||
return { | |||
...attributes, | |||
ids: map( attributes.images, ( { id } ) => { | |||
ids: ( attributes.images ?? [] ).map( ( { id } ) => { |
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.
Adding extra safety here, since post content could contain any surprises.
@@ -148,7 +148,7 @@ function GalleryEdit( props ) { | |||
...newAttrs, | |||
// Unlike images[ n ].id which is a string, always ensure the | |||
// ids array contains numbers as per its attribute type. | |||
ids: map( newAttrs.images, ( { id } ) => parseInt( id, 10 ) ), | |||
ids: newAttrs.images.map( ( { id } ) => parseInt( id, 10 ) ), |
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.
All calls of setAttributes()
will always call images
as arrays, so we're safe to directly call .map()
here.
( { name, slug } ) => ( { value: slug, label: name } ) | ||
); | ||
) | ||
.map( ( { name, slug } ) => ( { value: slug, label: name } ) ); |
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 an array since we're calling .map()
on the result of a .filter()
.
} | ||
|
||
function updateImagesSize( newSizeSlug ) { | ||
const updatedImages = map( images, ( image ) => { | ||
const updatedImages = ( images ?? [] ).map( ( image ) => { |
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.
Could come from the result of an onChange()
, so best to add extra safety here.
@@ -331,7 +330,7 @@ function GalleryEdit( props ) { | |||
images.length > 0 && | |||
images.every( ( { url } ) => isBlobURL( url ) ) | |||
) { | |||
const filesList = map( images, ( { url } ) => getBlobByURL( url ) ); | |||
const filesList = images.map( ( { url } ) => getBlobByURL( url ) ); |
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.
There are checks a few lines above that ensure that this is a non-empty array.
( { name, slug } ) => ( { value: slug, label: name } ) | ||
); | ||
) | ||
.map( ( { name, slug } ) => ( { value: slug, label: name } ) ); |
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 an array since we're calling .map()
on the result of a .filter()
.
); | ||
const imageSizeOptions = imageSizes | ||
.filter( ( { slug } ) => getImageSourceUrlBySizeSlug( image, slug ) ) | ||
.map( ( { name, slug } ) => ( { value: slug, label: name } ) ); |
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 an array since we're calling .map()
on the result of a .filter()
.
( item ) => { | ||
const taxonomyOptions = ( taxonomies ?? [] ) | ||
.filter( ( tax ) => !! tax.show_cloud ) | ||
.map( ( item ) => { |
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 an array since we're calling .map()
on the result of a .filter()
.
const taxonomyOptions = map( | ||
taxonomies?.filter( ( tax ) => !! tax.show_cloud ), | ||
( item ) => { | ||
const taxonomyOptions = ( taxonomies ?? [] ) |
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.
This actually fixes a bug introduced in #46114.
Optionally chaining .filter()
here will result in making taxonomyOptions
to be undefined
if there are no taxonomies registered. This will work with the original _.map()
since it will just return an empty array, but not with Array.prototype.map()
.
It's a subtle bug since 99.99% of the WordPress installations will contain at least one registered taxonomy, but still, this is fixing it for the remaining 0.001%.
Flaky tests detected in d844ee7. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3939093846
|
What?
This PR removes Lodash's
_.map()
from the block library package. There are just a few usages and conversion is pretty straightforward.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
Array.prototype.map()
instead. We're using nullish coalescing where necessary to cover nullish values.Testing Instructions