Skip to content

Commit

Permalink
Merge pull request #39 from github/kintner/offby1
Browse files Browse the repository at this point in the history
correct off by 1 error in findLast polyfills
  • Loading branch information
kintner committed Aug 14, 2023
2 parents 2bfa6e2 + 7ed757a commit dc2fbdb
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/array-findlast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function arrayFindLast<T>(
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]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/array-findlastindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function arrayFindLastIndex<T>(
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
Expand Down
1 change: 1 addition & 0 deletions test/array-findlast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions test/array-findlastindex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dc2fbdb

Please sign in to comment.