UNPKG

@extra-array/move-within-update

Version:
26 lines (25 loc) 674 B
'use strict'; function index(x, i = 0) { return i < 0 ? Math.max(x.length + i, 0) : Math.min(i, x.length); } function indexRange(x, i = 0, I = x.length) { i = index(x, i); I = Math.max(i, index(x, I)); return [i, I]; } function copy$(x, y, j = 0, i = 0, I = y.length) { var j = index(x, j); var [i, I] = indexRange(y, i, I); for (; i < I; i++, j++) x[j] = y[i]; return x; } function moveWithin$(x, j = 0, i = 0, I = x.length) { var p = x.slice(i, I), P = p.length; if (j < i) x.copyWithin(j + P, j, i); else x.copyWithin(i, I, j); return copy$(x, p, j < i ? j : j - P); } module.exports = moveWithin$;