Skip to content

Commit

Permalink
Refactor: optimize ArrayPoly.filterUniqueMapped() further (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Jun 11, 2024
1 parent c08e57a commit ed1f267
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/polyfill/arrayPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@ export default class ArrayPoly {
public static filterUniqueMapped<T, V>(
mapper: (arg: T) => V,
): (value: T, idx: number, values: T[]) => boolean {
const mappedValues = new Map<V, number>();
return (value: T, idx: number, values: T[]): boolean => {
if (mappedValues.size === 0) {
values.forEach((val, valIdx) => {
const mapped = mapper(val);
if (!mappedValues.has(mapped)) {
mappedValues.set(mapped, valIdx);
}
});
const seenMappedValues = new Set<V>();
return (value: T): boolean => {
const mapped = mapper(value);
if (!seenMappedValues.has(mapped)) {
seenMappedValues.add(mapped);
return true;
}
return mappedValues.get(mapper(value)) === idx;
return false;
};
}

Expand Down

0 comments on commit ed1f267

Please sign in to comment.