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
Enhancement: Custom equals #1969
Comments
Sounds a nice feature to have, especially if Map does already implement this! |
Implementing a real custom equals might become annoying quickly when using nested collections, mixed types or ordered collections. The "normal" equals does not need to care about depth and path of the comparison, but the custom callback would need to care for that: Fun fact: |
Ah I see, that's good to know! If
Just as a side note, I am trying to run update: found this knex/knex#5631 |
Sorry, that's a misunderstanding: What I wanted to say is that the internal deepEquals does not need to care about depth, it is simply called recursively and applies equals on everything. A single miss results in a But for an end-user to implement a custom equals, depth is likely important and probably annoying to deal with. But feel free to provide a Pull-Request 😁 |
Sorry I misread your initial post. I though that Map did implement this already! Just to understand the need here : is there a, real difference between your proposal a.equals(b, (a, B) => a.get('foo') === b.get('foo')) And an external function in your code? const compare Foes = (a, B) => a.get('foo') === b.get('foo'))
compareFoos(a, b) What I would like to put here is : is this really an immutable feature that might be useful for everyone or non? |
Thanks for your input!! For example:
implementation-wise, although deepEquals is not aware of depth, it seems to be aware of the key, where we can skip the iteration of comparison for any keys matching the "omit" array option, regardless of depth. The use case could possibly be what I had initially mentioned, sometimes we hydrate models with data from the API on update , where fields that we did not initially have (since we didn't fetch them on page load) are populated with data. However, it could be convenient to have an option to compare equality only a subset of the fields within the models with each other. It doesn't seem like a trivial task to modify |
For the record, we have a similar case, and knowing the keys, we do use the Record structure, which is slightly easier in that case : you can use myRecord.someKey |
Just a side
neither list does have a or b, only an element at position 0, test should fail. How would we even decide when a list is equal? If it has more than one element, relevance of order and position are very much dependent on your personal use case. Is it still equals when one has more elements than the others? How to figure out which ones fit together? As Julien mentioned, shallowly comparing records and maps is easy with some/someKey:
|
Enhancement request:
custom equals
allow
.equals
to accept a callback, for example:Consider the above for more complex structures such as Immutable Records, it would be helpful to be able to compare only certain fields within a collection. Sometimes, API responses may not be in the exact same shape as data stored in client state, and populating the immutable structures with these data that just have minor discrepancies causes
.equals()
to return false, when really we are interested in only certain fields.Any thoughts on this? ^
The text was updated successfully, but these errors were encountered: