|
1 | | -import { Key, pathToRegexp } from 'path-to-regexp' |
| 1 | +import { match, MatchFunction } from 'path-to-regexp' |
2 | 2 | import { MatcherFn } from 'wouter' |
3 | 3 |
|
| 4 | +type ParamData = Record<string, string | string[]> |
| 5 | + |
4 | 6 | /* |
5 | | - * This function specifies how strings like /app/:users/:items* are transformed into regular expressions to pass into path-to-regexp. |
| 7 | + * This function creates a matcher function for a given path pattern. |
6 | 8 | * |
7 | 9 | * @param {string} path — a path like "/:foo/:bar" |
8 | | - * @return {{ keys: [], regexp: RegExp }} |
| 10 | + * @return {MatchFunction<ParamData>} — a function that matches paths and extracts params |
9 | 11 | */ |
10 | | -const convertPathToRegexp = (path: string) => { |
11 | | - const keys: Key[] = [] |
12 | | - const regexp = pathToRegexp(path, keys) |
13 | | - return { keys, regexp } |
| 12 | +const createMatcher = (path: string): MatchFunction<ParamData> => { |
| 13 | + return match(path, { decode: decodeURIComponent }) |
14 | 14 | } |
15 | 15 |
|
16 | | -const cache: Record<string, ReturnType<typeof convertPathToRegexp>> = {} |
| 16 | +const cache: Record<string, MatchFunction<ParamData>> = {} |
17 | 17 |
|
18 | | -// obtains a cached regexp version of the pattern |
19 | | -const getRegexp = (pattern: string) => cache[pattern] || (cache[pattern] = convertPathToRegexp(pattern)) |
| 18 | +// obtains a cached matcher function for the pattern |
| 19 | +const getMatcher = (pattern: string) => cache[pattern] || (cache[pattern] = createMatcher(pattern)) |
20 | 20 |
|
21 | 21 | export const matcher: MatcherFn = (pattern, path) => { |
22 | | - const { regexp, keys } = getRegexp(pattern || '') |
23 | | - const out = regexp.exec(path.split('#')[0].split('?')[0]) |
24 | | - |
25 | | - if (!out) return [false, null] |
| 22 | + const matchFn = getMatcher(String(pattern || '')) |
| 23 | + const pathStr = String(path) |
| 24 | + const cleanPath = pathStr.split('#')[0].split('?')[0] |
| 25 | + const result = matchFn(cleanPath) |
26 | 26 |
|
27 | | - // formats an object with matched params |
28 | | - const params = keys.reduce((params, key, i) => { |
29 | | - params[key.name] = out[i + 1] |
30 | | - return params |
31 | | - }, {}) |
| 27 | + if (!result) return [false, null] |
32 | 28 |
|
33 | | - return [true, params] |
| 29 | + // return matched params |
| 30 | + return [true, result.params as Record<string, string>] |
34 | 31 | } |
0 commit comments