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: Refactor components away from _.find() #46537

Merged
merged 2 commits into from
Dec 15, 2022
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
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
- `Dashicon`: Convert to TypeScript ([#45924](https://github.com/WordPress/gutenberg/pull/45924)).
- `PaletteEdit`: add follow up changelog for #45681 and tests [#46095](https://github.com/WordPress/gutenberg/pull/46095).
- `AlignmentMatrixControl`: Convert to TypeScript ([#46162](https://github.com/WordPress/gutenberg/pull/46162)).
- `Autocomplete`: Refactor away from `_.find()` ([#46537](https://github.com/WordPress/gutenberg/pull/46537)).
- `TabPanel`: Refactor away from `_.find()` ([#46537](https://github.com/WordPress/gutenberg/pull/46537)).
- `BottomSheetPickerCell`: Refactor away from `_.find()` for mobile ([#46537](https://github.com/WordPress/gutenberg/pull/46537)).
- Refactor global styles context away from `_.find()` for mobile ([#46537](https://github.com/WordPress/gutenberg/pull/46537)).

### Documentation

Expand Down
4 changes: 1 addition & 3 deletions packages/components/src/autocomplete/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { find } from 'lodash';
import removeAccents from 'remove-accents';

/**
Expand Down Expand Up @@ -290,8 +289,7 @@ function useAutocomplete( {
const textAfterSelection = getTextContent(
slice( record, undefined, getTextContent( record ).length )
);
const completer = find(
completers,
const completer = completers?.find(
Copy link
Member Author

Choose a reason for hiding this comment

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

Relies on the editor.Autocomplete.completers filter, so although we have completers enabled by default, it could end up being nullish. Adding optional chaining just in case.

( { triggerPrefix, allowContext } ) => {
const index = text.lastIndexOf( triggerPrefix );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { find } from 'lodash';

/**
* Internal dependencies
*/
Expand All @@ -23,7 +18,7 @@ export default function BottomSheetPickerCell( props ) {
onChangeValue( newValue );
};

const option = find( options, { value } );
const option = options.find( ( opt ) => opt.value === value );
Copy link
Member Author

Choose a reason for hiding this comment

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

options will default to an array:

const label = option ? option.label : value;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { camelCase } from 'change-case';
import { find, get } from 'lodash';
import { get } from 'lodash';
import { Dimensions } from 'react-native';

/**
Expand Down Expand Up @@ -113,9 +113,9 @@ export function getBlockColors(
}

if ( ! isCustomColor ) {
const mappedColor = find( defaultColors, {
slug: value,
} );
const mappedColor = Object.values( defaultColors ?? {} ).find(
Copy link
Member Author

Choose a reason for hiding this comment

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

This comes in through an externally exposed API, so we're properly handling objects and nullish values.

( { slug } ) => slug === value
);

if ( mappedColor ) {
blockStyles[ styleKey ] = mappedColor.color;
Expand Down Expand Up @@ -143,6 +143,7 @@ export function getBlockTypography(
const typographyStyles = {};
const customBlockStyles = blockStyleAttributes?.style?.typography || {};
const blockGlobalStyles = baseGlobalStyles?.blocks?.[ blockName ];
const parsedFontSizes = Object.values( fontSizes ?? {} );
Copy link
Member Author

Choose a reason for hiding this comment

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

This comes in through an externally exposed API, so we're properly handling objects and nullish values.

It's used twice below, which is why we've moved it here.


// Global styles.
if ( blockGlobalStyles?.typography ) {
Expand All @@ -153,9 +154,9 @@ export function getBlockTypography(
if ( parseInt( fontSize, 10 ) ) {
typographyStyles.fontSize = fontSize;
} else {
const mappedFontSize = find( fontSizes, {
slug: fontSize,
} );
const mappedFontSize = parsedFontSizes.find(
Copy link
Member Author

Choose a reason for hiding this comment

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

See above comment on L146.

( { slug } ) => slug === fontSize
);

if ( mappedFontSize ) {
typographyStyles.fontSize = mappedFontSize?.size;
Expand All @@ -169,9 +170,9 @@ export function getBlockTypography(
}

if ( blockStyleAttributes?.fontSize && baseGlobalStyles ) {
const mappedFontSize = find( fontSizes, {
slug: blockStyleAttributes?.fontSize,
} );
const mappedFontSize = parsedFontSizes.find(
Copy link
Member Author

Choose a reason for hiding this comment

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

See above comment on L146.

( { slug } ) => slug === blockStyleAttributes?.fontSize
);

if ( mappedFontSize ) {
typographyStyles.fontSize = mappedFontSize?.size;
Expand Down Expand Up @@ -212,9 +213,9 @@ export function parseStylesVariables( styles, mappedValues, customValues ) {
const path = $2.split( '--' );
const mappedPresetValue = mappedValues[ path[ 0 ] ];
if ( mappedPresetValue && mappedPresetValue.slug ) {
const matchedValue = find( mappedPresetValue.values, {
slug: path[ 1 ],
} );
const matchedValue = Object.values(
Copy link
Member Author

Choose a reason for hiding this comment

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

Will be an object:

const mappedValues = {
color: {
values: colors,
slug: 'color',
},
'font-size': {
values: fontSizes,
slug: 'size',
},
};

The ?? {} adds additional safety in the case of non-existence.

mappedPresetValue.values ?? {}
).find( ( { slug } ) => slug === path[ 1 ] );
return matchedValue?.[ mappedPresetValue.slug ];
}
return UNKNOWN_VALUE;
Expand Down Expand Up @@ -244,9 +245,9 @@ export function parseStylesVariables( styles, mappedValues, customValues ) {
if ( variable === 'var' ) {
stylesBase = stylesBase.replace( varRegex, ( _$1, $2 ) => {
if ( mappedValues?.color ) {
const matchedValue = find( mappedValues.color?.values, {
slug: $2,
} );
const matchedValue = mappedValues.color?.values?.find(
Copy link
Member Author

Choose a reason for hiding this comment

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

Same as above.

( { slug } ) => slug === $2
);
return `"${ matchedValue?.color }"`;
}
return UNKNOWN_VALUE;
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/tab-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { find } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -102,7 +101,7 @@ export function TabPanel( {
) => {
child.click();
};
const selectedTab = find( tabs, { name: selected } );
const selectedTab = tabs.find( ( { name } ) => name === selected );
Copy link
Member Author

Choose a reason for hiding this comment

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

tabs is being used as an array universally in this component already.

const selectedId = `${ instanceId }-${ selectedTab?.name ?? 'none' }`;

useEffect( () => {
Expand Down