@@ -33,27 +33,84 @@ declare global {
3333 thisArg ?: any ,
3434 ) : number ;
3535 }
36+ interface Uint8Array {
37+ /**
38+ * Returns the value of the last element in the array where predicate is true, and undefined
39+ * otherwise.
40+ * @param predicate findLast calls predicate once for each element of the array, in descending
41+ * order, until it finds one where predicate returns true. If such an element is found, findLast
42+ * immediately returns that element value. Otherwise, findLast returns undefined.
43+ * @param thisArg If provided, it will be used as the this value for each invocation of
44+ * predicate. If it is not provided, undefined is used instead.
45+ */
46+ findLast < S extends number > (
47+ predicate : (
48+ value : number ,
49+ index : number ,
50+ array : Uint8Array ,
51+ ) => value is S ,
52+ thisArg ?: any ,
53+ ) : S | undefined ;
54+ findLast (
55+ predicate : ( value : number , index : number , array : Uint8Array ) => unknown ,
56+ thisArg ?: any ,
57+ ) : number | undefined ;
58+
59+ /**
60+ * Returns the index of the last element in the array where predicate is true, and -1
61+ * otherwise.
62+ * @param predicate findLastIndex calls predicate once for each element of the array, in descending
63+ * order, until it finds one where predicate returns true. If such an element is found,
64+ * findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
65+ * @param thisArg If provided, it will be used as the this value for each invocation of
66+ * predicate. If it is not provided, undefined is used instead.
67+ */
68+ findLastIndex (
69+ predicate : ( value : number , index : number , array : Uint8Array ) => unknown ,
70+ thisArg ?: any ,
71+ ) : number ;
72+ }
73+ }
74+
75+ function findLastIndex ( self : any , callbackfn : any , that : any ) {
76+ const boundFunc = that === undefined ? callbackfn : callbackfn . bind ( that ) ;
77+ let index = self . length - 1 ;
78+ while ( index >= 0 ) {
79+ const result = boundFunc ( self [ index ] , index , self ) ;
80+ if ( result ) {
81+ return index ;
82+ }
83+ index -- ;
84+ }
85+ return - 1 ;
86+ }
87+
88+ function findLast ( self : any , callbackfn : any , that : any ) {
89+ const index = self . findLastIndex ( callbackfn , that ) ;
90+ return index === - 1 ? undefined : self [ index ] ;
3691}
3792
3893if ( ! Array . prototype . findLastIndex ) {
3994 Array . prototype . findLastIndex = function ( callbackfn : any , that : any ) {
40- const boundFunc = that === undefined ? callbackfn : callbackfn . bind ( that ) ;
41- let index = this . length - 1 ;
42- while ( index >= 0 ) {
43- const result = boundFunc ( this [ index ] , index , this ) ;
44- if ( result ) {
45- return index ;
46- }
47- index -- ;
48- }
49- return - 1 ;
95+ return findLastIndex ( this , callbackfn , that ) ;
5096 } ;
5197}
5298
5399if ( ! Array . prototype . findLast ) {
54100 Array . prototype . findLast = function ( callbackfn : any , that : any ) {
55- const index = this . findLastIndex ( callbackfn , that ) ;
56- return index === - 1 ? undefined : this [ index ] ;
101+ return findLast ( this , callbackfn , that ) ;
102+ } ;
103+ }
104+
105+ if ( ! Uint8Array . prototype . findLastIndex ) {
106+ Uint8Array . prototype . findLastIndex = function ( callbackfn : any , that : any ) {
107+ return findLastIndex ( this , callbackfn , that ) ;
108+ } ;
109+ }
110+
111+ if ( ! Uint8Array . prototype . findLast ) {
112+ Uint8Array . prototype . findLast = function ( callbackfn : any , that : any ) {
113+ return findLast ( this , callbackfn , that ) ;
57114 } ;
58115}
59116
0 commit comments