Created
November 26, 2020 17:50
-
-
Save AitorAlejandro/7c2d46af843dff1aa497d74dc059d2cf to your computer and use it in GitHub Desktop.
Composition Example
This file contains hidden or 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
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x) | |
// example | |
const upperFirst = (srt) => srt.charAt(0).toUpperCase() + srt.slice(1); | |
const users = [ | |
{ id: 1, name: 'aitor', lastName: 'alejandro' } | |
]; | |
const first = x => x[0]; | |
const capitalizaNameAndLastname = x => ({ | |
name: upperFirst(x.name), | |
lastName: upperFirst(x.lastName), | |
}); | |
const fullName = x => `${x.name} ${x.lastName}`; | |
const getNombreCompleto = (users) => { | |
return compose( | |
fullName, | |
capitalizaNameAndLastname, | |
first, | |
)(users) | |
}; | |
getNombreCompleto(users); // Aitor Alejandro |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment