Hyperlinkv0.8.0-beta.28

Order

Order.clampconsteffect/Order.ts:873
<A>(O: Order<A>): {
  (options: { minimum: A; maximum: A }): (self: A) => A
  (self: A, options: { minimum: A; maximum: A }): A
}

Restricts a value between a minimum and a maximum according to the given order.

When to use

Use when you need to clamp a value to an inclusive range according to an Order.

Details

Returns the value itself when it is between minimum and maximum, inclusive. Values below the range return minimum, and values above the range return maximum. The minimum must be less than or equal to the maximum according to the order.

Example (Clamping values)

import { Order } from "effect"

const clamp = Order.clamp(Order.Number)({ minimum: 1, maximum: 5 })

console.log(clamp(3)) // 3
console.log(clamp(0)) // 1
console.log(clamp(6)) // 5
comparisonsminmaxisBetween
Source effect/Order.ts:87317 lines
export const clamp = <A>(O: Order<A>): {
  (options: {
    minimum: A
    maximum: A
  }): (self: A) => A
  (self: A, options: {
    minimum: A
    maximum: A
  }): A
} =>
  dual(
    2,
    (self: A, options: {
      minimum: A
      maximum: A
    }): A => min(O)(options.maximum, max(O)(options.minimum, self))
  )
Referenced by 4 symbols