Skip to content

Commit

Permalink
Lodash: Refactor block library away from _.reduce() (#45456)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Nov 3, 2022
1 parent 5fce0a8 commit d7e2d9a
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions packages/block-library/src/gallery/v1/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { filter, find, get, isEmpty, map, reduce } from 'lodash';
import { filter, find, get, isEmpty, map } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -98,34 +98,29 @@ function GalleryEdit( props ) {

const resizedImages = useMemo( () => {
if ( isSelected ) {
return reduce(
attributes.ids,
return ( attributes.ids ?? [] ).reduce(
( currentResizedImages, id ) => {
if ( ! id ) {
return currentResizedImages;
}
const image = getMedia( id );
const sizes = reduce(
imageSizes,
( currentSizes, size ) => {
const defaultUrl = get( image, [
'sizes',
size.slug,
'url',
] );
const mediaDetailsUrl = get( image, [
'media_details',
'sizes',
size.slug,
'source_url',
] );
return {
...currentSizes,
[ size.slug ]: defaultUrl || mediaDetailsUrl,
};
},
{}
);
const sizes = imageSizes.reduce( ( currentSizes, size ) => {
const defaultUrl = get( image, [
'sizes',
size.slug,
'url',
] );
const mediaDetailsUrl = get( image, [
'media_details',
'sizes',
size.slug,
'source_url',
] );
return {
...currentSizes,
[ size.slug ]: defaultUrl || mediaDetailsUrl,
};
}, {} );
return {
...currentResizedImages,
[ parseInt( id, 10 ) ]: sizes,
Expand Down

0 comments on commit d7e2d9a

Please sign in to comment.