UNPKG

@extra-array/swap

Version:
15 lines (14 loc) 322 B
function index(x, i = 0) { return i < 0 ? Math.max(x.length + i, 0) : Math.min(i, x.length); } function swap$(x, i, j) { var i = index(x, i), j = index(x, j); var t = x[i]; x[i] = x[j]; x[j] = t; return x; } function swap(x, i, j) { return swap$(x.slice(), i, j); } export { swap as default };