Skip to content

Commit

Permalink
fix segmentDaysByWeeks
Browse files Browse the repository at this point in the history
fixed bug where the returned array had 1 element but mismatched length, causing a second undefined element.
This was caused by assigning elements to the array by arr[index] rather than using arr.push().
New implementation uses .push() and also removes the unneeded weekIndex variable
  • Loading branch information
JGreenlee committed Apr 2, 2024
1 parent 4ca6e3f commit 5ae3ea8
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions www/js/metrics/metricsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ export const secondsToHours = (seconds: number) => formatForDisplay(seconds / 36

// segments metricsDays into weeks, with the most recent week first
export function segmentDaysByWeeks(days: DayOfMetricData[], lastDate: string) {
const weeks: DayOfMetricData[][] = [];
let weekIndex = 0;
let cutoff = isoDateWithOffset(lastDate, -7 * (weekIndex + 1));
const weeks: DayOfMetricData[][] = [[]];
let cutoff = isoDateWithOffset(lastDate, -7 * weeks.length);
[...days].reverse().forEach((d) => {
const date = dateForDayOfMetricData(d);
// if date is older than cutoff, start a new week
if (isoDatesDifference(date, cutoff) > 0) {
weekIndex += 1;
cutoff = isoDateWithOffset(lastDate, -7 * (weekIndex + 1));
weeks.push([]);
cutoff = isoDateWithOffset(lastDate, -7 * weeks.length);
}
if (!weeks[weekIndex]) weeks[weekIndex] = [];
weeks[weekIndex].push(d);
weeks[weeks.length - 1].push(d);
});
return weeks.map((week) => week.reverse());
}
Expand Down

0 comments on commit 5ae3ea8

Please sign in to comment.