-
Notifications
You must be signed in to change notification settings - Fork 7
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
Can this be more general? #8
Comments
https://github.com/tc39/proposal-collection-normalization seems relevant. |
@ljharb It seems collection normalization will convert the values but |
What i mean is, that approach could be used for a new collection hook that override the comparator (which defaults to SameValueZero) |
Not sure how it could be used to override the comparator --- As I understand, it's not override the comparator but just change the values, so u may need a map to keep the mappings from the converted values to original values. |
The normalization proposal can not, you’re correct. I’m suggesting the approach of “an options bag passed to the constructor that allows overriding internal algorithms”. |
So it would be |
I’m not sure why it wouldn’t be efficient as-is. |
This is exactly what I thought when I read " @hax No, it would be |
@ljharb Because comparator could be complex (eg. deep equal). @bergus I think we have similar thought about that. |
As others have pointed out, we already have
[...new Set(array)]
for primitives. So, I think that the question this proposal is addressing can be generalized to, "use something other than===
to find duplicate objects in a Set".Here are a couple other potential ways to solve this problem off the top of my head (I haven't thought them through; these are just ideas):
[...new Set(array, { comparator: (a, b) => a.id === b.id })]
-- this would set a comparator on the Set to be used for all future additions to that specific Set.Object.prototype[@@setKey]
-- if present on an object,[@@setKey]
should be used instead of the object pointer when checking for equality in the Set.The text was updated successfully, but these errors were encountered: