Tixit / odiff

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

Index for adding multiple things in an array before and after existing. #35

Closed dallinbjohnson closed 5 months ago

dallinbjohnson commented 5 months ago

the index for the items that are added after the item that is already in the array is the wrong index.

example:

odiff({ a: ['b'] }, { a: ['a', 'b', 'c', 'd'] })

result:

[ { "type": "add", "path": [ "a" ], "index": 1, "vals": [ "c", "d" ] }, { "type": "add", "path": [ "a" ], "index": 0, "vals": [ "a" ] } ]

the index of "vals": [ "c", "d" ] should be 2.

fresheneesz commented 5 months ago

The indexes are correct. The way they're done is so that if you go through each diff item in order and do what it says to the first object, you'll get the second object as a result. Inserting ['c', 'd'] at index 1 of object.a will give you {a:['b','c','d']}, and then inserting ['a'] at index 0 gives you {a:['a','b','c','d']} as you would expect.

dallinbjohnson commented 5 months ago

That makes more sense why you did what yall did. I am writing some code that diffs two objects and then async iterates over the changes and sends database requests using that index and it comes out wrong sometimes because of it. I think I will have to change my code to be synchronous to account for how you are handling this index problem for me.