Skip to content

Commit 3ebb38d

Browse files
blikblumjdalton
authored andcommitted
Coerce findLastIndex and lastIndexOf fromIndex parameter to integer (#4440)
* Enable lastIndexOf, findLast and findLastIndex * Coerce findLastIndex fromIndex parameter to integer * Coerce lastIndexOf fromIndex parameter to integer
1 parent ed4b3a2 commit 3ebb38d

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

findLastIndex.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import baseFindIndex from './.internal/baseFindIndex.js'
2+
import toInteger from './toInteger.js'
23

34
/**
45
* This method is like `findIndex` except that it iterates over elements
@@ -29,9 +30,10 @@ function findLastIndex(array, predicate, fromIndex) {
2930
}
3031
let index = length - 1
3132
if (fromIndex !== undefined) {
33+
index = toInteger(fromIndex)
3234
index = fromIndex < 0
33-
? Math.max(length + fromIndex, 0)
34-
: Math.min(fromIndex, length - 1)
35+
? Math.max(length + index, 0)
36+
: Math.min(index, length - 1)
3537
}
3638
return baseFindIndex(array, predicate, index, true)
3739
}

lastIndexOf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import baseFindIndex from './.internal/baseFindIndex.js'
22
import baseIsNaN from './.internal/baseIsNaN.js'
33
import strictLastIndexOf from './.internal/strictLastIndexOf.js'
4+
import toInteger from './toInteger.js'
45

56
/**
67
* This method is like `indexOf` except that it iterates over elements of
@@ -28,6 +29,7 @@ function lastIndexOf(array, value, fromIndex) {
2829
}
2930
let index = length
3031
if (fromIndex !== undefined) {
32+
index = toInteger(fromIndex)
3133
index = index < 0 ? Math.max(length + index, 0) : Math.min(index, length - 1)
3234
}
3335
return value === value
File renamed without changes.

test/findLastIndex-and-lastIndexOf.js renamed to test/findLastIndex-and-lastIndexOf.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import assert from 'assert';
22
import lodashStable from 'lodash';
3-
import { _, identity, stubZero, falsey } from './utils.js';
3+
import { identity, stubZero, falsey } from './utils.js';
4+
import findLastIndex from '../findLastIndex.js';
5+
import lastIndexOf from '../lastIndexOf.js';
6+
7+
const methods = {
8+
findLastIndex,
9+
lastIndexOf
10+
};
411

512
describe('findLastIndex and lastIndexOf', function() {
613
lodashStable.each(['findLastIndex', 'lastIndexOf'], function(methodName) {
714
var array = [1, 2, 3, 1, 2, 3],
8-
func = _[methodName],
15+
func = methods[methodName],
916
resolve = methodName == 'findLastIndex' ? lodashStable.curry(lodashStable.eq) : identity;
1017

1118
it('`_.' + methodName + '` should return the index of the last matched value', function() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy