Open Valian opened 4 weeks ago
Hi @Valian
I checkout jsondiffpatch. It sounds like a neat feature. I will plan it for version 2.3.
I wonder how much work it would take for CursorAI to implement 😅
I may try to give it a quick shot and see.
My time is a heavily limited but the feature is not forgotten. :laughing:
I've tried working on this, but it's not trivial. We have to generate removals, additions, and then adjust indexes of all elements and see if any "moves" are necessary.
Well by saying "tried working on this" I meant having a lengthy discussion with CursorAI and getting some starting implementation and tests, but sadly it's not really up to the standard I'd like it to be. And still has bugs 😅
In my head it would require to create a full hash-to-index map for source and then use it while checking the target only for moves. All detected moves will be ignored in the next step where the regular diff algorithm will run to detect changes, additions and deletions. But so far I had no chance to test it. 😕
Hmm... would it work for my initial example?
Eg. element with id 1 moved from index: 0
to index: 1
but that was not a real move, rather a result of inserting another item at index 0.
So personally I'm thinking about getting that full hash-to-index map, finding all additions and removals, and then updating the indexes of that map so we can find "real" moves (that are not results of additions / removals).
So in the previous example, after discovering insertion at index 0 we would increase
indexes of all source elements which are >= 0 to accomodate for that. And then checking for moves will properly give us nothing.
Oh, yes. The expected new index needs to be calculated for all subsequent items.
Is your feature request related to a problem? Please describe.
Currently items in lists are compared by index.
In some cases, list patches might be much smaller if we would use unique identifier present in items to distinguish if they were moved around. For example:
Output:
Describe the solution you'd like I'd love to have additional option to diff being a function returning unique identifier for a given object
&object_hash/1
that would trigger different diffing mechanism for lists.Output I'd like to get for the previous example:
Describe alternatives you've considered Some JS libraries support such option, eg jsondiffpatch
Additional context I'm planning to use
json_patch
in LiveVue to reduce payload size. Already playing with it. It's very common that items in lists have unique identifiers (eg.id
) that should make it possible to create much more optimized patches.