From 1a527bec42f28fc00d65af543beb6aa85831b4fa Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 10 Jan 2017 15:34:50 +0200 Subject: [PATCH] refactor(readify) replaceFromList --- lib/readify.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/readify.js b/lib/readify.js index 0727e5b..167bab1 100644 --- a/lib/readify.js +++ b/lib/readify.js @@ -156,7 +156,7 @@ function fillJSON(path, stats, type, callback) { return callback(null, json); changeUIDToName(json, (error, files) => { - json.files = files; + json.files = files || json.files; callback(null, json); }); } @@ -166,21 +166,12 @@ function changeUIDToName(json, callback) { callback(null, json.files); else nicki((error, names) => { - let files; - if (error) - files = json.files.slice(); - else - files = json.files.map((file) => { - const owner = file.owner; - - if (names[owner]) - file.owner = names[owner]; - - return file; - }); + return callback(error); + + const files = replaceFromList(names, 'owner', json.files); - callback(error, files); + callback(null, files); }); } @@ -203,3 +194,16 @@ function changeOrder(json) { return sorted; } +function replaceFromList(obj, prop, array) { + return array.map((a) => { + const n = a[prop]; + const data = obj[n]; + + if (!data) + return a; + + return Object.assign(a, { + [prop]: data + }); + }) +}