Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing partial objects in ramda.js

There is an equals function in Ramdajs which is totally awesome, it will provide the following:

// (1) true
R.equals({ id: 3}, { id: 3})

// (2) true
R.equals({ id: 3, name: 'freddy'}, { id: 3, name: 'freddy'})

// (3) false
R.equals({ id: 3, name: 'freddy'}, { id: 3, name: 'freddy', additional: 'item'});

How would I go about enhancing this function, or in some other way produce a true result for number 3

I would like to ignore all the properties of the rValue not present in the lValue, but faithfully compare the rest. I would prefer the recursive nature of equals remain intact - if that's possible.

I made a simple fiddle that shows the results above.

like image 423
Jim Avatar asked May 04 '16 06:05

Jim


2 Answers

There's a constraint on equals in order to play nicely with the Fantasy Land spec that requires the symmetry of equals(a, b) === equals(b, a) to hold, so to satisfy your case we'll need to get the objects into some equivalent shape for comparison.

We can achieve this by creating a new version of the second object that has had all properties removed that don't exist in the first object.

const intersectObj = (a, b) => pick(keys(a), b)

// or if you prefer the point-free edition
const intersectObj_ = useWith(pick, [keys, identity])

const a = { id: 3, name: 'freddy' },
      b = { id: 3, name: 'freddy', additional: 'item'}

intersectObj(a, b) // {"id": 3, "name": "freddy"}

Using this, we can now compare both objects according to the properties that exist in the first object a.

const partialEq = (a, b) => equals(a, intersectObj(a, b))

// again, if you prefer it point-free
const partialEq_ = converge(equals, [identity, intersectObj])

partialEq({ id: 3, person: { name: 'freddy' } },
          { id: 3, person: { name: 'freddy' }, additional: 'item'})
//=> true

partialEq({ id: 3, person: { name: 'freddy' } },
          { id: 3, person: { age: 15 }, additional: 'item'})
//=> false
like image 76
Scott Christopher Avatar answered Sep 23 '22 00:09

Scott Christopher


Use whereEq

From the docs: "Takes a spec object and a test object; returns true if the test satisfies the spec, false otherwise."

whereEq({ id: 3, name: 'freddy' }, { id: 3, name: 'freddy', additional: 'item' })

The other way around is to develop your own version. It boils down to:

if (is object):
  check all keys - recursive
otherwise:
  compare using `equals`

This is recursive point-free version that handles deep objects, arrays and non-object values.

const { equals, identity, ifElse, is, mapObjIndexed, useWith, where } = R

const partialEquals = ifElse(
  is(Object),
  useWith(where, [
    mapObjIndexed(x => partialEquals(x)),
    identity,
  ]),
  equals,
)

console.log(partialEquals({ id: 3 }, { id: 3 }))
console.log(partialEquals({ id: 3, name: 'freddy' }, { id: 3, name: 'freddy' }))
console.log(partialEquals({ id: 3, name: 'freddy' }, { id: 3, name: 'freddy', additional: 'item' }))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
like image 36
Tymek Avatar answered Sep 24 '22 00:09

Tymek