Created
April 26, 2019 06:05
-
-
Save evilsoft/ff17e1bef598ee8bfea12063117d4ab4 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 Maybe from 'crocks/Maybe' | |
import Star from 'crocks/Star' | |
import prop from 'crocks/Maybe/prop' | |
import propOr from 'crocks/helpers/propOr' | |
import resultToMaybe from 'crocks/Maybe/resultToMaybe' | |
import tryCatch from 'crocks/Result/tryCatch' | |
const MaybeStar = | |
Star(Maybe) | |
// safeBody :: MaybeStar a b | |
const safeJSON = MaybeStar( | |
resultToMaybe(tryCatch(x => JSON.parse(x))) | |
) | |
// safeBody :: MaybeStar b c | |
const safeBody = MaybeStar( | |
prop('body') | |
) | |
// safeBody :: MaybeStar a Boolean | |
const flow = | |
safeBody | |
.compose(safeJSON) | |
.map(propOr(false, 'nice')) | |
flow | |
.runWith({ body: '{"nice": true}' }) | |
//=> Just true | |
flow | |
.runWith({ body: { fast: true } }) | |
//=> Nothing | |
flow | |
.runWith({ payload: '{"nice": true}' }) | |
//=> Nothing | |
flow | |
.runWith({ body: '{"hippy": true}' }) | |
//=> Just false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment