Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso
Last active January 4, 2019 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FernandoBasso/4945d4c121f2369cc1abf18b8b9a9ba9 to your computer and use it in GitHub Desktop.
Save FernandoBasso/4945d4c121f2369cc1abf18b8b9a9ba9 to your computer and use it in GitHub Desktop.
Example of turning a procedural-style code into a more functional one.
// Procedural
const getPhoneAreaAndNumber = ({ whatsappPhone, cellPhone, homePhone }) => {
if (whatsappPhone && isCellPhone(whatsappPhone)) return { area: getPhoneArea(whatsappPhone), number: getPhoneNumber(whatsappPhone) }
if (cellPhone && isCellPhone(cellPhone)) return { area: getPhoneArea(cellPhone), number: getPhoneNumber(cellPhone) }
if (homePhone && isCellPhone(homePhone)) return { area: getPhoneArea(homePhone), number: getPhoneNumber(homePhone) }
return { area: '', number: '' }
}
// We can turn the above into this:
// Functional
const getPhoneAreaAndNumber = ({ whatsappPhone, cellPhone, homePhone }) => pipe(
find(isCellPhone),
formatters.phone.format,
)([whatsappPhone, cellPhone, homePhone])
// Cool, huh‽
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment