-
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.
Don't filter values from arrays (#31780)
- Loading branch information
Showing
1 changed file
with
5 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pickBy, isEqual, isObject, identity, mapValues } from 'lodash'; | ||
import { pickBy, isEmpty, isObject, identity, mapValues } from 'lodash'; | ||
|
||
/** | ||
* Removed undefined values from nested object. | ||
* Removed falsy values from nested object. | ||
* | ||
* @param {*} object | ||
* @return {*} Object cleaned from undefined values | ||
* @return {*} Object cleaned from falsy values | ||
*/ | ||
export const cleanEmptyObject = ( object ) => { | ||
if ( ! isObject( object ) ) { | ||
if ( ! isObject( object ) || Array.isArray( object ) ) { | ||
return object; | ||
} | ||
const cleanedNestedObjects = pickBy( | ||
mapValues( object, cleanEmptyObject ), | ||
identity | ||
); | ||
return isEqual( cleanedNestedObjects, {} ) | ||
? undefined | ||
: cleanedNestedObjects; | ||
return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects; | ||
}; |