Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions __tests__/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ describe('utils.vfs#transformReaddir', () => {
size: 666
}];

const check = (options = {}, capability = {}) => vfs.transformReaddir({path: root}, input, capability, options);
const checkMap = (options = {}, key = 'filename', capability = {}) => check(options, capability).map(iter => iter[key]);
const check = (options = {}) => vfs.transformReaddir({path: root}, input, options);
const checkMap = (options = {}, key = 'filename') => check(options).map(iter => iter[key]);

test('Should add parent directory', () => {
expect(check({
Expand All @@ -91,6 +91,16 @@ describe('utils.vfs#transformReaddir', () => {
})).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', () => {
const result = checkMap({
showHiddenFiles: false,
Expand Down Expand Up @@ -123,6 +133,7 @@ describe('utils.vfs#transformReaddir', () => {

expect(every).toEqual(true);
});

});

describe('utils.vfs#getFileIcon', () => {
Expand Down
10 changes: 4 additions & 6 deletions src/utils/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,16 @@ export const humanFileSize = (bytes, si = false) => {
* @param {string} [options.sortDir='asc'] Sort in this direction
* @return {Object[]}
*/
export const transformReaddir = ({path}, files, capabilityCache, options = {}) => {
const mountPoint = path => path.split(':/')[0];
const mountPointSort = capabilityCache[mountPoint(path)] ? capabilityCache[mountPoint(path)].sort : false;

export const transformReaddir = ({path}, files, options = {}) => {
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;
}
Expand All @@ -227,7 +225,7 @@ export const transformReaddir = ({path}, files, capabilityCache, options = {}) =
.filter(filter)
.map(modify);

if(!mountPointSort) {
if(!serverSorting) {
if (['asc', 'desc'].indexOf(sortDir) === -1) {
sortDir = 'asc';
}
Expand Down
8 changes: 6 additions & 2 deletions 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, capabilityCache, {
.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