Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Created June 18, 2020 15:25
Show Gist options
  • Save joalbertg/563a3f38c353a18fd281816c4a04ebd0 to your computer and use it in GitHub Desktop.
Save joalbertg/563a3f38c353a18fd281816c4a04ebd0 to your computer and use it in GitHub Desktop.
javascript: composition
const capitalize = str => (
str.replace(/\w\S*/g, txt =>
txt.charAt(0)
.toUpperCase() + txt.substr(1).toLowerCase())
);
const users = [{
id: 1, name: 'joalbert', surname: 'gonzález'
}];
//const getFullName = _users => {
// const first = _users[0];
// const capitalized = {
// name: capitalize(first.name),
// surname: capitalize(first.surname)
// }
// return `${capitalized.name} ${capitalized.surname}`;
//}
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
const head = _users => _users[0];
const capitalized = ({name, surname}) => ({
name: capitalize(name),
surname: capitalize(surname)
});
const print = ({name, surname}) => `${name} ${surname}`;
const getFullName_v1 = _users => print(capitalized(head(_users)));
//const getFullName_v2 = _users => compose(print, capitalized, head)(_users);
const getFullName_v2 = compose(print, capitalized, head);
console.log(getFullName_v1(users));
console.log(getFullName_v2(users));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment