Closed marksmccann closed 5 years ago
I attempted this. This following is the best I could come up with. I do not think this is any better than what exists currently. So, I'm gonna close this issue.
function reduceTasksByOutFile(tasks) {
const sourceType = tasks[0].data ? 'data' : 'file';
return Array.from(
tasks
.reduce((outFileTasks, task) => {
let reducedTask = outFileTasks.get(task.outFile);
if (!reducedTask) {
reducedTask = { ...task, [sourceType]: [] };
}
reducedTask[sourceType].push(task[sourceType]);
outFileTasks.set(task.outFile, reducedTask);
return outFileTasks;
}, new Map())
.values()
).map(task => {
const sources = task[sourceType];
let taskSourceType = sourceType;
let source = sources[0];
if (sources.length > 1) {
if (sourceType === 'data') {
source = sources.join('\n');
} else {
delete task.file;
taskSourceType = 'data';
source = joinSourceFiles(sources);
}
}
return { ...task, [taskSourceType]: source };
});
}
I have a hunch that I could simplify the
reduceTasksByOutFile
method via some of these few js features I have recently become more familiar with: