All recorded stats, keyed by date then supplier.
The window to keep.
Reference time; defaults to the current time. Injectable for tests.
A new object containing only the in-range days.
filterStatsByRange(stats, "today", new Date("2026-07-18T10:00:00Z"));
// => { "2026-07-18": { … } }
export function filterStatsByRange(
stats: SupplierStatsData,
range: StatsRange,
now: Date = new Date(),
): SupplierStatsData {
const from = rangeStartKey(range, now);
if (from === undefined) {
return stats;
}
const to = toDateKey(now);
const filtered: SupplierStatsData = {};
for (const [dateKey, dayStats] of Object.entries(stats)) {
if (dateKey >= from && dateKey <= to) {
filtered[dateKey] = dayStats;
}
}
return filtered;
}
Narrows recorded stats to the days falling inside a range. Because the store keys days as zero-padded
YYYY-MM-DD, lexicographic comparison is equivalent to chronological comparison, so no date parsing is needed per key.