All files / json-ot/types/ot-binary-irreversible apply.ts

100% Statements 31/31
100% Branches 4/4
100% Functions 1/1
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37    13x 3432x 3432x 3432x 3432x 3432x 11815x 11815x   4140x 4140x 4140x   7675x 2918x 2918x 2918x 2918x 4757x 7675x       3432x 3432x 3432x 3432x 3432x 10490x 10490x 10490x   3432x    
import type {BinaryOp} from './types';
 
export const apply = (val: Uint8Array, op: BinaryOp): Uint8Array => {
  const length = op.length;
  let offset = 0;
  const res: Uint8Array[] = [];
  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;
};