All files / array/src findIndex.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29              22x       22x 22x     22x         22x 32x 9x     13x      
import { Predicate } from "@benneq/predicate";
 
type FindIndex = <T, A extends ArrayLike<T> = ArrayLike<T>>(
  predicate: Predicate<[T, number, A]>,
  fromIndex?: number
) => (array: A) => number;
 
export const findIndex: FindIndex = <T, A extends ArrayLike<T> = ArrayLike<T>>(
  predicate: Predicate<[T, number, A]>,
  fromIndex = 0
) => {
  if (process.env.NODE_ENV !== "production") {
    console.assert(fromIndex >= 0);
  }
 
  return (
    array: A,
    // internal variables:
    i = fromIndex
  ): number => {
    for (; i < array.length; i++) {
      if (predicate(array[i] as T, i, array)) {
        return i;
      }
    }
    return -1;
  };
};