Skip to content

Commit

Permalink
refactor(readify) replaceFromList
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jan 10, 2017
1 parent 30ed482 commit 1a527be
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/readify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand All @@ -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);
});
}

Expand All @@ -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
});
})
}

0 comments on commit 1a527be

Please sign in to comment.