diff --git a/src/lib/index.test.ts b/src/lib/index.test.ts index b39f89e..ef14b71 100644 --- a/src/lib/index.test.ts +++ b/src/lib/index.test.ts @@ -17,6 +17,7 @@ describe('index', () => { 'initFull', 'getRouterContext', 'setRouterContext', + 'isRouteActive', ]; // Act. diff --git a/src/lib/index.ts b/src/lib/index.ts index 06db8f4..6c2f1f4 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -12,3 +12,4 @@ export type * from "./types.js"; export { location } from "./core/Location.js"; export * from './RouterTrace/RouterTrace.svelte'; export { default as RouterTrace } from './RouterTrace/RouterTrace.svelte'; +export * from "./public-utils.js"; diff --git a/src/lib/public-utils.ts b/src/lib/public-utils.ts new file mode 100644 index 0000000..7eb0a29 --- /dev/null +++ b/src/lib/public-utils.ts @@ -0,0 +1,18 @@ +import { RouterEngine } from "./core/RouterEngine.svelte.js"; +import type { RouteStatus } from "./types.js"; + +/** + * Checks if a specific route is active according to the provided router engine or route status record. + * + * **Note:** `false` is also returned if no router engine is provided or if no route key is specified. + * @param rsOrRouter A router engine or a router engine's route status record. + * @param key The route key to check for activity. + * @returns `true` if the specified route is active; otherwise, `false`. + */ +export function isRouteActive( + rsOrRouter: RouterEngine | Record | null | undefined, + key: string | null | undefined +): boolean { + const rs = rsOrRouter instanceof RouterEngine ? rsOrRouter.routeStatus : rsOrRouter; + return !!rs?.[key ?? '']?.match; +}