-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 _.omit()
#46674
Conversation
Size Change: +38 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
function removeEntitiesById( entities, ids ) { | ||
return Object.fromEntries( | ||
Object.entries( entities ).filter( | ||
( [ id ] ) => ! ids.some( ( itemId ) => +itemId === +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.
@tyxla, do you know if omit
also casts values internally?
Not every entity ID will be an int
. e.g., Templates and template parts use strings for IDs.
wp.data.select('core').getEntityRecords( 'postType', 'wp_template' );
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.
Yes, it does. It calls toKey
on each value:
function toKey(value) {
if (typeof value == 'string' || isSymbol(value)) {
return value;
}
var result = (value + '');
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
}
Thanks, @Mamaduka, I'll have to address your feedback and take strings into consideration there.
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.
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.
Thanks for the quick follow-up. Would adding the test case for items with string IDs make sense here?
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.
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.
This tests well for me. Manually tested on the screen where we can remove templates.
Considering the importance of the reducer, it would be nice to get one more review before merging the PR. Just in case we missed something :)
Thanks, @Mamaduka! I've also tested deleting a navigation menu entity from the Advanced settings of a Navigation block, so I'm pretty confident that this works well: |
What?
This PR removes Lodash's
_.omit()
completely and deprecates the import of that function. There are a few remaining usages in that package 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 creating an inline helper function to reduce repetition. It will omit the unnecessary IDs that are passed as an array, converting the IDs specifically to numbers to ensure that they're compared properly (like Lodash did under the hood when calling
_.omit
).I'm also updating docs that used
_.omit()
in a couple of examples and deprecating the import of_.omit()
in our ESLint config.Testing Instructions
Verify all checks are still green. The changes are covered by unit tests: