16 lines
299 B
TypeScript
16 lines
299 B
TypeScript
export function move<T>(arr: T[], from: number, to: number): T[] {
|
|
if (
|
|
from === to ||
|
|
from < 0 ||
|
|
to < 0 ||
|
|
from >= arr.length ||
|
|
to >= arr.length
|
|
) {
|
|
return arr
|
|
}
|
|
const next = arr.slice()
|
|
const [sp] = next.splice(from, 1)
|
|
next.splice(to, 0, sp)
|
|
return next
|
|
}
|