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 _.countBy() #42231

Merged
merged 5 commits into from
Jul 11, 2022
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 @@ -82,6 +82,7 @@ module.exports = {
'chunk',
'clamp',
'concat',
'countBy',
'defaults',
'defaultTo',
'differenceWith',
Expand Down
13 changes: 11 additions & 2 deletions bin/plugin/commands/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
const {
countBy,
groupBy,
escapeRegExp,
uniq,
Expand Down Expand Up @@ -309,7 +308,17 @@ function getIssueFeature( issue ) {
// 1. Prefer explicit mapping of label to feature.
if ( featureCandidates.length ) {
// Get occurances of the feature labels.
const featureCounts = countBy( featureCandidates );
const featureCounts = featureCandidates.reduce(
/**
* @param {Record<string,number>} acc Accumulator
* @param {string} feature Feature label
*/
( acc, feature ) => ( {
...acc,
[ feature ]: ( acc[ feature ] || 0 ) + 1,
} ),
{}
);

// Check which matching label occurs most often.
const rankedFeatures = Object.keys( featureCounts ).sort(
Expand Down
17 changes: 9 additions & 8 deletions packages/editor/src/components/document-outline/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { countBy, flatMap, get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -50,7 +45,7 @@ const multipleH1Headings = [
* @return {Array} An array of heading blocks enhanced with the properties described above.
*/
const computeOutlineHeadings = ( blocks = [] ) => {
return flatMap( blocks, ( block = {} ) => {
return blocks.flatMap( ( block = {} ) => {
if ( block.name === 'core/heading' ) {
return {
...block,
Expand Down Expand Up @@ -83,7 +78,13 @@ export const DocumentOutline = ( {
// Not great but it's the simplest way to locate the title right now.
const titleNode = document.querySelector( '.editor-post-title__input' );
const hasTitle = isTitleSupported && title && titleNode;
const countByLevel = countBy( headings, 'level' );
const countByLevel = headings.reduce(
( acc, heading ) => ( {
...acc,
[ heading.level ]: ( acc[ heading.level ] || 0 ) + 1,
} ),
{}
);
const hasMultipleH1 = countByLevel[ 1 ] > 1;

return (
Expand Down Expand Up @@ -155,7 +156,7 @@ export default compose(
return {
title: getEditedPostAttribute( 'title' ),
blocks: getBlocks(),
isTitleSupported: get( postType, [ 'supports', 'title' ], false ),
isTitleSupported: postType?.supports?.title ?? false,
};
} )
)( DocumentOutline );