Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Remove _.mapValues() from various blocks #49637

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: getColumnWidths will always return an object, assuming it doesn't throw.

( [ 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 ] ) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: tableSections will always be an object at this point.

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