-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Improve empty elements filters in Slot implementation (#9371
) * Components: Improve empty elements filters in Slot implementation * Components: Address feedback from the review
- Loading branch information
1 parent
ade2e1e
commit 0ea95fc
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createElement } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isEmptyElement } from '../utils'; | ||
|
||
describe( 'isEmptyElement', () => { | ||
test( 'should be empty', () => { | ||
expect( isEmptyElement( undefined ) ).toBe( true ); | ||
expect( isEmptyElement( false ) ).toBe( true ); | ||
expect( isEmptyElement( '' ) ).toBe( true ); | ||
expect( isEmptyElement( new String( '' ) ) ).toBe( true ); | ||
expect( isEmptyElement( [] ) ).toBe( true ); | ||
} ); | ||
|
||
test( 'should not be empty', () => { | ||
expect( isEmptyElement( 0 ) ).toBe( false ); | ||
expect( isEmptyElement( 100 ) ).toBe( false ); | ||
expect( isEmptyElement( 'test' ) ).toBe( false ); | ||
expect( isEmptyElement( createElement( 'div' ) ) ).toBe( false ); | ||
expect( isEmptyElement( [ 'x' ] ) ).toBe( false ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
isArray, | ||
isNumber, | ||
isString, | ||
} from 'lodash'; | ||
|
||
/** | ||
* Checks if the provided WP element is empty. | ||
* | ||
* @param {*} element WP element to check. | ||
* @return {boolean} True when an element is considered empty. | ||
*/ | ||
export const isEmptyElement = ( element ) => { | ||
if ( isNumber( element ) ) { | ||
return false; | ||
} | ||
|
||
if ( isString( element ) || isArray( element ) ) { | ||
return ! element.length; | ||
} | ||
|
||
return ! element; | ||
}; |