-
Notifications
You must be signed in to change notification settings - Fork 5
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
add optionnable sorting capability #2
Changes from all commits
71b8e98
8e99e96
a6cd387
d4b676b
caaaeb5
0ee33e5
bffb42b
d404ad3
1d6606a
97a8903
d38a7ea
7f11afd
3a6b4aa
85970c2
c8e3687
73e6639
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,7 @@ node_modules | |
legacy | ||
npm-debug.log | ||
coverage | ||
*.swp | ||
/.project | ||
/.*.md.html | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,25 +30,79 @@ const nicki = !WIN && !BROWSER && require('nicki/legacy'); | |
|
||
const readdir = promisify(fs.readdir, fs); | ||
|
||
// http://www.jstips.co/en/sorting-strings-with-accented-characters/ | ||
const sortFiles = sort((a, b) => { | ||
return a.name.localeCompare(b.name); | ||
/* sorting on Win and node v0.8.0 */ | ||
const sortFiles = currify((attr, order, array) => { | ||
const cmpCallbacks = { | ||
'numeric': (a, b) => (+a - +b), | ||
// http://www.jstips.co/en/sorting-strings-with-accented-characters/ | ||
'local_string': (a, b) => a.localeCompare(b), | ||
'default': (a, b) => (a > b ? 1 : -1), | ||
}; | ||
switch (order) { | ||
case 'asc': | ||
// nothing | ||
break; | ||
case 'desc': | ||
// nothing | ||
break; | ||
default: | ||
order = 'asc'; | ||
} | ||
var cmp; | ||
switch (attr) { | ||
case 'size': | ||
cmp = cmpCallbacks.numeric; | ||
break; | ||
case 'date': | ||
cmp = cmpCallbacks['default']; | ||
break; | ||
case 'owner': | ||
cmp = cmpCallbacks['default']; | ||
break; | ||
case 'name': | ||
cmp = cmpCallbacks.local_string; | ||
break; | ||
default: | ||
attr = 'name'; | ||
cmp = cmpCallbacks.local_string; | ||
} | ||
return sort((a, b) => { | ||
var res = cmp(a[attr], b[attr]); | ||
if (order === 'desc') { | ||
res = -res; | ||
} | ||
return res; | ||
}, array); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think sort-by can be used for sorting. It can simplify code a little bit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not but no commit since 1 year ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sort by seams very straightforward. We could fork it and write a backport with 'classic' attribute access. |
||
|
||
const good = (f) => (...a) => f(null, ...a); | ||
|
||
module.exports = readify; | ||
|
||
function readify(path, type, fn) { | ||
function readify(path, options, fn) { | ||
if (!fn) { | ||
fn = type; | ||
type = ''; | ||
fn = options; | ||
options = ''; | ||
} | ||
if (typeof options !== 'object') { | ||
options = { | ||
type: options, | ||
}; | ||
} | ||
if (typeof options.sort === 'undefined') { | ||
options.sort = ''; | ||
} | ||
if (typeof options.order === 'undefined') { | ||
options.order = ''; | ||
} | ||
if (typeof options.type === 'undefined') { | ||
options.type = ''; | ||
} | ||
|
||
check(path, fn); | ||
|
||
readdir(path) | ||
.then(getAllStats(path, type)) | ||
.then(getAllStats(path, options)) | ||
.then(good(fn)) | ||
.catch(fn); | ||
} | ||
|
@@ -68,20 +122,20 @@ function check(path, callback) { | |
* @param path | ||
* @param names | ||
*/ | ||
function _getAllStats(path, type, names, callback) { | ||
function _getAllStats(path, options, names, callback) { | ||
const length = names.length; | ||
const dir = format.addSlashToEnd(path); | ||
|
||
if (!length) | ||
return fillJSON(dir, [], type, callback); | ||
return fillJSON(dir, [], options, callback); | ||
|
||
const funcs = names.map((name) => { | ||
return getStat(name, dir + name); | ||
}); | ||
|
||
exec.parallel(funcs, (...args) => { | ||
const files = args.slice(1); | ||
fillJSON(dir, files, type, callback); | ||
fillJSON(dir, files, options, callback); | ||
}); | ||
} | ||
|
||
|
@@ -144,16 +198,16 @@ function parseStat(type, stat) { | |
* | ||
* @param params - { files, stats, path } | ||
*/ | ||
function fillJSON(path, stats, type, callback) { | ||
const processFiles = squad(changeOrder, sortFiles, parseAllStats(type)); | ||
function fillJSON(path, stats, options, callback) { | ||
const processFiles = squad(changeOrder, parseAllStats(options.type), sortFiles(options.sort, options.order)); | ||
const json = { | ||
path: '', | ||
files: processFiles(stats) | ||
}; | ||
|
||
json.path = format.addSlashToEnd(path); | ||
|
||
if (type === 'raw') | ||
if (options.type === 'raw') | ||
return callback(null, json); | ||
|
||
changeUIDToName(json, (error, files) => { | ||
|
@@ -208,4 +262,3 @@ function replaceFromList(obj, prop, array) { | |
}); | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
111 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
33 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What difference between
default
andnumeric
sorting?What purpose of
localeCompare
? As I understand from mdn withoutlocales
argument it will work in the same way asdefault
sorting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not much, it enforces the numeric cast with the
dir
value. But since I've switched formatting it maybe useless now.localCompare is for accents. I'm french and I deal with accentuated letters like é è ë ê. Those letters have the same rank than e. Standard comparison put them after z.
http://www.jstips.co/en/sorting-strings-with-accented-characters/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is interesting. Did not know about it.
Can you show me please how
ls -lha
sorts accents?