merged utils.ts
This commit is contained in:
parent
79ad930ba3
commit
a6a00066cc
|
|
@ -1,45 +0,0 @@
|
||||||
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 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 = -Number.MAX_VALUE, max: number = Number.MAX_VALUE): number {
|
|
||||||
if (isNaN(value)) {
|
|
||||||
if (CONSTANTS.isDevBranch) throw new Error("NaN passed into clampNumber()");
|
|
||||||
return min;
|
|
||||||
}
|
|
||||||
return Math.max(Math.min(value, max), min);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clamps the value to an integer within a lower and an upper bound
|
|
||||||
* @param {number} value Value to clamp
|
|
||||||
* @param {number} min Lower bound, defaults to negative Number.MAX_SAFE_INTEGER
|
|
||||||
* @param {number} max Upper bound, defaults to Number.MAX_SAFE_INTEGER
|
|
||||||
* @returns {number} Clamped integer value
|
|
||||||
*/
|
|
||||||
export function clampInteger(
|
|
||||||
value: number,
|
|
||||||
min: number = -Number.MAX_SAFE_INTEGER,
|
|
||||||
max: number = Number.MAX_SAFE_INTEGER,
|
|
||||||
): number {
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks that a variable is a valid number. A valid number
|
|
||||||
* must be a "number" type and cannot be NaN
|
|
||||||
* @param {number} n The number to check
|
|
||||||
* @returns {boolean} True if n is a valid number, false otherwise
|
|
||||||
*/
|
|
||||||
export function isValidNumber(n: number): boolean {
|
|
||||||
return typeof n === "number" && !isNaN(n);
|
|
||||||
}
|
|
||||||
|
|
@ -72,3 +72,47 @@ export const utilityExtractFilename = (filePath: string): { fileStem: string; fi
|
||||||
|
|
||||||
return { fileStem: fileStem, fileExtension: fileExtension };
|
return { fileStem: fileStem, fileExtension: fileExtension };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clamps the value on a lower and an upper bound
|
||||||
|
* @param {number} value Value to clamp
|
||||||
|
* @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 = -Number.MAX_VALUE, max: number = Number.MAX_VALUE): number {
|
||||||
|
if (isNaN(value)) {
|
||||||
|
// if (CONSTANTS.isDevBranch) throw new Error("NaN passed into clampNumber()");
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
return Math.max(Math.min(value, max), min);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clamps the value to an integer within a lower and an upper bound
|
||||||
|
* @param {number} value Value to clamp
|
||||||
|
* @param {number} min Lower bound, defaults to negative Number.MAX_SAFE_INTEGER
|
||||||
|
* @param {number} max Upper bound, defaults to Number.MAX_SAFE_INTEGER
|
||||||
|
* @returns {number} Clamped integer value
|
||||||
|
*/
|
||||||
|
export function clampInteger(
|
||||||
|
value: number,
|
||||||
|
min: number = -Number.MAX_SAFE_INTEGER,
|
||||||
|
max: number = Number.MAX_SAFE_INTEGER,
|
||||||
|
): number {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that a variable is a valid number. A valid number
|
||||||
|
* must be a "number" type and cannot be NaN
|
||||||
|
* @param {number} n The number to check
|
||||||
|
* @returns {boolean} True if n is a valid number, false otherwise
|
||||||
|
*/
|
||||||
|
export function isValidNumber(n: number): boolean {
|
||||||
|
return typeof n === "number" && !isNaN(n);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue