Skip to content

Instantly share code, notes, and snippets.

@josephfinlayson
Created February 10, 2017 11:00
Show Gist options
  • Save josephfinlayson/a7489dcba94840004e159c47e9425c13 to your computer and use it in GitHub Desktop.
Save josephfinlayson/a7489dcba94840004e159c47e9425c13 to your computer and use it in GitHub Desktop.
Difference between objects
import R from 'ramda'
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 diffObjs = 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