799 bytes · the TypeScript implementation · view raw
/** * [min, max] of a series, skipping gaps. * * `Math.min(...values)` is the usual one-liner and it is wrong here: it * treats null as 0, so a series of temperatures with a missing reading * suddenly starts at zero. It also throws a RangeError on a long enough list. */exportfunction extent(values: readonly (number | null)[]): readonly number[] | null {
let min = 0;
let max = 0;
let seen = false;
for (const v of values) {
if (v === null) continue;
if (typeof v !== "number" || !Number.isFinite(v)) {
thrownew TypeError(`values must be finite numbers or null, received ${v}`);
}
if (!seen) {
min = v;
max = v;
seen = true;
} else {
if (v < min) min = v;
if (v > max) max = v;
}
}
return seen ? [min, max] : null;
}