-
-
Save newtriks/439ab614869e30583c4fbb71d558ccba to your computer and use it in GitHub Desktop.
Kleisli composition example (JSON parsing + deep 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
// following along https://medium.com/@luijar/kliesli-compositions-in-javascript-7e1a7218f0c4 | |
const R = require('ramda') | |
const Maybe = require('ramda-fantasy').Maybe | |
// parse JSON string into object | |
// parse :: String -> Object | Null | |
function parse(s) { | |
try { | |
return JSON.parse(s) | |
} catch (e) { | |
return null | |
} | |
} | |
// prop :: (Object, String) -> Object | String | Null | |
const prop = (obj, name) => obj[name] | |
// maybeParseJson :: String -> Maybe | |
const maybeParseJson = json => Maybe.toMaybe(parse(json)) | |
// maybeProp :: String -> Object -> Maybe | |
const maybeProp = prop => obj => Maybe.toMaybe(obj[prop]) | |
// convert a "simple" function to return Maybe | |
// https://github.com/ramda/ramda-fantasy/blob/master/docs/Maybe.md#maybeof | |
const maybeUpper = R.compose(Maybe.of, R.toUpper) | |
// a typical user object is | |
/* | |
{ | |
user: | |
{ | |
address: | |
{ | |
state: "fl" | |
} | |
} | |
} | |
*/ | |
// getStateCode :: String -> Maybe | |
const getStateCode = R.composeK( | |
maybeUpper, | |
maybeProp('state'), | |
maybeProp('address'), | |
maybeProp('user'), | |
maybeParseJson | |
) | |
module.exports = getStateCode |
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
// testing getStateCode | |
const getStateCode = require('./index') | |
it('extracts from valid string', () => { | |
const s = '{"user": {"address": {"state": "in"}}}' | |
const state = getStateCode(s).getOrElse('an error') | |
console.assert(state === 'IN', state) | |
}) | |
it('handles invalid JSON string', () => { | |
// notice missing quotes | |
const s = '{"user": {"address": {state: in}}}' | |
const state = getStateCode(s).getOrElse('an error') | |
console.assert(state === 'an error', state) | |
}) | |
it('handles missing property', () => { | |
// notice missing address | |
const s = '{"user": {}}' | |
const state = getStateCode(s).getOrElse('an error') | |
console.assert(state === 'an error', state) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment