-
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 away from _.random()
#41634
Conversation
@@ -19,6 +14,12 @@ import { | |||
const TAG_TOKEN_SELECTOR = | |||
'.components-form-token-field__token-text span:not(.components-visually-hidden)'; | |||
|
|||
function generateRandomNumber() { |
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.
We're intentionally not parametrizing this function, since:
- It's only used for testing purposes.
- In each call, it always uses the same arguments, so we're keeping it simple.
function generateRandomNumber() { | ||
// Using `Math.random()` directly is fine in this testing context. | ||
// eslint-disable-next-line no-restricted-syntax | ||
return Math.round( 1 + Math.random() * ( Number.MAX_SAFE_INTEGER - 1 ) ); |
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.
We're definitely not generating a unique component instance here, which is why we're intentionally disabling this rule.
Since this looks like a false alarm, it could present an opportunity to improve the ESLint rule. Not anything urgent or directly related to the purpose of this PR, anyway.
Size Change: 0 B Total Size: 1.24 MB ℹ️ View Unchanged
|
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 taking on these lodash removal PRs @tyxla! ✨
This change LGTM, it appears the random
function was only used to generate random tag names to avoid collisions when creating new tags? The level of randomness appears to be reasonably safe to me in this test, and the tests are passing 👍
What?
Lodash's
random
is used only once in the entire codebase. This PR aims to remove that usage.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?
Removing
random
is straightforward in favor of a simpleMath
-based implementation.Testing Instructions
This only touches the taxonomies e2e test suite, so verify those still pass.