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

docs(collections): add the word "unique" to distinctBy JSDoc for grepping purposes (#6335) #6336

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions collections/distinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Returns all distinct elements in the given array, preserving order by first
* occurrence.
*
* Uniqueness is determined by [same-value-zero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality) equality.
*
* @typeParam T The type of the elements in the input array.
*
* @param array The array to filter for distinct elements.
Expand Down
28 changes: 15 additions & 13 deletions collections/distinct_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// This module is browser compatible.

/**
* Returns all elements in the given array that produce a distinct value using
* the given selector, preserving order by first occurrence.
* Returns all elements in the given array that produce a unique value using
* the given discriminator, with the first matching occurrence retained.
*
* Uniqueness is determined by same-value-zero equality of the returned values.
*
* @typeParam T The type of the elements in the input array.
* @typeParam D The type of the values produced by the selector function.
* @typeParam D The type of the values produced by the discriminator function.
*
* @param array The array to filter for distinct elements.
* @param selector The function to extract the value to compare for
* distinctness.
* @param discriminator The function to extract the value to compare for
* uniqueness.
*
* @returns An array of distinct elements in the input array.
*
Expand All @@ -19,22 +21,22 @@
* import { distinctBy } from "@std/collections/distinct-by";
* import { assertEquals } from "@std/assert";
*
* const names = ["Anna", "Kim", "Arnold", "Kate"];
* const exampleNamesByFirstLetter = distinctBy(names, (name) => name.charAt(0));
* const users = [{ id: 1, name: "Anna" }, { id: 2, name: "Kim" }, { id: 1, name: "Anna again" }];
* const uniqueUsers = distinctBy(users, (user) => user.id);
*
* assertEquals(exampleNamesByFirstLetter, ["Anna", "Kim"]);
* assertEquals(uniqueUsers, [{ id: 1, name: "Anna" }, { id: 2, name: "Kim" }]);
* ```
*/
export function distinctBy<T, D>(
array: Iterable<T>,
selector: (el: T) => D,
discriminator: (el: T) => D,
): T[] {
const selectedValues = new Set<D>();
const keys = new Set<D>();
const result: T[] = [];
for (const element of array) {
const selected = selector(element);
if (!selectedValues.has(selected)) {
selectedValues.add(selected);
const key = discriminator(element);
if (!keys.has(key)) {
keys.add(key);
result.push(element);
}
}
Expand Down
Loading