ptitlutins/app/utils/groupChildren.ts
2026-06-15 23:34:49 +02:00

20 lines
624 B
TypeScript

import type { Node, NodeDayGroup } from "~/types"
import { dayKey } from "./dayKey"
import { dayLabel } from "./dayLabel"
/** Split a node's children into an undated bucket + day-labelled groups. */
export const groupChildren = (children: Node[]): NodeDayGroup[] => {
const groups: NodeDayGroup[] = []
let curKey = ""
for (const ev of children) {
const start = ev.event.startDate
if (!start) continue
const key = dayKey(start)
if (key !== curKey) {
groups.push({ label: dayLabel(start), events: [] })
curKey = key
}
groups[groups.length - 1]!.events.push(ev)
}
return groups
}