Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions src/core/file/filePathSort.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import path from 'node:path';

// Sort paths for general use (not affected by git change count)
// Uses decorate-sort-undecorate to pre-compute path.split() once per path
// instead of O(N log N) repeated splits during comparisons
export const sortPaths = (filePaths: string[]): string[] => {
return filePaths.sort((a, b) => {
const partsA = a.split(path.sep);
const partsB = b.split(path.sep);
const decorated = filePaths.map((p) => ({ original: p, parts: p.split(path.sep) }));

decorated.sort((a, b) => {
const partsA = a.parts;
const partsB = b.parts;

for (let i = 0; i < Math.min(partsA.length, partsB.length); i++) {
if (partsA[i] !== partsB[i]) {
Expand All @@ -21,4 +25,6 @@ export const sortPaths = (filePaths: string[]): string[] => {
// Sort by path length when all parts are equal
return partsA.length - partsB.length;
});

return decorated.map((d) => d.original);
};
18 changes: 12 additions & 6 deletions tests/core/file/fileSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,25 @@ node_modules
},
});

const sep = path.sep;
const mockFileStructure = [
'root/file1.js',
'root/another/file3.js',
'root/subdir/file2.js',
'root/subdir/ignored.js',
`root${sep}file1.js`,
`root${sep}another${sep}file3.js`,
`root${sep}subdir${sep}file2.js`,
`root${sep}subdir${sep}ignored.js`,
];

vi.mocked(globby).mockResolvedValue(mockFileStructure);

const result = await searchFiles('/mock/root', mockConfig);

expect(result.filePaths).toEqual(mockFileStructure);
expect(result.filePaths).toContain('root/subdir/ignored.js');
expect(result.filePaths).toEqual([
`root${sep}another${sep}file3.js`,
`root${sep}subdir${sep}file2.js`,
`root${sep}subdir${sep}ignored.js`,
`root${sep}file1.js`,
]);
expect(result.filePaths).toContain(`root${sep}subdir${sep}ignored.js`);
expect(result.emptyDirPaths).toEqual([]);
});

Expand Down
Loading