Hello,
This PR adds new initializers that accept an arbitrary array that may include elements with duplicated IDs which will be deduped in the resulting identified array.
Motivation
Unfortunately is not uncommon to receive lists with unexpected duplicated IDs from backend APIs that are outside our control. In those cases the default and convenient usage of IdentifiedArray results in a runtime crash which is not good for the users.
To solve this the developer is forced to preemptively preprocess the array to remove duplicated and then initialize the IdentifiedArray. This can get cumbersome and may result in not ideal performance. A solution for this can be built outside the library as suggested by Stephen, and even though that's what I've done for now, I consider this a decently common scenario hence my proposing the inclusion of a solution in the library itself.
2 inits
The API I want to use in my code is the convenient form that accepts the arbitrary array and deduplicates it without me having to specify how to do it. In this my expectation is that the first added element is the one that takes priority, which surfaces from some common scenarios like paginating a BE API.
But I admit that this may not be what everybody wants and may not be an API surface worth exposing in the library. For that reason the other init is similar to the one in Dictionary which allows the developer to decide how to resolve the conflicts (thanks Stephen for the suggestion).
I've added both in this PR, the convenient built on top of the other one, but if the library authors prefer to keep only the dictionary-like init I'm happy to remove the other one and keep it as a helper in my code, even in that case being able to use a public init that doesn't crash and takes care of duplicates is a big win.
Docs
I've tried to replicate the docs that are already in the library with some changes where appropriate and taking some inspiration from the Dictionary docs.
@alexito4 Thanks for taking the time to PR! This looks great so we're going to merge as is and will make a few tweaks afterwards:
We'll probably workshop the naming a bit.
We'll probably ditch the overload that always takes the first value. We think it's probably best to explicitly pass the closure at the call site and announce the strategy for dealing with duplicate IDs.
Hello, This PR adds new initializers that accept an arbitrary array that may include elements with duplicated IDs which will be deduped in the resulting identified array.
Motivation
Unfortunately is not uncommon to receive lists with unexpected duplicated IDs from backend APIs that are outside our control. In those cases the default and convenient usage of
IdentifiedArray
results in a runtime crash which is not good for the users.To solve this the developer is forced to preemptively preprocess the array to remove duplicated and then initialize the
IdentifiedArray
. This can get cumbersome and may result in not ideal performance. A solution for this can be built outside the library as suggested by Stephen, and even though that's what I've done for now, I consider this a decently common scenario hence my proposing the inclusion of a solution in the library itself.2 inits
The API I want to use in my code is the convenient form that accepts the arbitrary array and deduplicates it without me having to specify how to do it. In this my expectation is that the first added element is the one that takes priority, which surfaces from some common scenarios like paginating a BE API.
But I admit that this may not be what everybody wants and may not be an API surface worth exposing in the library. For that reason the other init is similar to the one in
Dictionary
which allows the developer to decide how to resolve the conflicts (thanks Stephen for the suggestion).I've added both in this PR, the convenient built on top of the other one, but if the library authors prefer to keep only the dictionary-like init I'm happy to remove the other one and keep it as a helper in my code, even in that case being able to use a public init that doesn't crash and takes care of duplicates is a big win.
Docs
I've tried to replicate the docs that are already in the library with some changes where appropriate and taking some inspiration from the Dictionary docs.
Tests
I've written tests for all variants of the inits.
Slack thread