Untitled
unknown
javascript
3 years ago
1.3 kB
6
Indexable
function autopilot(routeTree, data, urls) {
const result = [];
// Рекурсивная функция для обхода древовидной структуры маршрутов
function traverseRoute(node, currentPath = '') {
const { route, title, redirectTo, children } = node;
// Проверка наличия параметризованного сегмента маршрута
const paramSegmentMatch = route.match(/^:(\w+)$/);
if (paramSegmentMatch) {
const paramName = paramSegmentMatch[1];
const entityId = currentPath.split('/').slice(-1)[0];
const entityName = data[paramName][entityId];
result.push({ route: currentPath, title: entityName });
} else {
result.push({ route: currentPath + route, title });
}
if (children) {
for (const childNode of children) {
if (redirectTo && redirectTo === childNode.route) {
traverseRoute(childNode, currentPath);
} else {
traverseRoute(childNode, currentPath + route);
}
}
}
}
for (const url of urls) {
const pathSegments = url.split('/').filter(segment => segment !== '');
const rootNode = routeTree.children.find(node => node.route === '/');
traverseRoute(rootNode, '/');
}
return result;
}
module.exports = autopilot;
Editor is loading...