From 6b383350a6de6d26b62cf62f619c89ffb0d6b0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= <9092381+Renegade334@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:37:45 +0100 Subject: [PATCH] types(collection): reduce* method signatures (#10405) * types(collection): reduce* method signatures * test: explicit expect() types * test: add tests for arbitrary accumulator type --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../collection/__tests__/collection.test.ts | 75 +++++++++++-------- packages/collection/src/collection.ts | 18 ++++- 2 files changed, 62 insertions(+), 31 deletions(-) diff --git a/packages/collection/__tests__/collection.test.ts b/packages/collection/__tests__/collection.test.ts index e0073ac07..0ea401fd4 100644 --- a/packages/collection/__tests__/collection.test.ts +++ b/packages/collection/__tests__/collection.test.ts @@ -713,14 +713,57 @@ describe('reduce() tests', () => { expect(sum).toStrictEqual(6); }); + test('reduce collection into a single value with different accumulator type', () => { + const str = coll.reduce((a, x) => a.concat(x.toString()), ''); + expect(str).toStrictEqual('123'); + }); + test('reduce empty collection with initial value', () => { const coll = createCollection(); - expect(coll.reduce((a, x) => a + x, 0)).toStrictEqual(0); + expect(coll.reduce((a, x) => a + x, 0)).toStrictEqual(0); }); test('reduce empty collection without initial value', () => { const coll = createCollection(); - expect(() => coll.reduce((a: number, x) => a + x)).toThrowError( + expect(() => coll.reduce((a, x) => a + x)).toThrowError( + new TypeError('Reduce of empty collection with no initial value'), + ); + }); +}); + +describe('reduceRight() tests', () => { + const coll = createTestCollection(); + + test('throws if fn is not a function', () => { + // @ts-expect-error: Invalid function + expectInvalidFunctionError(() => coll.reduceRight()); + // @ts-expect-error: Invalid function + expectInvalidFunctionError(() => coll.reduceRight(123), 123); + }); + + test('reduce collection into a single value with initial value', () => { + const sum = coll.reduceRight((a, x) => a + x, 0); + expect(sum).toStrictEqual(6); + }); + + test('reduce collection into a single value without initial value', () => { + const sum = coll.reduceRight((a, x) => a + x); + expect(sum).toStrictEqual(6); + }); + + test('reduce collection into a single value with different accumulator type', () => { + const str = coll.reduceRight((a, x) => a.concat(x.toString()), ''); + expect(str).toStrictEqual('321'); + }); + + test('reduce empty collection with initial value', () => { + const coll = createCollection(); + expect(coll.reduceRight((a, x) => a + x, 0)).toStrictEqual(0); + }); + + test('reduce empty collection without initial value', () => { + const coll = createCollection(); + expect(() => coll.reduceRight((a, x) => a + x)).toThrowError( new TypeError('Reduce of empty collection with no initial value'), ); }); @@ -1013,31 +1056,3 @@ describe('findLastKey() tests', () => { }, null); }); }); - -describe('reduceRight() tests', () => { - const coll = createTestCollection(); - - test('throws if fn is not a function', () => { - // @ts-expect-error: Invalid function - expectInvalidFunctionError(() => coll.reduceRight()); - // @ts-expect-error: Invalid function - expectInvalidFunctionError(() => coll.reduceRight(123), 123); - }); - - test('reduce collection into a single value with initial value', () => { - const sum = coll.reduceRight((a, x) => a + x, 0); - expect(sum).toStrictEqual(6); - }); - - test('reduce collection into a single value without initial value', () => { - const sum = coll.reduceRight((a, x) => a + x); - expect(sum).toStrictEqual(6); - }); - - test('reduce empty collection without initial value', () => { - const coll = createCollection(); - expect(() => coll.reduceRight((a: number, x) => a + x)).toThrowError( - new TypeError('Reduce of empty collection with no initial value'), - ); - }); -}); diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 35e21a93a..b0051ad42 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -616,7 +616,15 @@ export class Collection extends Map { * collection.reduce((acc, guild) => acc + guild.memberCount, 0); * ``` */ - public reduce( + public reduce( + fn: (accumulator: Value, value: Value, key: Key, collection: this) => Value, + initialValue?: Value, + ): Value; + public reduce( + fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, + initialValue: InitialValue, + ): InitialValue; + public reduce( fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue, ): InitialValue { @@ -645,6 +653,14 @@ export class Collection extends Map { * @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection` * @param initialValue - Starting value for the accumulator */ + public reduceRight( + fn: (accumulator: Value, value: Value, key: Key, collection: this) => Value, + initialValue?: Value, + ): Value; + public reduceRight( + fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, + initialValue: InitialValue, + ): InitialValue; public reduceRight( fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue,