summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorMathMan05 <mathmanrm@gmail.com>2025-11-25 23:01:52 -0600
committerMathMan05 <mathmanrm@gmail.com>2025-11-25 23:01:52 -0600
commit685552579b13873f70202a3388bc4981f0c750a9 (patch)
tree78e616e7bda947bbcda98e80e0f298a484bba3a2 /src/util
parentremove first/last (diff)
downloadserver-ts-685552579b13873f70202a3388bc4981f0c750a9.tar.xz
get rid of single
Diffstat (limited to 'src/util')
-rw-r--r--src/util/entities/User.ts4
-rw-r--r--src/util/util/extensions/Array.test.ts13
-rw-r--r--src/util/util/extensions/Array.ts13
3 files changed, 3 insertions, 27 deletions
diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts

index 4343aaf4a..afa7c00e4 100644 --- a/src/util/entities/User.ts +++ b/src/util/entities/User.ts
@@ -354,9 +354,9 @@ export class User extends BaseClass { for (const channel of qry) { console.warn(JSON.stringify(channel)); } + throw new Error("Array contains more than one matching element"); } - // throw if multiple - return qry.single((_) => true); + return qry[0]; } } diff --git a/src/util/util/extensions/Array.test.ts b/src/util/util/extensions/Array.test.ts
index 45c010384..454197c1f 100644 --- a/src/util/util/extensions/Array.test.ts +++ b/src/util/util/extensions/Array.test.ts
@@ -24,19 +24,6 @@ describe("Array extensions", () => { assert.deepEqual(odd, [1, 3, 5]); }); - it("single", () => { - const arr = [1, 2, 3, 4, 5]; - assert.strictEqual( - arr.single((n) => n === 3), - 3, - ); - assert.strictEqual( - arr.single((n) => n === 6), - null, - ); - assert.throws(() => arr.single((n) => n > 2)); - }); - it("forEachAsync", async () => { const arr = [1, 2, 3]; let sum = 0; diff --git a/src/util/util/extensions/Array.ts b/src/util/util/extensions/Array.ts
index 95ad0f3bf..d061edf96 100644 --- a/src/util/util/extensions/Array.ts +++ b/src/util/util/extensions/Array.ts
@@ -20,7 +20,6 @@ declare global { interface Array<T> { containsAll(target: T[]): boolean; 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>; filterAsync(callback: (elem: T, index: number, array: T[]) => Promise<boolean>): Promise<T[]>; remove(item: T): void; @@ -43,13 +42,6 @@ export function arrayPartition<T>(array: T[], filter: (elem: T) => boolean): [T[ return [pass, fail]; } -export function arraySingle<T>(array: T[], filter: (elem: T) => boolean): T | null { - const results = array.filter(filter); - if (results.length > 1) throw new Error("Array contains more than one matching element"); - if (results.length === 0) return null; - return results[0]; -} - export async function arrayForEachAsync<T>(array: T[], callback: (elem: T, index: number, array: T[]) => Promise<void>): Promise<void> { await Promise.all(array.map(callback)); } @@ -108,10 +100,7 @@ if (!Array.prototype.partition) Array.prototype.partition = function <T>(this: T[], filter: (elem: T) => boolean) { return arrayPartition(this, filter); }; -if (!Array.prototype.single) - Array.prototype.single = function <T>(this: T[], filter: (elem: T) => boolean) { - return arraySingle(this, filter); - }; + if (!Array.prototype.forEachAsync) Array.prototype.forEachAsync = function <T>(this: T[], callback: (elem: T, index: number, array: T[]) => Promise<void>) { return arrayForEachAsync(this, callback);