From e17215c396b12133f183d134eeb0662d43bd3f88 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 7 Jul 2022 13:23:05 +0300 Subject: [PATCH 1/5] Editor: Remove _.flatMap() from DocumentOutline --- .../src/components/document-outline/index.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/editor/src/components/document-outline/index.js b/packages/editor/src/components/document-outline/index.js index bf8c4ba234c36..b1dccf3275587 100644 --- a/packages/editor/src/components/document-outline/index.js +++ b/packages/editor/src/components/document-outline/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { countBy, flatMap, get } from 'lodash'; +import { countBy, get } from 'lodash'; /** * WordPress dependencies @@ -50,16 +50,18 @@ const multipleH1Headings = [ * @return {Array} An array of heading blocks enhanced with the properties described above. */ const computeOutlineHeadings = ( blocks = [] ) => { - return flatMap( blocks, ( block = {} ) => { - if ( block.name === 'core/heading' ) { - return { - ...block, - level: block.attributes.level, - isEmpty: isEmptyHeading( block ), - }; - } - return computeOutlineHeadings( block.innerBlocks ); - } ); + return blocks + .map( ( block = {} ) => { + if ( block.name === 'core/heading' ) { + return { + ...block, + level: block.attributes.level, + isEmpty: isEmptyHeading( block ), + }; + } + return computeOutlineHeadings( block.innerBlocks ); + } ) + .flat(); }; const isEmptyHeading = ( heading ) => From 9365618fe7310ed3787dcc0072f9410b1f35f44a Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 7 Jul 2022 15:04:56 +0300 Subject: [PATCH 2/5] Lodash: Refactor both countBy usages --- bin/plugin/commands/changelog.js | 13 +++++++++++-- .../editor/src/components/document-outline/index.js | 10 ++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/bin/plugin/commands/changelog.js b/bin/plugin/commands/changelog.js index bd5b7c1e61176..f4893a19d008e 100644 --- a/bin/plugin/commands/changelog.js +++ b/bin/plugin/commands/changelog.js @@ -2,7 +2,6 @@ * External dependencies */ const { - countBy, groupBy, escapeRegExp, uniq, @@ -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} 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( diff --git a/packages/editor/src/components/document-outline/index.js b/packages/editor/src/components/document-outline/index.js index b1dccf3275587..8e598e46fecb9 100644 --- a/packages/editor/src/components/document-outline/index.js +++ b/packages/editor/src/components/document-outline/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { countBy, get } from 'lodash'; +import { get } from 'lodash'; /** * WordPress dependencies @@ -85,7 +85,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 ( From 85d4065038e9a6c1563153edc097416f974df550 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 7 Jul 2022 15:08:59 +0300 Subject: [PATCH 3/5] Editor: Remove _.get() from DocumentOutline --- packages/editor/src/components/document-outline/index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/editor/src/components/document-outline/index.js b/packages/editor/src/components/document-outline/index.js index 8e598e46fecb9..b39bb2edb8f33 100644 --- a/packages/editor/src/components/document-outline/index.js +++ b/packages/editor/src/components/document-outline/index.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -163,7 +158,7 @@ export default compose( return { title: getEditedPostAttribute( 'title' ), blocks: getBlocks(), - isTitleSupported: get( postType, [ 'supports', 'title' ], false ), + isTitleSupported: postType?.supports?.title ?? false, }; } ) )( DocumentOutline ); From 7dbfb45e116660fe4b63352b56040fb1f8a32fe0 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 7 Jul 2022 15:09:29 +0300 Subject: [PATCH 4/5] ESLint: Deprecate countBy --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index cea14615959fe..de3e49ea36790 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -82,6 +82,7 @@ module.exports = { 'chunk', 'clamp', 'concat', + 'countBy', 'defaults', 'defaultTo', 'differenceWith', From 47fba751b8d0d5e590df36b067c8e85bed705b10 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 11 Jul 2022 11:39:36 +0300 Subject: [PATCH 5/5] Use .flatMap() --- .../src/components/document-outline/index.js | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/editor/src/components/document-outline/index.js b/packages/editor/src/components/document-outline/index.js index b39bb2edb8f33..49d19e360edc8 100644 --- a/packages/editor/src/components/document-outline/index.js +++ b/packages/editor/src/components/document-outline/index.js @@ -45,18 +45,16 @@ const multipleH1Headings = [ * @return {Array} An array of heading blocks enhanced with the properties described above. */ const computeOutlineHeadings = ( blocks = [] ) => { - return blocks - .map( ( block = {} ) => { - if ( block.name === 'core/heading' ) { - return { - ...block, - level: block.attributes.level, - isEmpty: isEmptyHeading( block ), - }; - } - return computeOutlineHeadings( block.innerBlocks ); - } ) - .flat(); + return blocks.flatMap( ( block = {} ) => { + if ( block.name === 'core/heading' ) { + return { + ...block, + level: block.attributes.level, + isEmpty: isEmptyHeading( block ), + }; + } + return computeOutlineHeadings( block.innerBlocks ); + } ); }; const isEmptyHeading = ( heading ) =>