@monstermann/fn
Version:
A utility library for TypeScript.
26 lines (24 loc) • 492 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/number/clamp.ts
/**
* `clamp(value, min, max)`
*
* Constrains `value` to be between `min` and `max` (inclusive).
*
* ```ts
* clamp(10, 0, 5); // 5
* clamp(-2, 0, 5); // 0
* clamp(3, 0, 5); // 3
* ```
*
* ```ts
* pipe(10, clamp(0, 5)); // 5
* pipe(-2, clamp(0, 5)); // 0
* pipe(3, clamp(0, 5)); // 3
* ```
*/
const clamp = dfdlT((value, min, max) => {
return Math.min(Math.max(value, min), max);
}, 3);
//#endregion
export { clamp };