UNPKG

funtool

Version:

A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.

62 lines (56 loc) 2.02 kB
/** * Retrieves the element at the specified index in the array. * Supports negative indices, which count from the end of the array. * * @template T - The type of elements in the array. * @param {T[]} arr - The input array. * @param {number} index - The index of the element to retrieve. Can be negative. * @returns {T | undefined} - The element at the specified index, or undefined if the index is out of bounds or the array is empty. * * @example * const arr = [1, 2, 3, 4, 5]; * at(arr, 2); // ✅ Returns 3 * at(arr, -1); // ✅ Returns 5 * at([], 0); // ✅ Returns undefined */ declare function at<T>(arr: T[], index: number): T | undefined; /** * Get the intersection of two arrays * @param arr1 - The first array * @param arr2 - The second array * @returns A new array containing elements present in both arrays * @example * intersect([1, 2, 3], [2, 3, 4]) // ✅ [2, 3] * intersect(['a', 'b'], ['b', 'c']) // ✅ ['b'] */ declare function intersect<T>(arr1: T[], arr2: T[]): T[]; /** * Remove specified values from an array * @param arr - The array to process * @param values - The values to remove * @returns A new array with specified values removed * @example * remove([1, 2, 3], [2]) // ✅ [1, 3] * remove(['a', 'b', 'c'], ['b', 'c']) // ✅ ['a'] */ declare function remove<T>(arr: T[], values: T[]): T[]; /** * Get the union of two arrays * @param arr1 - The first array * @param arr2 - The second array * @returns A new array containing elements from both arrays without duplicates * @example * union([1, 2], [2, 3]) // ✅ [1, 2, 3] * union(['a'], ['b', 'a']) // ✅ ['a', 'b'] */ declare function union<T>(arr1: T[], arr2: T[]): T[]; /** * Remove duplicate values from an array * @param arr - The array to process * @returns A new array with unique values * @example * unique([1, 2, 2, 3]) // ✅ [1, 2, 3] * unique(['a', 'b', 'a']) // ✅ ['a', 'b'] */ declare function unique<T>(arr: T[]): T[]; export { at, intersect, remove, union, unique };