UNPKG

@extra-array/rotate-update

Version:
27 lines (26 loc) 651 B
'use strict'; function mod(x, y) { return x - y * Math.floor(x / y); } 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 rotate$(x, n = 0) { var n = mod(n, x.length); var y = x.slice(0, n); x.copyWithin(0, n); return copy$(x, y, x.length - n); } module.exports = rotate$;