-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { add, subtract, roundClamp } from '../math'; | ||
import { add, clamp, subtract, roundClamp } from '../math'; | ||
|
||
describe( 'add', () => { | ||
it( 'should add string and number values', () => { | ||
|
@@ -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 commentThe 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 commentThe 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. |
||
expect( clamp( 10, 1, 10 ) ).toBe( 10 ); | ||
expect( clamp( 1, 1, 10 ) ).toBe( 1 ); | ||
expect( clamp( 0, 1, 10 ) ).toBe( 1 ); | ||
|
||
expect( clamp( 50, 1, 10 ) ).toBe( 10 ); | ||
expect( clamp( 50, -10, 10 ) ).toBe( 10 ); | ||
expect( clamp( -50, -10, 10 ) ).toBe( -10 ); | ||
|
||
expect( clamp( Infinity, -10, 10 ) ).toBe( 10 ); | ||
expect( clamp( -Infinity, -10, 10 ) ).toBe( -10 ); | ||
} ); | ||
|
||
it( 'should clamp number or string values', () => { | ||
expect( clamp( '50', 1, 10 ) ).toBe( 10 ); | ||
expect( clamp( '50', -10, 10 ) ).toBe( 10 ); | ||
expect( clamp( -50, -10, '10' ) ).toBe( -10 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'roundClamp', () => { | ||
it( 'should clamp a value between min and max', () => { | ||
expect( roundClamp( 10, 1, 10 ) ).toBe( 10 ); | ||
|
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.