insert

Inserts elements into an array at a specified index.

1. Code

/**
 * Inserts elements into an array at a specified index.
 *
 * @template T - The type of elements in the array.
 * @param {T[]} arr - The array to insert elements into.
 * @param {number} index - The index at which to insert the elements.
 * @param {boolean} [recursive=false] - Indicates whether to insert elements recursively. If true, the elements will be inserted at every nth index.
 * @param {...T[]} items - The elements to insert into the array.
 * @returns {T[]} - The modified array with the inserted elements.
 */
const insert = <T>(
  arr: T[],
  index: number,
  [...items]: T[],
  recursive: boolean = false
): T[] => {
  const isNegativeIndex = index < 0;
  // if index is negative, convert it to positive and reverse the array for easier insertion
  if (isNegativeIndex) {
    index = Math.abs(index);
    arr = arr.reverse();
    items = items.reverse();
  }

  if (!recursive) {
    const newArr = [...arr.slice(0, index), ...items, ...arr.slice(index)];
    return isNegativeIndex ? newArr.reverse() : newArr;
  } else {
    const shouldInsert = Math.floor(arr.length / index);
    let newArr = [...arr];
    for (let i = 0; i < shouldInsert; i++) {
      const insertIndex = (i + 1) * index + i * items.length;
      newArr = [
        ...newArr.slice(0, insertIndex),
        ...items,
        ...newArr.slice(insertIndex),
      ];
    }
    return isNegativeIndex ? newArr.reverse() : newArr;
  }
};

export default insert;

2. Installation

npx @jrtilak/lazykit@latest add insert -undefined

3. Description

The insert function is a generic function in TypeScript that is designed to insert elements into an array at a specified index. The function takes four parameters: an array arr of type T[], an index of type number, a spread parameter items of type T[], and an optional recursive parameter of type boolean which defaults to false.

It returns a new array with the elements inserted at the specified index without modifying the original array.

If the index is negative, the function treats it as an index from the end of the array. In this case, it converts the index to a positive number and reverses the array and the items to be inserted for easier insertion.

If the recursive parameter is false, the function simply inserts the items at the specified index in the array. If the recursive parameter is true, the function inserts the items at every nth index, where n is the absolute value of the provided index. The function calculates the number of insertions to be made by dividing the length of the array by the index.

4. Props

Prop

Type

Default Value

array*array---
index*number---
items*array---
strictbooleanfalse

5. Examples

import insert from ".";

const arr = [1, 2, 3];
const index = 1;
const items = [4, 5];

const result = insert(arr, index, items);
console.log(result);
// Expected Output: [1, 4, 5, 2, 3]

const arr2 = [1, 2, 3, 4, 5];
const index2 = 2;
const items2 = [6, 7];

const result2 = insert(arr2, index2, items2);
console.log(result2);
// Expected Output: [1, 2, 6, 7, 3, 4, 5 ]

// negative index
const arr3 = [1, 2, 3];
const index3 = -1;
const items3 = [4, 5];

const result3 = insert(arr3, index3, items3);
console.log(result3);
// Expected Output: [1, 2, 4, 5, 3]
//recursive examples

import insert from ".";

const arr = [1, 2, 3];
const index = 1;
const items = [4, 5];

const result = insert(arr, index, items, true);
console.log(result);
// Expected Output: [1, 4, 5, 2, 4, 5, 3, 4, 5]

const arr2 = [1, 2, 3, 4, 5];
const index2 = 2;
const items2 = [6, 7];

const result2 = insert(arr2, index2, items2, true);
console.log(result2);
// Expected Output: [1, 2, 6, 7, 3, 4, 6, 7, 5]

//negative index

const arr3 = [1, 2, 3];
const index3 = -1;
const items3 = [4, 5];

const result3 = insert(arr3, index3, items3, true);
console.log(result3);
// Expected Output: [4, 5, 1, 4, 5, 2, 4, 5, 3]