@monstermann/fn
Version:
A utility library for TypeScript.
31 lines (29 loc) • 902 B
JavaScript
import { FnError } from "../function/FnError.js";
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/medianOrThrow.ts
/**
* `medianOrThrow(array)`
*
* Returns the median value from `array`, or throws an error if the array is empty.
*
* ```ts
* medianOrThrow([1, 3, 5]); // 3
* medianOrThrow([1, 2, 3, 4]); // 2.5
* medianOrThrow([]); // throws FnError
* ```
*
* ```ts
* pipe([1, 3, 5], medianOrThrow()); // 3
* pipe([1, 2, 3, 4], medianOrThrow()); // 2.5
* pipe([], medianOrThrow()); // throws FnError
* ```
*/
const medianOrThrow = dfdlT((target) => {
if (target.length === 0) throw new FnError("Array.medianOrThrow: Target is empty.", [target]);
const sorted = target.toSorted((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) return (sorted[mid - 1] + sorted[mid]) / 2;
else return sorted[mid];
}, 1);
//#endregion
export { medianOrThrow };