All files / src/constants positions.js

94.78% Statements 109/115
88.88% Branches 16/18
100% Functions 3/3
94.78% Lines 109/115

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 1241x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 128x 1x 1x   1x 1x 1x 1x 1x 230x 230x 230x   1x 1x 131x 131x   1x 1x 1x 1x 1x 1x 1x 1x 462x 462x 456x             456x 393x 393x 456x 462x 462x 462x 462x 462x 462x 393x 393x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
// Player position taxonomy.
//
// A player's positions are an ORDERED array of specific codes; the first
// entry is their primary position. Each specific code belongs to exactly one
// broad group (GK/DEF/MID/FWD), used for lineup-slot filtering.
//
// KEEP IN SYNC with backend/constants/positions.py (guarded by a backend
// parity test that parses this file).
 
export const POSITION_GROUPS = {
  GK: ['GK'],
  DEF: ['CB', 'LB', 'RB', 'LWB', 'RWB'],
  MID: ['CDM', 'CM', 'CAM', 'LM', 'RM'],
  FWD: ['LW', 'RW', 'ST', 'CF'],
};
 
export const GROUP_NAMES = {
  GK: 'Goalkeeper',
  DEF: 'Defender',
  MID: 'Midfielder',
  FWD: 'Forward',
};
 
// Old side-specific codes that may still exist in cached/stale data.
export const LEGACY_POSITION_MAP = {
  LCB: 'CB',
  RCB: 'CB',
  LCM: 'CM',
  RCM: 'CM',
};
 
const CODE_TO_GROUP = Object.fromEntries(
  Object.entries(POSITION_GROUPS).flatMap(([group, codes]) =>
    codes.map(code => [code, group])
  )
);
 
/**
 * Broad group (GK/DEF/MID/FWD) for a specific position code, or null.
 * Consumer: lineup-slot filtering (SB-288).
 */
export function groupForPosition(code) {
  const canonical = LEGACY_POSITION_MAP[code] || code;
  return CODE_TO_GROUP[canonical] || null;
}
 
/** Primary position = first entry of the ordered positions array. */
export function primaryPosition(positions) {
  return positions && positions.length > 0 ? positions[0] : null;
}
 
/**
 * Parse a positions value into a clean ordered array of canonical codes.
 * Handles the three shapes that reach the frontend: a real array
 * (players.positions text[]), a JSON string (user_profiles.positions TEXT),
 * and null/undefined. Legacy codes (LCB/RCB/LCM/RCM) are remapped and
 * duplicates dropped (first occurrence wins).
 */
export function parsePositions(positions) {
  let list = positions;
  if (!list) return [];
  if (typeof list === 'string') {
    try {
      list = JSON.parse(list);
    } catch {
      return [];
    }
  }
  if (!Array.isArray(list)) return [];
  const seen = new Set();
  const result = [];
  for (const raw of list) {
    const code = LEGACY_POSITION_MAP[raw] || raw;
    if (!seen.has(code)) {
      seen.add(code);
      result.push(code);
    }
  }
  return result;
}
 
// Formation SLOT code -> group. Formation slots keep side-specific codes
// (LCB, RST, ...); this maps every slot code in config/formations.js
// (soccer + futsal) to the group used for player filtering.
// Consumer: lineup-slot filtering (SB-288); coverage pinned by the backend
// parity test.
export const SLOT_TO_GROUP = {
  // goalkeeper
  GK: 'GK',
  // defense (soccer)
  CB: 'DEF',
  LCB: 'DEF',
  RCB: 'DEF',
  LB: 'DEF',
  RB: 'DEF',
  LWB: 'DEF',
  RWB: 'DEF',
  // defense (futsal)
  CD: 'DEF',
  LD: 'DEF',
  RD: 'DEF',
  FIX: 'DEF',
  // midfield
  CDM: 'MID',
  LCDM: 'MID',
  RCDM: 'MID',
  CM: 'MID',
  LCM: 'MID',
  RCM: 'MID',
  CAM: 'MID',
  LM: 'MID',
  RM: 'MID',
  // attack (soccer)
  LW: 'FWD',
  RW: 'FWD',
  ST: 'FWD',
  LST: 'FWD',
  RST: 'FWD',
  // attack (futsal)
  PIV: 'FWD',
  LF: 'FWD',
  RF: 'FWD',
};