baseFindIndex.js 702 Bytes
Newer Older
徐立's avatar
徐立 committed
1 2 3 4 5 6 7 8 9 10 11
/**
 * The base implementation of `findIndex` and `findLastIndex`.
 *
 * @private
 * @param {Array} array The array to inspect.
 * @param {Function} predicate The function invoked per iteration.
 * @param {number} fromIndex The index to search from.
 * @param {boolean} [fromRight] Specify iterating from right to left.
 * @returns {number} Returns the index of the matched value, else `-1`.
 */
function baseFindIndex(array, predicate, fromIndex, fromRight) {
12 13
	const { length } = array;
	let index = fromIndex + (fromRight ? 1 : -1);
徐立's avatar
徐立 committed
14

15 16 17 18 19 20
	while (fromRight ? index-- : ++index < length) {
		if (predicate(array[index], index, array)) {
			return index;
		}
	}
	return -1;
徐立's avatar
徐立 committed
21 22
}

23
export default baseFindIndex;