cosmicanant/recursive-diff

before/after applydiff returning the same data

Closed this issue · 2 comments

Title says it all. Hoping someone can take a look. Thanks.

Screen Shot 2020-10-05 at 1 00 55 AM

https://codepen.io/KevinBatdorf/pen/601484f72bf013bcc851e5fa39ba4c75?editors=1010

I would expect the fresh variable to be { number: 0, history: [] }

hey, @KevinBatdorf I did not get the issue. I think you are doing the other way around. applyDiff just work like git apply ANY_DIFF_FILL.diff. So you can apply diff on the initial value and get the changed value ( and NOT the initial value)

var rdiff = require('recursive-diff')

var initialVal = {number: 0, path: [] }
var changedVal = { number: 7, path: [7] }
var diff = rdiff.getDiff(initialVal, changedVal)

var appliedDiff = rdiff.applyDiff(initialVal,  diff)  // this will be logged as: {number: 7, path: [7]} 

I think for your particular use case, you can swap the function argument order to get the desired result. eg: diff = rdiff.getDiff(current, initial) if you apply this diff on current you will get the fresh variable as { number: 0, history: [] }

Hi,

I was essentially trying to create an "undo" component so that I could undo changes made over time.

I went with another library so not sure I can be of much help here. I used the same approach there except I ended up keeping track of the new state separately, so maybe that was what was needed here.

I can help debug if someone else comes here with the same issue, but otherwise I'll just close it for now.

Thanks