-
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 _.clamp()
#41735
Conversation
Size Change: +184 B (0%) 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.
Thank you for your work, @tyxla !
Code changes look good overall, I only left a couple of comments
* @param {number} value The value. | ||
* @param {number} min The minimum range. | ||
* @param {number} max The maximum range. |
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.
These 3 parameters are marked as non-optional, but they are assigned a default value in the function's signature — wouldn't this kind-of make them optional?
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.
Good point 👍 I've actually gone and removed the default values. They actually make the function pretty meaningless if you ask me. If we want to clamp something, we better provide the right boundaries.
* | ||
* @return {number} The clamped value. | ||
*/ | ||
export function clamp( value = 0, min = -Infinity, max = Infinity ) { |
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.
Looking at types for lodash
's clamp
, the min
argument can be omitted (the function seems to have 2 different signatures, one with 3 arguments and one with 2 arguments).
Although it looks like throughout the repo we always use the version with 3 arguments, and so it's probably ok go forward with these changes
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.
I actually intentionally didn't replicate this behavior, because I find it ambiguous. Actually, usages like this are one of the primary reasons for my desire to remove any Lodash 😉 It tolerates so much, but that makes the code so ambiguous and error-prone. And we don't use that here, so what better reason to keep it simple.
@@ -37,6 +37,27 @@ describe( 'subtract', () => { | |||
} ); | |||
} ); | |||
|
|||
describe( 'clamp', () => { | |||
it( 'should clamp a value between min and max', () => { |
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.
Since all tree arguments are optional/have default fallback values, should we add tests for when some/all arguments are not provided?
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.
Good point, but that's no longer necessary since I removed the default values as I mentioned above.
Thanks a bunch for the feedback @ciampo! I think this is ready for another 👀 |
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 looks good to me and is testing well!
What?
Lodash's
clamp
is used only a few times in the entire codebase, and it's only in the components package. 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
clamp
is straightforward in favor of a simple custom implementation that does aMath.max()
andMax.min()
. Since we already have aroundClamp()
utility function in the package, it made sense to abstract theclamp()
part into its own. We're adding some tests as well. We're not exporting the utility function outside of the package intentionally, but that can easily be done if necessary.Testing Instructions
npm run test-unit packages/components