From 6d6557d3cb9697a7c581ddb8545573ea69d19064 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 29 Jan 2023 23:26:29 +0000 Subject: [PATCH] sunrise-sunset: refactor streak code --- .../SunriseSunsetStreakCalculator.ts | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/routes/sunrise-sunset/SunriseSunsetStreakCalculator.ts b/src/routes/sunrise-sunset/SunriseSunsetStreakCalculator.ts index e5d3c2c..c77afdd 100644 --- a/src/routes/sunrise-sunset/SunriseSunsetStreakCalculator.ts +++ b/src/routes/sunrise-sunset/SunriseSunsetStreakCalculator.ts @@ -29,24 +29,23 @@ export class SunriseSunsetStreakCalculator { return 1; } - let streakLength = 1; - for (const [index, day] of sortedDays.entries()) { - const nextDay = sortedDays[index + 1]; - - if (nextDay === undefined) { - console.log(`No next day, returning streak length of ${streakLength}`); - return streakLength; - } - - const daysBetween = differenceInCalendarDays(day, nextDay); - console.log(`Days Between ${day} - ${nextDay}`, daysBetween); + // A (reverse-order) list of streaks in the lis tof correct days. + const allStreaks: number[] = sortedDays.reduce((streaks: number[], day, index, days) => { + const currentStreakLength = streaks[0] ?? 1; + const daysBetween = differenceInCalendarDays(day, days[index + 1]); if (daysBetween === 1) { - streakLength++; + // The streak continues ! Add a day. + streaks[0] = currentStreakLength + 1; } else { - console.log(`Days between is not 1, returning streak length of ${streakLength}`); - return streakLength; + // We've hit a gap, so start a new streak + streaks.unshift(1); } - } + + return streaks; + }, []); + + // The streaks are in reverse order, so the most recent streak is the last one. + return allStreaks[allStreaks.length - 1]; } }