Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Refactor away from a few similar _.pickBy() #45312

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* External dependencies
*/
import { pickBy, isEmpty, mapValues, get, setWith, clone } from 'lodash';
import { isEmpty, mapValues, get, setWith, clone } from 'lodash';

/**
* WordPress dependencies
*/
import { getBlockSupport } from '@wordpress/blocks';

const identity = ( x ) => x;

/**
* Removed falsy values from nested object.
*
Expand All @@ -24,9 +22,10 @@ export const cleanEmptyObject = ( object ) => {
) {
return object;
}
const cleanedNestedObjects = pickBy(
mapValues( object, cleanEmptyObject ),
identity
const cleanedNestedObjects = Object.fromEntries(
Object.entries( mapValues( object, cleanEmptyObject ) ).filter(
( [ , value ] ) => Boolean( value )
)
);
return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects;
};
Expand Down
11 changes: 5 additions & 6 deletions packages/block-library/src/utils/clean-empty-object.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* External dependencies
*/
import { isEmpty, mapValues, pickBy } from 'lodash';

const identity = ( x ) => x;
import { isEmpty, mapValues } from 'lodash';

/**
* Removed empty nodes from nested objects.
Expand All @@ -19,9 +17,10 @@ const cleanEmptyObject = ( object ) => {
) {
return object;
}
const cleanedNestedObjects = pickBy(
mapValues( object, cleanEmptyObject ),
identity
const cleanedNestedObjects = Object.fromEntries(
Object.entries( mapValues( object, cleanEmptyObject ) ).filter(
( [ , value ] ) => Boolean( value )
)
);
return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { mergeWith, pickBy, isEmpty, mapValues } from 'lodash';
import { mergeWith, isEmpty, mapValues } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -15,8 +15,6 @@ import { store as coreStore } from '@wordpress/core-data';
*/
import { GlobalStylesContext } from './context';

const identity = ( x ) => x;

function mergeTreesCustomizer( _, srcValue ) {
// We only pass as arrays the presets,
// in which case we want the new array of values
Expand All @@ -38,9 +36,10 @@ const cleanEmptyObject = ( object ) => {
) {
return object;
}
const cleanedNestedObjects = pickBy(
mapValues( object, cleanEmptyObject ),
identity
const cleanedNestedObjects = Object.fromEntries(
Object.entries( mapValues( object, cleanEmptyObject ) ).filter(
( [ , value ] ) => Boolean( value )
)
);
return isEmpty( cleanedNestedObjects ) ? undefined : cleanedNestedObjects;
};
Expand Down