Skip to content

Commit

Permalink
segmentDaysByWeeks: use for loop, not forEach
Browse files Browse the repository at this point in the history
From suggestion #1138 (comment)
  • Loading branch information
JGreenlee committed Apr 5, 2024
1 parent 1269aad commit f1961a6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions www/js/metrics/metricsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export const secondsToHours = (seconds: number) => formatForDisplay(seconds / 36
export function segmentDaysByWeeks(days: DayOfMetricData[], lastDate: string) {
const weeks: DayOfMetricData[][] = [[]];
let cutoff = isoDateWithOffset(lastDate, -7 * weeks.length);
[...days].reverse().forEach((d) => {
const date = dateForDayOfMetricData(d);
for (let i = days.length - 1; i >= 0; i--) {
const date = dateForDayOfMetricData(days[i]);
// if date is older than cutoff, start a new week
if (isoDatesDifference(date, cutoff) > 0) {
weeks.push([]);
cutoff = isoDateWithOffset(lastDate, -7 * weeks.length);
}
weeks[weeks.length - 1].push(d);
});
weeks[weeks.length - 1].push(days[i]);
}
return weeks.map((week) => week.reverse());
}

Expand Down

0 comments on commit f1961a6

Please sign in to comment.