ChemPal Documentation - v1.6.0
    Preparing search index...

    Function filterStatsByRange

    • 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.

      Parameters

      • stats: SupplierStatsData

        All recorded stats, keyed by date then supplier.

      • range: StatsRange

        The window to keep.

      • now: Date = ...

        Reference time; defaults to the current time. Injectable for tests.

      Returns SupplierStatsData

      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;
      }