-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 core data away from _.map()
#47189
Conversation
@@ -213,7 +213,7 @@ async function loadPostTypeEntities() { | |||
const postTypes = await apiFetch( { | |||
path: '/wp/v2/types?context=view', | |||
} ); | |||
return map( postTypes, ( postType, name ) => { | |||
return Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postTypes
could be null
, so adding nullish coalescing:
gutenberg/packages/core-data/src/selectors.ts
Line 519 in f558281
): EntityRecord[] | null => { |
@@ -251,7 +251,7 @@ async function loadTaxonomyEntities() { | |||
const taxonomies = await apiFetch( { | |||
path: '/wp/v2/taxonomies?context=view', | |||
} ); | |||
return map( taxonomies, ( taxonomy, name ) => { | |||
return Object.entries( taxonomies ?? {} ).map( ( [ name, taxonomy ] ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
taxonomies
could be null
, so adding nullish coalescing:
gutenberg/packages/core-data/src/selectors.ts
Line 519 in f558281
): EntityRecord[] | null => { |
@@ -230,7 +230,7 @@ const receiveQueries = compose( [ | |||
|
|||
return getMergedItemIds( | |||
state || [], | |||
map( action.items, key ), | |||
action.items.map( ( item ) => item[ key ] ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always an array:
itemIds: Array.isArray( records ) ? records : [ records ], |
action.users, | ||
( user ) => user.id | ||
), | ||
[ action.queryID ]: action.users.map( ( user ) => user.id ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always an array:
users: Array.isArray( users ) ? users : [ users ], |
@@ -147,9 +147,9 @@ export function getCurrentUser( state: State ): ET.User< 'edit' > { | |||
*/ | |||
export const getUserQueryResults = createSelector( | |||
( state: State, queryID: string ): ET.User< 'edit' >[] => { | |||
const queryResults = state.users.queries[ queryID ]; | |||
const queryResults = state.users.queries[ queryID ] ?? []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be undefined if we don't have such a query, therefore coalescing to an empty array. That handles the nullish scenario for .map()
below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as expected.
✅ Verify you're still able to fetch posts when rendering the Latest Posts block in the post editor.
✅ Verify you're still able to fetch categories when rendering the Categories List block in the post editor.
Size Change: +21 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
What?
This PR removes Lodash's
_.map()
from the core data package. There are just a few usages and conversion is pretty straightforward.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're using
Array.prototype.map()
instead, with nullish coalescing when necessary, and withObject.entries()
to convert from an object when necessary.Testing Instructions