brackt/app/lib/parse-results-text.ts

51 lines
1.4 KiB
TypeScript

export interface ParsedLine {
placement: number;
rawName: string;
}
/**
* Parse a pasted results text into placement + name pairs.
*
* Supported formats per line:
* 1. Name (dot separator)
* 1, Name (comma separator)
* 1 Name (space separator)
* T3. Name (T-prefix tie, dot separator)
* T3, Name (T-prefix tie, comma separator)
* T3 Name (T-prefix tie, space separator)
*
* Repeated rank numbers across consecutive lines are both emitted
* with the same placement (treated as ties automatically).
* Blank lines are ignored.
*/
export function parseResultsText(text: string): ParsedLine[] {
const results: ParsedLine[] = [];
for (const rawLine of text.split("\n")) {
const line = rawLine.trim();
if (!line) continue;
// Try to match: optional T, digits, optional separator (. or ,), then whitespace and the name
// Pattern 1: "1. Name", "1, Name", "T3. Name", "T3, Name"
const match = line.match(/^T?(\d+)[.,]\s*(.+)$/);
if (match) {
results.push({
placement: parseInt(match[1], 10),
rawName: match[2].trim(),
});
continue;
}
// Pattern 2: "1 Name", "T3 Name" (space-only separator)
const spaceMatch = line.match(/^T?(\d+)\s+(.+)$/);
if (spaceMatch) {
results.push({
placement: parseInt(spaceMatch[1], 10),
rawName: spaceMatch[2].trim(),
});
continue;
}
}
return results;
}