Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify sortDirectoriesByDepth function #144

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,11 @@ export const groupByDirectory = <T = unknown>(files: Record<string, T>) => {
}

export const sortDirectoriesByDepth = <T>(directories: Record<string, T>) => {
const sortedKeys = Object.keys(directories)
.sort((a, b) => {
const depthA = a.split('/').length
const depthB = b.split('/').length
return depthA - depthB
})
.sort((a, b) => {
const depthA = a.split('/').length
const depthB = b.split('/').length
if (depthA === depthB) {
return b.localeCompare(a)
}
return 1
})
const sortedKeys = Object.keys(directories).sort((a, b) => {
const depthA = a.split('/').length
const depthB = b.split('/').length
return depthA - depthB || b.localeCompare(a)
})

// if we have root routes, make sure they are registered last
if (sortedKeys.find((x) => /.*\/routes$/.test(x))) {
Expand Down