| 70 | |
| 71 | /** Return every ISO week label between the first and last entry in `weeks` (inclusive). */ |
| 72 | export function fillWeekRange(weeks: string[]): string[] { |
| 73 | if (weeks.length <= 1) return weeks; |
| 74 | const sorted = [...weeks].sort(); |
| 75 | const toMonday = (label: string): Date => { |
| 76 | const [y, w] = label.split('-W').map(Number); |
| 77 | const jan4 = new Date(y, 0, 4); |
| 78 | const dow = (jan4.getDay() + 6) % 7; |
| 79 | const mon = new Date(jan4); |
| 80 | mon.setDate(jan4.getDate() - dow + (w - 1) * 7); |
| 81 | return mon; |
| 82 | }; |
| 83 | const start = toMonday(sorted[0]); |
| 84 | const end = toMonday(sorted[sorted.length - 1]); |
| 85 | const result: string[] = []; |
| 86 | for (const d = new Date(start); d <= end; d.setDate(d.getDate() + 7)) { |
| 87 | result.push(isoWeek(d)); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | /** Return every YYYY-MM between the first and last entry in `months` (inclusive). */ |
| 93 | export function fillMonthRange(months: string[]): string[] { |