UNPKG

572 BJavaScriptView Raw
1import pathToRegexp from "path-to-regexp";
2
3const cache = {};
4const cacheLimit = 10000;
5let cacheCount = 0;
6
7function 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 */
23function generatePath(path = "/", params = {}) {
24 return path === "/" ? path : compilePath(path)(params, { pretty: true });
25}
26
27export default generatePath;