Skip to content

Commit 3bb9c0e

Browse files
fix: check for function type (#8064)
1 parent 9c8b310 commit 3bb9c0e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Diff for: packages/collection/src/index.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
12
/**
23
* @internal
34
*/
@@ -40,6 +41,7 @@ export class Collection<K, V> extends Map<K, V> {
4041
*/
4142
public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V {
4243
if (this.has(key)) return this.get(key)!;
44+
if (typeof defaultValueGenerator !== 'function') throw new TypeError(`${defaultValueGenerator} is not a function.`);
4345
const defaultValue = defaultValueGenerator(key, this);
4446
this.set(key, defaultValue);
4547
return defaultValue;
@@ -238,6 +240,7 @@ export class Collection<K, V> extends Map<K, V> {
238240
): V2 | undefined;
239241
public find<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): V | undefined;
240242
public find(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): V | undefined {
243+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
241244
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
242245
for (const [key, val] of this) {
243246
if (fn(val, key, this)) return val;
@@ -264,6 +267,7 @@ export class Collection<K, V> extends Map<K, V> {
264267
): K2 | undefined;
265268
public findKey<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): K | undefined;
266269
public findKey(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): K | undefined {
270+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
267271
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
268272
for (const [key, val] of this) {
269273
if (fn(val, key, this)) return key;
@@ -282,6 +286,7 @@ export class Collection<K, V> extends Map<K, V> {
282286
public sweep(fn: (value: V, key: K, collection: this) => boolean): number;
283287
public sweep<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): number;
284288
public sweep(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): number {
289+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
285290
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
286291
const previousSize = this.size;
287292
for (const [key, val] of this) {
@@ -314,6 +319,7 @@ export class Collection<K, V> extends Map<K, V> {
314319
): Collection<K, V2>;
315320
public filter<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): Collection<K, V>;
316321
public filter(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): Collection<K, V> {
322+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
317323
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
318324
const results = new this.constructor[Symbol.species]<K, V>();
319325
for (const [key, val] of this) {
@@ -355,6 +361,7 @@ export class Collection<K, V> extends Map<K, V> {
355361
fn: (value: V, key: K, collection: this) => boolean,
356362
thisArg?: unknown,
357363
): [Collection<K, V>, Collection<K, V>] {
364+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
358365
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
359366
const results: [Collection<K, V>, Collection<K, V>] = [
360367
new this.constructor[Symbol.species]<K, V>(),
@@ -403,6 +410,7 @@ export class Collection<K, V> extends Map<K, V> {
403410
public map<T>(fn: (value: V, key: K, collection: this) => T): T[];
404411
public map<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[];
405412
public map<T>(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): T[] {
413+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
406414
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
407415
const iter = this.entries();
408416
return Array.from({ length: this.size }, (): T => {
@@ -426,6 +434,7 @@ export class Collection<K, V> extends Map<K, V> {
426434
public mapValues<T>(fn: (value: V, key: K, collection: this) => T): Collection<K, T>;
427435
public mapValues<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection<K, T>;
428436
public mapValues<T>(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): Collection<K, T> {
437+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
429438
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
430439
const coll = new this.constructor[Symbol.species]<K, T>();
431440
for (const [key, val] of this) coll.set(key, fn(val, key, this));
@@ -445,6 +454,7 @@ export class Collection<K, V> extends Map<K, V> {
445454
public some(fn: (value: V, key: K, collection: this) => boolean): boolean;
446455
public some<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): boolean;
447456
public some(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): boolean {
457+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
448458
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
449459
for (const [key, val] of this) {
450460
if (fn(val, key, this)) return true;
@@ -475,6 +485,7 @@ export class Collection<K, V> extends Map<K, V> {
475485
): this is Collection<K, V2>;
476486
public every<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): boolean;
477487
public every(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): boolean {
488+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
478489
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
479490
for (const [key, val] of this) {
480491
if (!fn(val, key, this)) return false;
@@ -494,6 +505,7 @@ export class Collection<K, V> extends Map<K, V> {
494505
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
495506
*/
496507
public reduce<T>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T {
508+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
497509
let accumulator!: T;
498510

499511
if (typeof initialValue !== 'undefined') {
@@ -536,6 +548,7 @@ export class Collection<K, V> extends Map<K, V> {
536548
public each(fn: (value: V, key: K, collection: this) => void): this;
537549
public each<T>(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this;
538550
public each(fn: (value: V, key: K, collection: this) => void, thisArg?: unknown): this {
551+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
539552
this.forEach(fn as (value: V, key: K, map: Map<K, V>) => void, thisArg);
540553
return this;
541554
}
@@ -555,6 +568,7 @@ export class Collection<K, V> extends Map<K, V> {
555568
public tap(fn: (collection: this) => void): this;
556569
public tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this;
557570
public tap(fn: (collection: this) => void, thisArg?: unknown): this {
571+
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
558572
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
559573
fn(this);
560574
return this;

0 commit comments

Comments
 (0)