summaryrefslogtreecommitdiff
path: root/node_modules/swup/src/helpers/matchPath.ts
blob: e133291166eb3ffa30c610e2c8de2fddfd76a558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { match } from 'path-to-regexp';

import type {
	Path,
	ParseOptions,
	TokensToRegexpOptions,
	RegexpToFunctionOptions,
	MatchFunction
} from 'path-to-regexp';

export { type Path };

/** Create a match function from a path pattern that checks if a URLs matches it. */
export const matchPath = <P extends object = object>(
	path: Path,
	options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions
): MatchFunction<P> => {
	try {
		return match<P>(path, options);
	} catch (error) {
		throw new Error(`[swup] Error parsing path "${String(path)}":\n${String(error)}`);
	}
};