Skip to content

Instantly share code, notes, and snippets.

@jrnail23
Created July 6, 2017 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrnail23/90dbd307dbc69a06a42a88e3d03a4d57 to your computer and use it in GitHub Desktop.
Save jrnail23/90dbd307dbc69a06a42a88e3d03a4d57 to your computer and use it in GitHub Desktop.
objectDiff in Ramda
// https://github.com/ramda/ramda/wiki/Cookbook#diffobjs---diffing-objects-similar-to-guavas-mapsdifference
const groupObjBy = R.curry(R.pipe(
// Call groupBy with the object as pairs, passing only the value to the key function
R.useWith(R.groupBy, [R.useWith(R.__, [R.last]), R.toPairs]),
R.map(R.fromPairs)
))
const objectDiff = R.pipe(
R.useWith(R.mergeWith(R.merge), [R.map(R.objOf('leftValue')), R.map(R.objOf('rightValue'))]),
groupObjBy(R.cond([
[
R.both(R.has('leftValue'), R.has('rightValue')),
R.pipe(R.values, R.ifElse(R.apply(R.equals), R.always('common'), R.always('difference')))
],
[R.has('leftValue'), R.always('onlyOnLeft')],
[R.has('rightValue'), R.always('onlyOnRight')]
])),
R.evolve({
common: R.map(R.prop('leftValue')),
onlyOnLeft: R.map(R.prop('leftValue')),
onlyOnRight: R.map(R.prop('rightValue'))
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment