brackt/app/lib/normalize-team-name.ts

56 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

/**
Auto-populate & auto-score tennis Grand Slam brackets from Wikipedia Add a per-event "Sync Draw" that pulls a tennis major's full 128-player draw from its Wikipedia article, auto-creates/links participants (and propagates them to every linked sibling season), builds the bracket, and runs the qualifying-points scorer. Re-running advances the bracket and scoring as matches complete. Core - match-sync: WikipediaTennisAdapter + wikitext bracket parser, DrawSync DTOs, syncTennisDraw orchestrator, pure draw->rows mapping - playoff-match: populateBracketFromDraw (idempotent upsert on externalMatchId) - scoring_events.externalSourceKey column (Wikipedia article; migration 0123) - admin bracket "Sync Draw" card (accepts URL or title) + cron pass Scoring fix - deriveBracketQualifyingStates only floors players who have reached the scoring stage; for deep brackets (tennis_128) early-round losers earn 0 QP instead of a phantom 9th-place floor. CS2/simple_8 behavior preserved (gated on rounds[0].isScoring). Re-sync reconciles stale QP rows in a transaction. Matching & parsing - accent-folding in normalizeTeamName; strip Wikipedia "(tennis)" disambiguators; treat Bye/TBD/Qualifier as TBD; extract wikilink before template-stripping so {{nowrap}}-wrapped players parse Admin UX - dry-run preview (matched / will-create / possible duplicates / unfilled slots) with inline "rename existing" / "create as new" resolution via fetcher (no full reload) Tests: parser vs real 2025 Wimbledon fixture, draw mapping, tennis_128 scoring, accent/disambiguator/nowrap parsing, URL parsing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 00:32:35 -07:00
* Normalize a team/participant name for fuzzy matching across data sources.
* Folds accents (so "Stéfanos Tsitsipás" matches a plain "Stefanos Tsitsipas"),
* lowercases, trims, and collapses whitespace.
*/
export function normalizeTeamName(name: string): string {
Auto-populate & auto-score tennis Grand Slam brackets from Wikipedia Add a per-event "Sync Draw" that pulls a tennis major's full 128-player draw from its Wikipedia article, auto-creates/links participants (and propagates them to every linked sibling season), builds the bracket, and runs the qualifying-points scorer. Re-running advances the bracket and scoring as matches complete. Core - match-sync: WikipediaTennisAdapter + wikitext bracket parser, DrawSync DTOs, syncTennisDraw orchestrator, pure draw->rows mapping - playoff-match: populateBracketFromDraw (idempotent upsert on externalMatchId) - scoring_events.externalSourceKey column (Wikipedia article; migration 0123) - admin bracket "Sync Draw" card (accepts URL or title) + cron pass Scoring fix - deriveBracketQualifyingStates only floors players who have reached the scoring stage; for deep brackets (tennis_128) early-round losers earn 0 QP instead of a phantom 9th-place floor. CS2/simple_8 behavior preserved (gated on rounds[0].isScoring). Re-sync reconciles stale QP rows in a transaction. Matching & parsing - accent-folding in normalizeTeamName; strip Wikipedia "(tennis)" disambiguators; treat Bye/TBD/Qualifier as TBD; extract wikilink before template-stripping so {{nowrap}}-wrapped players parse Admin UX - dry-run preview (matched / will-create / possible duplicates / unfilled slots) with inline "rename existing" / "create as new" resolution via fetcher (no full reload) Tests: parser vs real 2025 Wimbledon fixture, draw mapping, tennis_128 scoring, accent/disambiguator/nowrap parsing, URL parsing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 00:32:35 -07:00
return name
.normalize("NFKD")
.replace(/[̀-ͯ]/g, "") // strip combining diacritical marks
.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;
}