All files / src/utils formStrip.js

100% Statements 50/50
100% Branches 21/21
100% Functions 5/5
100% Lines 50/50

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x 1x 1x 1x 1x 1x   1x   1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 224x 224x 1x   1x 1x 24x 24x 22x 22x 22x 22x 22x   1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x  
/**
 * "Last N" form strips (SB-1869). Every W/D/L strip in the UI reads oldest to
 * newest, latest result on the right — the football convention, and the order
 * the league table's Form column already uses. Kept pure so the order and the
 * labels are tested without mounting anything.
 */
 
const RESULT_WORDS = { W: 'Win', D: 'Draw', L: 'Loss' };
 
/** "Win" / "Draw" / "Loss"; anything else passes through unchanged. */
export const resultWord = r => RESULT_WORDS[r] || r;
 
/**
 * Oldest-first copy of a newest-first list (the dossier's `form`). The API
 * order stays newest first — MT AI facts read it that way.
 */
export const oldestFirst = newestFirst => [...(newestFirst || [])].reverse();
 
/**
 * Screen-reader label for a strip; `results` already oldest first. Undefined
 * for an empty strip, so the attribute is dropped rather than "Last 0".
 */
export const formStripLabel = results =>
  results?.length
    ? `Last ${results.length} results, oldest to newest: ${results.map(resultWord).join(', ')}`
    : undefined;
 
// "2026-11-21" -> "Nov 21", without the UTC shift `new Date(iso)` would add.
const shortDate = iso => {
  const [y, m, d] = String(iso).slice(0, 10).split('-').map(Number);
  if (!y || !m || !d) return null;
  return new Date(y, m - 1, d).toLocaleDateString('en-US', {
    month: 'short',
    day: 'numeric',
  });
};
 
/**
 * Chip tooltip, e.g. "Draw 1-1 v AC River, Nov 21"; missing parts are skipped,
 * and a forfeit says so.
 */
export const formChipTitle = ({
  result,
  score,
  opponent,
  date,
  forfeit,
} = {}) => {
  let out = resultWord(result);
  if (score) out += ` ${score}`;
  if (opponent?.name) out += ` v ${opponent.name}`;
  const when = date ? shortDate(date) : null;
  if (when) out += `, ${when}`;
  if (forfeit) out += ' (forfeit)';
  return out;
};