Skip to content

Commit

Permalink
Lodash: Refactor away from _.mapKeys() (#43258)
Browse files Browse the repository at this point in the history
* Lodash: Refactor away from _.mapKeys()

* Add changelog to @wordpress/components package
  • Loading branch information
tyxla authored Aug 16, 2022
1 parent cedbd43 commit 44b9b59
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 33 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ module.exports = {
'keyBy',
'keys',
'lowerCase',
'mapKeys',
'maxBy',
'memoize',
'negate',
Expand Down
8 changes: 6 additions & 2 deletions packages/block-directory/src/store/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { camelCase } from 'change-case';
import { mapKeys } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -27,7 +26,12 @@ export const getDownloadableBlocks =
path: `wp/v2/block-directory/search?term=${ filterValue }`,
} );
const blocks = results.map( ( result ) =>
mapKeys( result, ( value, key ) => camelCase( key ) )
Object.fromEntries(
Object.entries( result ).map( ( [ key, value ] ) => [
camelCase( key ),
value,
] )
)
);

dispatch( receiveDownloadableBlocks( blocks, filterValue ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { mapKeys } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -35,7 +30,12 @@ const interactiveContentTags = new Set( [

function prefixSelectKeys( selected, prefix ) {
if ( typeof selected !== 'object' ) return { [ prefix ]: selected };
return mapKeys( selected, ( value, key ) => `${ prefix }.${ key }` );
return Object.fromEntries(
Object.entries( selected ).map( ( [ key, value ] ) => [
`${ prefix }.${ key }`,
value,
] )
);
}

function getPrefixedSelectKeys( selected, prefix ) {
Expand Down
9 changes: 5 additions & 4 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
some,
find,
filter,
mapKeys,
orderBy,
} from 'lodash';
import createSelector from 'rememo';
Expand Down Expand Up @@ -2078,9 +2077,11 @@ export const getBlockTransformItems = createSelector(
)
.map( buildBlockTypeTransformItem );

const itemsByName = mapKeys(
blockTypeTransformItems,
( { name } ) => name
const itemsByName = Object.fromEntries(
Object.entries( blockTypeTransformItems ).map( ( [ , value ] ) => [
value.name,
value,
] )
);

// Consider unwraping the highest priority.
Expand Down
16 changes: 9 additions & 7 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* External dependencies
*/
import { camelCase } from 'change-case';
import { isEmpty, mapKeys, pick, pickBy } from 'lodash';
import { isEmpty, pick, pickBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -172,12 +172,14 @@ export function unstable__bootstrapServerSideBlockDefinitions( definitions ) {
}
continue;
}
serverSideBlockDefinitions[ blockName ] = mapKeys(
pickBy(
definitions[ blockName ],
( value ) => value !== null && value !== undefined
),
( value, key ) => camelCase( key )

serverSideBlockDefinitions[ blockName ] = Object.fromEntries(
Object.entries(
pickBy(
definitions[ blockName ],
( value ) => value !== null && value !== undefined
)
).map( ( [ key, value ] ) => [ camelCase( key ), value ] )
);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- `DateTimePicker`, `TimePicker`, `DatePicker`: Switch from `moment` to `date-fns` ([#43005](https://github.com/WordPress/gutenberg/pull/43005)).
- `DatePicker`: Switch from `react-dates` to `use-lilius` ([#43005](https://github.com/WordPress/gutenberg/pull/43005)).
- `convertLTRToRTL()`: Refactor away from `_.mapKeys()` ([#43258](https://github.com/WordPress/gutenberg/pull/43258/)).

## 19.17.0 (2022-08-10)

Expand Down
8 changes: 6 additions & 2 deletions packages/components/src/utils/rtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { css } from '@emotion/react';
import { mapKeys } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -57,7 +56,12 @@ function getConvertedKey( key ) {
* @return {import('react').CSSProperties} Converted ltr -> rtl styles
*/
export const convertLTRToRTL = ( ltrStyles = {} ) => {
return mapKeys( ltrStyles, ( _value, key ) => getConvertedKey( key ) );
return Object.fromEntries(
Object.entries( ltrStyles ).map( ( [ key, value ] ) => [
getConvertedKey( key ),
value,
] )
);
};

/**
Expand Down
26 changes: 14 additions & 12 deletions packages/editor/src/hooks/custom-sources-backwards-compatibility.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { pickBy, mapValues, isEmpty, mapKeys } from 'lodash';
import { pickBy, mapValues, isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -69,17 +69,19 @@ const createWithMetaAttributeSource = ( metaAttributes ) =>
<BlockEdit
attributes={ mergedAttributes }
setAttributes={ ( nextAttributes ) => {
const nextMeta = mapKeys(
// Filter to intersection of keys between the updated
// attributes and those with an associated meta key.
pickBy(
nextAttributes,
( value, key ) => metaAttributes[ key ]
),

// Rename the keys to the expected meta key name.
( value, attributeKey ) =>
metaAttributes[ attributeKey ]
const nextMeta = Object.fromEntries(
Object.entries(
// Filter to intersection of keys between the updated
// attributes and those with an associated meta key.
pickBy(
nextAttributes,
( value, key ) => metaAttributes[ key ]
)
).map( ( [ attributeKey, value ] ) => [
// Rename the keys to the expected meta key name.
metaAttributes[ attributeKey ],
value,
] )
);

if ( ! isEmpty( nextMeta ) ) {
Expand Down

0 comments on commit 44b9b59

Please sign in to comment.