Tixit / odiff

Gets a list of differences between two javascript values.
MIT License
88 stars 13 forks source link

Help - how do I actually use the diff between two objects #5

Closed tonebender closed 6 years ago

tonebender commented 6 years ago

Hi,

Maybe a stupid question, but I would like to know if this is something that odiff can do ... I have two objects a and b, and I want to get a resulting third object that contains only the contents that differ between a and b (or, in other words, the variables that are not in both a and b).

My objects can be rather complicated with nested json-sourced data, but here's a simple example:

let a = {x: 1, y: 2, z: 3}; let b = {y: 2}; diff(a, b) // {x: 1, z: 3}

Is this something that can be done with odiff? If so, how do I call it?

If not, do you have any suggestions for other methods or libraries for this?

Thanks!!

fresheneesz commented 6 years ago

This is definitely something that odiff can do. Did you try out the library? You'd do something like let diff = odiff(b,a) and diff would then contain an array containing those two differences. Check the readme for details on how that works.

tonebender commented 6 years ago

Thanks for the reply!

I've tested it a bit now, but can't wrap my head around what odiff outputs... If I take the above example again, odiff(a,b) gives the array: [ { "type": "set", "path": [ "x" ], "val": 1 }, { "type": "set", "path": [ "z" ], "val": 3 } ]

But is there a quick way to get the actual plain diff, which I believe is {x: 1, z: 3} ?

Thanks!

fresheneesz commented 6 years ago

Ah, i see what you're looking for. But no, odiff doesn't have that functionality. One reason is that simple format you gave an example of can't describe many cases. For example, what if you want to get the diff of {a:1, b:2} and {a:1, c:3}. How do you describe that removal? With a simple result format like in your example, you lose information in those cases.

fresheneesz commented 6 years ago

Closing cause sounds like maybe the issue was addressed