All files / json-patch-ot util.ts

100% Statements 11/11
100% Branches 9/9
100% Functions 1/1
100% Lines 10/10

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 131x   1x 14x 14x 14x 14x 14x 14x 14x 14x    
import {isValidIndex} from '@jsonjoy.com/json-pointer';
 
export function replaceIndices(path: string, arrayPath: string, index: string, incUp: boolean): string {
  const remainder = path.substr(arrayPath.length);
  let slashIndex = remainder.indexOf('/');
  if (slashIndex === -1) slashIndex = remainder.length;
  const oldIndex = remainder.substr(0, slashIndex);
  const rest = remainder.substr(slashIndex);
  const isOldBigger = incUp ? oldIndex >= index : oldIndex > index;
  const shouldChangeIndex = isValidIndex(oldIndex) && isOldBigger;
  return shouldChangeIndex ? `${arrayPath}${~~oldIndex + (incUp ? 1 : -1)}${rest}` : path;
}