MISC: clamping numbers (#1104)

This commit is contained in:
Caldwell
2024-02-27 15:47:00 +01:00
committed by GitHub
parent 153dbfff12
commit 3d6692b292
2 changed files with 27 additions and 16 deletions
+15
View File
@@ -0,0 +1,15 @@
import { CONSTANTS } from "../../Constants";
/**
* Clamps the value on a lower and an upper bound
* @param {number} value Value to clamp
* @param {number} min Lower bound, defaults to Number.MIN_VALUE
* @param {number} max Upper bound, defaults to Number.MAX_VALUE
* @returns {number} Clamped value
*/
export function clampNumber(value: number, min = Number.MIN_VALUE, max = Number.MAX_VALUE) {
if (isNaN(value)) {
if (CONSTANTS.isDevBranch) throw new Error("NaN passed into clampNumber()");
return min;
}
return Math.max(Math.min(value, max), min);
}