Add Array.remove extension

This commit is contained in:
Rory& 2025-10-03 20:30:45 +02:00
parent 7d2fd7b4f8
commit 8bd0d5c06b

View File

@ -22,6 +22,7 @@ declare global {
partition(filter: (elem: T) => boolean): [T[], T[]];
single(filter: (elem: T) => boolean): T | null;
forEachAsync(callback: (elem: T, index: number, array: T[]) => Promise<void>): Promise<void>;
remove(item: T): void;
}
}
@ -48,6 +49,13 @@ export async function forEachAsync<T>(array: T[], callback: (elem: T, index: num
await Promise.all(array.map(callback));
}
export function remove<T>(this: T[], item: T): void {
const index = this.indexOf(item);
if (index > -1) {
this.splice(index, 1);
}
}
// register extensions
if (!Array.prototype.containsAll)
Array.prototype.containsAll = function <T>(this: T[], target: T[]) {
@ -65,3 +73,7 @@ if (!Array.prototype.forEachAsync)
Array.prototype.forEachAsync = function <T>(this: T[], callback: (elem: T, index: number, array: T[]) => Promise<void>) {
return forEachAsync(this, callback);
};
if (!Array.prototype.remove)
Array.prototype.remove = function <T>(this: T[], item: T) {
return remove.call(this, item);
};