Comments (8)
Sounds a nice feature to have, especially if Map does already implement this!
from immutable-js.
collection.equals
is the same (at least for the user) as Immutable.is(), would probably have to change both.
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:
x.get("a") === y.get("a")
might be fine on first level, but then it goes into the next depth level and a
might be meaningless.
Fun fact:
the example could be replaced by x.get("a") === y.get("a");
. But ===
isn't a good operator if "a" is an immutable object itself, it would have to rely on the immutable is
or equals
itself: x.get("a").equals(y.get("a"));
from immutable-js.
Ah I see, that's good to know! If equals
only cares about the first level, then perhaps an array of keys to omit on the first level would be acceptable?
something like
const x = Map({a: 1, b:2})
const y = Map({a:1, b:5})
x.equals(y, ["b"]); // true, only "a" is compared
Just as a side note, I am trying to run npm run test
on this repo locally, but have this error message pop up, seems to be from dtslint
dependency. Anywhere I can go to resolve these issues or help get up and running? my node version is 20.5.0
and npm version is 10.2.4
. Thanks in advance
update: found this knex/knex#5631
but i tried with node 14, node 18.15.0, and node 20, and they all have same error
from immutable-js.
Ah I see, that's good to know! If
equals
only cares about the first level, then perhaps an array of keys to omit on the first level would be acceptable? something like
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 false
.
But for an end-user to implement a custom equals, depth is likely important and probably annoying to deal with.
To deal with such a "similar-ish" comparison, I believe that a simple forEach
or x.get('b') === y.get('b')
is likely easier.
But feel free to provide a Pull-Request 😁
from immutable-js.
Sounds a nice feature to have, especially if Map does already implement this!
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?
from immutable-js.
Thanks for your input!!
Was able to play around with the deepEquals
method a bit. Probably my initial idea of a callback would be quite complicated and less useful. But perhaps an optional array of keys to omit would be a better idea?
For example:
const list1 = List([Map({a:1,b:2,c:3}), 4,5]);
const list2 = List([Map({a:10,b:11,c:3}), 4,5])
expect(list1.equals(list2, ["a", "b"])).toBe(true);
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 deepEquals
, but I guess that's why I wanted to discuss a bit before investing too much time into it :D Thanks again for your insights!
from immutable-js.
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 get
or other immutable methods, but you can also use it as a plain object :
myRecord.someKey
from immutable-js.
Just a side
const list1 = List([Map({a:1,b:2,c:3}), 4,5]); const list2 = List([Map({a:10,b:11,c:3}), 4,5]) expect(list1.equals(list2, ["a", "b"])).toBe(true);
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:
const isNotEquals = ["a", "b"].some((key) => mapA.get(key) !== mapB.get(key))
from immutable-js.
Related Issues (20)
- Converting a Seq to a list causes RangeError (max call size exceeded) HOT 1
- 4.2.0: partition missing in flow HOT 3
- Question: Wiki article for upgrading to v4 HOT 3
- `toJS` breaks type compatibility between records with object values HOT 8
- Wrong typescript declarations for `.update()` method (continued) HOT 2
- Record with more keys should be assignable to Record with less keys if type is a subset of the first one
- An argument for 'reviver' was not provided. HOT 4
- ReferenceError: Immutable is not defined HOT 9
- Highlight inputs where `Set` can't maintain uniqueness HOT 4
- Flow type definition outdated, not working with last year changes HOT 1
- Urgent: Package Installation Reveals Multiple Vulnerabilities HOT 1
- Type predicate missing on isOrderedSet function type declaration
- support deno HOT 2
- immutable default import in 5.x
- version 4.3.3 breaks TS/Angular/webpack HOT 2
- Typescript: Implement `MapOf::merge `
- Extract sort function from Collection types HOT 6
- Strange edge case with Map.update(...) HOT 3
- Moving off deprecated dependencies HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from immutable-js.