From 3ff2c699edf8c49dfa62ff42e718fc3fa776b270 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 6 Apr 2023 16:28:23 +0300 Subject: [PATCH 1/2] Lodash: Remove _.mapValues() from various blocks --- packages/block-library/src/columns/utils.js | 17 +- .../src/navigation/deprecated.js | 34 ++-- packages/block-library/src/table/state.js | 149 ++++++++++-------- .../src/utils/clean-empty-object.js | 6 +- 4 files changed, 109 insertions(+), 97 deletions(-) diff --git a/packages/block-library/src/columns/utils.js b/packages/block-library/src/columns/utils.js index 12cb45a9874d74..6134b6fa456ddb 100644 --- a/packages/block-library/src/columns/utils.js +++ b/packages/block-library/src/columns/utils.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { mapValues } from 'lodash'; - /** * Returns a column width attribute value rounded to standard precision. * Returns `undefined` if the value is not a valid finite number. @@ -86,10 +81,14 @@ export function getRedistributedColumnWidths( ) { const totalWidth = getTotalColumnsWidth( blocks, totalBlockCount ); - return mapValues( getColumnWidths( blocks, totalBlockCount ), ( width ) => { - const newWidth = ( availableWidth * width ) / totalWidth; - return toWidthPrecision( newWidth ); - } ); + return Object.fromEntries( + Object.entries( getColumnWidths( blocks, totalBlockCount ) ).map( + ( [ clientId, width ] ) => { + const newWidth = ( availableWidth * width ) / totalWidth; + return [ clientId, toWidthPrecision( newWidth ) ]; + } + ) + ); } /** diff --git a/packages/block-library/src/navigation/deprecated.js b/packages/block-library/src/navigation/deprecated.js index 95802a77f2256e..c2b1a2fd3bf48d 100644 --- a/packages/block-library/src/navigation/deprecated.js +++ b/packages/block-library/src/navigation/deprecated.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { mapValues } from 'lodash'; - /** * WordPress dependencies */ @@ -331,22 +326,23 @@ const migrateTypographyPresets = function ( attributes ) { ...attributes, style: { ...attributes.style, - typography: mapValues( - attributes.style.typography, - ( value, key ) => { - const prefix = TYPOGRAPHY_PRESET_DEPRECATION_MAP[ key ]; - if ( prefix && value.startsWith( prefix ) ) { - const newValue = value.slice( prefix.length ); - if ( - 'textDecoration' === key && - 'strikethrough' === newValue - ) { - return 'line-through'; + typography: Object.fromEntries( + Object.entries( attributes.style.typography ?? {} ).map( + ( [ key, value ] ) => { + const prefix = TYPOGRAPHY_PRESET_DEPRECATION_MAP[ key ]; + if ( prefix && value.startsWith( prefix ) ) { + const newValue = value.slice( prefix.length ); + if ( + 'textDecoration' === key && + 'strikethrough' === newValue + ) { + return [ key, 'line-through' ]; + } + return [ key, newValue ]; } - return newValue; + return [ key, value ]; } - return value; - } + ) ), }, }; diff --git a/packages/block-library/src/table/state.js b/packages/block-library/src/table/state.js index c16784645c1f50..961e0bd8415a46 100644 --- a/packages/block-library/src/table/state.js +++ b/packages/block-library/src/table/state.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { mapValues } from 'lodash'; - const INHERITED_COLUMN_ATTRIBUTES = [ 'align' ]; /** @@ -82,33 +77,45 @@ export function updateSelectedCell( state, selection, updateCell ) { const { sectionName: selectionSectionName, rowIndex: selectionRowIndex } = selection; - return mapValues( tableSections, ( section, sectionName ) => { - if ( selectionSectionName && selectionSectionName !== sectionName ) { - return section; - } - - return section.map( ( row, rowIndex ) => { - if ( selectionRowIndex && selectionRowIndex !== rowIndex ) { - return row; + return Object.fromEntries( + Object.entries( tableSections ).map( ( [ sectionName, section ] ) => { + if ( + selectionSectionName && + selectionSectionName !== sectionName + ) { + return [ sectionName, section ]; } - return { - cells: row.cells.map( ( cellAttributes, columnIndex ) => { - const cellLocation = { - sectionName, - columnIndex, - rowIndex, - }; - - if ( ! isCellSelected( cellLocation, selection ) ) { - return cellAttributes; + return [ + sectionName, + section.map( ( row, rowIndex ) => { + if ( selectionRowIndex && selectionRowIndex !== rowIndex ) { + return row; } - return updateCell( cellAttributes ); + return { + cells: row.cells.map( + ( cellAttributes, columnIndex ) => { + const cellLocation = { + sectionName, + columnIndex, + rowIndex, + }; + + if ( + ! isCellSelected( cellLocation, selection ) + ) { + return cellAttributes; + } + + return updateCell( cellAttributes ); + } + ), + }; } ), - }; - } ); - } ); + ]; + } ) + ); } /** @@ -224,31 +231,36 @@ export function insertColumn( state, { columnIndex } ) { ) ); - return mapValues( tableSections, ( section, sectionName ) => { - // Bail early if the table section is empty. - if ( isEmptyTableSection( section ) ) { - return section; - } - - return section.map( ( row ) => { - // Bail early if the row is empty or it's an attempt to insert past - // the last possible index of the array. - if ( isEmptyRow( row ) || row.cells.length < columnIndex ) { - return row; + return Object.fromEntries( + Object.entries( tableSections ).map( ( [ sectionName, section ] ) => { + // Bail early if the table section is empty. + if ( isEmptyTableSection( section ) ) { + return [ sectionName, section ]; } - return { - cells: [ - ...row.cells.slice( 0, columnIndex ), - { - content: '', - tag: sectionName === 'head' ? 'th' : 'td', - }, - ...row.cells.slice( columnIndex ), - ], - }; - } ); - } ); + return [ + sectionName, + section.map( ( row ) => { + // Bail early if the row is empty or it's an attempt to insert past + // the last possible index of the array. + if ( isEmptyRow( row ) || row.cells.length < columnIndex ) { + return row; + } + + return { + cells: [ + ...row.cells.slice( 0, columnIndex ), + { + content: '', + tag: sectionName === 'head' ? 'th' : 'td', + }, + ...row.cells.slice( columnIndex ), + ], + }; + } ), + ]; + } ) + ); } /** @@ -267,23 +279,28 @@ export function deleteColumn( state, { columnIndex } ) { ) ); - return mapValues( tableSections, ( section ) => { - // Bail early if the table section is empty. - if ( isEmptyTableSection( section ) ) { - return section; - } + return Object.fromEntries( + Object.entries( tableSections ).map( ( [ sectionName, section ] ) => { + // Bail early if the table section is empty. + if ( isEmptyTableSection( section ) ) { + return [ sectionName, section ]; + } - return section - .map( ( row ) => ( { - cells: - row.cells.length >= columnIndex - ? row.cells.filter( - ( cell, index ) => index !== columnIndex - ) - : row.cells, - } ) ) - .filter( ( row ) => row.cells.length ); - } ); + return [ + sectionName, + section + .map( ( row ) => ( { + cells: + row.cells.length >= columnIndex + ? row.cells.filter( + ( cell, index ) => index !== columnIndex + ) + : row.cells, + } ) ) + .filter( ( row ) => row.cells.length ), + ]; + } ) + ); } /** diff --git a/packages/block-library/src/utils/clean-empty-object.js b/packages/block-library/src/utils/clean-empty-object.js index adc78556a657e4..39e67a7657f331 100644 --- a/packages/block-library/src/utils/clean-empty-object.js +++ b/packages/block-library/src/utils/clean-empty-object.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { isEmpty, mapValues } from 'lodash'; +import { isEmpty } from 'lodash'; /** * Removed empty nodes from nested objects. @@ -18,8 +18,8 @@ const cleanEmptyObject = ( object ) => { return object; } const cleanedNestedObjects = Object.fromEntries( - Object.entries( mapValues( object, cleanEmptyObject ) ).filter( - ( [ , value ] ) => Boolean( value ) + Object.entries( object ).filter( ( [ , value ] ) => + Boolean( cleanEmptyObject( value ) ) ) ); return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects; From ec78782caa09e3068d37bdce8b9a261e7f81c43d Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 6 Apr 2023 17:15:51 +0300 Subject: [PATCH 2/2] Properly clean up nested objects --- packages/block-library/src/utils/clean-empty-object.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/utils/clean-empty-object.js b/packages/block-library/src/utils/clean-empty-object.js index 39e67a7657f331..e0e55c659bf575 100644 --- a/packages/block-library/src/utils/clean-empty-object.js +++ b/packages/block-library/src/utils/clean-empty-object.js @@ -18,9 +18,9 @@ const cleanEmptyObject = ( object ) => { return object; } const cleanedNestedObjects = Object.fromEntries( - Object.entries( object ).filter( ( [ , value ] ) => - Boolean( cleanEmptyObject( value ) ) - ) + Object.entries( object ) + .map( ( [ key, value ] ) => [ key, cleanEmptyObject( value ) ] ) + .filter( ( [ , value ] ) => Boolean( value ) ) ); return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects; };