Skip to content

Commit

Permalink
fix: When sorting by filename the extension should only be considered…
Browse files Browse the repository at this point in the history
… if the basename is equal

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jun 12, 2024
1 parent d170d84 commit 232d60c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
38 changes: 38 additions & 0 deletions __tests__/utils/fileSorting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,42 @@ describe('sortNodes', () => {

expect(sortNodes(array, { sortingMode: FilesSortingMode.Modified, sortingOrder: 'desc' })).toEqual(['a', 'b', 'c'])
})

test('Names with underscores are sorted correctly', () => {
const array = [
file('file_1.txt'),
file('file_3.txt'),
file('file.txt'),
file('file_2.txt'),
] as const

expect(
sortNodes(
array,
{
sortingMode: FilesSortingMode.Name,
sortingOrder: 'asc',
},
),
).toEqual(['file.txt', 'file_1.txt', 'file_2.txt', 'file_3.txt'])
})

test('Names are sorted correctly by extension', () => {
const array = [
file('file.b'),
file('file.c'),
file('file.a'),
file('file.d'),
] as const

expect(
sortNodes(
array,
{
sortingMode: FilesSortingMode.Name,
sortingOrder: 'asc',
},
),
).toEqual(['file.a', 'file.b', 'file.c', 'file.d'])
})
})
10 changes: 8 additions & 2 deletions lib/utils/fileSorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface FilesSortingOptions {
* @param nodes Nodes to sort
* @param options Sorting options
*/
export function sortNodes(nodes: Node[], options: FilesSortingOptions = {}): Node[] {
export function sortNodes(nodes: readonly Node[], options: FilesSortingOptions = {}): Node[] {
const sortingOptions = {
// Default to sort by name
sortingMode: FilesSortingMode.Name,
Expand All @@ -46,6 +46,12 @@ export function sortNodes(nodes: Node[], options: FilesSortingOptions = {}): Nod
...options,
}

/**
* Get the basename without any extension
* @param name The filename to extract the basename from
*/
const basename = (name: string) => name.lastIndexOf('.') > 0 ? name.slice(0, name.lastIndexOf('.')) : name

const identifiers = [
// 1: Sort favorites first if enabled
...(sortingOptions.sortFavoritesFirst ? [(v: Node) => v.attributes?.favorite !== 1] : []),
Expand All @@ -54,7 +60,7 @@ export function sortNodes(nodes: Node[], options: FilesSortingOptions = {}): Nod
// 3: Use sorting mode if NOT basename (to be able to use displayName too)
...(sortingOptions.sortingMode !== FilesSortingMode.Name ? [(v: Node) => v[sortingOptions.sortingMode]] : []),
// 4: Use displayName if available, fallback to name
(v: Node) => v.attributes?.displayName || v.basename,
(v: Node) => basename(v.attributes?.displayName || v.basename),
// 5: Finally, use basename if all previous sorting methods failed
(v: Node) => v.basename,
]
Expand Down

0 comments on commit 232d60c

Please sign in to comment.