hashdiff
hashdiff copied to clipboard
Unexpected difference between diffs of hashes vs. arrays
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?
Maybe you can try diff_array_lcs
https://github.com/liufengyun/hashdiff/blob/master/lib/hashdiff/diff.rb#L179
I get the same results using diff_array_lcs
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.
It works with: use_lcs: false
pry(main)> HashDiff.diff [1, 2, 3], [1, 10, 3], use_lcs: false
=> [["~", "[1]", 2, 10]]
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]]