Skip to content

Commit bcfc463

Browse files
authored
feat: Add isRouteActive() function (#90)
1 parent cc8106e commit bcfc463

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/lib/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('index', () => {
1717
'initFull',
1818
'getRouterContext',
1919
'setRouterContext',
20+
'isRouteActive',
2021
];
2122

2223
// Act.

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export type * from "./types.js";
1212
export { location } from "./core/Location.js";
1313
export * from './RouterTrace/RouterTrace.svelte';
1414
export { default as RouterTrace } from './RouterTrace/RouterTrace.svelte';
15+
export * from "./public-utils.js";

src/lib/public-utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { RouterEngine } from "./core/RouterEngine.svelte.js";
2+
import type { RouteStatus } from "./types.js";
3+
4+
/**
5+
* Checks if a specific route is active according to the provided router engine or route status record.
6+
*
7+
* **Note:** `false` is also returned if no router engine is provided or if no route key is specified.
8+
* @param rsOrRouter A router engine or a router engine's route status record.
9+
* @param key The route key to check for activity.
10+
* @returns `true` if the specified route is active; otherwise, `false`.
11+
*/
12+
export function isRouteActive(
13+
rsOrRouter: RouterEngine | Record<string, RouteStatus> | null | undefined,
14+
key: string | null | undefined
15+
): boolean {
16+
const rs = rsOrRouter instanceof RouterEngine ? rsOrRouter.routeStatus : rsOrRouter;
17+
return !!rs?.[key ?? '']?.match;
18+
}

0 commit comments

Comments
 (0)