From f00f2f777cdc02227ad48e78f6f9ddab66f6f8ea Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Mon, 25 Feb 2019 18:17:41 +0000 Subject: [PATCH 1/5] fix: use localeCompare to sort files --- src/bundles/files.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/bundles/files.js b/src/bundles/files.js index d4d9d92bd..b8b7866ed 100644 --- a/src/bundles/files.js +++ b/src/bundles/files.js @@ -21,18 +21,6 @@ export const sorts = { BY_SIZE: 'size' } -function compare (a, b, asc) { - const strings = typeof a === 'string' && typeof b === 'string' - - if (strings ? a.toLowerCase() > b.toLowerCase() : a > b) { - return asc ? 1 : -1 - } else if (strings ? a.toLowerCase() < b.toLowerCase() : a < b) { - return asc ? -1 : 1 - } else { - return 0 - } -} - const make = (basename, action) => (...args) => async (args2) => { const id = Symbol(basename) const { dispatch, getIpfs, store } = args2 @@ -361,9 +349,23 @@ export default (opts = {}) => { content: pageContent.content.sort((a, b) => { if (a.type === b.type || isMac) { if (sorting.by === sorts.BY_NAME) { - return compare(a.name, b.name, sorting.asc) + a = a.name + b = b.name + + return sorting.asc + ? a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }) + : b.localeCompare(a, undefined, { numeric: true, sensitivity: 'base' }) } else { - return compare(a.cumulativeSize || a.size, b.cumulativeSize || b.size, sorting.asc) + a = a.cumulativeSize || a.size + b = b.cumulativeSize || b.size + + if (a > b) { + return sorting.asc ? 1 : -1 + } else if (a < b) { + return sorting.asc ? -1 : 1 + } else { + return 0 + } } } From d1f383ff9a9a4837b38c235a2bf1f8f42e4d3450 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Tue, 26 Feb 2019 11:47:54 +0000 Subject: [PATCH 2/5] chore: extract sort logic --- src/bundles/files.js | 19 +++---------------- src/lib/sort.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 src/lib/sort.js diff --git a/src/bundles/files.js b/src/bundles/files.js index b8b7866ed..21879af9d 100644 --- a/src/bundles/files.js +++ b/src/bundles/files.js @@ -2,6 +2,7 @@ import { join, dirname } from 'path' import { createSelector } from 'redux-bundler' import { getDownloadLink, getShareableLink, filesToStreams } from '../lib/files' import countDirs from '../lib/count-dirs' +import { sortByName, sortBySize } from '../lib/sort' const isMac = navigator.userAgent.indexOf('Mac') !== -1 @@ -349,23 +350,9 @@ export default (opts = {}) => { content: pageContent.content.sort((a, b) => { if (a.type === b.type || isMac) { if (sorting.by === sorts.BY_NAME) { - a = a.name - b = b.name - - return sorting.asc - ? a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }) - : b.localeCompare(a, undefined, { numeric: true, sensitivity: 'base' }) + return sortByName(sorting.asc ? 1 : -1)(a.name, b.name) } else { - a = a.cumulativeSize || a.size - b = b.cumulativeSize || b.size - - if (a > b) { - return sorting.asc ? 1 : -1 - } else if (a < b) { - return sorting.asc ? -1 : 1 - } else { - return 0 - } + return sortBySize(sorting.asc ? 1 : -1)(a.cumulativeSize || a.size, b.cumulativeSize || b.size) } } diff --git a/src/lib/sort.js b/src/lib/sort.js new file mode 100644 index 000000000..648cd9f7f --- /dev/null +++ b/src/lib/sort.js @@ -0,0 +1,17 @@ +/** + * Natural sort comparator for strings. + * + * @param {Number} dir - sorting direction, 1 for ascending or -1 for descending + * @param {Object} opts - localeCompare options (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) + */ +export function sortByName (dir = 1, opts = { numeric: true, sensitivity: 'base' }) { + return (a, b) => a.localeCompare(b, undefined, opts) * dir +} + +/** + * Numerical sort comparator. + * @param {Number} dir - sorting direction, 1 for ascending or -1 for descending + */ +export function sortBySize (dir = 1) { + return (a, b) => (a - b) * dir +} From 2d67998ab4b722a35524025479c769fbbc9960c9 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Tue, 26 Feb 2019 11:51:27 +0000 Subject: [PATCH 3/5] dealing with my ocd --- src/lib/sort.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/sort.js b/src/lib/sort.js index 648cd9f7f..5c1f185c6 100644 --- a/src/lib/sort.js +++ b/src/lib/sort.js @@ -10,6 +10,7 @@ export function sortByName (dir = 1, opts = { numeric: true, sensitivity: 'base' /** * Numerical sort comparator. + * * @param {Number} dir - sorting direction, 1 for ascending or -1 for descending */ export function sortBySize (dir = 1) { From 87d775027cefa4bc937e552709f92248f2137b41 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Wed, 27 Feb 2019 10:47:09 +0000 Subject: [PATCH 4/5] chore: simplify sort --- src/bundles/files.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bundles/files.js b/src/bundles/files.js index 21879af9d..9097048c6 100644 --- a/src/bundles/files.js +++ b/src/bundles/files.js @@ -340,6 +340,7 @@ export default (opts = {}) => { selectFiles: (state) => { const { pageContent, sorting } = state.files + const sortDir = sorting.asc ? 1 : -1 if (pageContent === null || pageContent.type === 'file') { return pageContent @@ -350,9 +351,9 @@ export default (opts = {}) => { content: pageContent.content.sort((a, b) => { if (a.type === b.type || isMac) { if (sorting.by === sorts.BY_NAME) { - return sortByName(sorting.asc ? 1 : -1)(a.name, b.name) + return sortByName(sortDir)(a.name, b.name) } else { - return sortBySize(sorting.asc ? 1 : -1)(a.cumulativeSize || a.size, b.cumulativeSize || b.size) + return sortBySize(sortDir)(a.cumulativeSize || a.size, b.cumulativeSize || b.size) } } From 5b1a0dc96512279013e3eb3fda5a996fb2d64bd7 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Thu, 28 Feb 2019 11:11:58 +0000 Subject: [PATCH 5/5] fix: create fn outside loop --- src/bundles/files.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bundles/files.js b/src/bundles/files.js index 9097048c6..f44316504 100644 --- a/src/bundles/files.js +++ b/src/bundles/files.js @@ -341,6 +341,8 @@ export default (opts = {}) => { selectFiles: (state) => { const { pageContent, sorting } = state.files const sortDir = sorting.asc ? 1 : -1 + const nameSort = sortByName(sortDir) + const sizeSort = sortBySize(sortDir) if (pageContent === null || pageContent.type === 'file') { return pageContent @@ -351,9 +353,9 @@ export default (opts = {}) => { content: pageContent.content.sort((a, b) => { if (a.type === b.type || isMac) { if (sorting.by === sorts.BY_NAME) { - return sortByName(sortDir)(a.name, b.name) + return nameSort(a.name, b.name) } else { - return sortBySize(sortDir)(a.cumulativeSize || a.size, b.cumulativeSize || b.size) + return sizeSort(a.cumulativeSize || a.size, b.cumulativeSize || b.size) } }