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 _.merge() from getMappedColumnWidths() #48032

Merged
merged 1 commit into from
Feb 20, 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
54 changes: 54 additions & 0 deletions packages/block-library/src/columns/test/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -280,4 +285,53 @@ describe( 'getMappedColumnWidths', () => {
{ clientId: 'b', attributes: { width: '35%' } },
] );
} );

it( 'always returns new objects and does not mutate input blocks', () => {
const blocks = [
deepFreeze( { clientId: 'a', attributes: { width: 30 } } ),
deepFreeze( { clientId: 'b', attributes: { width: 40 } } ),
];
const widths = {
a: 25,
b: 35,
};

const result = getMappedColumnWidths( blocks, widths );

expect( blocks[ 0 ] ).not.toBe( result[ 0 ] );
expect( blocks[ 1 ] ).not.toBe( result[ 1 ] );
} );

it( 'merges to block attributes if original blocks do not have any attributes', () => {
const blocks = [ { clientId: 'a' }, { clientId: 'b' } ];
const widths = {
a: 25,
b: 35,
};

const result = getMappedColumnWidths( blocks, widths );

expect( result ).toEqual( [
{ clientId: 'a', attributes: { width: '25%' } },
{ clientId: 'b', attributes: { width: '35%' } },
] );
} );

it( 'merges to block attributes if original blocks do not have a width attribute', () => {
const blocks = [
{ clientId: 'a', attributes: { align: 'left' } },
{ clientId: 'b', attributes: { align: 'right' } },
];
const widths = {
a: 25,
b: 35,
};

const result = getMappedColumnWidths( blocks, widths );

expect( result ).toEqual( [
{ clientId: 'a', attributes: { align: 'left', width: '25%' } },
{ clientId: 'b', attributes: { align: 'right', width: '35%' } },
] );
} );
} );
16 changes: 8 additions & 8 deletions packages/block-library/src/columns/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { merge, mapValues } from 'lodash';
import { mapValues } from 'lodash';

/**
* Returns a column width attribute value rounded to standard precision.
Expand Down Expand Up @@ -121,13 +121,13 @@ export function hasExplicitPercentColumnWidths( blocks ) {
* @return {WPBlock[]} blocks Mapped block objects.
*/
export function getMappedColumnWidths( blocks, widths ) {
return blocks.map( ( block ) =>
merge( {}, block, {
attributes: {
width: `${ widths[ block.clientId ] }%`,
},
} )
);
return blocks.map( ( block ) => ( {
...block,
attributes: {
...block.attributes,
width: `${ widths[ block.clientId ] }%`,
},
} ) );
}

/**
Expand Down