All files / components/EnrollmentHistory EnrollmentHistoryHelper.jsx

100% Statements 25/25
100% Branches 4/4
100% Functions 8/8
100% Lines 23/23

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  3x                             3x       3x 15x 15x 15x       3x 178x 178x 178x 178x       3x 16x   56x         3x 12x 12x 63x 63x 63x   12x         3x 61x          
// Stryker disable all
const MONTH_NAMES = [
  "Jan",
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec",
];
 
const quarterLabel = { 1: "W", 2: "S", 3: "M", 4: "F" };
// Stryker restore all
 
// Converts yyyyq (e.g. "20252") to a short display label (e.g. "S25")
export const formatQuarter = (yyyyq) => {
  const year = yyyyq.slice(2, 4);
  const qtr = yyyyq[4];
  return `${quarterLabel[qtr] || "?"}${year}`;
};
 
// Converts a UTC ms timestamp to a display string like "Mar 01"
export const formatDateForAxis = (timestamp) => {
  const date = new Date(timestamp);
  const month = MONTH_NAMES[date.getUTCMonth()];
  const day = String(date.getUTCDate()).padStart(2, "0");
  return `${month} ${day}`;
};
 
// Returns a new array sorted by dateCreated ascending (oldest first)
export const sortByDateCreated = (data) => {
  return [...data].sort(
    (a, b) =>
      new Date(a.dateCreated).getTime() - new Date(b.dateCreated).getTime(),
  );
};
 
// Groups enrollment data by "yyyyq-section" key
export const groupBySectionAndQuarter = (data) => {
  const groups = {};
  data.forEach((item) => {
    const key = `${item.yyyyq}-${item.section}`;
    if (!groups[key]) groups[key] = [];
    groups[key].push(item);
  });
  return groups;
};
 
// Transforms section data into a Recharts-friendly time series.
// Returns [{ timestamp: ms, enrollment: number }, ...] sorted oldest first.
export const buildTimeSeries = (sectionData) => {
  return sortByDateCreated(sectionData).map((item) => ({
    timestamp: new Date(item.dateCreated).getTime(),
    enrollment: item.enrollment,
  }));
};