All files / json-patch/applyPatch v1.ts

85% Statements 17/20
50% Branches 1/2
66.66% Functions 2/3
88.23% Lines 15/17

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 281x     1x     1x         1x 307x 307x 307x 307x 320x 250x 250x   237x     1x 307x 237x    
import {clone as deepClone} from '@jsonjoy.com/util/lib/json-clone/clone';
import type {Operation} from '../types';
import type {Op} from '../op';
import {decode} 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 {
  const result = applyOps(doc, decode(patch, options), options.mutate);
  return result;
}