Skip to content

clamp

Limit the range of a variable number

141 bytes

Usage

The clamp function restricts a number to be within a specified range.

  • It takes three arguments: the number to clamp, the minimum value, and the maximum value.
  • If the number is less than the minimum, it returns the minimum.
  • If the number is greater than the maximum, it returns the maximum.
  • Otherwise, it returns the number itself.
import * as
import _
_
from 'radashi'
const
const inRange: number
inRange
=
import _
_
.
function clamp(n: number, min: number | null | undefined, max: number | null | undefined): number
export clamp

The clamp function restricts a number to be within a specified range.

  • It takes three arguments: the number to clamp, the minimum value, and the maximum value.
  • If the number is less than the minimum, it returns the minimum.
  • If the number is greater than the maximum, it returns the maximum.
  • Otherwise, it returns the number itself.

@seehttps://radashi.js.org/reference/number/clamp

@example

clamp(5, 1, 10) // returns 5
clamp(0, 1, 10) // returns 1
clamp(15, 1, 10) // returns 10

@version12.2.0

clamp
(5, 1, 10) // 5
const
const smaller: number
smaller
=
import _
_
.
function clamp(n: number, min: number | null | undefined, max: number | null | undefined): number
export clamp

The clamp function restricts a number to be within a specified range.

  • It takes three arguments: the number to clamp, the minimum value, and the maximum value.
  • If the number is less than the minimum, it returns the minimum.
  • If the number is greater than the maximum, it returns the maximum.
  • Otherwise, it returns the number itself.

@seehttps://radashi.js.org/reference/number/clamp

@example

clamp(5, 1, 10) // returns 5
clamp(0, 1, 10) // returns 1
clamp(15, 1, 10) // returns 10

@version12.2.0

clamp
(0, 1, 10) // 1
const
const bigger: number
bigger
=
import _
_
.
function clamp(n: number, min: number | null | undefined, max: number | null | undefined): number
export clamp

The clamp function restricts a number to be within a specified range.

  • It takes three arguments: the number to clamp, the minimum value, and the maximum value.
  • If the number is less than the minimum, it returns the minimum.
  • If the number is greater than the maximum, it returns the maximum.
  • Otherwise, it returns the number itself.

@seehttps://radashi.js.org/reference/number/clamp

@example

clamp(5, 1, 10) // returns 5
clamp(0, 1, 10) // returns 1
clamp(15, 1, 10) // returns 10

@version12.2.0

clamp
(15, 1, 10) // 10

Invalid ranges

If the minimum is greater than the maximum, an error is thrown.

const
const clamped: number
clamped
=
import _
_
.
function clamp(n: number, min: number | null | undefined, max: number | null | undefined): number
export clamp

The clamp function restricts a number to be within a specified range.

  • It takes three arguments: the number to clamp, the minimum value, and the maximum value.
  • If the number is less than the minimum, it returns the minimum.
  • If the number is greater than the maximum, it returns the maximum.
  • Otherwise, it returns the number itself.

@seehttps://radashi.js.org/reference/number/clamp

@example

clamp(5, 1, 10) // returns 5
clamp(0, 1, 10) // returns 1
clamp(15, 1, 10) // returns 10

@version12.2.0

clamp
(1, 10, 1) // throws Error