All files / json-patch/applyPatch v4.ts

89.65% Statements 26/29
66.66% Branches 2/3
66.66% Functions 2/3
91.66% Lines 22/24

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 367x     7x     7x         7x 182x 182x 182x 182x 183x 183x 183x   182x     7x 852x 852x 852x 852x 890x 890x 772x 772x   734x    
import {clone as deepClone} from '@jsonjoy.com/util/lib/json-clone/clone';
import type {Operation} from '../types';
import type {Op} from '../op';
import {operationToOp} from '../codec/json';
import type {ApplyPatchOptions, OpResult, PatchResult} from './types';
 
export function applyOp(doc: unknown, op: Op, mutate: boolean): OpResult {
  Iif (!mutate) doc = deepClone(doc);
  return op.apply(doc);
}
 
export function applyOps(doc: unknown, ops: readonly Op[], mutate: boolean): PatchResult {
  if (!mutate) doc = deepClone(doc);
  const res: OpResult[] = [];
  const length = ops.length;
  for (let i = 0; i < length; i++) {
    const opResult = ops[i].apply(doc);
    doc = opResult.doc;
    res.push(opResult);
  }
  return {doc, res};
}
 
export function applyPatch(doc: unknown, patch: readonly Operation[], options: ApplyPatchOptions): PatchResult {
  if (!options.mutate) doc = deepClone(doc);
  const res: OpResult[] = [];
  const length = patch.length;
  for (let i = 0; i < length; i++) {
    const op = operationToOp(patch[i], options);
    const opResult = op.apply(doc);
    doc = opResult.doc;
    res.push(opResult);
  }
  return {doc, res};
}