UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

37 lines (36 loc) 1.1 kB
export const apply = (val, op) => { const length = op.length; let offset = 0; const res = []; let outputLength = 0; for (let i = 0; i < length; i++) { const component = op[i]; switch (typeof component) { case 'object': res.push(component); outputLength += component.length; break; case 'number': { if (component > 0) { const end = offset + component; res.push(val.subarray(offset, end)); outputLength += end - offset; offset = end; } else offset -= component; break; } } } res.push(val.subarray(offset)); outputLength += val.length - offset; const output = new Uint8Array(outputLength); const resLength = res.length; for (let i = 0, j = 0; i < resLength; i++) { const component = res[i]; output.set(component, j); j += component.length; } return output; };