Skip to content

Instantly share code, notes, and snippets.

@diogojorgebasso
Created January 12, 2021 21:27
Show Gist options
  • Save diogojorgebasso/d6e66ecb707c94b2c71a7cb2ca625d40 to your computer and use it in GitHub Desktop.
Save diogojorgebasso/d6e66ecb707c94b2c71a7cb2ca625d40 to your computer and use it in GitHub Desktop.
How to create composition for N functions in Javascript
function composition(...functions){
return function(value){
return functions.reduce((acc, fn) =>fn(acc), value)
}
}//very flexible
function scream(text){
return text.toUpperCase()
}
function emphasize(text){
return `${text}!!!`
}
function splitChar(text){
return text.split('').join(' ')
}
const actionPhrase = composition(
scream,
emphasize,
splitChar
)
//TEST: console.log(actionPhrase('Testing'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment