mirror of
https://github.com/callstack/agent-device.git
synced 2026-09-14 20:06:34 +08:00
af6f12e391
* chore: adopt shared oxlint config * fix: preserve project lint boundaries * fix: remove redundant oxlint config
23 lines
704 B
TypeScript
23 lines
704 B
TypeScript
import type { Stat } from './types.ts';
|
|
|
|
// Nearest-rank percentile over a copy of the values.
|
|
function percentile(sorted: number[], p: number): number {
|
|
if (sorted.length === 0) return Number.NaN;
|
|
const rank = Math.ceil((p / 100) * sorted.length);
|
|
const idx = Math.min(sorted.length - 1, Math.max(0, rank - 1));
|
|
return sorted[idx];
|
|
}
|
|
|
|
export function summarize(values: number[]): Stat | null {
|
|
const clean = values.filter((v) => Number.isFinite(v));
|
|
if (clean.length === 0) return null;
|
|
const sorted = [...clean].sort((a, b) => a - b);
|
|
return {
|
|
n: sorted.length,
|
|
min: sorted[0],
|
|
median: percentile(sorted, 50),
|
|
p95: percentile(sorted, 95),
|
|
max: sorted.at(-1),
|
|
};
|
|
}
|