aboutsummaryrefslogtreecommitdiff
path: root/util/number.ts
blob: 98b7e5051ea3bb60fc2361b7f32248c8f75ae31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
declare global {
	interface Number {
		/** @summary convert number to character by charCode */
		toChar(): string;
	}
}

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);
}