From f466acc4f1985fc3b771f1d3482690e006007b0a Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 20 Apr 2023 15:44:56 +0100 Subject: [PATCH] Replace lodash camelCase Replace lodash mapKeys with function Move mapkeys to utility Create camelCaseKeys which replaces usage of mapKeys --- assets/js/base/utils/camel-case-keys.ts | 19 +++++++++++++++++++ assets/js/base/utils/index.js | 3 +++ assets/js/data/cart/actions.ts | 6 ++---- .../data/cart/test/notify-quantity-changes.ts | 13 ++++++------- assets/js/data/cart/thunks.ts | 6 ++---- assets/js/data/cart/utils.ts | 6 ++---- 6 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 assets/js/base/utils/camel-case-keys.ts diff --git a/assets/js/base/utils/camel-case-keys.ts b/assets/js/base/utils/camel-case-keys.ts new file mode 100644 index 00000000000..e2f438212c0 --- /dev/null +++ b/assets/js/base/utils/camel-case-keys.ts @@ -0,0 +1,19 @@ +/** + * External dependencies + */ +import { camelCase } from 'change-case'; + +const mapKeys = ( + obj: object, + mapper: ( value: unknown, key: string ) => string +) => + Object.entries( obj ).reduce( + ( acc, [ key, value ] ) => ( { + ...acc, + [ mapper( value, key ) ]: value, + } ), + {} + ); + +export const camelCaseKeys = ( obj: object ) => + mapKeys( obj, ( _, key ) => camelCase( key ) ); diff --git a/assets/js/base/utils/index.js b/assets/js/base/utils/index.js index 295a68e742a..9ba997d7f02 100644 --- a/assets/js/base/utils/index.js +++ b/assets/js/base/utils/index.js @@ -10,3 +10,6 @@ export * from './get-icons-from-payment-methods'; export * from './parse-style'; export * from './create-notice'; export * from './get-navigation-type'; +export * from './camel-case-keys'; +export * from './debounce'; +export * from './keyby'; diff --git a/assets/js/data/cart/actions.ts b/assets/js/data/cart/actions.ts index 393680c7cbf..646e3dfacab 100644 --- a/assets/js/data/cart/actions.ts +++ b/assets/js/data/cart/actions.ts @@ -11,11 +11,11 @@ import type { CartShippingPackageShippingRate, CartShippingRate, } from '@woocommerce/types'; -import { camelCase, mapKeys } from 'lodash'; import { BillingAddress, ShippingAddress } from '@woocommerce/settings'; import { triggerAddedToCartEvent, triggerAddingToCartEvent, + camelCaseKeys, } from '@woocommerce/base-utils'; /** @@ -70,9 +70,7 @@ export const setErrorData = ( export const receiveCartContents = ( response: CartResponse ): { type: string; response: Partial< Cart > } => { - const cart = mapKeys( response, ( _, key ) => - camelCase( key ) - ) as unknown as Cart; + const cart = camelCaseKeys( response ) as unknown as Cart; const { shippingAddress, billingAddress, ...cartWithoutAddress } = cart; return { type: types.SET_CART_DATA, diff --git a/assets/js/data/cart/test/notify-quantity-changes.ts b/assets/js/data/cart/test/notify-quantity-changes.ts index cfd8ac59135..45e0f905584 100644 --- a/assets/js/data/cart/test/notify-quantity-changes.ts +++ b/assets/js/data/cart/test/notify-quantity-changes.ts @@ -3,8 +3,9 @@ */ import { dispatch, select } from '@wordpress/data'; import { previewCart } from '@woocommerce/resource-previews'; -import { camelCase, cloneDeep, mapKeys } from 'lodash'; +import { cloneDeep } from 'lodash'; import { Cart, CartResponse } from '@woocommerce/types'; +import { camelCaseKeys } from '@woocommerce/base-utils'; /** * Internal dependencies @@ -34,13 +35,11 @@ select.mockImplementation( () => { * Clones the preview cart and turns it into a `Cart`. */ const getFreshCarts = (): { oldCart: Cart; newCart: Cart } => { - const oldCart = mapKeys( - cloneDeep< CartResponse >( previewCart ), - ( _, key ) => camelCase( key ) + const oldCart = camelCaseKeys( + cloneDeep< CartResponse >( previewCart ) ) as unknown as Cart; - const newCart = mapKeys( - cloneDeep< CartResponse >( previewCart ), - ( _, key ) => camelCase( key ) + const newCart = camelCaseKeys( + cloneDeep< CartResponse >( previewCart ) ) as unknown as Cart; return { oldCart, newCart }; }; diff --git a/assets/js/data/cart/thunks.ts b/assets/js/data/cart/thunks.ts index 5528668326b..105206cd507 100644 --- a/assets/js/data/cart/thunks.ts +++ b/assets/js/data/cart/thunks.ts @@ -7,7 +7,7 @@ import { ApiErrorResponse, isApiErrorResponse, } from '@woocommerce/types'; -import { camelCase, mapKeys } from 'lodash'; +import { camelCaseKeys } from '@woocommerce/base-utils'; /** * Internal dependencies @@ -31,9 +31,7 @@ export const receiveCart = dispatch: CartDispatchFromMap; select: CartSelectFromMap; } ) => { - const newCart = mapKeys( response, ( _, key ) => - camelCase( key ) - ) as unknown as Cart; + const newCart = camelCaseKeys( response ) as unknown as Cart; const oldCart = select.getCartData(); notifyCartErrors( newCart.errors, oldCart.errors ); notifyQuantityChanges( { diff --git a/assets/js/data/cart/utils.ts b/assets/js/data/cart/utils.ts index 19afead5670..5bc2148a23a 100644 --- a/assets/js/data/cart/utils.ts +++ b/assets/js/data/cart/utils.ts @@ -1,9 +1,9 @@ /** * External dependencies */ -import { camelCase, mapKeys } from 'lodash'; import { Cart, CartResponse } from '@woocommerce/types'; import { select } from '@wordpress/data'; +import { camelCaseKeys } from '@woocommerce/base-utils'; /** * Internal dependencies @@ -11,9 +11,7 @@ import { select } from '@wordpress/data'; import { STORE_KEY as VALIDATION_STORE_KEY } from '../validation/constants'; export const mapCartResponseToCart = ( responseCart: CartResponse ): Cart => { - return mapKeys( responseCart, ( _, key ) => - camelCase( key ) - ) as unknown as Cart; + return camelCaseKeys( responseCart ) as unknown as Cart; }; export const shippingAddressHasValidationErrors = () => {