clamp

Returns the receiver limited to an inclusive [min, max] range.

import { Arith } from "@teakit/arith";

Arith.from("1.08").clamp("0", "1").toString(); // "1"
Arith.from("0.42").clamp("0", "1").toString(); // "0.42"
Arith.from("-0.1").clamp("0", "1").toString(); // "0"

// Bounds can be Arith values as well as strings or numbers.
const min = Arith.from("10");
const max = Arith.from("20");
Arith.from("25").clamp(min, max).toString(); // "20"

// Use the static helper when no method chain is needed.
Arith.clamp("1.08", "0", "1").toString(); // "1"

API Reference

Signature

clamp(min: ArithValue, max: ArithValue): ArithInstance;

Parameters

ParameterTypeRequiredNotes
minArithValueYesInclusive lower bound.
maxArithValueYesInclusive upper bound.

Returns

Returns a new Arith instance: min if the receiver is below range, max if above range, otherwise a copy of the receiver.

Throws

  • Throws if min is greater than max.
  • Throws if an input value is invalid while STRICT is true.

Agent Contract

FieldValue
Kindinstance method and static helper
Canonical nameclamp
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesSTRICT
Related methodsmin, max, cmp

Agent Notes

  • Prefer clamp() over manual lt/gt branching when returning a bounded Arith value.
  • Do not use JavaScript Math.min or Math.max on Arith values.
  • Treat Arith instances as immutable.