Skip to content

Commit

Permalink
Lodash: Refactor away from _.keyBy() (#42044)
Browse files Browse the repository at this point in the history
* Lodash: Refactor away from _.keyBy()

* Add an explanatory comment
  • Loading branch information
tyxla authored Jul 4, 2022
1 parent 301dec6 commit 7af66e9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module.exports = {
'isNumber',
'isObjectLike',
'isUndefined',
'keyBy',
'keys',
'lowerCase',
'memoize',
Expand Down
18 changes: 14 additions & 4 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
find,
get,
isEmpty,
keyBy,
map,
mapValues,
omit,
Expand Down Expand Up @@ -41,6 +40,17 @@ export const DEFAULT_CATEGORIES = [
{ slug: 'reusable', title: __( 'Reusable blocks' ) },
];

// Key block types by their name.
function keyBlockTypesByName( types ) {
return types.reduce(
( newBlockTypes, block ) => ( {
...newBlockTypes,
[ block.name ]: block,
} ),
{}
);
}

/**
* Reducer managing the unprocessed block types in a form passed when registering the by block.
* It's for internal use only. It allows recomputing the processed block types on-demand after block type filters
Expand Down Expand Up @@ -79,7 +89,7 @@ export function blockTypes( state = {}, action ) {
case 'ADD_BLOCK_TYPES':
return {
...state,
...keyBy( action.blockTypes, 'name' ),
...keyBlockTypesByName( action.blockTypes ),
};
case 'REMOVE_BLOCK_TYPES':
return omit( state, action.names );
Expand All @@ -102,7 +112,7 @@ export function blockStyles( state = {}, action ) {
return {
...state,
...mapValues(
keyBy( action.blockTypes, 'name' ),
keyBlockTypesByName( action.blockTypes ),
( blockType ) => {
return uniqBy(
[
Expand Down Expand Up @@ -159,7 +169,7 @@ export function blockVariations( state = {}, action ) {
return {
...state,
...mapValues(
keyBy( action.blockTypes, 'name' ),
keyBlockTypesByName( action.blockTypes ),
( blockType ) => {
return uniqBy(
[
Expand Down
11 changes: 9 additions & 2 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { keyBy, map, groupBy, flowRight, isEqual, get } from 'lodash';
import { map, groupBy, flowRight, isEqual, get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -55,7 +55,14 @@ export function users( state = { byId: {}, queries: {} }, action ) {
return {
byId: {
...state.byId,
...keyBy( action.users, 'id' ),
// Key users by their ID.
...action.users.reduce(
( newUsers, user ) => ( {
...newUsers,
[ user.id ]: user,
} ),
{}
),
},
queries: {
...state.queries,
Expand Down
16 changes: 11 additions & 5 deletions packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { map, keyBy } from 'lodash';
import { map } from 'lodash';
import createSelector from 'rememo';

/**
Expand Down Expand Up @@ -341,10 +341,16 @@ export const getCurrentTemplateTemplateParts = createRegistrySelector(
'wp_template_part',
{ per_page: -1 }
);
const templatePartsById = keyBy(
templateParts,
( templatePart ) => templatePart.id
);
const templatePartsById = templateParts
? // Key template parts by their ID.
templateParts.reduce(
( newTemplateParts, part ) => ( {
...newTemplateParts,
[ part.id ]: part,
} ),
{}
)
: {};

return ( template.blocks ?? [] )
.filter( ( block ) => isTemplatePart( block ) )
Expand Down
16 changes: 10 additions & 6 deletions packages/edit-widgets/src/store/selectors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { keyBy } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -36,7 +31,16 @@ export const getWidgets = createRegistrySelector( ( select ) => () => {
buildWidgetsQuery()
);

return keyBy( widgets, 'id' );
return (
// Key widgets by their ID.
widgets?.reduce(
( allWidgets, widget ) => ( {
...allWidgets,
[ widget.id ]: widget,
} ),
{}
) || {}
);
} );

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/rich-text/src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { keyBy, omit } from 'lodash';
import { omit } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -21,7 +21,14 @@ export function formatTypes( state = {}, action ) {
case 'ADD_FORMAT_TYPES':
return {
...state,
...keyBy( action.formatTypes, 'name' ),
// Key format types by their name.
...action.formatTypes.reduce(
( newFormatTypes, type ) => ( {
...newFormatTypes,
[ type.name ]: type,
} ),
{}
),
};
case 'REMOVE_FORMAT_TYPES':
return omit( state, action.names );
Expand Down

0 comments on commit 7af66e9

Please sign in to comment.