Last active
August 25, 2017 12:21
-
-
Save amwmedia/2c12b9f664b331b8c4d670048da50a9a to your computer and use it in GitHub Desktop.
Glean... what you want from any data source.
This file contains 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
/** | |
* glean - Access nested paths in an object whos structure we are uncertain of. | |
* Unholy marriage of Facebook's existential function and lodash get() | |
* https://facebook.github.io/react-native/blog/2017/03/13/idx-the-existential-function.html | |
* https://lodash.com/docs#get | |
* | |
* @param {Object} source the source object we are operating on | |
* @param {Function} accessor attepts to access the property desired | |
* @param {Any} defaultValue = null value to return if the accessor throws | |
* @return {Any} | |
*/ | |
glean(source, accessor, defaultValue = null) { | |
try { | |
const val = accessor(source); | |
if (typeof val === 'undefined') { return defaultValue; } | |
return val; | |
} catch(err) { | |
return defaultValue; | |
} | |
} |
Author
amwmedia
commented
Aug 25, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment