Skip to content

Commit 1f5b832

Browse files
committed
tentative
1 parent c733c1b commit 1f5b832

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

app/utils/chart-data-buckets.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,30 @@ import type {
77
} from '~/types/chart'
88
import { DAY_MS, parseIsoDate, toIsoDate, addDays, daysInMonth, daysInYear } from '~/utils/date'
99

10-
// ---------------------------------------------------------------------------
11-
// Fill partial bucket
12-
// ---------------------------------------------------------------------------
13-
1410
/** Proportionally scale a partial bucket to estimate the full-period value. */
1511
export function fillPartialBucket(value: number, actualDays: number, totalDays: number): number {
1612
if (actualDays <= 0 || actualDays >= totalDays) return value
1713
return Math.round((value * totalDays) / actualDays)
1814
}
1915

20-
// ---------------------------------------------------------------------------
21-
// Builders
22-
// ---------------------------------------------------------------------------
16+
function sortedDaily(daily: DailyRawPoint[]): DailyRawPoint[] {
17+
return daily.slice().sort((a, b) => a.day.localeCompare(b.day))
18+
}
2319

2420
export function buildDailyEvolution(daily: DailyRawPoint[]): DailyDataPoint[] {
25-
return daily
26-
.slice()
27-
.sort((a, b) => a.day.localeCompare(b.day))
28-
.map(item => ({
29-
day: item.day,
30-
value: item.value,
31-
timestamp: parseIsoDate(item.day).getTime(),
32-
}))
21+
return sortedDaily(daily).map(item => ({
22+
day: item.day,
23+
value: item.value,
24+
timestamp: parseIsoDate(item.day).getTime(),
25+
}))
3326
}
3427

3528
export function buildWeeklyEvolution(
3629
daily: DailyRawPoint[],
3730
rangeStartIso: string,
3831
rangeEndIso: string,
3932
): WeeklyDataPoint[] {
40-
const sorted = daily.slice().sort((a, b) => a.day.localeCompare(b.day))
33+
const sorted = sortedDaily(daily)
4134
if (sorted.length === 0) return []
4235

4336
const rangeStartDate = parseIsoDate(rangeStartIso)
@@ -89,7 +82,7 @@ export function buildMonthlyEvolution(
8982
rangeStartIso?: string,
9083
rangeEndIso?: string,
9184
): MonthlyDataPoint[] {
92-
const sorted = daily.slice().sort((a, b) => a.day.localeCompare(b.day))
85+
const sorted = sortedDaily(daily)
9386
const byMonth = new Map<string, number>()
9487
for (const item of sorted) {
9588
const m = item.day.slice(0, 7)
@@ -119,7 +112,7 @@ export function buildYearlyEvolution(
119112
rangeStartIso?: string,
120113
rangeEndIso?: string,
121114
): YearlyDataPoint[] {
122-
const sorted = daily.slice().sort((a, b) => a.day.localeCompare(b.day))
115+
const sorted = sortedDaily(daily)
123116
const byYear = new Map<string, number>()
124117
for (const item of sorted) {
125118
const y = item.day.slice(0, 4)

0 commit comments

Comments
 (0)