Created
August 26, 2016 07:32
-
-
Save williamcspace/18e1e55bcc7ed1ba1b6fc9bd55740757 to your computer and use it in GitHub Desktop.
Recursion with pure function - Tasks Runner example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = ['dist']; | |
var config = { | |
'dist': ['build', 'deploy'], | |
'build': ['js', 'css', 'version-rev'], | |
'js': ['lint','uglify'], | |
'css': ['sass', 'css-min'] | |
}; | |
var tasks = []; | |
//hell | |
input.forEach(task => { | |
if (config[task]){ | |
config[task].forEach(task =>{ | |
if (config[task]){ | |
config[task].forEach(task =>{ | |
if (config[task]){ | |
config[task].forEach(task =>{ | |
tasks.push(task); | |
}) | |
} else { | |
tasks.push(task); | |
} | |
}) | |
} else { | |
tasks.push(task); | |
} | |
}) | |
} else { | |
tasks.push(task); | |
} | |
}) | |
console.log(tasks); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = ['dist']; | |
var config = { | |
'dist': ['build', 'deploy'], | |
'build': ['js', 'css', 'version-rev'], | |
'js': ['lint','uglify'], | |
'css': ['sass', 'css-min'] | |
}; | |
//external state | |
var tasks = []; | |
getTasks(input); | |
//hard to test | |
function getTasks(input){ | |
input.forEach(task => { | |
if (config[task]){ | |
getTasks(config[task]); | |
} else { | |
tasks.push(task); | |
} | |
}); | |
} | |
console.log(tasks); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = ['build']; | |
var config = { | |
'build': ['js', 'css', 'version-rev']; | |
'js': ['lint','uglify'], | |
'css': ['sass', 'css-min'] | |
}; | |
var tasks = getTasks(config, input, []); | |
//pure function | |
function getTasks(config, input, initial){ | |
return input.reduce((prev, next) => { | |
if(config[next]){ | |
return getTasks(config, config[next], prev); | |
} else { | |
return prev.concat(next); | |
} | |
}, initial); | |
} | |
console.log(tasks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment