@future-widget-lab/safe-ops
Version:
A set of helper functions for performing operations safely, preventing runtime errors from disrupting your application.
20 lines (19 loc) • 1.08 kB
TypeScript
import type { OnError } from '../../types/errors.type';
import type { Predicate } from '../../types/find.type';
/**
* @description
* Use this helper return the index of the first element in the array where predicate is true, and -1 otherwise.
*
* This function behaves similarly to `Array.prototype.findIndex`, but with added error handling:
* - If the predicate throws an error for any element, the error is handled via the onError callback.
* - Allows for custom error handling through the onError option.
*
* @param {Array<TInput>} collection The array of items to test.
* @param {Predicate<TInput>} predicate A function that tests each element of the array. Called once for each item in the array.
* @param {{ onError?: OnError<TInput> }} options An optional object for error handling.
*
* @returns {number} The index of the first element that satisfies the test. Returns -1 if no element matches.
*/
export declare const safeFindIndex: <TInput>(collection: Array<TInput>, predicate: Predicate<TInput>, options?: {
onError?: OnError<TInput>;
}) => number;