Skip to content

Commit

Permalink
Switch sorting and parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
quazardous committed Jan 8, 2017
1 parent d404ad3 commit 1d6606a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ readify('/', (error, data) => {
date: '20.02.2016',
owner: 'coderaiser',
mode: 'rw- rw- r--',
_raw: [object] // see bellow
}]
}
});
Expand Down
21 changes: 7 additions & 14 deletions lib/readify.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const readdir = promisify(fs.readdir, fs);
/* sorting on Win and node v0.8.0 */
const sortFiles = currify((attr, order, array) => {
switch (order) {
case 'desc':
case 'asc':
// nothing
break;
case 'asc':
case 'desc':
// nothing
break;
default:
Expand All @@ -61,12 +61,7 @@ const sortFiles = currify((attr, order, array) => {
cmp = (a, b) => a.localeCompare(b.attr);
}
return sort((a, b) => {
var res;
if (typeof a.raw !== 'undefined' && typeof b.raw !== 'undefined') {
res = cmp(a.raw[attr], b.raw[attr]);
} else {
res = cmp(a[attr], b[attr]);
}
var res = cmp(a[attr], b[attr]);
if (order === 'desc') {
res = -res;
}
Expand Down Expand Up @@ -164,16 +159,15 @@ function _parseAllStats(type, array) {
function parseStat(type, stat) {
const isDir = stat.isDirectory();
const size = isDir ? 'dir' : stat.size;
const raw = {

if (type === 'raw')
return {
name: stat.name,
size: size,
date: stat.mtime,
owner: stat.uid,
mode: stat.mode
};

if (type === 'raw')
return raw;

/* Переводим права доступа в 8-ричную систему */
const modeStr = Number(stat.mode).toString(8);
Expand All @@ -189,7 +183,6 @@ function parseStat(type, stat) {
date: mtime,
owner: owner,
mode: mode && format.permissions.symbolic(mode),
raw: raw
};
}

Expand All @@ -199,7 +192,7 @@ function parseStat(type, stat) {
* @param params - { files, stats, path }
*/
function fillJSON(path, stats, options, callback) {
const processFiles = squad(changeOrder, sortFiles(options.sort, options.order), parseAllStats(options.type));
const processFiles = squad(changeOrder, parseAllStats(options.type), sortFiles(options.sort, options.order));
const json = {
path: '',
files: processFiles(stats)
Expand Down
3 changes: 1 addition & 2 deletions test/readify.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ test('readify: result', (t) => {

readify('.', (error, result) => {
result.files = result.files.map(function(file) {
delete file.raw;
delete file.owner;
return file;
});
Expand Down Expand Up @@ -237,7 +236,7 @@ test('result: files should have fields name, size, date, owner, mode', (t) => {
length = files.length,
check = () =>
files.filter((file) =>
Object.keys(file).join(':') === 'name:size:date:owner:mode:raw'
Object.keys(file).join(':') === 'name:size:date:owner:mode'
).length;

t.notOk(error, 'no error');
Expand Down

0 comments on commit 1d6606a

Please sign in to comment.