mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor(collection): reduce reduce's code (#9581)
* refactor(collection): reduce `reduce`'s code * fix: resolved bug in tests --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -700,12 +700,17 @@ describe('reduce() tests', () => {
|
|||||||
|
|
||||||
test('reduce collection into a single value with initial value', () => {
|
test('reduce collection into a single value with initial value', () => {
|
||||||
const sum = coll.reduce((a, x) => a + x, 0);
|
const sum = coll.reduce((a, x) => a + x, 0);
|
||||||
expect(sum).toStrictEqual(6);
|
expect<number>(sum).toStrictEqual(6);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('reduce collection into a single value without initial value', () => {
|
test('reduce collection into a single value without initial value', () => {
|
||||||
const sum = coll.reduce<number>((a, x) => a + x);
|
const sum = coll.reduce((a, x) => a + x);
|
||||||
expect(sum).toStrictEqual(6);
|
expect<number>(sum).toStrictEqual(6);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('reduce empty collection with initial value', () => {
|
||||||
|
const coll = createCollection();
|
||||||
|
expect(coll.reduce((a, x) => a + x, 0)).toStrictEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('reduce empty collection without initial value', () => {
|
test('reduce empty collection without initial value', () => {
|
||||||
|
|||||||
@@ -514,30 +514,20 @@ export class Collection<K, V> extends Map<K, V> {
|
|||||||
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
public reduce<T>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T {
|
public reduce<T = V>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T {
|
||||||
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
|
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
|
||||||
let accumulator!: T;
|
let accumulator!: T;
|
||||||
|
|
||||||
if (initialValue !== undefined) {
|
const iterator = this.entries();
|
||||||
|
if (initialValue === undefined) {
|
||||||
|
if (this.size === 0) throw new TypeError('Reduce of empty collection with no initial value');
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
accumulator = initialValue;
|
accumulator = initialValue;
|
||||||
for (const [key, val] of this) accumulator = fn(accumulator, val, key, this);
|
|
||||||
return accumulator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let first = true;
|
for (const [key, value] of iterator) {
|
||||||
for (const [key, val] of this) {
|
accumulator = fn(accumulator, value, key, this);
|
||||||
if (first) {
|
|
||||||
accumulator = val as unknown as T;
|
|
||||||
first = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
accumulator = fn(accumulator, val, key, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
// No items iterated.
|
|
||||||
if (first) {
|
|
||||||
throw new TypeError('Reduce of empty collection with no initial value');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return accumulator;
|
return accumulator;
|
||||||
|
|||||||
Reference in New Issue
Block a user