Skip to content

Instantly share code, notes, and snippets.

@evilsoft
Created April 26, 2019 06:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilsoft/ff17e1bef598ee8bfea12063117d4ab4 to your computer and use it in GitHub Desktop.
Save evilsoft/ff17e1bef598ee8bfea12063117d4ab4 to your computer and use it in GitHub Desktop.
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