Created
July 6, 2017 23:48
-
-
Save jrnail23/90dbd307dbc69a06a42a88e3d03a4d57 to your computer and use it in GitHub Desktop.
objectDiff in Ramda
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
// 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