element-book
Version:
An [`element-vir`](https://npmjs.com/package/element-vir) drop-in element for building, testing, and demonstrating a collection of elements (or, in other words, a design system).
44 lines (43 loc) • 1.21 kB
JavaScript
import { check } from '@augment-vir/assert';
import { SpaRouter } from 'spa-router-vir';
import { BookMainRoute, defaultBookFullRoute } from './book-routing.js';
export function createBookRouter(basePath) {
return new SpaRouter({
basePath,
sanitizeRoute(rawRoute) {
const sanitizedPaths = sanitizePaths(rawRoute.paths);
return {
paths: sanitizedPaths,
hash: undefined,
search: undefined,
};
},
});
}
function sanitizePaths(paths) {
const firstPath = paths[0];
if (!check.isEnumValue(firstPath, BookMainRoute)) {
return defaultBookFullRoute.paths;
}
else if (firstPath === BookMainRoute.Book) {
return [
BookMainRoute.Book,
...paths.slice(1),
];
}
else if (firstPath === BookMainRoute.Search) {
if (!paths[1]) {
return [
BookMainRoute.Book,
...paths.slice(1),
];
}
return [
firstPath,
paths[1],
];
}
else {
throw new Error(`Route path not handled for sanitization: ${paths.join('/')}`);
}
}