Function
arraySwap
(
array: T,
from: number,
to: number
)
Source from the content-addressed store, hash-verified
| 28 | * Swap two array items with each other. Returns a new array with the two items at `from` and `to` exchanged in place. |
| 29 | */ |
| 30 | export function arraySwap<T extends any[]>( |
| 31 | array: T, |
| 32 | from: number, |
| 33 | to: number |
| 34 | ): T { |
| 35 | if (from === to) { |
| 36 | return array; |
| 37 | } |
| 38 | |
| 39 | const newArray = array.slice() as T; |
| 40 | const item = newArray[from]; |
| 41 | |
| 42 | newArray[from] = newArray[to]; |
| 43 | newArray[to] = item; |
| 44 | |
| 45 | return newArray; |
| 46 | } |
| 47 | |
| 48 | type Items = UniqueIdentifier[] | {id: UniqueIdentifier}[]; |
| 49 | |
Tested by
no test coverage detected