Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array order considered as not equal? #100

Open
trainoasis opened this issue Feb 12, 2024 · 0 comments
Open

Array order considered as not equal? #100

trainoasis opened this issue Feb 12, 2024 · 0 comments

Comments

@trainoasis
Copy link

trainoasis commented Feb 12, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant