import type { OnError } from '../../types/errors.type'; import type { Predicate } from '../../types/find.type'; /** * @description * Use this helper to return the elements of an array that meet the condition specified in a callback function. * * This function behaves similarly to `Array.prototype.filter`, 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} collection The array of items to filter. * @param {Predicate} predicate A function that tests each element of the array. Called once for each item in the array. * @param {{ onError?: OnError }} options An optional object for error handling. * * @returns {Array} An array of items that match the predicate. */ export const safeFilter = ( collection: Array, predicate: Predicate, options: { onError?: OnError; } = {} ): Array => { const { onError = () => {} } = options; const results: Array = []; for (let index = 0; index < collection.length; index++) { const item = collection[index]; try { const matches = predicate(item, index, collection); if (matches) { results.push(item); } } catch (error) { onError(error, item, index); } } return results; };