From 7ed757ae5a93f5c0d58626aa420c1ea91c08fdf1 Mon Sep 17 00:00:00 2001 From: Christopher Kintner Date: Mon, 14 Aug 2023 18:41:08 +0000 Subject: [PATCH] correct off by 1 error in findLast polyfills --- src/array-findlast.ts | 2 +- src/array-findlastindex.ts | 2 +- test/array-findlast.js | 1 + test/array-findlastindex.js | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/array-findlast.ts b/src/array-findlast.ts index 54dc30c..14eef66 100644 --- a/src/array-findlast.ts +++ b/src/array-findlast.ts @@ -3,7 +3,7 @@ export function arrayFindLast( pred: (this: T[], value: T, i: number, array: T[]) => boolean, recv = this ): T | void { - for (let i = this.length; i > 0; i -= 1) { + for (let i = this.length - 1; i >= 0; i -= 1) { if (pred.call(recv, this[i], i, this)) return this[i] } } diff --git a/src/array-findlastindex.ts b/src/array-findlastindex.ts index fec08fb..d2f6494 100644 --- a/src/array-findlastindex.ts +++ b/src/array-findlastindex.ts @@ -3,7 +3,7 @@ export function arrayFindLastIndex( pred: (this: T[], value: T, i: number, array: T[]) => boolean, recv = this ): number { - for (let i = this.length; i > 0; i -= 1) { + for (let i = this.length - 1; i >= 0; i -= 1) { if (pred.call(recv, this[i], i, this)) return i } return -1 diff --git a/test/array-findlast.js b/test/array-findlast.js index 57f9935..de11f59 100644 --- a/test/array-findlast.js +++ b/test/array-findlast.js @@ -11,6 +11,7 @@ describe('arrayFindLast', () => { it('returns value that passes truthy', () => { expect(arrayFindLast.call([1, 2, 3], v => v === 3)).to.equal(3) + expect(arrayFindLast.call([1, 2, 3], v => v === 1)).to.equal(1) const arr = [1, 2, 3] const recv = {} expect( diff --git a/test/array-findlastindex.js b/test/array-findlastindex.js index 7248ee1..e599470 100644 --- a/test/array-findlastindex.js +++ b/test/array-findlastindex.js @@ -11,6 +11,7 @@ describe('arrayFindLastIndex', () => { it('returns value that passes truthy', () => { expect(arrayFindLastIndex.call([1, 2, 3], v => v === 3)).to.equal(2) + expect(arrayFindLastIndex.call([1, 2, 3], v => v === 1)).to.equal(0) const arr = [1, 2, 3] const recv = {} expect(