Skip to content

Commit dc2fbdb

Browse files
authored
Merge pull request #39 from github/kintner/offby1
correct off by 1 error in findLast polyfills
2 parents 2bfa6e2 + 7ed757a commit dc2fbdb

4 files changed

+4
-2
lines changed

src/array-findlast.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export function arrayFindLast<T>(
33
pred: (this: T[], value: T, i: number, array: T[]) => boolean,
44
recv = this
55
): T | void {
6-
for (let i = this.length; i > 0; i -= 1) {
6+
for (let i = this.length - 1; i >= 0; i -= 1) {
77
if (pred.call(recv, this[i], i, this)) return this[i]
88
}
99
}

src/array-findlastindex.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export function arrayFindLastIndex<T>(
33
pred: (this: T[], value: T, i: number, array: T[]) => boolean,
44
recv = this
55
): number {
6-
for (let i = this.length; i > 0; i -= 1) {
6+
for (let i = this.length - 1; i >= 0; i -= 1) {
77
if (pred.call(recv, this[i], i, this)) return i
88
}
99
return -1

test/array-findlast.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('arrayFindLast', () => {
1111

1212
it('returns value that passes truthy', () => {
1313
expect(arrayFindLast.call([1, 2, 3], v => v === 3)).to.equal(3)
14+
expect(arrayFindLast.call([1, 2, 3], v => v === 1)).to.equal(1)
1415
const arr = [1, 2, 3]
1516
const recv = {}
1617
expect(

test/array-findlastindex.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('arrayFindLastIndex', () => {
1111

1212
it('returns value that passes truthy', () => {
1313
expect(arrayFindLastIndex.call([1, 2, 3], v => v === 3)).to.equal(2)
14+
expect(arrayFindLastIndex.call([1, 2, 3], v => v === 1)).to.equal(0)
1415
const arr = [1, 2, 3]
1516
const recv = {}
1617
expect(

0 commit comments

Comments
 (0)