Fix directory sorting to correctly prioritize `..` before anything else
parent
e19279db73
commit
d6bdee2b6d
@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const naturalSort = require("natural-sort");
|
||||
const rfr = require("rfr");
|
||||
const priorityRules = rfr("lib/sorting/priority-rules");
|
||||
|
||||
module.exports = function() {
|
||||
let naturalSorter = naturalSort();
|
||||
|
||||
let ruleSorter = priorityRules([
|
||||
function(entry) {
|
||||
return entry.name === "..";
|
||||
},
|
||||
function(entry) {
|
||||
return entry.targetType === "folder";
|
||||
}
|
||||
]);
|
||||
|
||||
return function(entryA, entryB) {
|
||||
let result = ruleSorter(entryA, entryB);
|
||||
|
||||
if (result !== 0) {
|
||||
return result;
|
||||
} else {
|
||||
return naturalSorter(entryA.name, entryB.name);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(rules) {
|
||||
return function(entryA, entryB) {
|
||||
for (let i = 0; i < rules.length; i++) {
|
||||
let rule = rules[i];
|
||||
let resultA = !!(rule(entryA));
|
||||
let resultB = !!(rule(entryB));
|
||||
|
||||
if (resultA && !resultB) {
|
||||
return -1;
|
||||
} else if (resultB && !resultA) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue