Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created November 26, 2020 17:50
Show Gist options
  • Save AitorAlejandro/7c2d46af843dff1aa497d74dc059d2cf to your computer and use it in GitHub Desktop.
Save AitorAlejandro/7c2d46af843dff1aa497d74dc059d2cf to your computer and use it in GitHub Desktop.
Composition Example
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