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 _.difference() #43224

Merged
merged 2 commits into from
Aug 16, 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 @@ -89,6 +89,7 @@ module.exports = {
'defaults',
'defaultTo',
'delay',
'difference',
'differenceWith',
'dropRight',
'each',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { difference } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -126,9 +121,8 @@ export default function useBlockSelection() {
return;
}

const selectionDiff = difference(
selectedBlocks,
updatedSelectedBlocks
const selectionDiff = selectedBlocks.filter(
( blockId ) => ! updatedSelectedBlocks.includes( blockId )
);

let label;
Expand Down
7 changes: 5 additions & 2 deletions packages/blocks/src/api/parser/fix-custom-classname.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { difference, omit } from 'lodash';
import { omit } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -58,7 +58,10 @@ export function fixCustomClassname( blockAttributes, blockType, innerHTML ) {
const serialized = getSaveContent( blockType, attributesSansClassName );
const defaultClasses = getHTMLRootElementClasses( serialized );
const actualClasses = getHTMLRootElementClasses( innerHTML );
const customClasses = difference( actualClasses, defaultClasses );

const customClasses = actualClasses.filter(
( className ) => ! defaultClasses.includes( className )
);

if ( customClasses.length ) {
blockAttributes.className = customClasses.join( ' ' );
Expand Down
9 changes: 3 additions & 6 deletions packages/blocks/src/api/raw-handling/is-inline-content.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { difference } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -33,7 +28,9 @@ function isInline( node, contextTag ) {
];

return inlineAllowedTagGroups.some(
( tagGroup ) => difference( [ tag, contextTag ], tagGroup ).length === 0
( tagGroup ) =>
[ tag, contextTag ].filter( ( t ) => ! tagGroup.includes( t ) )
.length === 0
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements

- `ComboboxControl`: Normalize hyphen-like characters to an ASCII hyphen ([#42942](https://github.com/WordPress/gutenberg/pull/42942)).
- `FormTokenField`: Refactor away from `_.difference()` ([#43224](https://github.com/WordPress/gutenberg/pull/43224/)).

## 19.17.0 (2022-08-10)

Expand Down
6 changes: 4 additions & 2 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { last, clone, uniq, map, difference, some } from 'lodash';
import { last, clone, uniq, map, some } from 'lodash';
import classnames from 'classnames';
import type { KeyboardEvent, MouseEvent, TouchEvent } from 'react';

Expand Down Expand Up @@ -475,7 +475,9 @@ export function FormTokenField( props: FormTokenFieldProps ) {
} );

if ( match.length === 0 ) {
_suggestions = difference( _suggestions, normalizedValue );
_suggestions = _suggestions.filter(
( suggestion ) => ! normalizedValue.includes( suggestion )
);
} else {
match = match.toLocaleLowerCase();

Expand Down
18 changes: 9 additions & 9 deletions packages/edit-navigation/src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { difference } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -128,10 +123,15 @@ const batchSaveMenuItems =
);

// Delete menu items.
const deletedIds = difference(
oldMenuItems.map( ( { id } ) => id ),
blocksTreeToList( navBlockAfterUpdates ).map( getRecordIdFromBlock )
);
const deletedIds = oldMenuItems
.map( ( { id } ) => id )
.filter(
( id ) =>
! blocksTreeToList( navBlockAfterUpdates )
.map( getRecordIdFromBlock )
.includes( id )
);

await dispatch( batchDeleteMenuItems( deletedIds ) );

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

/**
* WordPress dependencies
*/
Expand All @@ -11,7 +6,7 @@ import { MenuItem } from '@wordpress/components';
import { compose } from '@wordpress/compose';

const isEverySelectedBlockAllowed = ( selected, allowed ) =>
difference( selected, allowed ).length === 0;
selected.filter( ( id ) => ! allowed.includes( id ) ).length === 0;

/**
* Plugins may want to add an item to the menu either for every block
Expand Down