All files / array/src deleteAt.ts

100% Statements 4/4
100% Branches 1/1
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 28 29 30 31 32 33 34                                                        10x 4x 10x 10x    
import { RelativeIndex } from "./_types";
 
/**
 * Removes elements of an Array at a given index
 *
 * @mutation
 *
 * @example
 * Delete a single element at index 1
 * ```ts
 * const array = [1, 2, 3];
 * deleteAt(1)(array);
 * console.log(array); // [1, 3]
 * ```
 *
 * @example
 * Delete two elements at index 1
 * ```ts
 * const array = [1, 2, 3, 4];
 * deleteAt(1, 2)(array);
 * console.log(array); // [1, 4]
 * ```
 *
 * @param array - the {@link Array} to modify
 * @param index - the index to start deletion
 * @param deleteCount - the optional number of elements to delete
 * @returns `true` if an element was deleted, otherwise `false`
 */
export const deleteAt =
  (index: RelativeIndex, deleteCount = 1) =>
  (array: unknown[]): boolean => {
    return !!array.splice(index, deleteCount).length;
  };