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 away from _.compact() #42438

Merged
merged 2 commits into from
Jul 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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module.exports = {
importNames: [
'chunk',
'clamp',
'compact',
'concat',
'countBy',
'defaults',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { Platform, findNodeHandle } from 'react-native';
import { partial, first, castArray, last, compact, every } from 'lodash';
import { partial, first, castArray, last, every } from 'lodash';
/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -202,7 +202,7 @@ const BlockActionsMenu = ( {
},
};

const options = compact( [
const options = [
wrapBlockMover && allOptions.backwardButton,
wrapBlockMover && allOptions.forwardButton,
wrapBlockSettings && allOptions.settings,
Expand All @@ -215,7 +215,7 @@ const BlockActionsMenu = ( {
canDuplicate && allOptions.duplicateButton,
isReusableBlockType && allOptions.convertToRegularBlocks,
! isLocked && allOptions.delete,
] );
].filter( Boolean );

// End early if there are no options to show.
if ( ! options.length ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { compact, map } from 'lodash';
import { map } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -37,7 +37,7 @@ const BlockSettingsMenuControlsSlot = ( { fillProps, clientIds = null } ) => {
clientIds !== null ? clientIds : getSelectedBlockClientIds();
return {
selectedBlocks: map(
compact( getBlocksByClientId( ids ) ),
getBlocksByClientId( ids ).filter( Boolean ),
( block ) => block.name
),
selectedClientIds: ids,
Expand Down
7 changes: 1 addition & 6 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { compact } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -95,7 +90,7 @@ function ListViewBranch( props ) {

const { expandedState, draggedClientIds } = useListViewContext();

const filteredBlocks = compact( blocks );
const filteredBlocks = blocks.filter( Boolean );
const blockCount = filteredBlocks.length;
let nextPosition = listPosition;

Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/columns/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { View, Dimensions } from 'react-native';
import { times, map, compact, delay } from 'lodash';
import { times, map, delay } from 'lodash';
/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -474,7 +474,7 @@ const ColumnsEdit = ( props ) => {

return {
columnCount: getBlockCount( clientId ),
isDefaultColumns: ! compact( isContentEmpty ).length,
isDefaultColumns: ! isContentEmpty.filter( Boolean ).length,
innerWidths: innerColumnsWidths,
hasParents: !! parents.length,
parentBlockAlignment: getBlockAttributes( parents[ 0 ] )?.align,
Expand Down
5 changes: 2 additions & 3 deletions packages/block-library/src/embed/embed-placeholder.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { View, Text, TouchableWithoutFeedback } from 'react-native';
import { compact } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -68,11 +67,11 @@ const EmbedPlaceholder = ( {
},
};

const options = compact( [
const options = [
cannotEmbed && errorPickerOptions.retry,
cannotEmbed && errorPickerOptions.convertToLink,
cannotEmbed && errorPickerOptions.editLink,
] );
].filter( Boolean );

function onPickerSelect( value ) {
const selectedItem = options.find( ( item ) => item.value === value );
Expand Down
9 changes: 3 additions & 6 deletions packages/block-library/src/more/save.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { compact } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -14,6 +9,8 @@ export default function save( { attributes: { customText, noTeaser } } ) {
const noTeaserTag = noTeaser ? '<!--noteaser-->' : '';

return (
<RawHTML>{ compact( [ moreTag, noTeaserTag ] ).join( '\n' ) }</RawHTML>
<RawHTML>
{ [ moreTag, noTeaserTag ].filter( Boolean ).join( '\n' ) }
</RawHTML>
);
}
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- `Divider`, `Flex`, `Spacer`: Improve documentation for the `SpaceInput` prop ([#42376](https://github.com/WordPress/gutenberg/pull/42376)).
- `Elevation`: Convert to TypeScript ([#42302](https://github.com/WordPress/gutenberg/pull/42302)).
- `ScrollLock`: Convert to TypeScript ([#42303](https://github.com/WordPress/gutenberg/pull/42303)).
- `TreeSelect`: Refactor away from `_.compact()` ([#42438](https://github.com/WordPress/gutenberg/pull/42438)).
- `MediaEdit`: Refactor away from `_.compact()` for mobile ([#42438](https://github.com/WordPress/gutenberg/pull/42438)).

## 19.15.0 (2022-07-13)

Expand Down
9 changes: 2 additions & 7 deletions packages/components/src/mobile/media-edit/index.native.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { compact } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -46,11 +41,11 @@ export class MediaEdit extends Component {
getMediaOptionsItems() {
const { pickerOptions, openReplaceMediaOptions, source } = this.props;

return compact( [
return [
source?.uri && editOption,
openReplaceMediaOptions && replaceOption,
...( pickerOptions ? pickerOptions : [] ),
] );
].filter( Boolean );
}

getDestructiveButtonIndex() {
Expand Down
9 changes: 5 additions & 4 deletions packages/components/src/tree-select/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { unescape as unescapeString, compact } from 'lodash';
import { unescape as unescapeString } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -11,7 +11,7 @@ import { useMemo } from '@wordpress/element';
* Internal dependencies
*/
import { SelectControl } from '../select-control';
import type { TreeSelectProps, Tree, SelectOptions } from './types';
import type { TreeSelectProps, Tree, SelectOptions, Truthy } from './types';

function getSelectOptions( tree: Tree[], level = 0 ): SelectOptions {
return tree.flatMap( ( treeNode ) => [
Expand Down Expand Up @@ -72,6 +72,7 @@ function getSelectOptions( tree: Tree[], level = 0 ): SelectOptions {
* }
* ```
*/

export function TreeSelect( {
label,
noOptionLabel,
Expand All @@ -81,10 +82,10 @@ export function TreeSelect( {
...props
}: TreeSelectProps ) {
const options = useMemo( () => {
return compact( [
return [
noOptionLabel && { value: '', label: noOptionLabel },
...getSelectOptions( tree ),
] );
].filter( < T, >( option: T ): option is Truthy< T > => !! option );
}, [ noOptionLabel, tree ] );

return (
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/tree-select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type { ComponentProps } from 'react';
import type SelectControl from '../select-control';
import type { SelectControlProps } from '../select-control/types';

export type Truthy< T > = T extends false | '' | 0 | null | undefined
? never
: T;

export type SelectOptions = Required<
ComponentProps< typeof SelectControl >
>[ 'options' ];
Expand Down
13 changes: 2 additions & 11 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
/**
* External dependencies
*/
import {
camelCase,
compact,
find,
get,
includes,
map,
mapKeys,
uniq,
} from 'lodash';
import { camelCase, find, get, includes, map, mapKeys, uniq } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -313,7 +304,7 @@ export const canUser =
// return the expected result in the native version. Instead, API requests
// only return the result, without including response properties like the headers.
const allowHeader = response.headers?.get( 'allow' );
const key = compact( [ action, resource, id ] ).join( '/' );
const key = [ action, resource, id ].filter( Boolean ).join( '/' );
const isAllowed = includes( allowHeader, method );
dispatch.receiveUserPermission( key, isAllowed );
};
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import createSelector from 'rememo';
import { set, map, find, get, filter, compact } from 'lodash';
import { set, map, find, get, filter } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -1115,7 +1115,7 @@ export function canUser(
resource: string,
id?: GenericRecordKey
): boolean | undefined {
const key = compact( [ action, resource, id ] ).join( '/' );
const key = [ action, resource, id ].filter( Boolean ).join( '/' );
return get( state, [ 'userPermissions', key ] );
}

Expand Down
5 changes: 2 additions & 3 deletions tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const { DefinePlugin } = require( 'webpack' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const { compact } = require( 'lodash' );
const postcss = require( 'postcss' );

/**
Expand Down Expand Up @@ -41,13 +40,13 @@ const baseConfig = {
},
mode,
module: {
rules: compact( [
rules: [
{
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
},
] ),
],
},
watchOptions: {
ignored: [
Expand Down