UNPKG

@extra-array/swap

Version:
16 lines (15 loc) 331 B
'use strict'; 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); } module.exports = swap;