Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.