feat: add comparePeriods utility for sorting OKR periods and refactor ObjectivesPage to utilize it for improved period sorting
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 8m28s

This commit is contained in:
Julien Froidefond
2026-01-28 13:58:01 +01:00
parent e848e85b63
commit 3a2eb83197
2 changed files with 44 additions and 10 deletions

View File

@@ -14,3 +14,45 @@ export function getCurrentQuarterPeriod(date: Date = new Date()): string {
export function isCurrentQuarterPeriod(period: string, date: Date = new Date()): boolean {
return period === getCurrentQuarterPeriod(date);
}
/**
* Parse a period string to extract year and quarter
* Returns { year, quarter } or null if format is not recognized
* Supports formats like "Q1 2025", "Q2 2024", etc.
*/
function parsePeriod(period: string): { year: number; quarter: number } | null {
// Match format "Q{quarter} {year}"
const match = period.match(/^Q(\d)\s+(\d{4})$/);
if (match) {
return {
year: parseInt(match[2], 10),
quarter: parseInt(match[1], 10),
};
}
return null;
}
/**
* Compare two period strings for sorting (most recent first)
* Returns negative if a should come before b, positive if after, 0 if equal
*/
export function comparePeriods(a: string, b: string): number {
const aParsed = parsePeriod(a);
const bParsed = parsePeriod(b);
// If both can be parsed, compare by year then quarter
if (aParsed && bParsed) {
// Most recent year first
const yearDiff = bParsed.year - aParsed.year;
if (yearDiff !== 0) return yearDiff;
// Most recent quarter first (same year)
return bParsed.quarter - aParsed.quarter;
}
// Fallback: if one can be parsed, prioritize it
if (aParsed && !bParsed) return -1;
if (!aParsed && bParsed) return 1;
// Both unparseable: fallback to string comparison (descending)
return b.localeCompare(a);
}