All files / json-patch/codec/compact decode.ts

98.87% Statements 88/89
98.52% Branches 67/68
100% Functions 6/6
98.83% Lines 85/86

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174    2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x   2x 166x     14x     8x     6x     4x     8x     4x     4x     4x     6x     6x     4x     6x   92x       2x 140x     34x     6x     6x     14x     6x     4x     4x     4x     6x     6x     2x     10x     10x     10x     10x 10x     24x 24x 24x           4x 4x     12x 12x 12x           4x 4x     12x 12x 12x                 2x 166x 166x 166x 166x 166x   166x    
import type {Op, PredicateOp} from '../../op';
import type {CompactOp} from './types';
import {OpAdd} from '../../op/OpAdd';
import {OpRemove} from '../../op/OpRemove';
import {OpReplace} from '../../op/OpReplace';
import {OpMove} from '../../op/OpMove';
import {OpCopy} from '../../op/OpCopy';
import {OpTest} from '../../op/OpTest';
import {OpFlip} from '../../op/OpFlip';
import {OpInc} from '../../op/OpInc';
import {OpStrIns} from '../../op/OpStrIns';
import {OpStrDel} from '../../op/OpStrDel';
import {OpSplit} from '../../op/OpSplit';
import {OpMerge} from '../../op/OpMerge';
import {OpExtend} from '../../op/OpExtend';
import {OpDefined} from '../../op/OpDefined';
import {OpUndefined} from '../../op/OpUndefined';
import {OpTestType} from '../../op/OpTestType';
import {OpTestString} from '../../op/OpTestString';
import {OpTestStringLen} from '../../op/OpTestStringLen';
import {OpContains} from '../../op/OpContains';
import {OpEnds} from '../../op/OpEnds';
import {OpStarts} from '../../op/OpStarts';
import {OpIn} from '../../op/OpIn';
import {OpLess} from '../../op/OpLess';
import {OpMore} from '../../op/OpMore';
import {OpAnd} from '../../op/OpAnd';
import {OpOr} from '../../op/OpOr';
import {OpNot} from '../../op/OpNot';
import {OpMatches} from '../../op/OpMatches';
import {OpType} from '../../op/OpType';
import {toPath} from '@jsonjoy.com/json-pointer';
import {OPCODE} from '../../constants';
import type {JsonPatchOptions} from '../../types';
import {createMatcherDefault} from '../../util';
 
export const compactToOp = (op: CompactOp, options: JsonPatchOptions): Op => {
  switch (op[0]) {
    case OPCODE.add:
    case 'add':
      return new OpAdd(toPath(op[1]), op[2]);
    case OPCODE.remove:
    case 'remove':
      return new OpRemove(toPath(op[1]), op[2]);
    case OPCODE.replace:
    case 'replace':
      return new OpReplace(toPath(op[1]), op[2], op[3]);
    case OPCODE.move:
    case 'move':
      return new OpMove(toPath(op[1]), toPath(op[2]));
    case OPCODE.copy:
    case 'copy':
      return new OpCopy(toPath(op[1]), toPath(op[2]));
    case OPCODE.flip:
    case 'flip':
      return new OpFlip(toPath(op[1]));
    case OPCODE.inc:
    case 'inc':
      return new OpInc(toPath(op[1]), op[2]);
    case OPCODE.str_ins:
    case 'str_ins':
      return new OpStrIns(toPath(op[1]), op[2], op[3]);
    case OPCODE.str_del:
    case 'str_del':
      return new OpStrDel(toPath(op[1]), op[2], op[3] || undefined, op[4]);
    case OPCODE.split:
    case 'split':
      return new OpSplit(toPath(op[1]), op[2], op[3] || null);
    case OPCODE.merge:
    case 'merge':
      return new OpMerge(toPath(op[1]), op[2], op[3] || null);
    case OPCODE.extend:
    case 'extend':
      return new OpExtend(toPath(op[1]), op[2], !!op[3]);
    default:
      return compactToPredicateOp(op, options);
  }
};
 
export const compactToPredicateOp = (op: CompactOp, options: JsonPatchOptions): PredicateOp => {
  switch (op[0]) {
    case OPCODE.test:
    case 'test':
      return new OpTest(toPath(op[1]), op[2], !!op[3]);
    case OPCODE.defined:
    case 'defined':
      return new OpDefined(toPath(op[1]));
    case OPCODE.undefined:
    case 'undefined':
      return new OpUndefined(toPath(op[1]));
    case OPCODE.type:
    case 'type':
      return new OpType(toPath(op[1]), op[2]);
    case OPCODE.test_type:
    case 'test_type':
      return new OpTestType(toPath(op[1]), op[2]);
    case OPCODE.test_string:
    case 'test_string':
      return new OpTestString(toPath(op[1]), op[2], op[3], !!op[4]);
    case OPCODE.test_string_len:
    case 'test_string_len':
      return new OpTestStringLen(toPath(op[1]), op[2], !!op[3]);
    case OPCODE.contains:
    case 'contains':
      return new OpContains(toPath(op[1]), op[2], !!op[3]);
    case OPCODE.ends:
    case 'ends':
      return new OpEnds(toPath(op[1]), op[2], !!op[3]);
    case OPCODE.starts:
    case 'starts':
      return new OpStarts(toPath(op[1]), op[2], !!op[3]);
    case OPCODE.matches:
    case 'matches':
      return new OpMatches(toPath(op[1]), op[2], !!op[3], options.createMatcher || createMatcherDefault);
    case OPCODE.in:
    case 'in':
      return new OpIn(toPath(op[1]), op[2]);
    case OPCODE.less:
    case 'less':
      return new OpLess(toPath(op[1]), op[2]);
    case OPCODE.more:
    case 'more':
      return new OpMore(toPath(op[1]), op[2]);
    case OPCODE.and:
    case 'and': {
      const path = toPath(op[1]);
      return new OpAnd(
        path,
        op[2].map((x) => {
          const copy = [...x];
          copy[1] = [...path, ...toPath(x[1])];
          return compactToPredicateOp(copy as CompactOp, options);
        }),
      );
    }
    case OPCODE.or:
    case 'or': {
      const path = toPath(op[1]);
      return new OpOr(
        path,
        op[2].map((x) => {
          const copy = [...x];
          copy[1] = [...path, ...toPath(x[1])];
          return compactToPredicateOp(copy as CompactOp, options);
        }),
      );
    }
    case OPCODE.not:
    case 'not': {
      const path = toPath(op[1]);
      return new OpNot(
        path,
        op[2].map((x) => {
          const copy = [...x];
          copy[1] = [...path, ...toPath(x[1])];
          return compactToPredicateOp(copy as CompactOp, options);
        }),
      );
    }
    default:
      throw new Error('OP_UNKNOWN');
  }
};
 
export function decode(patch: readonly CompactOp[], options: JsonPatchOptions): Op[] {
  const ops: Op[] = [];
  const length = patch.length;
  for (let i = 0; i < length; i++) {
    const op = compactToOp(patch[i], options);
    ops.push(op);
  }
  return ops;
}