From 380fedbbd7cf60d65ff3fef77811db426b94c8b8 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 20 Apr 2023 12:43:27 +0100 Subject: [PATCH] Remove lodash `difference` --- assets/js/blocks/attribute-filter/block.tsx | 11 ++++++++--- assets/js/blocks/rating-filter/block.tsx | 11 ++++++++--- assets/js/blocks/stock-filter/block.tsx | 9 ++++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/assets/js/blocks/attribute-filter/block.tsx b/assets/js/blocks/attribute-filter/block.tsx index b3e72e472f8..a29cb2ad40d 100644 --- a/assets/js/blocks/attribute-filter/block.tsx +++ b/assets/js/blocks/attribute-filter/block.tsx @@ -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'; @@ -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 ] ); } diff --git a/assets/js/blocks/rating-filter/block.tsx b/assets/js/blocks/rating-filter/block.tsx index 64c8559504a..d006db19116 100644 --- a/assets/js/blocks/rating-filter/block.tsx +++ b/assets/js/blocks/rating-filter/block.tsx @@ -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'; /** @@ -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 ] ); } diff --git a/assets/js/blocks/stock-filter/block.tsx b/assets/js/blocks/stock-filter/block.tsx index 12e4dd6433c..430cd511de2 100644 --- a/assets/js/blocks/stock-filter/block.tsx +++ b/assets/js/blocks/stock-filter/block.tsx @@ -37,7 +37,6 @@ import { PREFIX_QUERY_ARG_FILTER_TYPE, normalizeQueryParams, } from '@woocommerce/utils'; -import { difference } from 'lodash'; import classnames from 'classnames'; /** @@ -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 ] ); }