From 1bb333217df64a524ac6b502a10a529ba009fd71 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 18 Aug 2022 14:09:26 +0300 Subject: [PATCH 1/2] Lodash: Refactor away from _.times() --- .eslintrc.js | 1 + .../src/components/block-ratings/stars.js | 11 ++--- packages/block-library/src/categories/edit.js | 4 +- packages/block-library/src/columns/edit.js | 10 +++-- .../block-library/src/columns/edit.native.js | 10 +++-- packages/block-library/src/table/state.js | 40 ++++++++++--------- .../block-library/src/text-columns/edit.js | 4 +- .../block-library/src/text-columns/save.js | 4 +- packages/components/src/guide/page-control.js | 7 +--- .../components/src/guide/stories/index.js | 23 ++++++----- .../specs/performance/post-editor.test.js | 6 +-- 11 files changed, 61 insertions(+), 59 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 554d52e2f5299..e36a5bed75e64 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -141,6 +141,7 @@ module.exports = { 'sum', 'sumBy', 'take', + 'times', 'toString', 'trim', 'truncate', diff --git a/packages/block-directory/src/components/block-ratings/stars.js b/packages/block-directory/src/components/block-ratings/stars.js index 96c03f36e1be5..21c64290201b2 100644 --- a/packages/block-directory/src/components/block-ratings/stars.js +++ b/packages/block-directory/src/components/block-ratings/stars.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { times } from 'lodash'; - /** * WordPress dependencies */ @@ -24,7 +19,7 @@ function Stars( { rating } ) { stars ) } > - { times( fullStarCount, ( i ) => ( + { Array.from( { length: fullStarCount } ).map( ( _, i ) => ( ) ) } - { times( halfStarCount, ( i ) => ( + { Array.from( { length: halfStarCount } ).map( ( _, i ) => ( ) ) } - { times( emptyStarCount, ( i ) => ( + { Array.from( { length: emptyStarCount } ).map( ( _, i ) => ( - { times( level * 3, () => '\xa0' ) } + { Array.from( { length: level * 3 } ).map( () => '\xa0' ) } { renderCategoryName( name ) } { showPostCounts && ` (${ count })` } , diff --git a/packages/block-library/src/columns/edit.js b/packages/block-library/src/columns/edit.js index 1edb80df45073..8879f0e52f2e7 100644 --- a/packages/block-library/src/columns/edit.js +++ b/packages/block-library/src/columns/edit.js @@ -2,7 +2,7 @@ * External dependencies */ import classnames from 'classnames'; -import { get, times } from 'lodash'; +import { get } from 'lodash'; /** * WordPress dependencies @@ -183,7 +183,9 @@ const ColumnsEditContainerWrapper = withDispatch( innerBlocks = [ ...getMappedColumnWidths( innerBlocks, widths ), - ...times( newColumns - previousColumns, () => { + ...Array.from( { + length: newColumns - previousColumns, + } ).map( () => { return createBlock( 'core/column', { width: `${ newColumnWidth }%`, } ); @@ -192,7 +194,9 @@ const ColumnsEditContainerWrapper = withDispatch( } else if ( isAddingColumn ) { innerBlocks = [ ...innerBlocks, - ...times( newColumns - previousColumns, () => { + ...Array.from( { + length: newColumns - previousColumns, + } ).map( () => { return createBlock( 'core/column' ); } ), ]; diff --git a/packages/block-library/src/columns/edit.native.js b/packages/block-library/src/columns/edit.native.js index 4f62e227d890e..96855e3da17e4 100644 --- a/packages/block-library/src/columns/edit.native.js +++ b/packages/block-library/src/columns/edit.native.js @@ -2,7 +2,7 @@ * External dependencies */ import { View, Dimensions } from 'react-native'; -import { times, map } from 'lodash'; +import { map } from 'lodash'; /** * WordPress dependencies */ @@ -374,7 +374,9 @@ const ColumnsEditContainerWrapper = withDispatch( innerBlocks = [ ...getMappedColumnWidths( innerBlocks, widths ), - ...times( newColumns - previousColumns, () => { + ...Array.from( { + length: newColumns - previousColumns, + } ).map( () => { return createBlock( 'core/column', { width: `${ newColumnWidth }%`, verticalAlignment, @@ -384,7 +386,9 @@ const ColumnsEditContainerWrapper = withDispatch( } else if ( isAddingColumn ) { innerBlocks = [ ...innerBlocks, - ...times( newColumns - previousColumns, () => { + ...Array.from( { + length: newColumns - previousColumns, + } ).map( () => { return createBlock( 'core/column', { verticalAlignment, } ); diff --git a/packages/block-library/src/table/state.js b/packages/block-library/src/table/state.js index 185a9d69c2b1b..ffd469e98d96e 100644 --- a/packages/block-library/src/table/state.js +++ b/packages/block-library/src/table/state.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { times, get, mapValues, every, pick } from 'lodash'; +import { get, mapValues, every, pick } from 'lodash'; const INHERITED_COLUMN_ATTRIBUTES = [ 'align' ]; @@ -16,8 +16,8 @@ const INHERITED_COLUMN_ATTRIBUTES = [ 'align' ]; */ export function createTable( { rowCount, columnCount } ) { return { - body: times( rowCount, () => ( { - cells: times( columnCount, () => ( { + body: Array.from( { length: rowCount } ).map( () => ( { + cells: Array.from( { length: columnCount } ).map( () => ( { content: '', tag: 'td', } ) ), @@ -167,23 +167,25 @@ export function insertRow( state, { sectionName, rowIndex, columnCount } ) { [ sectionName ]: [ ...state[ sectionName ].slice( 0, rowIndex ), { - cells: times( cellCount, ( index ) => { - const firstCellInColumn = get( - firstRow, - [ 'cells', index ], - {} - ); - const inheritedAttributes = pick( - firstCellInColumn, - INHERITED_COLUMN_ATTRIBUTES - ); + cells: Array.from( { length: cellCount } ).map( + ( _, index ) => { + const firstCellInColumn = get( + firstRow, + [ 'cells', index ], + {} + ); + const inheritedAttributes = pick( + firstCellInColumn, + INHERITED_COLUMN_ATTRIBUTES + ); - return { - ...inheritedAttributes, - content: '', - tag: sectionName === 'head' ? 'th' : 'td', - }; - } ), + return { + ...inheritedAttributes, + content: '', + tag: sectionName === 'head' ? 'th' : 'td', + }; + } + ), }, ...state[ sectionName ].slice( rowIndex ), ], diff --git a/packages/block-library/src/text-columns/edit.js b/packages/block-library/src/text-columns/edit.js index d9e2a0c6c1fd9..180c786226c80 100644 --- a/packages/block-library/src/text-columns/edit.js +++ b/packages/block-library/src/text-columns/edit.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { get, times } from 'lodash'; +import { get } from 'lodash'; /** * WordPress dependencies @@ -55,7 +55,7 @@ export default function TextColumnsEdit( { attributes, setAttributes } ) { className: `align${ width } columns-${ columns }`, } ) } > - { times( columns, ( index ) => { + { Array.from( { length: columns } ).map( ( _, index ) => { return (
- { times( columns, ( index ) => ( + { Array.from( { length: columns } ).map( ( _, index ) => (
- { times( numberOfPages, ( page ) => ( + { Array.from( { length: numberOfPages } ).map( ( _, page ) => (
  • { ( { - content: ( - <> -

    - Page { page + 1 } of { numberOfPages } -

    -

    { loremIpsum }

    - - ), - } ) ) } + pages={ Array.from( { length: numberOfPages } ).map( + ( _, page ) => ( { + content: ( + <> +

    + Page { page + 1 } of { numberOfPages } +

    +

    { loremIpsum }

    + + ), + } ) + ) } /> ) } diff --git a/packages/e2e-tests/specs/performance/post-editor.test.js b/packages/e2e-tests/specs/performance/post-editor.test.js index 87395ae0bfda2..933b4973fb055 100644 --- a/packages/e2e-tests/specs/performance/post-editor.test.js +++ b/packages/e2e-tests/specs/performance/post-editor.test.js @@ -157,9 +157,9 @@ describe( 'Post Editor Performance', () => { await page.evaluate( () => { const { createBlock } = window.wp.blocks; const { dispatch } = window.wp.data; - const blocks = window.lodash - .times( 1000 ) - .map( () => createBlock( 'core/paragraph' ) ); + const blocks = Array.from( { length: 1000 } ).map( () => + createBlock( 'core/paragraph' ) + ); dispatch( 'core/block-editor' ).resetBlocks( blocks ); } ); const paragraphs = await page.$$( '.wp-block' ); From 6bee883d9503fb4cfc80825c678a2128e4b901e4 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 18 Aug 2022 14:15:14 +0300 Subject: [PATCH 2/2] Add components changelog --- packages/components/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index ac38491c12dc0..88b13b46ecef2 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -30,6 +30,7 @@ - `FormTokenField`: Refactor away from `_.uniq()` ([#43330](https://github.com/WordPress/gutenberg/pull/43330/)). - `contextConnect`: Refactor away from `_.uniq()` ([#43330](https://github.com/WordPress/gutenberg/pull/43330/)). - `ColorPalette`: Refactor away from `_.uniq()` ([#43330](https://github.com/WordPress/gutenberg/pull/43330/)). +- `Guide`: Refactor away from `_.times()` ([#43374](https://github.com/WordPress/gutenberg/pull/43374/)). ## 19.17.0 (2022-08-10)