Skip to content

Commit

Permalink
Lodash: Refactor away from _.groupBy() (#49755)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Apr 13, 2023
1 parent 95035db commit 2929720
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const restrictedImports = [
'flowRight',
'forEach',
'fromPairs',
'groupBy',
'has',
'identity',
'includes',
Expand Down
32 changes: 27 additions & 5 deletions bin/plugin/commands/changelog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
const { groupBy } = require( 'lodash' );
const Octokit = require( '@octokit/rest' );
const { sprintf } = require( 'sprintf-js' );
const semver = require( 'semver' );
Expand Down Expand Up @@ -711,9 +710,19 @@ async function fetchAllPullRequests( octokit, settings ) {
function getChangelog( pullRequests ) {
let changelog = '## Changelog\n\n';

const groupedPullRequests = groupBy(
skipCreatedByBots( pullRequests ),
getIssueType
const groupedPullRequests = skipCreatedByBots( pullRequests ).reduce(
(
/** @type {Record<string, IssuesListForRepoResponseItem[]>} */ acc,
pr
) => {
const issueType = getIssueType( pr );
if ( ! acc[ issueType ] ) {
acc[ issueType ] = [];
}
acc[ issueType ].push( pr );
return acc;
},
{}
);

const sortedGroups = Object.keys( groupedPullRequests ).sort( sortGroup );
Expand All @@ -732,7 +741,20 @@ function getChangelog( pullRequests ) {
changelog += '### ' + group + '\n\n';

// Group PRs within this section into "Features".
const featureGroups = groupBy( groupPullRequests, getIssueFeature );
const featureGroups = groupPullRequests.reduce(
(
/** @type {Record<string, IssuesListForRepoResponseItem[]>} */ acc,
pr
) => {
const issueFeature = getIssueFeature( pr );
if ( ! acc[ issueFeature ] ) {
acc[ issueFeature ] = [];
}
acc[ issueFeature ].push( pr );
return acc;
},
{}
);

const featuredGroupNames = sortFeatureGroups( featureGroups );

Expand Down
15 changes: 9 additions & 6 deletions packages/block-editor/src/components/inserter/block-types-tab.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { groupBy } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -59,7 +54,15 @@ export function BlockTypesTab( {
itemList.filter(
( item ) => item.category && item.category !== 'reusable'
),
( itemList ) => groupBy( itemList, 'category' )
( itemList ) =>
itemList.reduce( ( acc, item ) => {
const { category } = item;
if ( ! acc[ category ] ) {
acc[ category ] = [];
}
acc[ category ].push( item );
return acc;
}, {} )
)( items );
}, [ items ] );

Expand Down
14 changes: 8 additions & 6 deletions packages/editor/src/components/entities-saved-states/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { groupBy } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -83,7 +78,14 @@ export default function EntitiesSavedStates( { close } ) {
useDispatch( noticesStore );

// To group entities by type.
const partitionedSavables = groupBy( dirtyEntityRecords, 'name' );
const partitionedSavables = dirtyEntityRecords.reduce( ( acc, record ) => {
const { name } = record;
if ( ! acc[ name ] ) {
acc[ name ] = [];
}
acc[ name ].push( record );
return acc;
}, {} );

// Sort entity groups.
const {
Expand Down

0 comments on commit 2929720

Please sign in to comment.