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

Commit

Permalink
Replace lodash isEmpty with type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Apr 20, 2023
1 parent 380fedb commit a8dda6e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useAddToCartFormContext,
} from '@woocommerce/base-context';
import { useProductDataContext } from '@woocommerce/shared-context';
import { isEmpty } from 'lodash';
import { isEmpty } from '@woocommerce/types';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
import { useState, useEffect, useMemo } from '@wordpress/element';
import { useDebounce } from 'use-debounce';
import { isEmpty, sortBy } from 'lodash';
import { sortBy } from 'lodash';
import { useShallowEqual } from '@woocommerce/base-hooks';
import { objectHasProp } from '@woocommerce/types';
import { objectHasProp, isEmpty } from '@woocommerce/types';

/**
* Internal dependencies
Expand Down
3 changes: 1 addition & 2 deletions assets/js/blocks/featured-items/with-featured-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* External dependencies
*/
import type { BlockAlignment } from '@wordpress/blocks';
import { ProductResponseItem } from '@woocommerce/types';
import { ProductResponseItem, isEmpty } from '@woocommerce/types';
import { __experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles } from '@wordpress/block-editor';
import { Icon, Placeholder, Spinner } from '@wordpress/components';
import classnames from 'classnames';
import { isEmpty } from 'lodash';
import { useCallback, useState } from '@wordpress/element';
import { WP_REST_API_Category } from 'wp-types';
import { useBorderProps } from '@woocommerce/base-hooks';
Expand Down
2 changes: 1 addition & 1 deletion assets/js/editor-components/product-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import { isEmpty } from 'lodash';
import { isEmpty } from '@woocommerce/types';
import PropTypes from 'prop-types';
import {
SearchListControl,
Expand Down
8 changes: 8 additions & 0 deletions assets/js/types/type-guards/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const isEmpty = ( value: unknown ): boolean => {
return (
value === null ||
value === undefined ||
( typeof value === 'object' && Object.keys( value ).length === 0 ) ||
( typeof value === 'string' && value.trim().length === 0 )
);
};
1 change: 1 addition & 0 deletions assets/js/types/type-guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './error';
export * from './function';
export * from './null';
export * from './number';
export * from './empty';
export * from './object';
export * from './string';
export * from './attributes';
Expand Down
25 changes: 25 additions & 0 deletions assets/js/types/type-guards/test/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* External dependencies
*/
import { isEmpty } from '@woocommerce/types';

describe( 'Testing isEmpty()', () => {
it( 'Correctly handles null', () => {
expect( isEmpty( null ) ).toBe( true );
} );
it( 'Correctly handles undefined', () => {
expect( isEmpty( undefined ) ).toBe( true );
} );
it( 'Correctly handles empty objects', () => {
expect( isEmpty( {} ) ).toBe( true );
} );
it( 'Correctly handles empty arrays', () => {
expect( isEmpty( [] ) ).toBe( true );
} );
it( 'Correctly handles empty strings', () => {
expect( isEmpty( '' ) ).toBe( true );
} );
it( 'Correctly handles object with values', () => {
expect( isEmpty( { a: '1' } ) ).toBe( false );
} );
} );

0 comments on commit a8dda6e

Please sign in to comment.