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

100% Statements 31/31
100% Branches 6/6
100% Functions 2/2
100% Lines 29/29

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 401x   1x 27x 27x 27x 27x 24x 24x 24x 24x 24x 16x 16x 16x       11x     1x 22x 22x 22x 22x 15x 15x 15x 15x 15x 6x 6x 6x       16x    
import {isValidIndex, isChild, parent, type Path} from '@jsonjoy.com/json-pointer';
 
export const bumpArrayPath = (path1: Path, path2: Path): undefined | Path => {
  const folder = parent(path1);
  const lastIndex = path1.length - 1;
  const step1 = path1[lastIndex];
  if (isChild(folder, path2)) {
    const step2 = path2[lastIndex];
    if (isValidIndex(step2)) {
      const index1 = Number(step1);
      const index2 = Number(step2);
      if (index1 <= index2) {
        const steps = [...path2];
        steps[lastIndex] = String(index2 + 1);
        return steps;
      }
    }
  }
  return undefined;
};
 
export const lowerArrayPath = (path1: Path, path2: Path): undefined | Path => {
  const folder = parent(path1);
  const lastIndex = path1.length - 1;
  const step1 = path1[lastIndex];
  if (isChild(folder, path2)) {
    const step2 = path2[lastIndex];
    if (isValidIndex(step2)) {
      const index1 = Number(step1);
      const index2 = Number(step2);
      if (index1 < index2) {
        const steps = [...path2];
        steps[lastIndex] = String(index2 - 1);
        return steps;
      }
    }
  }
  return undefined;
};