deep-object-diff icon indicating copy to clipboard operation
deep-object-diff copied to clipboard

Array order considered as not equal?

Open trainoasis opened this issue 1 year ago • 1 comments

Hey, thanks for the code. I'm trying to replace our custom implementation with this lib and I noticed that

filter1: {
    something: {
        values: ["Item-1", "Item-2"],
        operator: 1,
        extra: { data: "something", another: "xyz" },
    }
},
filter2: {
     something: {
        extra: { data: "something", another: "xyz" },
        operator: 1,
        values: ["Item-2", "Item-1"],
    },
}

these two are not considered equal when using diff() dye to array order in "values". detailedDiff confirms that:

Object {
       "added": Object {},
       "deleted": Object {},
       "updated":{"something":{"values":{"0":"Item-2","1":"Item-1"}}},
}

Is this expected? I didn't see any options to ignore the order in arrays for example, but that could probably be added, right?

I can close this if this is expected and won't be addressed.

PS: what I currently do to go around this is order all arrays within the object which is obviously adding yet another loop that's otherwise already happening in your code - but this could probably be simply added for each array in your codebase. LMK if you want me to make a PR.

detailedDiff(
  sortObjectArrays(filter1),
  sortObjectArrays(filter2)
)

Another thing I noticed is that in React, having undefined as value is problematic as the renderer just assumes empty object and removes keys with values undefined.

{
   added: {},
   deleted: {
       someProp: undefined,
   },
   updated: {},
}

this becomes just

{
   added: {},
   deleted: {},
   updated: {},
}

which effectively loses it's value unfortunately. Perhaps keys should be listed in an array that show what was removed?

trainoasis avatar Feb 12 '24 18:02 trainoasis