Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Remove lodash difference
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Apr 20, 2023
1 parent ebec4bc commit 380fedb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
11 changes: 8 additions & 3 deletions assets/js/blocks/attribute-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
PREFIX_QUERY_ARG_FILTER_TYPE,
PREFIX_QUERY_ARG_QUERY_TYPE,
} from '@woocommerce/utils';
import { difference } from 'lodash';
import FormTokenField from '@woocommerce/base-components/form-token-field';
import FilterTitlePlaceholder from '@woocommerce/base-components/filter-placeholder';
import classnames from 'classnames';
Expand Down Expand Up @@ -591,13 +590,19 @@ const AttributeFilterBlock = ( {
: token;
} );

const added = difference( tokens, checked );
const added = [ tokens, checked ].reduce(
( a, b ) =>
a.filter( ( c ) => ! b.includes( c ) )
);

if ( added.length === 1 ) {
return onChange( added[ 0 ] );
}

const removed = difference( checked, tokens );
const removed = [ checked, tokens ].reduce(
( a, b ) =>
a.filter( ( c ) => ! b.includes( c ) )
);
if ( removed.length === 1 ) {
onChange( removed[ 0 ] );
}
Expand Down
11 changes: 8 additions & 3 deletions assets/js/blocks/rating-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import FormTokenField from '@woocommerce/base-components/form-token-field';
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
import { changeUrl, normalizeQueryParams } from '@woocommerce/utils';
import classnames from 'classnames';
import { difference } from 'lodash';
import type { ReactElement } from 'react';

/**
Expand Down Expand Up @@ -381,13 +380,19 @@ const RatingFilterBlock = ( {
: token;
} );

const added = difference( tokens, checked );
const added = [ tokens, checked ].reduce(
( a, b ) =>
a.filter( ( c ) => ! b.includes( c ) )
);

if ( added.length === 1 ) {
return onClick( added[ 0 ] );
}

const removed = difference( checked, tokens );
const removed = [ checked, tokens ].reduce(
( a, b ) =>
a.filter( ( c ) => ! b.includes( c ) )
);
if ( removed.length === 1 ) {
onClick( removed[ 0 ] );
}
Expand Down
9 changes: 6 additions & 3 deletions assets/js/blocks/stock-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
PREFIX_QUERY_ARG_FILTER_TYPE,
normalizeQueryParams,
} from '@woocommerce/utils';
import { difference } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -395,13 +394,17 @@ const StockStatusFilterBlock = ( {
return displayOption ? displayOption.value : token;
} );

const added = difference( tokens, checked );
const added = [ tokens, checked ].reduce( ( a, b ) =>
a.filter( ( c ) => ! b.includes( c ) )
);

if ( added.length === 1 ) {
return onChange( added[ 0 ] );
}

const removed = difference( checked, tokens );
const removed = [ checked, tokens ].reduce( ( a, b ) =>
a.filter( ( c ) => ! b.includes( c ) )
);
if ( removed.length === 1 ) {
onChange( removed[ 0 ] );
}
Expand Down

0 comments on commit 380fedb

Please sign in to comment.