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

100% Statements 18/18
100% Branches 4/4
100% Functions 1/1
100% Lines 16/16

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    11x 3433x 3433x 3433x 3433x 11976x 11976x   4250x 4250x   7726x 2867x 2867x 2867x 4859x 7726x       3433x    
import type {StringOp} from './types';
 
export const apply = (str: string, op: StringOp): string => {
  const length = op.length;
  let res = '';
  let offset = 0;
  for (let i = 0; i < length; i++) {
    const component = op[i];
    switch (typeof component) {
      case 'string':
        res += component;
        break;
      case 'number': {
        if (component > 0) {
          const end = offset + component;
          res += str.substring(offset, end);
          offset = end;
        } else offset -= component;
        break;
      }
    }
  }
  return res + str.substring(offset);
};