1 |
3a515b92
|
cagy
|
import pathToRegexp from "path-to-regexp";
|
2 |
|
|
|
3 |
|
|
const cache = {};
|
4 |
|
|
const cacheLimit = 10000;
|
5 |
|
|
let cacheCount = 0;
|
6 |
|
|
|
7 |
|
|
function compilePath(path) {
|
8 |
|
|
if (cache[path]) return cache[path];
|
9 |
|
|
|
10 |
|
|
const generator = pathToRegexp.compile(path);
|
11 |
|
|
|
12 |
|
|
if (cacheCount < cacheLimit) {
|
13 |
|
|
cache[path] = generator;
|
14 |
|
|
cacheCount++;
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
return generator;
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* Public API for generating a URL pathname from a path and parameters.
|
22 |
|
|
*/
|
23 |
|
|
function generatePath(path = "/", params = {}) {
|
24 |
|
|
return path === "/" ? path : compilePath(path)(params, { pretty: true });
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
export default generatePath;
|