diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/number.ts | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/util/number.ts b/util/number.ts index c28864f..98b7e50 100644 --- a/util/number.ts +++ b/util/number.ts @@ -9,3 +9,15 @@ Number.prototype.toChar = function() { return String.fromCharCode(this as number); } +/** @summary get minimum of valid numbers, returns NaN when no valid values are entered */ +export function min(...values: Array<number | null | undefined>) { + values.push(NaN); // make sure .reduce doesn't crash + return (values.filter(v => typeof v === "number") as Array<number>)!.reduce((acc, v) => acc = v < acc ? v : acc); +} + +/** @summary get maximum of valid numbers, returns NaN when no valid values are entered */ +export function max(...values: Array<number | null | undefined>) { + values.push(NaN); // make sure .reduce doesn't crash + return (values.filter(v => typeof v === "number") as Array<number>)!.reduce((acc, v) => acc = v > acc ? v : acc); +} + |