Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.