Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1x 4x 1x 4x 4x 10x 10x 2x 8x 2x | import { identity, Mapper } from "@benneq/function";
import { Predicate } from "@benneq/predicate";
/**
* Creates a {@link Predicate} that checks if an {@link Iterable} contains only
* unique values.
*
* @example
* ```ts
* const iterable = [1,2,3,2];
* const result = isDistinct()(iterable);
* console.log(result); // false
* ```
*
* @param keyExtractor - the optional key extractor, defaults to {@link identity}
* @returns the new {@link Predicate}
*/
export const isDistinct =
<T>(keyExtractor: Mapper<T, unknown> = identity): Predicate<[Iterable<T>]> =>
(iterable, seen = new Set(), key: unknown = undefined) => {
for (const value of iterable) {
key = keyExtractor(value);
if (seen.has(key)) {
return false;
}
seen.add(key);
}
return true;
};
|