packages/patch/lib/buildRevertPatch.js
function reverse(patch, previous, idx) {
  const op = patch.op;
  const path = patch.path;

  if (op === "copy" || (op === "add" && previous === undefined)) {
    if (idx === undefined) return { op: "remove", path: path };

    // for item pushed to array with -
    const tokens = decode(path);
    tokens[tokens.length - 1] = idx.toString();
    return { op: "remove", path: encode(tokens) };
  }
  if (op === "replace") return { op: "replace", path: path, value: previous };
  if (op === "move") return { op: "move", path: patch.from, from: path };
  if (op === "add" || op === "remove")
    return { op: "add", path: path, value: previous };
  if (op === "test") return { op: "test", path: path, value: patch.value };
}
function reverse(patch, previous, idx) {
  const op = patch.op;
  const path = patch.path;

  if (op === "copy" || (op === "add" && previous === undefined)) {
    if (idx === undefined) return { op: "remove", path: path };

    // for item pushed to array with -
    const tokens = decode(path);
    tokens[tokens.length - 1] = idx.toString();
    return { op: "remove", path: encode(tokens) };
  }
  if (op === "replace") return { op: "replace", path: path, value: previous };
  if (op === "move") return { op: "move", path: patch.from, from: path };
  if (op === "add" || op === "remove")
    return { op: "add", path: path, value: previous };
  if (op === "test") return { op: "test", path: path, value: patch.value };
}
function reverse ( patch , previous , idx ) { const op = patch . op ; const path = patch . path ; if ( op === "copy" || ( op === "add" && previous === undefined ) ) { if ( idx === undefined ) return { op : "remove" , path : path } ; // for item pushed to array with - const tokens = decode ( path ) ; tokens [ tokens . length - 1 ] = idx . toString ( ) ; return { op : "remove" , path : encode ( tokens ) } ; } if ( op === "replace" ) return { op : "replace" , path : path , value : previous } ; if ( op === "move" ) return { op : "move" , path : patch . from , from : path } ; if ( op === "add" || op === "remove" ) return { op : "add" , path : path , value : previous } ; if ( op === "test" ) return { op : "test" , path : path , value : patch . value } ; }
Return the reverse operation to a JSON Patch operation
@param  {Object}       patch     - JSON Patch operation object
@param  {Any}          previous  - previous value for add and replace operations
@param  {Number}       idx       - index of the item for array
@return {Object}
Return the reverse operation to a JSON Patch operation