fn
swap
v0.0.10testedReturn a new array with the items at indices `a` and `b` swapped. Returns a shallow copy unchanged when either index is out of range or equal. Never mutates.
Example
ts
swap(['a', 'b', 'c'], 0, 2); // ['c', 'b', 'a']Signature
ts
export function swap<T>(array: readonly T[], a: number, b: number): T[]{ ... }Type Parameters
TParameters
| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | The source array |
a | number | The source array |
b | number | First index |
Returns
T[]A new array with the two items swapped