Created
January 26, 2017 12:47
-
-
Save sharpred/c58ab2029239de8f8c27708ac0cf45fe to your computer and use it in GitHub Desktop.
use of ramda fantasy Maybe for deep inspection of an object property
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
/* | |
* use of ramda fantasy Maybe for deep inspection of an object property | |
* see https://github.com/ramda/ramda-fantasy/blob/master/docs/Maybe.md | |
* for more info | |
*/ | |
const R = require("ramda"); | |
const M = require("ramda-fantasy").Maybe; | |
const Just = M.Just; | |
const Nothing = M.Nothing; | |
const response = { | |
"ota-api": | |
{ | |
serverError: | |
{ | |
error: 1902, | |
message: "ERROR_ACCESS_DEVICE_ALREADY_AUTHENTICATE", | |
exception: false, | |
authenticated: true | |
}, serverInformation: { | |
beginTime: "2017-01-26T10:07:04.936Z", | |
endTime: "2017-01-26T10:07:04.941Z", | |
serverTime: 1485425224941, | |
duration: 5 | |
} | |
}, debug: { | |
body: { | |
accessDeviceToken: "NmUwMzY1MGY0ZGU1M2I3NDYyYmE0RFNTU5NDZDQUIzNkEzQTU5Mzc3QkNGQjQ=" | |
}, | |
headers: { | |
Accept: "application/json; charset=utf8", | |
"auth-apiKey": "YOURAPIKEY", | |
"auth-signature": "SOMEMADEUPVALUE", "auth-timestamp": 1485425224791, "auth-token": "ANOTHERMADEUPVALUE", "Content-Type": "application/json; charset=utf8" | |
}, | |
preHashedSignature: "yada" | |
} | |
}; | |
const lookup = R.curry((k, obj) => k in obj ? Just(obj[k]) : Nothing()); | |
console.log(lookup("ota-api", response).chain(lookup('serverError')).chain(lookup("authenticated")).getOrElse(false)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment