All files / json-patch-ot/transforms xStrIns.ts

90.47% Statements 19/21
81.81% Branches 9/11
100% Functions 1/1
88.88% Lines 16/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 29 30 31 32 331x 1x   1x 8x 5x 4x 3x 3x 3x 2x 2x 1x 1x 1x                       2x 1x          
import {operationToOp} from '../../json-patch/codec/json';
import {type Op, OpStrDel, OpStrIns} from '../../json-patch/op';
 
export const xStrIns = (ins: OpStrIns, op: Op): null | Op | Op[] => {
  if (op instanceof OpStrIns) {
    if (ins.pos > op.pos) return op;
    return operationToOp({...op.toJson(), pos: op.pos + ins.str.length}, {});
  } else if (op instanceof OpStrDel) {
    const del = op;
    if (del.pos < ins.pos) {
      const deleteLength: number = typeof del.str === 'string' ? del.str.length : del.len!;
      if (del.pos + deleteLength > ins.pos) {
        const beforeLength = ins.pos - del.pos;
        if (typeof del.str === 'string') {
          return [
            operationToOp({...del.toJson(), pos: ins.pos + ins.str.length, str: del.str.substr(beforeLength)}, {}),
            operationToOp({...del.toJson(), pos: del.pos, str: del.str.substr(0, beforeLength)}, {}),
          ];
        } else E{
          return [
            operationToOp({...del.toJson(), pos: ins.pos + ins.str.length, len: deleteLength - beforeLength}, {}),
            operationToOp({...del.toJson(), pos: del.pos, len: beforeLength}, {}),
          ];
        }
      }
    }
    if (ins.pos < del.pos) return operationToOp({...op.toJson(), pos: op.pos + ins.str.length}, {});
    return op;
  }
 
  return op;
};