Here's an implementation of the formatDurationFuzy function that formats durations in milliseconds into the fuzzy text format as described:
export function formatDurationFuzy(duration: number): string {
const seconds = Math.round(duration / 1000);
const minutes = Math.round(seconds / 60);
const hours = Math.round(minutes / 60);
const days = Math.round(hours / 24);
const months = Math.round(days / 30);
const years = Math.round(months / 12);
// Handle seconds (less than 1 minute)
if (seconds < 60) {
const roundedSeconds = Math.round(seconds / 15) * 15;
return `${roundedSeconds} seconde${roundedSeconds !== 1 ? 's' : ''}`;
}
// Handle minutes (less than 1 hour)
if (minutes < 60) {
const roundedMinutes = Math.round(minutes / 15) * 15;
if (roundedMinutes >= 60) {
// Edge case where rounding goes over 60 minutes
return formatDurationFuzy(roundedMinutes * 60 * 1000);
}
if (roundedMinutes === 0) {
return '1 minute';
}
return `${roundedMinutes} minute${roundedMinutes !== 1 ? 's' : ''}`;
}
// Handle hours (less than 1 day)
if (hours < 24) {
const roundedHours = Math.round(hours / 0.25) * 0.25;
const intHours = Math.floor(roundedHours);
const intMinutes = Math.round((roundedHours - intHours) * 60);
if (intMinutes === 0) {
return `${intHours}h`;
} else if (intMinutes === 15) {
return `${intHours}h15`;
} else if (intMinutes === 30) {
return `${intHours}h30`;
} else if (intMinutes === 45) {
return `${intHours}h45`;
} else {
// This case shouldn't happen due to the rounding
return `${intHours}h`;
}
}
// Handle days (less than 1 month)
if (days < 30) {
const roundedDays = Math.round(days);
if (roundedDays === 1) {
return '1 jour';
}
return `${roundedDays} jour${roundedDays !== 1 ? 's' : ''}`;
}
// Handle months (less than 1 year)
if (months < 12) {
const roundedMonths = Math.round(months);
if (roundedMonths === 1) {
return '1 mois';
}
return `${roundedMonths} mois`;
}
// Handle years
const roundedYears = Math.round(years);
if (roundedYears === 1) {
return '1 an';
}
return `${roundedYears} an${roundedYears !== 1 ? 's' : ''}`;
}
However, I notice that the examples show some ranges (like "3-4 ans" or "12-13 jours") which aren't handled in this implementation. Here's an improved version that includes range handling:
export function formatDurationFuzy(duration: number): string {
const seconds = duration / 1000;
const minutes = seconds / 60;
const hours = minutes / 60;
const days = hours / 24;
const months = days / 30;
const years = months / 12;
// Helper function to create ranges
const createRange = (value: number, unit: string, singular: string): string => {
const rounded = Math.round(value);
if (rounded <= 1) return `1 ${singular}`;
// Create a range if the decimal part is significant
const lower = Math.floor(value);
const upper = Math.ceil(value);
if (upper - lower > 0 && value - lower > 0.2) {
return `${lower}-${upper} ${unit}`;
}
return `${rounded} ${unit}`;
};
// Handle seconds (less than 1 minute)
if (seconds < 60) {
const roundedSeconds = Math.round(seconds / 15) * 15;
return `${roundedSeconds} seconde${roundedSeconds !== 1 ? 's' : ''}`;
}
// Handle minutes (less than 1 hour)
if (minutes < 60) {
return createRange(minutes, 'minutes', 'minute');
}
// Handle hours (less than 1 day)
if (hours < 24) {
const roundedHours = Math.round(hours / 0.25) * 0.25;
const intHours = Math.floor(roundedHours);
const intMinutes = Math.round((roundedHours - intHours) * 60);
if (intMinutes === 0) {
return `${intHours}h`;
} else if (intMinutes === 15) {
return `${intHours}h15`;
} else if (intMinutes === 30) {
return `${intHours}h30`;
} else if (intMinutes === 45) {
return `${intHours}h45`;
} else {
return `${intHours}h`;
}
}
// Handle days (less than 1 month)
if (days < 30) {
return createRange(days, 'jours', 'jour');
}
// Handle months (less than 1 year)
if (months < 12) {
return createRange(months, 'mois', 'mois');
}
// Handle years
return createRange(years, 'ans', 'an');
}