Files
warren/frontend/utils/index.ts
2025-07-13 07:17:40 +02:00

63 lines
1.3 KiB
TypeScript

import type { BreadcrumbData } from '~/types';
export function getBreadcrumbs(path: string): BreadcrumbData[] {
const { warrens } = useWarrenStore();
const crumbs = path
.split('/')
.filter((v) => v.length > 0)
.map((v) => ({
name: v,
href: '#',
}));
crumbs.unshift({ name: '/', href: '/' });
for (let i = 1; i < crumbs.length; i++) {
crumbs[i].name = decodeURI(crumbs[i].name);
crumbs[i].href =
'/' +
path
.split('/')
.slice(1, i + 1)
.join('/');
}
if (
crumbs.length >= 3 &&
crumbs[1].href === '/warrens' &&
crumbs[2].name in warrens
) {
crumbs[2].name = warrens[crumbs[2].name].name;
}
return crumbs;
}
export function preventDefault(event: Event) {
event.preventDefault();
return event;
}
export function joinPaths(base: string, other: string): string {
if (!base.endsWith('/')) {
base += '/';
}
return base + other;
}
export function splitOnce(
str: string,
search: string
): [string, string | null] {
const index = str.indexOf(search);
if (index === -1) {
return [str, null];
}
return [str.slice(0, index), str.slice(index + 1)];
}