Last active
November 11, 2016 16:20
-
-
Save davidchase/31a9e5722a657be7592ca871e214839d to your computer and use it in GitHub Desktop.
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
import {curryN, has, values, is} from 'ramda' | |
// given a error object from a third party source | |
// which may not guarantee structure | |
// quick search via recursion to find property | |
const err = { | |
system: { | |
status: { | |
err: { | |
response: 'boom', | |
id: 12 | |
}, | |
code: 400 | |
} | |
} | |
} | |
// does not have system parent | |
const err2 = { | |
status: { | |
err: { | |
response: 'boom', | |
id: 12 | |
}, | |
code: 400 | |
} | |
} | |
const findProp = curryN(2, (prop, obj) => has(prop, obj) ? obj[prop] : values(obj).reduce((acc, value) => is(Object, value) ? findProp(prop, value) : acc, {})) | |
findProp('response', err) // => 'boom' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment