Safely Access Nested Object Properties with `propPath`

InstructorAndy Van Slaars

Share this video with your friends

Send Tweet

In this lesson, we’ll look at the propPath utility function. We’ll ask for a property multiple levels deep in an object and get back a Maybe. We’ll get a Just when the property exists at our path and a Nothing if any part of the path is undefined.

Paweł Waszczyński
~ 5 years ago

Is it possible to make it look like:

getPostalCode(user) // returns real postal code OR not available

and put option as a part of postal code? I looked for call function in crocks but I didn't found it. Otherwise I would look something similar to:

// call option function with argument `not available` on result of propPath(['address', 'postalCode']
const getPostalCode = compose(call('not available', 'option'), propPath(['address', 'postalCode'])
const zip = getPostalCode(user)