24 lines
750 B
Plaintext
24 lines
750 B
Plaintext
import { compile } from "path-to-regexp";
|
|
function getRouteGenerator(segments, addTrailingSlash) {
|
|
const template = segments.map((segment) => {
|
|
return "/" + segment.map((part) => {
|
|
if (part.spread) {
|
|
return `:${part.content.slice(3)}(.*)?`;
|
|
} else if (part.dynamic) {
|
|
return `:${part.content}`;
|
|
} else {
|
|
return part.content.normalize().replace(/\?/g, "%3F").replace(/#/g, "%23").replace(/%5B/g, "[").replace(/%5D/g, "]").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
}
|
|
}).join("");
|
|
}).join("");
|
|
let trailing = "";
|
|
if (addTrailingSlash === "always" && segments.length) {
|
|
trailing = "/";
|
|
}
|
|
const toPath = compile(template + trailing);
|
|
return toPath;
|
|
}
|
|
export {
|
|
getRouteGenerator
|
|
};
|