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

Commit

Permalink
Replace lodash camelCase
Browse files Browse the repository at this point in the history
Replace lodash mapKeys with function

Move mapkeys to utility

Create camelCaseKeys which replaces usage of mapKeys
  • Loading branch information
mikejolley committed Apr 21, 2023
1 parent 2e91696 commit f466acc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
19 changes: 19 additions & 0 deletions assets/js/base/utils/camel-case-keys.ts
Original file line number Diff line number Diff line change
@@ -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 ) );
3 changes: 3 additions & 0 deletions assets/js/base/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
6 changes: 2 additions & 4 deletions assets/js/data/cart/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 6 additions & 7 deletions assets/js/data/cart/test/notify-quantity-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 };
};
Expand Down
6 changes: 2 additions & 4 deletions assets/js/data/cart/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ApiErrorResponse,
isApiErrorResponse,
} from '@woocommerce/types';
import { camelCase, mapKeys } from 'lodash';
import { camelCaseKeys } from '@woocommerce/base-utils';

/**
* Internal dependencies
Expand All @@ -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( {
Expand Down
6 changes: 2 additions & 4 deletions assets/js/data/cart/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/**
* 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
*/
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 = () => {
Expand Down

0 comments on commit f466acc

Please sign in to comment.