File size: 2,082 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
export function moveToStart<T>(array: T[], selectItemCallback: (item: T) => boolean): T[];
export function moveToStart<T>(array: T[], item: T): T[];
export function moveToStart<T>(array: T[], arg1: T | ((item: T) => boolean)): T[] {
  const index = arg1 instanceof Function ? array.findIndex(arg1) : array.indexOf(arg1);
  if (index > 0) {
    const [item] = array.splice(index, 1);
    //@ts-expect-error - These indicies are safe per the previous check
    array.unshift(item);
  }
  return array;
}

export function moveOneToStart<T>(array: T[], selectItemCallback: (item: T) => boolean): T[];
export function moveOneToStart<T>(array: T[], item: T): T[];
export function moveOneToStart<T>(array: T[], arg1: T | ((item: T) => boolean)): T[] {
  const index = arg1 instanceof Function ? array.findIndex(arg1) : array.indexOf(arg1);
  if (index > 0) {
    //@ts-expect-error - These indicies are safe per the previous check
    [array[index], array[index - 1]] = [array[index - 1], array[index]];
  }
  return array;
}

export function moveToEnd<T>(array: T[], selectItemCallback: (item: T) => boolean): T[];
export function moveToEnd<T>(array: T[], item: T): T[];
export function moveToEnd<T>(array: T[], arg1: T | ((item: T) => boolean)): T[] {
  const index = arg1 instanceof Function ? array.findIndex(arg1) : array.indexOf(arg1);
  if (index >= 0 && index < array.length - 1) {
    const [item] = array.splice(index, 1);
    //@ts-expect-error - These indicies are safe per the previous check
    array.push(item);
  }
  return array;
}

export function moveOneToEnd<T>(array: T[], selectItemCallback: (item: T) => boolean): T[];
export function moveOneToEnd<T>(array: T[], item: T): T[];
export function moveOneToEnd<T>(array: T[], arg1: T | ((item: T) => boolean)): T[] {
  const index = arg1 instanceof Function ? array.findIndex(arg1) : array.indexOf(arg1);
  if (index >= 0 && index < array.length - 1) {
    //@ts-expect-error - These indicies are safe per the previous check
    [array[index], array[index + 1]] = [array[index + 1], array[index]];
  }
  return array;
}