From be07aeee1ee64eb0f0e8aefc9113aa9ddd63d2ec Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 20 Apr 2023 15:31:58 +0100 Subject: [PATCH] Replace lodash isEqual with fastDeepEqual --- .../js/base/components/product-list/product-list.tsx | 10 ++++++---- assets/js/base/context/hooks/cart/use-store-cart.ts | 4 ++-- .../pickup-location/settings-context.tsx | 9 ++++++--- package-lock.json | 8 +++++--- package.json | 1 + 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/assets/js/base/components/product-list/product-list.tsx b/assets/js/base/components/product-list/product-list.tsx index 0adcfaf9084..e1661673ed2 100644 --- a/assets/js/base/components/product-list/product-list.tsx +++ b/assets/js/base/components/product-list/product-list.tsx @@ -2,7 +2,7 @@ * External dependencies */ import { __, _n, sprintf } from '@wordpress/i18n'; -import { isEqual } from 'lodash'; +import fastDeepEqual from 'fast-deep-equal/es6'; import classnames from 'classnames'; import Pagination from '@woocommerce/base-components/pagination'; import { useEffect } from '@wordpress/element'; @@ -113,7 +113,9 @@ const announceLoadingCompletion = ( totalProducts: number ): void => { const areQueryTotalsDifferent: AreQueryTotalsDifferent = ( { totalQuery: nextQuery, totalProducts: nextProducts }, { totalQuery: currentQuery } = {} -) => ! isEqual( nextQuery, currentQuery ) && Number.isFinite( nextProducts ); +) => + ! fastDeepEqual( nextQuery, currentQuery ) && + Number.isFinite( nextProducts ); const ProductList = ( { attributes, @@ -169,7 +171,7 @@ const ProductList = ( { // If query state (excluding pagination/sorting attributes) changed, reset pagination to the first page. useEffect( () => { - if ( isEqual( totalQuery, previousQueryTotals?.totalQuery ) ) { + if ( fastDeepEqual( totalQuery, previousQueryTotals?.totalQuery ) ) { return; } onPageChange( 1 ); @@ -210,7 +212,7 @@ const ProductList = ( { const totalPages = ! Number.isFinite( totalProducts ) && Number.isFinite( previousQueryTotals?.totalProducts ) && - isEqual( totalQuery, previousQueryTotals?.totalQuery ) + fastDeepEqual( totalQuery, previousQueryTotals?.totalQuery ) ? Math.ceil( ( previousQueryTotals?.totalProducts || 0 ) / perPage ) : Math.ceil( totalProducts / perPage ); const listProducts = products.length diff --git a/assets/js/base/context/hooks/cart/use-store-cart.ts b/assets/js/base/context/hooks/cart/use-store-cart.ts index 9b8f223f988..135de0ddc3b 100644 --- a/assets/js/base/context/hooks/cart/use-store-cart.ts +++ b/assets/js/base/context/hooks/cart/use-store-cart.ts @@ -3,7 +3,7 @@ /** * External dependencies */ -import { isEqual } from 'lodash'; +import fastDeepEqual from 'fast-deep-equal/es6'; import { useRef } from '@wordpress/element'; import { CART_STORE_KEY as storeKey, @@ -247,7 +247,7 @@ export const useStoreCart = ( if ( ! currentResults.current || - ! isEqual( currentResults.current, results ) + ! fastDeepEqual( currentResults.current, results ) ) { currentResults.current = results; } diff --git a/assets/js/extensions/shipping-methods/pickup-location/settings-context.tsx b/assets/js/extensions/shipping-methods/pickup-location/settings-context.tsx index b4fb8dce90f..f865ef437e7 100644 --- a/assets/js/extensions/shipping-methods/pickup-location/settings-context.tsx +++ b/assets/js/extensions/shipping-methods/pickup-location/settings-context.tsx @@ -12,7 +12,7 @@ import type { UniqueIdentifier } from '@dnd-kit/core'; import apiFetch from '@wordpress/api-fetch'; import { dispatch } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; -import { isEqual } from 'lodash'; +import fastDeepEqual from 'fast-deep-equal/es6'; /** * Internal dependencies @@ -139,11 +139,14 @@ export const SettingsProvider = ( { } ).then( ( response ) => { setIsSaving( false ); if ( - isEqual( + fastDeepEqual( response.pickup_location_settings, data.pickup_location_settings ) && - isEqual( response.pickup_locations, data.pickup_locations ) + fastDeepEqual( + response.pickup_locations, + data.pickup_locations + ) ) { dispatch( 'core/notices' ).createSuccessNotice( __( diff --git a/package-lock.json b/package-lock.json index 3defc326f69..763943f4dc5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,7 @@ "dinero.js": "1.9.1", "dompurify": "^2.4.0", "downshift": "6.1.7", + "fast-deep-equal": "^3.1.3", "html-react-parser": "3.0.4", "postcode-validator": "3.7.0", "preact": "^10.11.3", @@ -26955,8 +26956,8 @@ }, "node_modules/fast-deep-equal": { "version": "3.1.3", - "dev": true, - "license": "MIT" + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" }, "node_modules/fast-diff": { "version": "1.2.0", @@ -69401,7 +69402,8 @@ }, "fast-deep-equal": { "version": "3.1.3", - "dev": true + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" }, "fast-diff": { "version": "1.2.0", diff --git a/package.json b/package.json index 080a415a949..307f466b7a2 100644 --- a/package.json +++ b/package.json @@ -257,6 +257,7 @@ "dinero.js": "1.9.1", "dompurify": "^2.4.0", "downshift": "6.1.7", + "fast-deep-equal": "^3.1.3", "html-react-parser": "3.0.4", "postcode-validator": "3.7.0", "preact": "^10.11.3",