summaryrefslogtreecommitdiff
path: root/src/lib/is-active.ts
blob: f931065c71ae88c0d7822e9208a6c8d881c68c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export function isActive(
  url: string,
  pathname: string,
  nested = true,
): boolean {
  const normalizedUrl = url.endsWith('/') ? url.slice(0, -1) : url;
  const normalizedPathname = pathname.endsWith('/')
    ? pathname.slice(0, -1)
    : pathname;

  return (
    normalizedUrl === normalizedPathname ||
    (nested && normalizedPathname.startsWith(`${normalizedUrl}/`))
  );
}