[Feature]: common > utils > clamp error case
See original GitHub issuePackage Scope
- Add to an existing package
- New package
Package name: @toss/utils
Overview
In clamp.ts
Currently, the function is only supposed to work if bound1 is less than bound2. If user unaware of this rule and can put a larger value in bound1 than bound2. In that case, this function will not function normally. For example, if value=3, bound1=10, bound2=1, the return value must be 3, but the error results in 1.
Describe the solution you’d like
i think this code works well if you agree I will make Pull request
export function clamp(value: number, bound1: number, bound2?: number) {
if (bound2 == null) {
return Math.min(value, bound1);
}
if (bound1 > bound2) {
return Math.min(Math.max(value, bound2), bound1);
}
return Math.min(Math.max(value, bound1), bound2);
}
Additional context
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
[ktx] build failure · Issue #24496 · microsoft/vcpkg - GitHub
Host Environment OS: Linux Compiler: revision To Reproduce I'm using the vcpkg.json: { "name": "colitaire", "version-string": "0.0.1", ...
Read more >mathutil - Go Packages
Package mathutil provides utilities supplementing the standard 'math' and 'math/rand' packages. Release history and compatibility issues ¶.
Read more >Function clamp - D Programming Language
Function std.algorithm.comparison.clamp. Clamps val into the given bounds. Result has the same type as val . T1 clamp(T1, T2, T3) ( T1 val,...
Read more >DX APM - UMA - Troubleshooting and Common issues
USE-CASE#1: UMA metrics not reporting due to EM /Agent metric clamps ... Suggestion#1: Check if EM or Agent metric clamps have been reached....
Read more >clamp() - CSS: Cascading Style Sheets - MDN Web Docs
The clamp() CSS function clamps a middle value within a range of values ... in which case the inner ones are treated as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, sure!
I guess this is in fact a bug. However, I think throwing an error early when our assumption (
bound1 <= bound2
) is broken might be a better approach, rather than returning an arbitrary value. (I think no one intends to write code likeclamp(3, 10, 1)
😄 ) What do you think?