Last active
January 4, 2019 19:29
-
-
Save FernandoBasso/4945d4c121f2369cc1abf18b8b9a9ba9 to your computer and use it in GitHub Desktop.
Example of turning a procedural-style code into a more functional one.
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
// 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