|
|
@ -16,26 +16,22 @@ import { Sitemap } from './types'; |
|
|
|
); |
|
|
|
); |
|
|
|
};*/ |
|
|
|
};*/ |
|
|
|
|
|
|
|
|
|
|
|
const createPaths = ( |
|
|
|
const createPaths = (node: Sitemap): Readonly<[string, number]>[] => [ |
|
|
|
sitemap: Sitemap | null | undefined, |
|
|
|
['', node.id], |
|
|
|
): [string, number][] => |
|
|
|
...node.children.flatMap((childNode) => |
|
|
|
!sitemap |
|
|
|
createPaths(childNode).map( |
|
|
|
? [] |
|
|
|
([path, id]) => [`/${childNode.name}${path}`, id] as const, |
|
|
|
: sitemap.children.flatMap((childNode) => [ |
|
|
|
|
|
|
|
[childNode.name, childNode.id] as [string, number], |
|
|
|
|
|
|
|
...createPaths(childNode).map( |
|
|
|
|
|
|
|
([path, id]) => |
|
|
|
|
|
|
|
[`${childNode.name}/${path}`, id] as [string, number], |
|
|
|
|
|
|
|
), |
|
|
|
), |
|
|
|
]); |
|
|
|
), |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
// Second, improved implementation, where we build a static hashmap from complete paths to IDs once,
|
|
|
|
// Second, improved implementation, where we build a static hashmap from complete paths to IDs once,
|
|
|
|
// and then use it as a single lookup table.
|
|
|
|
// and then use it as a single lookup table.
|
|
|
|
export const createPathResolver = (sitemap: Sitemap | null) => { |
|
|
|
export const createPathResolver = (sitemap: Sitemap | null) => { |
|
|
|
const paths = new Map( |
|
|
|
const rawPathsData = sitemap ? createPaths(sitemap) : []; |
|
|
|
createPaths(sitemap).map(([path, id]) => [`/${path}`, id]), |
|
|
|
const pathsMap = new Map( |
|
|
|
|
|
|
|
rawPathsData.map(([path, id]) => [path.length ? path : '/', id]), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return (path: string) => |
|
|
|
return (path: string) => pathsMap.get(path) ?? null; |
|
|
|
(path === '/' ? sitemap?.id : paths.get(path)) ?? null; |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|