Files
faunosteo/apps/app/src/animals/age.ts
T
bchanot 930499cd25 feat(app): animal screens, relation panels, animals on the client page
Routes /app/animals, new, :animalId, edit under one workspace gate; list with
species filter; form (D-R7, U3 birth, no default sex or role, optional first
client); page with current and former relations, end, dates, void; client page
links its animals, adds one pre-associated, links an existing one. Card now
passes aria-labelledby, which it silently dropped.
2026-10-26 17:11:28 +01:00

30 lines
948 B
TypeScript

/**
* The age shown on an animal's page, for display only (contract §5.2): from
* the full date when known, else « environ N ans » from the year. Nothing is
* stored or sent.
*/
export interface AnimalAge {
unit: 'year' | 'month'
count: number
/** From the year alone: the day is unknown. */
about: boolean
}
export function animalAge(
birthYear: number | null,
birthDate: string | null,
today: string,
): AnimalAge | null {
const [ty, tm, td] = today.split('-').map(Number)
if (birthDate) {
const [by, bm, bd] = birthDate.split('-').map(Number)
const months = (ty - by) * 12 + (tm - bm) - (td < bd ? 1 : 0)
if (months < 0) return null
return months >= 12
? { unit: 'year', count: Math.floor(months / 12), about: false }
: { unit: 'month', count: months, about: false }
}
if (birthYear === null || birthYear > ty) return null
return { unit: 'year', count: ty - birthYear, about: true }
}