mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-03 14:27:03 +02:00
MISC: clamping numbers (#1104)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user