All files / array/src copy.ts

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

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                              2x 2x    
/**
 * Copies all elements of an Array into a new Array
 *
 * @example
 * Copy an Array
 * ```ts
 * const array = [1, 2, 3];
 * const copiedArray = copy(array);
 * console.log(copiedArray); // [1, 2, 3]
 * console.log(array === copiedArray); // false
 * ```
 *
 * @typeParam T - the {@link Array} element type
 * @returns a new Array containing all elements of the source Array
 */
export const copy = <T>(array: ArrayLike<T>): T[] => {
  return Array.from(array);
};