All files / src/utils scoutingCompare.js

100% Statements 137/137
86.56% Branches 58/67
100% Functions 14/14
100% Lines 137/137

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 1521x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 51x 51x 51x 51x 51x 51x 51x   1x   1x 1x 1x   1x 1x 1x 1x 1x 35x 35x 35x 35x   1x 1x 1x 1x 1x 83x 83x 83x   1x 1x 26x 26x 26x   1x 42x 42x 1x   1x 42x 42x 1x   1x   1x 1x 32x 26x 32x 26x 26x 26x 26x 26x   1x 1x 32x 32x 32x   1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x   1x 1x 1x 1x 1x 26x 26x 26x 26x 1x 1x 1x 1x 1x 26x 26x 2x 2x 2x 2x 2x 26x 26x 26x  
/**
 * The Scouting tab's head-to-head rows (SB-1906): both teams' main tables,
 * row by row, the way a tale of the tape reads.
 *
 * Fest teams come from different conferences, so a row compares two numbers
 * that each mean "inside my own table"; the panel says so. Absent stays
 * absent: a side with no value renders "—", never 0, and is never called
 * better or worse.
 */
 
/** "—" for absent, never 0: a team nobody measured has no value. */
export const fmt = v => (v == null ? '—' : v);
export const perGame = v => (v == null ? '—' : Number(v).toFixed(1));
 
export const ordinal = n => {
  const mod100 = n % 100;
  const suffix =
    mod100 >= 11 && mod100 <= 13
      ? 'th'
      : { 1: 'st', 2: 'nd', 3: 'rd' }[n % 10] || 'th';
  return `${n}${suffix}`;
};
 
const KIND_LABELS = { flex: 'Flex', pathway: 'Pro Player Pathway' };
 
/** "Pro Player Pathway", "Flex", or the league's own name. */
export const kindLabel = line =>
  KIND_LABELS[line?.kind] || line?.league || line?.competition || 'League';
 
/**
 * A conference's name without the league it already sits under: "Northeast",
 * not "Northeast (Pro Player Pathway)" next to "Pro Player Pathway".
 */
export const conferenceName = line => {
  const name = line?.division?.name || '';
  const suffix = ` (${kindLabel(line)})`;
  return name.endsWith(suffix) ? name.slice(0, -suffix.length) : name;
};
 
/**
 * The table a team is measured in first: its league (Homegrown, Academy, Pro
 * Player Pathway) when it has one, else its first conference. Null without any.
 */
export const primaryLine = dossier => {
  const lines = dossier?.conferences || [];
  return lines.find(l => l.kind !== 'flex') || lines[0] || null;
};
 
/** Every conference line but the primary one, in the API's order. */
export const otherLines = dossier => {
  const primary = primaryLine(dossier);
  return (dossier?.conferences || []).filter(l => l !== primary);
};
 
export const positionText = line =>
  line?.position == null
    ? '—'
    : `${ordinal(line.position)} of ${fmt(line.table_size)}`;
 
export const recordText = line =>
  !line || line.no_results || line.wins == null
    ? '—'
    : `${fmt(line.wins)}-${fmt(line.draws)}-${fmt(line.losses)}`;
 
const number = v => (v == null || Number.isNaN(Number(v)) ? null : Number(v));
 
/** Bar widths for two values, the larger one full; null when either is absent. */
const bars = (a, b) => {
  if (a == null || b == null) return null;
  const max = Math.max(a, b);
  if (max <= 0) return { home: 0, away: 0 };
  return {
    home: Math.round((a / max) * 100),
    away: Math.round((b / max) * 100),
  };
};
 
/** Which side reads better, only when both have a value and they differ. */
const better = (a, b, higherIsBetter) => {
  if (a == null || b == null || a === b) return null;
  return a > b === higherIsBetter ? 'home' : 'away';
};
 
/**
 * Rows for the two primary lines (either may be null). Each row:
 * `{ key, label, home, away, better, bars }`; `better` and `bars` are null
 * whenever a comparison would rest on an absent value.
 */
export function compareRows(home, away) {
  const gf = [number(home?.gf_per_game), number(away?.gf_per_game)];
  const ga = [number(home?.ga_per_game), number(away?.ga_per_game)];
  return [
    {
      key: 'position',
      label: 'Position',
      home: positionText(home),
      away: positionText(away),
      better: null,
      bars: null,
    },
    {
      key: 'record',
      label: 'W-D-L',
      home: recordText(home),
      away: recordText(away),
      better: null,
      bars: null,
    },
    {
      key: 'gf',
      label: 'Goals for / game',
      home: perGame(gf[0]),
      away: perGame(gf[1]),
      better: better(gf[0], gf[1], true),
      bars: bars(gf[0], gf[1]),
    },
    {
      key: 'ga',
      label: 'Goals against / game',
      home: perGame(ga[0]),
      away: perGame(ga[1]),
      better: better(ga[0], ga[1], false),
      bars: bars(ga[0], ga[1]),
    },
  ];
}
 
/**
 * The context chips for one team: playing up or down, a pro academy whose
 * league sits a year up (SB-1900), a small sample.
 */
export function dossierLabels(dossier) {
  const flags = dossier?.flags || [];
  const context = dossier?.context;
  const out = [];
  if (flags.includes('playing_up')) {
    const from = context?.season_age_group?.name;
    let label = from ? `Playing up from ${from}` : 'Playing up';
    if (flags.includes('playing_up_inferred')) label += ' (inferred)';
    out.push(label);
  }
  if (flags.includes('playing_down')) out.push('Older age group');
  if (flags.includes('league_age_up')) {
    const at = context?.season_age_group?.name;
    out.push(
      at ? `Pro academy · league at ${at}` : 'Pro academy · league a year up'
    );
  }
  if (flags.includes('low_sample')) out.push('Small sample');
  return out;
}