All files / src/hooks useAgeLabel.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                          148x   148x 128x   128x 31x     97x 97x   97x          
import { useTranslation } from 'react-i18next'
 
import { ageInMonths } from '@/lib/date'
 
/**
 * How old a child is, the way a parent answers the question.
 *
 * The app said "66 meses" for a five-year-old everywhere an age appeared. That
 * was tolerable while ages lived inside a card someone had already opened; it is
 * not tolerable in a menu that lists the children side by side, where it is the
 * only thing distinguishing two names.
 *
 * The cut is at two years, which is where people themselves stop counting in
 * months. Under it, months are what a parent uses ("3 meses"); over it, nobody
 * says "vinte e nove meses". The months are dropped when they are zero, so a
 * birthday reads "5 anos" and not "5 anos e 0 meses".
 *
 * A hook rather than a pure function because the answer is translated, and the
 * plural rules are i18next's — "1 mês" against "2 meses" is not a suffix.
 */
export function useAgeLabel(): (birthDate: string) => string {
  const { t } = useTranslation()
 
  return (birthDate: string) => {
    const total = ageInMonths(birthDate)
 
    if (total < 24) {
      return t('babies.age.months', { count: total })
    }
 
    const years = Math.floor(total / 12)
    const months = total % 12
 
    return months === 0
      ? t('babies.age.years', { count: years })
      : `${t('babies.age.years', { count: years })} ${t('babies.age.and')} ${t('babies.age.months', { count: months })}`
  }
}