UNPKG

@extra-array/copy

Version:

Copies part of array to another.

20 lines (19 loc) 521 B
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 copy(x, y, j = 0, i = 0, I = y.length) { return copy$(x.slice(), y, j, i, I); } export { copy as default };