Skip to content

Commit

Permalink
Lodash: Refactor away from _.countBy() (#42231)
Browse files Browse the repository at this point in the history
* Editor: Remove _.flatMap() from DocumentOutline

* Lodash: Refactor both countBy usages

* Editor: Remove _.get() from DocumentOutline

* ESLint: Deprecate countBy

* Use .flatMap()
  • Loading branch information
tyxla authored Jul 11, 2022
1 parent b79f747 commit 80444af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
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 );

0 comments on commit 80444af

Please sign in to comment.