From 1d6606ac8bc64d17b0c26e9d20e48bf7796bde64 Mon Sep 17 00:00:00 2001 From: quazardous Date: Sun, 8 Jan 2017 18:44:24 +0100 Subject: [PATCH] Switch sorting and parsing logic --- README.md | 1 - lib/readify.js | 21 +++++++-------------- test/readify.js | 3 +-- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f35ee0f..81cf39f 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ readify('/', (error, data) => { date: '20.02.2016', owner: 'coderaiser', mode: 'rw- rw- r--', - _raw: [object] // see bellow }] } }); diff --git a/lib/readify.js b/lib/readify.js index 54c167e..be64a45 100644 --- a/lib/readify.js +++ b/lib/readify.js @@ -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: @@ -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; } @@ -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); @@ -189,7 +183,6 @@ function parseStat(type, stat) { date: mtime, owner: owner, mode: mode && format.permissions.symbolic(mode), - raw: raw }; } @@ -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) diff --git a/test/readify.js b/test/readify.js index 7ab8b40..226a58c 100644 --- a/test/readify.js +++ b/test/readify.js @@ -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; }); @@ -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');