refactor(timeline): count timelineMeta totals in a single pass
Replace the flatMap intermediate array plus two filter passes with one walk over the year bands and the undated bucket. Same counts, no throwaway allocation. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -21,11 +21,18 @@ export interface TimelineMeta {
|
||||
*/
|
||||
export function timelineMeta(timeline: TimelineDTO): TimelineMeta {
|
||||
const years = timeline.years;
|
||||
const allEntries = [...years.flatMap((y) => y.entries), ...timeline.undated];
|
||||
let letterCount = 0;
|
||||
let eventCount = 0;
|
||||
const tally = (e: TimelineDTO['undated'][number]) => {
|
||||
if (e.kind === 'LETTER') letterCount += 1;
|
||||
else if (e.kind === 'EVENT') eventCount += 1;
|
||||
};
|
||||
for (const y of years) for (const e of y.entries) tally(e);
|
||||
for (const e of timeline.undated) tally(e);
|
||||
return {
|
||||
firstYear: years.length ? years[0].year : null,
|
||||
lastYear: years.length ? years[years.length - 1].year : null,
|
||||
letterCount: allEntries.filter((e) => e.kind === 'LETTER').length,
|
||||
eventCount: allEntries.filter((e) => e.kind === 'EVENT').length
|
||||
letterCount,
|
||||
eventCount
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user