marksmccann / node-sass-extra

A drop-in replacement for node-sass' Node API that adds support for globs, promises and more.
MIT License
2 stars 1 forks source link

Refactor `reduceTasksByOutfile` #51

Closed marksmccann closed 5 years ago

marksmccann commented 5 years ago

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:

marksmccann commented 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 };
    });
}