2026-03-27 20:45:15 -07:00
|
|
|
/**
|
|
|
|
|
* Returns a function that reports whether a given rank is shared by more than
|
|
|
|
|
* one team. Pass an array of all the rank values in the standings.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* const isTied = buildTiedRankChecker(standings.map(s => s.currentRank));
|
|
|
|
|
* isTied(3); // true if two or more teams share rank 3
|
|
|
|
|
*/
|
|
|
|
|
export function buildTiedRankChecker(ranks: number[]): (rank: number) => boolean {
|
|
|
|
|
const counts = new Map<number, number>();
|
|
|
|
|
for (const rank of ranks) counts.set(rank, (counts.get(rank) ?? 0) + 1);
|
|
|
|
|
return (rank) => (counts.get(rank) ?? 1) > 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 15:57:51 -08:00
|
|
|
/**
|
|
|
|
|
* Returns the display rank for a team on the league home standings preview.
|
|
|
|
|
*
|
|
|
|
|
* Rules:
|
2026-03-27 20:45:15 -07:00
|
|
|
* - If the team has a standing, return its numeric rank, prefixed with "T" if
|
|
|
|
|
* the rank is shared by more than one team.
|
2026-03-07 15:57:51 -08:00
|
|
|
* - If the team has no standing, it is tied at the position just after all
|
|
|
|
|
* ranked teams. For example, if 3 teams have standings the unranked teams
|
|
|
|
|
* show "T4". If no team has standings yet (scores haven't been calculated)
|
|
|
|
|
* every team is genuinely tied at zero and shows "T1".
|
|
|
|
|
*
|
|
|
|
|
* @param standing - The team's standing record, or undefined if none exists.
|
|
|
|
|
* @param standingsCount - Total number of teams that have a standing record.
|
2026-03-27 20:45:15 -07:00
|
|
|
* @param isTied - Whether this team's rank is shared with another team.
|
2026-03-07 15:57:51 -08:00
|
|
|
*/
|
|
|
|
|
export function getDisplayRank(
|
|
|
|
|
standing: { currentRank: number } | undefined,
|
2026-03-27 20:45:15 -07:00
|
|
|
standingsCount: number,
|
|
|
|
|
isTied?: boolean
|
2026-03-07 15:57:51 -08:00
|
|
|
): number | string {
|
2026-03-27 20:45:15 -07:00
|
|
|
if (standing) return isTied ? `T${standing.currentRank}` : standing.currentRank;
|
2026-03-07 15:57:51 -08:00
|
|
|
return `T${standingsCount + 1}`;
|
|
|
|
|
}
|