mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-03 14:27:03 +02:00
BLADEBURNER: Typesafety / refactoring (#1154)
This commit is contained in:
@@ -2,14 +2,22 @@ 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} min Lower bound, defaults to negative Number.MAX_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) {
|
||||
export function clampNumber(value: number, min = -Number.MAX_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);
|
||||
}
|
||||
|
||||
export function clampInteger(value: number, min = -Number.MAX_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) {
|
||||
if (isNaN(value)) {
|
||||
if (CONSTANTS.isDevBranch) throw new Error("NaN passed into clampInteger()");
|
||||
return min;
|
||||
}
|
||||
return Math.round(Math.max(Math.min(value, max), min));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user