Skip to content

Add VFS context aware sorting #189

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
16 changes: 13 additions & 3 deletions __tests__/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,17 @@ describe('utils.vfs#transformReaddir', () => {
test('Should remove dotfiles', () => {
expect(checkMap({
showHiddenFiles: false
})).toEqual(['..', 'directory', 'xdirectory', 'file', 'xfile']);
})).toEqual(['..', 'directory', 'file', 'xdirectory', 'xfile']);
});

test('Should not sort due to server sorting', () => {
const result = checkMap({
showHiddenFiles: false,
serverSorting: true
});

return expect(result)
.toEqual(['..', 'directory', 'xdirectory', 'file', 'xfile']);
});

test('Should sort by descending order', () => {
Expand All @@ -98,7 +108,7 @@ describe('utils.vfs#transformReaddir', () => {
});

return expect(result)
.toEqual(['..', 'xdirectory', 'directory', 'xfile', 'file']);
.toEqual(['..', 'xfile', 'xdirectory', 'file', 'directory']);
});

test('Should sort by ascending order', () => {
Expand All @@ -108,7 +118,7 @@ describe('utils.vfs#transformReaddir', () => {
});

return expect(result)
.toEqual(['..', 'directory', 'xdirectory', 'file', 'xfile']);
.toEqual(['..', 'directory', 'file', 'xdirectory', 'xfile']);
});

test('Should sort by specified column', () => {
Expand Down
49 changes: 28 additions & 21 deletions src/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ const sortDefault = (k, d) => (a, b) =>
? (d === 'asc' ? 1 : 0)
: (d === 'asc' ? 0 : 1));

/*
* Sort by ascii codes
*/
const sortAscii = (k, d) => (a, b) => d === 'asc'
? (a[k] < b[k]) ? -1 : 1
: (a[k] < b[k]) ? 1 : -1;

/*
* Sorts an array of files
*/
Expand All @@ -86,6 +93,8 @@ const sortFn = t => {
return sortString;
} else if (t === 'date') {
return sortDate;
} else if (t === 'ascii') {
return sortAscii;
}

return sortDefault;
Expand Down Expand Up @@ -187,52 +196,50 @@ export const transformReaddir = ({path}, files, options = {}) => {
showHiddenFiles: false,
sortBy: 'filename',
sortDir: 'asc',
serverSorting: false,
...options
};

let {sortDir, sortBy, filter} = options;
let {sortDir, sortBy, filter, serverSorting} = options;
if (typeof filter !== 'function') {
filter = () => true;
}

if (['asc', 'desc'].indexOf(sortDir) === -1) {
sortDir = 'asc';
}

const filterHidden = options.showHiddenFiles
? () => true
: file => file.filename.substr(0, 1) !== '.';

const sorter = sortMap[sortBy]
? sortMap[sortBy]
: sortFn('string');

const modify = (file) => ({
...file,
humanSize: humanFileSize(file.size)
});

// FIXME: Optimize this to one chain!
let sortedSpecial = [];
let sortedFiles = [];

const sortedSpecial = createSpecials(path)
.sort(sorter(sortBy, sortDir))
// FIXME: Optimize this to one chain!
sortedSpecial = createSpecials(path)
.map(modify);

const sortedDirectories = files.filter(file => file.isDirectory)
.sort(sorter(sortBy, sortDir))
.filter(filterHidden)
sortedFiles = files.filter(filterHidden)
.filter(filter)
.map(modify);

const sortedFiles = files.filter(file => !file.isDirectory)
.sort(sorter(sortBy, sortDir))
.filter(filterHidden)
.filter(filter)
.map(modify);
if(!serverSorting) {
if (['asc', 'desc'].indexOf(sortDir) === -1) {
sortDir = 'asc';
}
const sorter = sortMap[sortBy]
? sortMap[sortBy]
: sortFn('ascii');
sortedSpecial = sortedSpecial
.sort(sorter(sortBy, sortDir));
sortedFiles = sortedFiles
.sort(sorter(sortBy, sortDir));
}

return [
...sortedSpecial,
...sortedDirectories,
...sortedFiles
];
};
Expand Down
6 changes: 5 additions & 1 deletion src/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ const pathToObject = path => ({
...typeof path === 'string' ? {path} : path
});

// Extract the mountpoint of a path
const mountPoint = ({path}) => path.split(':/')[0];

// Handles directory listing result(s)
const handleDirectoryList = (path, options) => result =>
Promise.resolve(result.map(stat => createFileIter(stat)))
.then(result => transformReaddir(pathToObject(path), result, {
showHiddenFiles: options.showHiddenFiles !== false,
filter: options.filter
filter: options.filter,
serverSorting: capabilityCache[mountPoint(pathToObject(path))] ? capabilityCache[mountPoint(pathToObject(path))].sort : false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to Anders' judgement on this, but I think this could stand to be broken up with new lines since it is so long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to Anders' judgement on this, but I think this could stand to be broken up with new lines since it is so long.

How about below code? @ajmeese7 @andersevenrud

const handleDirectoryList = (path, options) => result =>
  Promise.resolve(result.map(stat => createFileIter(stat)))
    .then(result => {
      let capability = capabilityCache[mountPoint(pathToObject(path))];
      return transformReaddir(pathToObject(path), result, {
        showHiddenFiles: options.showHiddenFiles !== false,
        filter: options.filter,
        serverSorting: capability.sort ? capability.sort : false
      });
    });

Copy link
Member

@ajmeese7 ajmeese7 Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mahsashadi you can change:

serverSorting: capability.sort ? capability.sort : false

To the following:

serverSorting: capability.sort || false

To reduce the redundancy of it a bit.

Also I recommend changing the variable from a let to a const since there is no mutation of the contents.

}));

/**
Expand Down