mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 09:55:15 +00:00
28 lines
572 B
JavaScript
28 lines
572 B
JavaScript
|
import pathToRegexp from "path-to-regexp";
|
||
|
|
||
|
const cache = {};
|
||
|
const cacheLimit = 10000;
|
||
|
let cacheCount = 0;
|
||
|
|
||
|
function compilePath(path) {
|
||
|
if (cache[path]) return cache[path];
|
||
|
|
||
|
const generator = pathToRegexp.compile(path);
|
||
|
|
||
|
if (cacheCount < cacheLimit) {
|
||
|
cache[path] = generator;
|
||
|
cacheCount++;
|
||
|
}
|
||
|
|
||
|
return generator;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Public API for generating a URL pathname from a path and parameters.
|
||
|
*/
|
||
|
function generatePath(path = "/", params = {}) {
|
||
|
return path === "/" ? path : compilePath(path)(params, { pretty: true });
|
||
|
}
|
||
|
|
||
|
export default generatePath;
|