hashdiff icon indicating copy to clipboard operation
hashdiff copied to clipboard

Unexpected difference between diffs of hashes vs. arrays

Open dweebo opened this issue 6 years ago • 5 comments

I have inputs

a = [1, 2, 3]
b = [1, 10, 3]

and expect a diff of

[["~", "[1]", 2, 10 ]]

but instead I get

[["-", "[1]", 2], ["+", "[1]", 10]]

I expect that because of how hash comparisons work

a = {a:1, b:2, c:3}
b = {a:1, b:10, c:3}
[["~", "b", 2, 10]]

and

a = [1, {a: 0, b: 1}, 3]
b = [1, {a: 0, b: 2}, 3]
[["~", "[1].b", 1, 2]]

Is it possible to get the results I expect?

dweebo avatar Jul 30 '18 18:07 dweebo

Maybe you can try diff_array_lcs https://github.com/liufengyun/hashdiff/blob/master/lib/hashdiff/diff.rb#L179

liufengyun avatar Jul 30 '18 20:07 liufengyun

I get the same results using diff_array_lcs

dweebo avatar Jul 30 '18 20:07 dweebo

That probably means there are bugs in the implementation. Unfortunately, I don't have time budge to work on it. If you are interested in digging deeper and make a PR, that is always appreciated.

liufengyun avatar Jul 30 '18 21:07 liufengyun

It works with: use_lcs: false

pry(main)> HashDiff.diff [1, 2, 3], [1, 10, 3], use_lcs: false
=> [["~", "[1]", 2, 10]]

krzysiek1507 avatar Apr 18 '19 18:04 krzysiek1507

I think LCS doesn't really work here.

obj1 = [1, 1, 1, 2]
obj2 = [2, 1, 1, 1]

HashDiff.diff(obj1, obj2, { use_lcs: true })
=> [["+", "[0]", 2], ["-", "[4]", 2]]

HashDiff.diff(obj1, obj2, { use_lcs: false })
=> [["~", "[0]", 1, 2], ["~", "[3]", 2, 1]]

krzysiek1507 avatar Apr 22 '19 18:04 krzysiek1507