remove distinct by

This commit is contained in:
MathMan05
2025-11-25 23:54:33 -06:00
parent bbd75f1e6c
commit bcef42fe1f
2 changed files with 0 additions and 34 deletions
-12
View File
@@ -40,16 +40,4 @@ describe("Array extensions", () => {
assert.deepEqual(arr.distinct(), [1, 2, 3]);
assert.deepEqual([].distinct(), []);
});
it("distinctBy", () => {
const arr = [{ id: 1 }, { id: 2 }, { id: 1 }, { id: 3 }];
assert.deepEqual(
arr.distinctBy((x) => x.id),
[{ id: 1 }, { id: 2 }, { id: 3 }],
);
assert.deepEqual(
[].distinctBy((x) => x),
[],
);
});
});
-22
View File
@@ -23,7 +23,6 @@ declare global {
filterAsync(callback: (elem: T, index: number, array: T[]) => Promise<boolean>): Promise<T[]>;
remove(item: T): void;
distinct(): T[];
distinctBy<K>(key: (elem: T) => K): T[];
}
}
@@ -55,23 +54,6 @@ export function arrayDistinct<T>(this: T[]): T[] {
return Array.from(new Set(this));
}
export function arrayDistinctBy<T, K>(this: T[], key: (elem: T) => K): T[] {
const seen = new Set<K>();
return this.filter((item) => {
const k = key(item);
if (seen.has(k)) {
return false;
} else {
seen.add(k);
return true;
}
});
}
export function arrayIntersect<T>(this: T[], other: T[]): T[] {
return this.filter((value) => other.includes(value));
}
// register extensions
if (!Array.prototype.partition)
Array.prototype.partition = function <T>(this: T[], filter: (elem: T) => boolean) {
@@ -95,7 +77,3 @@ if (!Array.prototype.distinct)
Array.prototype.distinct = function <T>(this: T[]) {
return arrayDistinct.call(this);
};
if (!Array.prototype.distinctBy)
Array.prototype.distinctBy = function <T, K>(this: T[], key: (elem: T) => K) {
return arrayDistinctBy.call(this, key as (elem: unknown) => unknown);
};