@@ -274,6 +274,64 @@ export class Collection<K, V> extends Map<K, V> {
274
274
return undefined ;
275
275
}
276
276
277
+ /**
278
+ * Searches for a last item where the given function returns a truthy value. This behaves like
279
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast | Array.findLast()}.
280
+ *
281
+ * @param fn - The function to test with (should return a boolean)
282
+ * @param thisArg - Value to use as `this` when executing the function
283
+ */
284
+ public findLast < V2 extends V > ( fn : ( value : V , key : K , collection : this) => value is V2 ) : V2 | undefined ;
285
+ public findLast ( fn : ( value : V , key : K , collection : this) => unknown ) : V | undefined ;
286
+ public findLast < This , V2 extends V > (
287
+ fn : ( this : This , value : V , key : K , collection : this) => value is V2 ,
288
+ thisArg : This ,
289
+ ) : V2 | undefined ;
290
+ public findLast < This > ( fn : ( this : This , value : V , key : K , collection : this) => unknown , thisArg : This ) : V | undefined ;
291
+ public findLast ( fn : ( value : V , key : K , collection : this) => unknown , thisArg ?: unknown ) : V | undefined {
292
+ if ( typeof fn !== 'function' ) throw new TypeError ( `${ fn } is not a function` ) ;
293
+ if ( thisArg !== undefined ) fn = fn . bind ( thisArg ) ;
294
+ const entries = [ ...this . entries ( ) ] ;
295
+ for ( let index = entries . length - 1 ; index >= 0 ; index -- ) {
296
+ const val = entries [ index ] ! [ 1 ] ;
297
+ const key = entries [ index ] ! [ 0 ] ;
298
+ if ( fn ( val , key , this ) ) return val ;
299
+ }
300
+
301
+ return undefined ;
302
+ }
303
+
304
+ /**
305
+ * Searches for the key of a last item where the given function returns a truthy value. This behaves like
306
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex | Array.findLastIndex()},
307
+ * but returns the key rather than the positional index.
308
+ *
309
+ * @param fn - The function to test with (should return a boolean)
310
+ * @param thisArg - Value to use as `this` when executing the function
311
+ */
312
+ public findLastKey < K2 extends K > ( fn : ( value : V , key : K , collection : this) => key is K2 ) : K2 | undefined ;
313
+ public findLastKey ( fn : ( value : V , key : K , collection : this) => unknown ) : K | undefined ;
314
+ public findLastKey < This , K2 extends K > (
315
+ fn : ( this : This , value : V , key : K , collection : this) => key is K2 ,
316
+ thisArg : This ,
317
+ ) : K2 | undefined ;
318
+ public findLastKey < This > (
319
+ fn : ( this : This , value : V , key : K , collection : this) => unknown ,
320
+ thisArg : This ,
321
+ ) : K | undefined ;
322
+ public findLastKey ( fn : ( value : V , key : K , collection : this) => unknown , thisArg ?: unknown ) : K | undefined {
323
+ if ( typeof fn !== 'function' ) throw new TypeError ( `${ fn } is not a function` ) ;
324
+ if ( thisArg !== undefined ) fn = fn . bind ( thisArg ) ;
325
+ const entries = [ ...this . entries ( ) ] ;
326
+ for ( let index = entries . length - 1 ; index >= 0 ; index -- ) {
327
+ const key = entries [ index ] ! [ 0 ] ;
328
+ const val = entries [ index ] ! [ 1 ] ;
329
+ if ( fn ( val , key , this ) ) return key ;
330
+ }
331
+
332
+ return undefined ;
333
+ }
334
+
277
335
/**
278
336
* Removes items that satisfy the provided filter function.
279
337
*
@@ -533,6 +591,37 @@ export class Collection<K, V> extends Map<K, V> {
533
591
return accumulator ;
534
592
}
535
593
594
+ /**
595
+ * Applies a function to produce a single value. Identical in behavior to
596
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
597
+ *
598
+ * @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
599
+ * @param initialValue - Starting value for the accumulator
600
+ */
601
+ public reduceRight < T > ( fn : ( accumulator : T , value : V , key : K , collection : this) => T , initialValue ?: T ) : T {
602
+ if ( typeof fn !== 'function' ) throw new TypeError ( `${ fn } is not a function` ) ;
603
+ const entries = [ ...this . entries ( ) ] ;
604
+ let accumulator ! : T ;
605
+
606
+ let index : number ;
607
+ if ( initialValue === undefined ) {
608
+ if ( entries . length === 0 ) throw new TypeError ( 'Reduce of empty collection with no initial value' ) ;
609
+ accumulator = entries [ entries . length - 1 ] ! [ 1 ] as unknown as T ;
610
+ index = entries . length - 1 ;
611
+ } else {
612
+ accumulator = initialValue ;
613
+ index = entries . length ;
614
+ }
615
+
616
+ while ( -- index >= 0 ) {
617
+ const key = entries [ index ] ! [ 0 ] ;
618
+ const val = entries [ index ] ! [ 1 ] ;
619
+ accumulator = fn ( accumulator , val , key , this ) ;
620
+ }
621
+
622
+ return accumulator ;
623
+ }
624
+
536
625
/**
537
626
* Identical to
538
627
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
0 commit comments