import type { NavItem } from '../navigation.ts'; // Lower case and without one trailing slash (not for "/"), the way the // router matches a path (BR-NAV-04) function normalise(path: string): string { const lower = path.toLowerCase(); return lower.length > 1 && lower.endsWith('/') ? lower.slice(0, -1) : lower; } // Whether the item is the page at `pathname`. Pure string comparison: never // throws and never renders the pathname (SECURITY-05, SECURITY-15). export function isItemCurrent(item: NavItem, pathname: string): boolean { return normalise(item.to) === normalise(pathname); } // Whether the item or one of its children is the page at `pathname`; marks // the parent while a page below it is open (D4) export function isSectionCurrent(item: NavItem, pathname: string): boolean { return ( isItemCurrent(item, pathname) || (item.children?.some((child) => isItemCurrent(child, pathname)) ?? false) ); }