Util

clamp

Clamps a number into an inclusive [min, max] range.

Usage

import { clamp } from 'ngwr/utils';

clamp(140, 0, 100); // 100
clamp(-3, 0, 100);  // 0
clamp(42, 0, 100);  // 42

Why ngwr provides this

Before v7 ten components each carried their own private copy of this three-liner — sliders, knobs, croppers, windows. One shared, tested implementation beats ten identical ones, and your app code gets it for free.

// Typical: keep a drag position inside its track.
const x = clamp(pointerX - trackLeft, 0, trackWidth);

API

NameDescriptionTypeDefault
clampClamps value into the inclusive [min, max] range.(value: number, min: number, max: number) => number