UNPKG

compare-path

Version:

An easy-to-use package to detect if two URLs match each other by comparing their abstract paths

24 lines 1.02 kB
import { ExtractRouteParams } from '../type-helpers/extract-route-params'; /** * Compares a given URL path to a defined shape and extracts matching parameters. * * Supports: * * - Dynamic segments via `:param` or `[param]` * - Catch-all segments via `**` * * @example * comparePath('/users/:id', '/users/123') // [true, { id: '123' }] * comparePath('/docs/**', '/docs/foo/bar') // [true, { rest: ['foo', 'bar'] }] * * @template T - Route shape string (e.g., '/users/:id' or '/users/[id]') * @template U - Actual URL path string to compare * @param shape - The route definition with dynamic/wildcard segments * @param path - The current URL path to match against the shape * @returns A tuple: * * - [true, params] if the path matches, with extracted parameters * - [false, null] if the path does not match */ export declare function comparePath<T extends string, U extends string>(shape: T, path: U): [true, ExtractRouteParams<T>] | [false, null]; //# sourceMappingURL=compare-path.d.ts.map