Fixed getRelativeString function

We don't need to have a precise day for display (for now) but it sure would be nice to have
This commit is contained in:
Alexis
2024-05-20 18:17:13 +02:00
parent 784b5cf421
commit a1d0ae0f35

View File

@@ -467,10 +467,6 @@ export const useCalendar = defineStore('calendar', () => {
let direction: 'past' | 'present' | 'future' = 'present' let direction: 'past' | 'present' | 'future' = 'present'
let directionPrefix: string = '' let directionPrefix: string = ''
// To modify, obviously
const daysPerMonth = 32
const monthsPerYear = 10
// Check whether it's a past or future date // Check whether it's a past or future date
if (differenceInDays > 0) { if (differenceInDays > 0) {
direction = 'future' direction = 'future'
@@ -506,45 +502,60 @@ export const useCalendar = defineStore('calendar', () => {
output += directionPrefix output += directionPrefix
} }
const yearPackets: number = Math.abs(Math.trunc(differenceInDays / daysPerYear.value)) const isSameMonth = baseDate.month === relativeDate.month
const monthPackets: number = Math.abs(Math.trunc(differenceInDays / daysPerMonth) % monthsPerYear) const isSameYear = baseDate.year === relativeDate.year
const yearPackets: number = direction === 'future' || isSameYear ? Math.abs(baseDate.year - relativeDate.year) : Math.abs(baseDate.year - relativeDate.year)
// const monthPackets: number = Math.abs(Math.trunc(differenceInDays / daysPerMonth) % months.value.length)
const monthPackets: number = direction === 'future' || isSameMonth ? Math.abs(baseDate.month - relativeDate.month) : Math.abs(months.value.length - relativeDate.month)
// This would need to be a reduce with an array of valid months appart like in the previous refactor
const remainingDays: number = const remainingDays: number =
Math.abs(differenceInDays) - (yearPackets * daysPerYear.value + monthPackets * daysPerMonth) Math.abs(differenceInDays) - (yearPackets * daysPerYear.value * monthPackets)
// Assign day part
// In the meantime : It's not really that much of a problem on larger scale to not compute days, honestly.
if (isSameMonth && isSameYear) {
if (remainingDays) {
if (remainingDays === 1) {
output += ` ${remainingDays} jour`
} else {
output += ` ${remainingDays} jours`
}
}
return output
}
// Assign year part // Assign year part
if (yearPackets) { if (yearPackets) {
if (yearPackets === 1) { if (yearPackets === 1) {
output += `${yearPackets} an` if (direction === 'future') {
} else { return "L'année prochaine"
output += `${yearPackets} ans` } else {
return "L'année dernière"
}
} }
output += `${yearPackets} ans`
} }
// Assign month part
if (monthPackets) { if (monthPackets) {
// If there was a year packet(s), separate from them if (monthPackets === 1) {
if (direction === 'future') {
return 'Le mois prochain'
} else {
return 'Le mois dernier'
}
}
if (yearPackets) { if (yearPackets) {
output += ',' output += ' et '
} }
output += ` ${monthPackets} mois` output += ` ${monthPackets} mois`
} }
// Assign day part
if (remainingDays) {
// If there was a year OR month packet(s), separate from them
if (yearPackets || monthPackets) {
output += ' et'
}
if (remainingDays === 1) {
output += ` ${remainingDays} jour`
} else {
output += ` ${remainingDays} jours`
}
}
return output return output
} }