You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The findLastIndex function has a small bug where it invokes callback with undefined as the first argument before moving on to the other items. Not always a problem, but if the callback is expecting an object then it can cause exceptions to be thrown - e.g. for callback element => element.foo = 'bar'.
The fix is changing this:
for(leti=this.length;i>=0;i--){
to this:
for(leti=this.length-1;i>=0;i--){
We can also add the following test to make sure this is fixed and won't happen again:
test("invokes callback in the expected way",()=>{constarr=[123,456,789];constcallback=jest.fn(()=>false);arr.myFindLastIndex(callback);expect(callback).toHaveBeenCalledTimes(3);expect(callback).toHaveBeenNthCalledWith(1,789,2,arr);expect(callback).toHaveBeenNthCalledWith(2,456,1,arr);expect(callback).toHaveBeenNthCalledWith(3,123,0,arr);});
And also maybe this to make sure we're not calling callback more often than needed:
test("does not invoke callback after element found",()=>{constarr=[123,456,789];constcallback=jest.fn((element)=>element===456);arr.myFindLastIndex(callback);expect(callback).toHaveBeenCalledTimes(2);expect(callback).toHaveBeenNthCalledWith(1,789,2,arr);expect(callback).toHaveBeenNthCalledWith(2,456,1,arr);});
I was going to open a PR for you, but I don't have permission, hopefully the above is simple for you to use anyway.
Thanks!
The text was updated successfully, but these errors were encountered:
Hi Siddhi,
The
findLastIndex
function has a small bug where it invokescallback
withundefined
as the first argument before moving on to the other items. Not always a problem, but if the callback is expecting an object then it can cause exceptions to be thrown - e.g. for callbackelement => element.foo = 'bar'
.The fix is changing this:
to this:
We can also add the following test to make sure this is fixed and won't happen again:
And also maybe this to make sure we're not calling callback more often than needed:
I was going to open a PR for you, but I don't have permission, hopefully the above is simple for you to use anyway.
Thanks!
The text was updated successfully, but these errors were encountered: