Skip to content

Commit

Permalink
Lodash: Remove _.mapValues() from various blocks (#49637)
Browse files Browse the repository at this point in the history
* Lodash: Remove _.mapValues() from various blocks

* Properly clean up nested objects
  • Loading branch information
tyxla authored Apr 6, 2023
1 parent 14f1ea8 commit a3faaef
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 98 deletions.
17 changes: 8 additions & 9 deletions packages/block-library/src/columns/utils.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 ) ];
}
)
);
}

/**
Expand Down
34 changes: 15 additions & 19 deletions packages/block-library/src/navigation/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { mapValues } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -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;
}
)
),
},
};
Expand Down
149 changes: 83 additions & 66 deletions packages/block-library/src/table/state.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { mapValues } from 'lodash';

const INHERITED_COLUMN_ATTRIBUTES = [ 'align' ];

/**
Expand Down Expand Up @@ -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 );
}
),
};
} ),
};
} );
} );
];
} )
);
}

/**
Expand Down Expand Up @@ -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 ),
],
};
} ),
];
} )
);
}

/**
Expand All @@ -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 ),
];
} )
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/utils/clean-empty-object.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEmpty, mapValues } from 'lodash';
import { isEmpty } from 'lodash';

/**
* Removed empty nodes from nested objects.
Expand All @@ -18,9 +18,9 @@ const cleanEmptyObject = ( object ) => {
return object;
}
const cleanedNestedObjects = Object.fromEntries(
Object.entries( mapValues( object, cleanEmptyObject ) ).filter(
( [ , value ] ) => Boolean( value )
)
Object.entries( object )
.map( ( [ key, value ] ) => [ key, cleanEmptyObject( value ) ] )
.filter( ( [ , value ] ) => Boolean( value ) )
);
return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects;
};
Expand Down

1 comment on commit a3faaef

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in a3faaef.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4630414322
📝 Reported issues:

Please sign in to comment.