From 3bb9c0e5c37311044ff41761b572ac4f91cda57c Mon Sep 17 00:00:00 2001 From: Parbez Date: Sat, 11 Jun 2022 00:51:45 +0530 Subject: [PATCH] fix: check for function type (#8064) --- packages/collection/src/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/collection/src/index.ts b/packages/collection/src/index.ts index 91a3b94f4..1f5380d0d 100644 --- a/packages/collection/src/index.ts +++ b/packages/collection/src/index.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/restrict-template-expressions */ /** * @internal */ @@ -40,6 +41,7 @@ export class Collection extends Map { */ public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V { if (this.has(key)) return this.get(key)!; + if (typeof defaultValueGenerator !== 'function') throw new TypeError(`${defaultValueGenerator} is not a function.`); const defaultValue = defaultValueGenerator(key, this); this.set(key, defaultValue); return defaultValue; @@ -238,6 +240,7 @@ export class Collection extends Map { ): V2 | undefined; public find(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): V | undefined; public find(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): V | undefined { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); for (const [key, val] of this) { if (fn(val, key, this)) return val; @@ -264,6 +267,7 @@ export class Collection extends Map { ): K2 | undefined; public findKey(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): K | undefined; public findKey(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): K | undefined { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); for (const [key, val] of this) { if (fn(val, key, this)) return key; @@ -282,6 +286,7 @@ export class Collection extends Map { public sweep(fn: (value: V, key: K, collection: this) => boolean): number; public sweep(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): number; public sweep(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): number { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); const previousSize = this.size; for (const [key, val] of this) { @@ -314,6 +319,7 @@ export class Collection extends Map { ): Collection; public filter(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): Collection; public filter(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): Collection { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); const results = new this.constructor[Symbol.species](); for (const [key, val] of this) { @@ -355,6 +361,7 @@ export class Collection extends Map { fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown, ): [Collection, Collection] { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); const results: [Collection, Collection] = [ new this.constructor[Symbol.species](), @@ -403,6 +410,7 @@ export class Collection extends Map { public map(fn: (value: V, key: K, collection: this) => T): T[]; public map(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[]; public map(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): T[] { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); const iter = this.entries(); return Array.from({ length: this.size }, (): T => { @@ -426,6 +434,7 @@ export class Collection extends Map { public mapValues(fn: (value: V, key: K, collection: this) => T): Collection; public mapValues(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection; public mapValues(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): Collection { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); const coll = new this.constructor[Symbol.species](); for (const [key, val] of this) coll.set(key, fn(val, key, this)); @@ -445,6 +454,7 @@ export class Collection extends Map { public some(fn: (value: V, key: K, collection: this) => boolean): boolean; public some(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): boolean; public some(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): boolean { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); for (const [key, val] of this) { if (fn(val, key, this)) return true; @@ -475,6 +485,7 @@ export class Collection extends Map { ): this is Collection; public every(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): boolean; public every(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): boolean { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); for (const [key, val] of this) { if (!fn(val, key, this)) return false; @@ -494,6 +505,7 @@ export class Collection extends Map { * collection.reduce((acc, guild) => acc + guild.memberCount, 0); */ public reduce(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`); let accumulator!: T; if (typeof initialValue !== 'undefined') { @@ -536,6 +548,7 @@ export class Collection extends Map { public each(fn: (value: V, key: K, collection: this) => void): this; public each(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this; public each(fn: (value: V, key: K, collection: this) => void, thisArg?: unknown): this { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); this.forEach(fn as (value: V, key: K, map: Map) => void, thisArg); return this; } @@ -555,6 +568,7 @@ export class Collection extends Map { public tap(fn: (collection: this) => void): this; public tap(fn: (this: T, collection: this) => void, thisArg: T): this; public tap(fn: (collection: this) => void, thisArg?: unknown): this { + if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg); fn(this); return this;