23 lines
931 B
TypeScript
23 lines
931 B
TypeScript
import type { Category, Finding, InspectionScore } from "./types";
|
|
|
|
const CATEGORY_ORDER: Category[] = ["dns", "tls", "http"];
|
|
|
|
function clamp(value: number) {
|
|
return Math.max(0, Math.min(100, Math.round(value)));
|
|
}
|
|
|
|
/**
|
|
* Scores are calculated per category first, then averaged equally. This keeps
|
|
* one category's findings from being charged again against the global score.
|
|
*/
|
|
export function score(findings: Finding[]): InspectionScore {
|
|
const categories: Record<Category, number> = { dns: 100, tls: 100, http: 100 };
|
|
for (const finding of findings) {
|
|
const impact = Number.isFinite(finding.scoreImpact) ? Math.min(0, finding.scoreImpact) : 0;
|
|
categories[finding.category] = clamp(categories[finding.category] + impact);
|
|
}
|
|
|
|
const overall = clamp(CATEGORY_ORDER.reduce((sum, category) => sum + categories[category], 0) / CATEGORY_ORDER.length);
|
|
return { overall, deductions: 100 - overall, categories };
|
|
}
|