Files
tuono/packages/tuono-fs-router-vite-plugin/src/sort-route-nodes.ts
T

18 lines
818 B
TypeScript
Raw Normal View History

2024-07-23 19:13:18 +02:00
import type { RouteNode } from './types'
import { multiSortBy } from './utils'
import { LAYOUT_PATH_ID } from './constants'
2024-07-23 19:13:18 +02:00
// Routes need to be sorted in order to iterate over the handleNode fn
// with first the items that might be parent routes
export const sortRouteNodes = (routes: Array<RouteNode>): Array<RouteNode> =>
2024-07-23 19:13:18 +02:00
multiSortBy(routes, [
(d): number => (d.routePath === '/' ? -1 : 1),
(d): number => d.routePath.split('/').length,
// Dynamic route
(d): number => (d.routePath.endsWith(']') ? 1 : -1),
(d): number => (d.filePath.match(/[./]index[.]/) ? 1 : -1),
(d): number => (d.filePath.match(/[./]route[.]/) ? -1 : 1),
(d): number => (d.routePath.endsWith('/') ? -1 : 1),
(d): string => d.routePath,
]).filter((d) => ![`/${LAYOUT_PATH_ID}`].includes(d.routePath || ''))