brackt/app/lib/normalize-team-name.ts
Chris Parsons bcca8b76fa
Add regular season standings for NBA/NHL (fixes #89) (#192)
Adds live standings sync and display for bracket-based sports (NBA/NHL),
so league members can see W/L tables and which teams their opponents drafted
during the regular season — not just after the playoff bracket is set.

- New `regular_season_standings` table with upsert-on-conflict sync
- Standings sync service with NHL (api-web.nhle.com) and NBA (ESPN) adapters,
  externalId write-back for future syncs, and unmatched-team resolution UI
- `RegularSeasonStandings` component: flat (NBA) + division/wild-card (NHL) modes,
  playoff line, TeamOwnerBadge, projected Brackt points (EV), mobile horizontal scroll
- Admin "Sync Standings" card + "Resolve Unmatched" UI on sports season page
- Admin manual standings edit hatch at /admin/sports-seasons/:id/regular-standings
- Show standings above bracket until matches exist; below once bracket is set
- `normalize-team-name` utility extracted to shared lib

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 00:12:01 -07:00

49 lines
1.6 KiB
TypeScript

/**
* Normalize a team name for fuzzy matching across data sources.
* Lowercases, trims, and collapses whitespace.
*/
export function normalizeTeamName(name: string): string {
return name.toLowerCase().trim().replace(/\s+/g, " ");
}
/**
* Try to match `apiName` to one of the participant names.
* Returns the matched participant name, or null if no match found.
*
* Strategy:
* 1. Exact normalized match (case-insensitive)
* 2. Substring: one contains the other (handles "Golden State" vs "Golden State Warriors")
*
* Note: City abbreviations like "LA" ↔ "Los Angeles" are NOT handled here — the
* NBA/NHL APIs typically return full city names so the admin should enter full names
* in the participant list to ensure reliable matching.
*/
export function findMatchingTeamName(
apiName: string,
participantNames: string[]
): string | null {
const normalizedApi = normalizeTeamName(apiName);
// Guard: empty API name should never match anything
if (!normalizedApi) return null;
// 1. Exact match
for (const name of participantNames) {
if (normalizeTeamName(name) === normalizedApi) return name;
}
// 2. Substring match (only when the shorter string is at least 4 chars to avoid false positives)
for (const name of participantNames) {
const normalizedParticipant = normalizeTeamName(name);
if (
normalizedApi.length >= 4 &&
normalizedParticipant.length >= 4 &&
(normalizedApi.includes(normalizedParticipant) ||
normalizedParticipant.includes(normalizedApi))
) {
return name;
}
}
return null;
}