From 08ce4ee4d9fb81fdbba0b5848d089ff105c418bd Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Wed, 3 Aug 2022 15:29:33 +0430 Subject: [PATCH 1/5] Add context-aware VFS sorting --- __tests__/utils/vfs.js | 6 +++--- src/utils/vfs.js | 45 ++++++++++++++++++++++-------------------- src/vfs.js | 2 +- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/__tests__/utils/vfs.js b/__tests__/utils/vfs.js index f37ade9..54ea68e 100644 --- a/__tests__/utils/vfs.js +++ b/__tests__/utils/vfs.js @@ -88,7 +88,7 @@ describe('utils.vfs#transformReaddir', () => { test('Should remove dotfiles', () => { expect(checkMap({ showHiddenFiles: false - })).toEqual(['..', 'directory', 'xdirectory', 'file', 'xfile']); + })).toEqual(['..', 'directory', 'file', 'xdirectory', 'xfile']); }); test('Should sort by descending order', () => { @@ -98,7 +98,7 @@ describe('utils.vfs#transformReaddir', () => { }); return expect(result) - .toEqual(['..', 'xdirectory', 'directory', 'xfile', 'file']); + .toEqual(['..', 'xfile', 'xdirectory', 'file', 'directory']); }); test('Should sort by ascending order', () => { @@ -108,7 +108,7 @@ describe('utils.vfs#transformReaddir', () => { }); return expect(result) - .toEqual(['..', 'directory', 'xdirectory', 'file', 'xfile']); + .toEqual(['..', 'directory', 'file', 'xdirectory', 'xfile']); }); test('Should sort by specified column', () => { diff --git a/src/utils/vfs.js b/src/utils/vfs.js index 2bd11e2..f699348 100644 --- a/src/utils/vfs.js +++ b/src/utils/vfs.js @@ -182,7 +182,13 @@ export const humanFileSize = (bytes, si = false) => { * @param {string} [options.sortDir='asc'] Sort in this direction * @return {Object[]} */ -export const transformReaddir = ({path}, files, options = {}) => { +export const transformReaddir = ({path}, files, capabilityCache, options = {}) => { + const mountPoint = path => path.split(':/')[0]; + let mountPointSort = false; + if(capabilityCache[mountPoint(path)] !== undefined) { + mountPointSort = capabilityCache[mountPoint(path)].sort; + } + options = { showHiddenFiles: false, sortBy: 'filename', @@ -195,44 +201,41 @@ export const transformReaddir = ({path}, files, options = {}) => { 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(!mountPointSort) { + 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 ]; }; diff --git a/src/vfs.js b/src/vfs.js index 79f8782..1eb7c52 100644 --- a/src/vfs.js +++ b/src/vfs.js @@ -72,7 +72,7 @@ const pathToObject = path => ({ // Handles directory listing result(s) const handleDirectoryList = (path, options) => result => Promise.resolve(result.map(stat => createFileIter(stat))) - .then(result => transformReaddir(pathToObject(path), result, { + .then(result => transformReaddir(pathToObject(path), result, capabilityCache, { showHiddenFiles: options.showHiddenFiles !== false, filter: options.filter })); From 54be856369f5c5c3cba9ece0d42f19ed5420b649 Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Tue, 9 Aug 2022 14:32:21 +0430 Subject: [PATCH 2/5] Add SortAscii and fix unit tests --- __tests__/utils/vfs.js | 4 ++-- src/utils/vfs.js | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/__tests__/utils/vfs.js b/__tests__/utils/vfs.js index 54ea68e..4cb781f 100644 --- a/__tests__/utils/vfs.js +++ b/__tests__/utils/vfs.js @@ -67,8 +67,8 @@ describe('utils.vfs#transformReaddir', () => { size: 666 }]; - const check = (options = {}) => vfs.transformReaddir({path: root}, input, options); - const checkMap = (options = {}, key = 'filename') => check(options).map(iter => iter[key]); + const check = (options = {}, capability = {}) => vfs.transformReaddir({path: root}, input, capability, options); + const checkMap = (options = {}, key = 'filename', capability = {}) => check(options, capability).map(iter => iter[key]); test('Should add parent directory', () => { expect(check({ diff --git a/src/utils/vfs.js b/src/utils/vfs.js index f699348..3108c4b 100644 --- a/src/utils/vfs.js +++ b/src/utils/vfs.js @@ -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 */ @@ -86,6 +93,8 @@ const sortFn = t => { return sortString; } else if (t === 'date') { return sortDate; + } else if (t === 'ascii') { + return sortAscii; } return sortDefault; @@ -184,10 +193,7 @@ export const humanFileSize = (bytes, si = false) => { */ export const transformReaddir = ({path}, files, capabilityCache, options = {}) => { const mountPoint = path => path.split(':/')[0]; - let mountPointSort = false; - if(capabilityCache[mountPoint(path)] !== undefined) { - mountPointSort = capabilityCache[mountPoint(path)].sort; - } + const mountPointSort = capabilityCache[mountPoint(path)] ? capabilityCache[mountPoint(path)].sort : false; options = { showHiddenFiles: false, From 11e4437738c91c8771f349a1c4ae07af65bac09f Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Wed, 10 Aug 2022 10:57:59 +0430 Subject: [PATCH 3/5] add serverSorting to options --- __tests__/utils/vfs.js | 15 +++++++++++++-- src/utils/vfs.js | 10 ++++------ src/vfs.js | 8 ++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/__tests__/utils/vfs.js b/__tests__/utils/vfs.js index 4cb781f..6bf1fec 100644 --- a/__tests__/utils/vfs.js +++ b/__tests__/utils/vfs.js @@ -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({ @@ -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, @@ -123,6 +133,7 @@ describe('utils.vfs#transformReaddir', () => { expect(every).toEqual(true); }); + }); describe('utils.vfs#getFileIcon', () => { diff --git a/src/utils/vfs.js b/src/utils/vfs.js index 3108c4b..7f73372 100644 --- a/src/utils/vfs.js +++ b/src/utils/vfs.js @@ -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; } @@ -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'; } diff --git a/src/vfs.js b/src/vfs.js index 1eb7c52..da16170 100644 --- a/src/vfs.js +++ b/src/vfs.js @@ -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 })); /** From 827cd3e77bd8eecb4bd49246fc70bc638c98a8cd Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Wed, 10 Aug 2022 11:06:47 +0430 Subject: [PATCH 4/5] Remove a blank line --- __tests__/utils/vfs.js | 1 - 1 file changed, 1 deletion(-) diff --git a/__tests__/utils/vfs.js b/__tests__/utils/vfs.js index 6bf1fec..4cd63ef 100644 --- a/__tests__/utils/vfs.js +++ b/__tests__/utils/vfs.js @@ -133,7 +133,6 @@ describe('utils.vfs#transformReaddir', () => { expect(every).toEqual(true); }); - }); describe('utils.vfs#getFileIcon', () => { From 5dd444756987228b2256ae5d727f6cd2232d67ae Mon Sep 17 00:00:00 2001 From: mahsa shadi Date: Sun, 28 Aug 2022 16:19:29 +0430 Subject: [PATCH 5/5] Improve readability of some code lines --- src/vfs.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/vfs.js b/src/vfs.js index da16170..7ec6824 100644 --- a/src/vfs.js +++ b/src/vfs.js @@ -75,11 +75,14 @@ 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, - serverSorting: capabilityCache[mountPoint(pathToObject(path))] ? capabilityCache[mountPoint(pathToObject(path))].sort : false - })); + .then(result => { + const capability = capabilityCache[mountPoint(pathToObject(path))]; + return transformReaddir(pathToObject(path), result, { + showHiddenFiles: options.showHiddenFiles !== false, + filter: options.filter, + serverSorting: capability.sort || false + }); + }); /** * Get vfs capabilities