All files / array/src isIndex.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 3/3
100% Lines 4/4

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                                          12x 1x 12x 12x    
/**
 * Checks if a Number is an index of an {@link ArrayLike}
 *
 * @example
 * Is `index` in bounds of `array`?
 * ```ts
 * const result = isIndex(4)([1, 2, 3]);
 *
 * console.log(result); // false
 * ```
 *
 * @example
 * Works with strings, too
 * ```ts
 * const result = isIndex(3)("test");
 *
 * console.log(result); // true
 * ```
 *
 * @returns `true` if the `index` is between `0` and `array.length`, otherwise `false`
 */
export const isIndex =
  (index: number) =>
  (array: ArrayLike<unknown>): boolean => {
    return index >= 0 && index < array.length;
  };