jsondiffpatch icon indicating copy to clipboard operation
jsondiffpatch copied to clipboard

Detect renamed keys

Open domoritz opened this issue 7 years ago • 3 comments

I'd like to have a small diff for when keys change.

screen shot 2018-03-05 at 14 10 53

I set objectHash: JSON.stringify. Is there another option to detect when only the key is renamed?

domoritz avatar Mar 05 '18 22:03 domoritz

objectHash is only used among items on the same array, not with objects. that type of change seem hard to detect in a generic way, especially when changes are nested, in the example it seems trivial, but how much similarity between 2 branches of the graph would indicate the keys swapped?

to detect key changes, I'd recommend to convert encoding to an array, (and have key be another property of the array items) if that's possible (using the objectHash you mention). you can always convert to object map afterwards.

otherwise it's always possible to write a jsondiffpatch plugin that adds new type of diffs, but that's not going to be trivial.

benjamine avatar Mar 06 '18 01:03 benjamine

Are you suggestions an objectHash method that ignores the key property? That's a great idea. How do I convert the data back to an object map afterwards and make sure the diff visualization still works?

domoritz avatar Mar 06 '18 18:03 domoritz

correct, but you got me there, the visualization wouldn't show it as an object if you convert to array to generate the diff, I guess I was assuming, that while not ideal, the visual representation is still OK as an array to represent this in your case.

benjamine avatar Mar 06 '18 19:03 benjamine