@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
36 lines (35 loc) • 1.39 kB
JavaScript
import { uniKey } from "../../utils/uniKey.js";
import { routerOptions } from "./routerDefineOptions.js";
export function Routes(inputRoutes, target, prefix = "") {
const outputRoutes = [];
const prefixOptions = routerOptions.prefix || "";
function buildRoutes(routes, prefix, parentLayoutId) {
if (prefixOptions && prefix.includes(prefixOptions)) {
prefix = prefix.replace(prefixOptions, "");
}
for (const route of routes){
const newPath = [
prefixOptions,
prefix,
route.path
].join("/").replace(/\/+$/, "").replace(/\/{2,}/g, "/");
const routeId = uniKey();
if (route.element) {
const routeBuild = {
id: routeId,
path: newPath,
element: route.element,
target: route.target || target || document.body
};
route.layout && (routeBuild.layout = route.layout);
parentLayoutId && (routeBuild.parentLayoutId = parentLayoutId);
outputRoutes.push(routeBuild);
}
if (route.children) {
buildRoutes(route.children, newPath, route.layout ? routeId : parentLayoutId);
}
}
}
buildRoutes(inputRoutes, prefix);
return outputRoutes;
}