Skip to content
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 away from _.groupBy() #49755

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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