question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature]: common > utils > clamp error case

See original GitHub issue

Package 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
raon0211commented, Oct 12, 2022

Yes, sure!

1reaction
raon0211commented, Oct 12, 2022

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 like clamp(3, 10, 1) 😄 ) What do you think?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found