1
+ /* eslint-disable @typescript-eslint/restrict-template-expressions */
1
2
/**
2
3
* @internal
3
4
*/
@@ -40,6 +41,7 @@ export class Collection<K, V> extends Map<K, V> {
40
41
*/
41
42
public ensure ( key : K , defaultValueGenerator : ( key : K , collection : this) => V ) : V {
42
43
if ( this . has ( key ) ) return this . get ( key ) ! ;
44
+ if ( typeof defaultValueGenerator !== 'function' ) throw new TypeError ( `${ defaultValueGenerator } is not a function.` ) ;
43
45
const defaultValue = defaultValueGenerator ( key , this ) ;
44
46
this . set ( key , defaultValue ) ;
45
47
return defaultValue ;
@@ -238,6 +240,7 @@ export class Collection<K, V> extends Map<K, V> {
238
240
) : V2 | undefined ;
239
241
public find < This > ( fn : ( this : This , value : V , key : K , collection : this) => boolean , thisArg : This ) : V | undefined ;
240
242
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` ) ;
241
244
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
242
245
for ( const [ key , val ] of this ) {
243
246
if ( fn ( val , key , this ) ) return val ;
@@ -264,6 +267,7 @@ export class Collection<K, V> extends Map<K, V> {
264
267
) : K2 | undefined ;
265
268
public findKey < This > ( fn : ( this : This , value : V , key : K , collection : this) => boolean , thisArg : This ) : K | undefined ;
266
269
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` ) ;
267
271
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
268
272
for ( const [ key , val ] of this ) {
269
273
if ( fn ( val , key , this ) ) return key ;
@@ -282,6 +286,7 @@ export class Collection<K, V> extends Map<K, V> {
282
286
public sweep ( fn : ( value : V , key : K , collection : this) => boolean ) : number ;
283
287
public sweep < T > ( fn : ( this : T , value : V , key : K , collection : this) => boolean , thisArg : T ) : number ;
284
288
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` ) ;
285
290
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
286
291
const previousSize = this . size ;
287
292
for ( const [ key , val ] of this ) {
@@ -314,6 +319,7 @@ export class Collection<K, V> extends Map<K, V> {
314
319
) : Collection < K , V2 > ;
315
320
public filter < This > ( fn : ( this : This , value : V , key : K , collection : this) => boolean , thisArg : This ) : Collection < K , V > ;
316
321
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` ) ;
317
323
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
318
324
const results = new this . constructor [ Symbol . species ] < K , V > ( ) ;
319
325
for ( const [ key , val ] of this ) {
@@ -355,6 +361,7 @@ export class Collection<K, V> extends Map<K, V> {
355
361
fn : ( value : V , key : K , collection : this) => boolean ,
356
362
thisArg ?: unknown ,
357
363
) : [ Collection < K , V > , Collection < K , V > ] {
364
+ if ( typeof fn !== 'function' ) throw new TypeError ( `${ fn } is not a function` ) ;
358
365
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
359
366
const results : [ Collection < K , V > , Collection < K , V > ] = [
360
367
new this . constructor [ Symbol . species ] < K , V > ( ) ,
@@ -403,6 +410,7 @@ export class Collection<K, V> extends Map<K, V> {
403
410
public map < T > ( fn : ( value : V , key : K , collection : this) => T ) : T [ ] ;
404
411
public map < This , T > ( fn : ( this : This , value : V , key : K , collection : this) => T , thisArg : This ) : T [ ] ;
405
412
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` ) ;
406
414
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
407
415
const iter = this . entries ( ) ;
408
416
return Array . from ( { length : this . size } , ( ) : T => {
@@ -426,6 +434,7 @@ export class Collection<K, V> extends Map<K, V> {
426
434
public mapValues < T > ( fn : ( value : V , key : K , collection : this) => T ) : Collection < K , T > ;
427
435
public mapValues < This , T > ( fn : ( this : This , value : V , key : K , collection : this) => T , thisArg : This ) : Collection < K , T > ;
428
436
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` ) ;
429
438
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
430
439
const coll = new this . constructor [ Symbol . species ] < K , T > ( ) ;
431
440
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> {
445
454
public some ( fn : ( value : V , key : K , collection : this) => boolean ) : boolean ;
446
455
public some < T > ( fn : ( this : T , value : V , key : K , collection : this) => boolean , thisArg : T ) : boolean ;
447
456
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` ) ;
448
458
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
449
459
for ( const [ key , val ] of this ) {
450
460
if ( fn ( val , key , this ) ) return true ;
@@ -475,6 +485,7 @@ export class Collection<K, V> extends Map<K, V> {
475
485
) : this is Collection < K , V2 > ;
476
486
public every < This > ( fn : ( this : This , value : V , key : K , collection : this) => boolean , thisArg : This ) : boolean ;
477
487
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` ) ;
478
489
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
479
490
for ( const [ key , val ] of this ) {
480
491
if ( ! fn ( val , key , this ) ) return false ;
@@ -494,6 +505,7 @@ export class Collection<K, V> extends Map<K, V> {
494
505
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
495
506
*/
496
507
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` ) ;
497
509
let accumulator ! : T ;
498
510
499
511
if ( typeof initialValue !== 'undefined' ) {
@@ -536,6 +548,7 @@ export class Collection<K, V> extends Map<K, V> {
536
548
public each ( fn : ( value : V , key : K , collection : this) => void ) : this;
537
549
public each < T > ( fn : ( this : T , value : V , key : K , collection : this) => void , thisArg : T ) : this;
538
550
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` ) ;
539
552
this . forEach ( fn as ( value : V , key : K , map : Map < K , V > ) => void , thisArg ) ;
540
553
return this ;
541
554
}
@@ -555,6 +568,7 @@ export class Collection<K, V> extends Map<K, V> {
555
568
public tap ( fn : ( collection : this) => void ) : this;
556
569
public tap < T > ( fn : ( this : T , collection : this) => void , thisArg : T ) : this;
557
570
public tap ( fn : ( collection : this) => void , thisArg ?: unknown ) : this {
571
+ if ( typeof fn !== 'function' ) throw new TypeError ( `${ fn } is not a function` ) ;
558
572
if ( typeof thisArg !== 'undefined' ) fn = fn . bind ( thisArg ) ;
559
573
fn ( this ) ;
560
574
return this ;
0 commit comments