From 7af66e9a8bf3c825e17a4471103569b74c8fb328 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Mon, 4 Jul 2022 17:00:06 +0300 Subject: [PATCH] Lodash: Refactor away from `_.keyBy()` (#42044) * Lodash: Refactor away from _.keyBy() * Add an explanatory comment --- .eslintrc.js | 1 + packages/blocks/src/store/reducer.js | 18 ++++++++++++++---- packages/core-data/src/reducer.js | 11 +++++++++-- packages/edit-site/src/store/selectors.js | 16 +++++++++++----- packages/edit-widgets/src/store/selectors.js | 16 ++++++++++------ packages/rich-text/src/store/reducer.js | 11 +++++++++-- 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7422ad432c63ab..34def1b5a413d4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -99,6 +99,7 @@ module.exports = { 'isNumber', 'isObjectLike', 'isUndefined', + 'keyBy', 'keys', 'lowerCase', 'memoize', diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 79169f5e415f9a..c9582926318e0d 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -6,7 +6,6 @@ import { find, get, isEmpty, - keyBy, map, mapValues, omit, @@ -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 @@ -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 ); @@ -102,7 +112,7 @@ export function blockStyles( state = {}, action ) { return { ...state, ...mapValues( - keyBy( action.blockTypes, 'name' ), + keyBlockTypesByName( action.blockTypes ), ( blockType ) => { return uniqBy( [ @@ -159,7 +169,7 @@ export function blockVariations( state = {}, action ) { return { ...state, ...mapValues( - keyBy( action.blockTypes, 'name' ), + keyBlockTypesByName( action.blockTypes ), ( blockType ) => { return uniqBy( [ diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index f0f93b111234d8..5a5b017ef9d53c 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -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 @@ -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, diff --git a/packages/edit-site/src/store/selectors.js b/packages/edit-site/src/store/selectors.js index 08e7fdd1395ca1..66e4d540ca680e 100644 --- a/packages/edit-site/src/store/selectors.js +++ b/packages/edit-site/src/store/selectors.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { map, keyBy } from 'lodash'; +import { map } from 'lodash'; import createSelector from 'rememo'; /** @@ -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 ) ) diff --git a/packages/edit-widgets/src/store/selectors.js b/packages/edit-widgets/src/store/selectors.js index 06a3f489899364..80bcddfead7b3f 100644 --- a/packages/edit-widgets/src/store/selectors.js +++ b/packages/edit-widgets/src/store/selectors.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { keyBy } from 'lodash'; - /** * WordPress dependencies */ @@ -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, + } ), + {} + ) || {} + ); } ); /** diff --git a/packages/rich-text/src/store/reducer.js b/packages/rich-text/src/store/reducer.js index 6002b1f55c34d5..86148eb7f76ff9 100644 --- a/packages/rich-text/src/store/reducer.js +++ b/packages/rich-text/src/store/reducer.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { keyBy, omit } from 'lodash'; +import { omit } from 'lodash'; /** * WordPress dependencies @@ -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 );