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

100% Statements 20/20
100% Branches 5/5
100% Functions 1/1
100% Lines 18/18

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    3x 3445x 3445x 3445x 3445x 13050x 13050x   5801x 2843x 2843x 2843x 2958x 5801x     4392x 4392x   2857x 2857x     3445x    
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 'number': {
        if (component > 0) {
          const end = offset + component;
          res += str.substring(offset, end);
          offset = end;
        } else offset -= component;
        break;
      }
      case 'string':
        res += component;
        break;
      case 'object':
        offset += component[0].length;
        break;
    }
  }
  return res + str.substring(offset);
};