From d38a7eab165c1cf4ca8d71918718fd4bf093f0e9 Mon Sep 17 00:00:00 2001 From: quazardous Date: Sun, 8 Jan 2017 21:44:11 +0100 Subject: [PATCH] try to be less messy --- lib/readify.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/readify.js b/lib/readify.js index be64a45..b475c39 100644 --- a/lib/readify.js +++ b/lib/readify.js @@ -32,6 +32,11 @@ const readdir = promisify(fs.readdir, fs); /* sorting on Win and node v0.8.0 */ const sortFiles = currify((attr, order, array) => { + const cmpCallbacks = { + 'numeric': (a, b) => (+a - +b), + 'local_string': (a, b) => a.localeCompare(b.attr), + 'default': (a, b) => (a > b ? 1 : -1), + }; switch (order) { case 'asc': // nothing @@ -45,20 +50,20 @@ const sortFiles = currify((attr, order, array) => { var cmp; switch (attr) { case 'size': - cmp = (a, b) => (+a - +b); + cmp = cmpCallbacks.numeric; break; case 'date': - cmp = (a, b) => (a > b ? 1 : -1); + cmp = cmpCallbacks['default']; break; case 'owner': - cmp = (a, b) => (a > b ? 1 : -1); + cmp = cmpCallbacks['default']; break; case 'name': - cmp = (a, b) => a.localeCompare(b.attr); + cmp = cmpCallbacks.local_string; break; default: attr = 'name'; - cmp = (a, b) => a.localeCompare(b.attr); + cmp = cmpCallbacks.local_string; } return sort((a, b) => { var res = cmp(a[attr], b[attr]);