Codacy is telling me this method has a complexity of 13. I need some tips on how to reduce it, more specifically inside the forEach loop.
I've already changed it from an if/else` if hell, but it seems it wasn't enough.
function addGenresAndThemesFromWork(work){
let workDetails = {workId: work.$.id, genres: [], themes: []};
let workInfo = work.info;
if (typeof workInfo === 'object') {
Object.keys(workInfo).forEach(function (key) {
let workfInfoType = workInfo[key].$.type;
let workInfoValue = workInfo[key]._;
switch (workfInfoType.toLowerCase()) {
case 'genres':
workDetails.genres.push(workInfoValue);
if (Genres.findOne({name: workInfoValue}) === undefined) {
Genres.insert({
name: workInfoValue
});
}
break;
case 'themes':
workDetails.themes.push(workInfoValue);
if (Themes.findOne({name: workInfoValue.toLowerCase()}) === undefined) {
Themes.insert({
name: workInfoValue.toLowerCase()
});
}
break;
case 'main title':
workDetails.name = workInfoValue;
break;
case 'plot summary':
workDetails.plot = workInfoValue;
break;
case 'objectionable content':
if (workInfoValue.toLowerCase() === 'ma') {
workDetails.mature = true;
}
break;
case 'picture':
let workImg = workInfo[key].img[1];
if (workImg !== undefined) {
workDetails.picture = workImg.$;
} else {
if (workInfo[key].img[0] !== undefined) {
workDetails.picture = workInfo[key].img[0].$;
}
}
break;
case 'alternative title':
if (workInfo[key].$.lang.toLowerCase() === 'ja') {
workDetails.alternativeTitle = workInfoValue;
}
break;
}
});
}
if (!_.has(workDetails, 'picture')) {
workDetails.picture = {src: '/default.jpg', temporary: true};
}
persistWorkDetails(workDetails);
}