JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "move-item-in-array.ts"

Full Path: /home/markqprx/iniasli.pro/resources/client/utils/array/move-item-in-array.ts
File size: 484 bytes
MIME-type: text/plain
Charset: utf-8

import {clamp} from '../number/clamp';

export function moveItemInArray<T = any>(
  array: T[],
  fromIndex: number,
  toIndex: number
): T[] {
  const from = clamp(fromIndex, 0, array.length - 1);
  const to = clamp(toIndex, 0, array.length - 1);

  if (from === to) {
    return array;
  }

  const target = array[from];
  const delta = to < from ? -1 : 1;

  for (let i = from; i !== to; i += delta) {
    array[i] = array[i + delta];
  }

  array[to] = target;

  return array;
}