All files / json-patch-diff JsonPatchDiff.ts

94.8% Statements 73/77
92% Branches 23/25
90% Functions 9/10
93.93% Lines 62/66

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1172x 2x 2x 2x     2x 262x     224x 223x       20x 16x 16x     22x 15x                 28x 28x 64x 62x 62x 62x 32x   2x     28x 64x 2x         238x 238x 238x 238x 945x 888x 238x 238x 238x 238x 238x 656x 656x   73x   216x 216x 216x 216x     155x 155x   212x 212x           510x   22x 2x 22x         221x 221x     267x 1x 1x   266x 238x   238x   28x 28x                 478x 478x      
import {deepEqual} from '@jsonjoy.com/util/lib/json-equal/deepEqual';
import * as str from '../util/diff/str';
import * as line from '../util/diff/line';
import {structHash} from '../json-hash';
import type {Operation} from '../json-patch/codec/json/types';
 
export class JsonPatchDiff {
  protected patch: Operation[] = [];
 
  protected diffVal(path: string, src: unknown, dst: unknown): void {
    if (deepEqual(src, dst)) return;
    this.patch.push({op: 'replace', path, value: dst});
  }
 
  protected diffStr(path: string, src: string, dst: string): void {
    if (src === dst) return;
    const patch = this.patch;
    str.apply(
      str.diff(src, dst),
      src.length,
      (pos, str) => patch.push({op: 'str_ins', path, pos, str}),
      (pos, len, str) => patch.push({op: 'str_del', path, pos, len, str}),
    );
  }
 
  protected diffBin(path: string, src: Uint8Array, dst: Uint8Array): void {
    throw new Error('Not implemented');
  }
 
  protected diffObj(path: string, src: Record<string, unknown>, dst: Record<string, unknown>): void {
    const patch = this.patch;
    for (const key in src) {
      if (key in dst) {
        const val1 = src[key];
        const val2 = dst[key];
        if (val1 === val2) continue;
        this.diffAny(path + '/' + key, val1, val2);
      } else {
        patch.push({op: 'remove', path: path + '/' + key});
      }
    }
    for (const key in dst) {
      if (key in src) continue;
      patch.push({op: 'add', path: path + '/' + key, value: dst[key]});
    }
  }
 
  protected diffArr(path: string, src: unknown[], dst: unknown[]): void {
    const srcLines: string[] = [];
    const dstLines: string[] = [];
    const srcLen = src.length;
    const dstLen = dst.length;
    for (let i = 0; i < srcLen; i++) srcLines.push(structHash(src[i]));
    for (let i = 0; i < dstLen; i++) dstLines.push(structHash(dst[i]));
    const pfx = path + '/';
    const patch = this.patch;
    const linePatch = line.diff(srcLines, dstLines);
    const length = linePatch.length;
    for (let i = length - 1; i >= 0; i--) {
      const [type, srcIdx, dstIdx] = linePatch[i];
      switch (type) {
        case line.LINE_PATCH_OP_TYPE.EQL:
          break;
        case line.LINE_PATCH_OP_TYPE.MIX: {
          const srcValue = src[srcIdx];
          const dstValue = dst[dstIdx];
          this.diff(pfx + srcIdx, srcValue, dstValue);
          break;
        }
        case line.LINE_PATCH_OP_TYPE.INS:
          patch.push({op: 'add', path: pfx + (srcIdx + 1), value: dst[dstIdx]});
          break;
        case line.LINE_PATCH_OP_TYPE.DEL:
          patch.push({op: 'remove', path: pfx + srcIdx});
          break;
      }
    }
  }
 
  public diffAny(path: string, src: unknown, dst: unknown): void {
    switch (typeof src) {
      case 'string': {
        if (typeof dst === 'string') this.diffStr(path, src, dst);
        else this.diffVal(path, src, dst);
        break;
      }
      case 'number':
      case 'boolean':
      case 'bigint': {
        this.diffVal(path, src, dst);
        break;
      }
      case 'object': {
        if (!src || !dst || typeof dst !== 'object') {
          this.diffVal(path, src, dst);
          return;
        }
        if (Array.isArray(src)) {
          if (Array.isArray(dst)) this.diffArr(path, src, dst);
          else Ethis.diffVal(path, src, dst);
          return;
        }
        this.diffObj(path, src as Record<string, unknown>, dst as Record<string, unknown>);
        break;
      }
      default:
        this.diffVal(path, src, dst);
        break;
    }
  }
 
  public diff(path: string, src: unknown, dst: unknown): Operation[] {
    this.diffAny(path, src, dst);
    return this.patch;
  }
}