packages/patch/lib/apply.js
function apply(doc, patch, options) {
  if (!Array.isArray(patch))
    throw new Error("Invalid argument, patch must be an array");

  const done = [];

  for (let i = 0, len = patch.length; i < len; i++) {
    const p = patch[i];
    let r;

    try {
      r = run(doc, p);
    } catch (err) {
      // restore document
      // does not use ./revert.js because it is a circular dependency
      const revertPatch = buildRevertPatch(done);
      apply(doc, revertPatch);
      throw err;
    }

    doc = r.doc;
    done.push([p, r.previous, r.idx]);
  }

  const result = { doc: doc };

  if (options && typeof options === "object" && options.reversible === true)
    result.revert = done;

  return result;
}
function apply(doc, patch, options) {
  if (!Array.isArray(patch))
    throw new Error("Invalid argument, patch must be an array");

  const done = [];

  for (let i = 0, len = patch.length; i < len; i++) {
    const p = patch[i];
    let r;

    try {
      r = run(doc, p);
    } catch (err) {
      // restore document
      // does not use ./revert.js because it is a circular dependency
      const revertPatch = buildRevertPatch(done);
      apply(doc, revertPatch);
      throw err;
    }

    doc = r.doc;
    done.push([p, r.previous, r.idx]);
  }

  const result = { doc: doc };

  if (options && typeof options === "object" && options.reversible === true)
    result.revert = done;

  return result;
}
function apply ( doc , patch , options ) { if ( ! Array . isArray ( patch ) ) throw new Error ( "Invalid argument, patch must be an array" ) ; const done = [ ] ; for ( let i = 0 , len = patch . length ; i < len ; i ++ ) { const p = patch [ i ] ; let r ; try { r = run ( doc , p ) ; } catch ( err ) { // restore document // does not use ./revert.js because it is a circular dependency const revertPatch = buildRevertPatch ( done ) ; apply ( doc , revertPatch ) ; throw err ; } doc = r . doc ; done . push ( [ p , r . previous , r . idx ] ) ; } const result = { doc : doc } ; if ( options && typeof options === "object" && options . reversible === true ) result . revert = done ; return result ; }
Apply a JSON Patch to a JSON document
@param  {Any}          doc                 - JSON document to apply the patch to
@param  {Array}        patch               - JSON Patch array
@param  {Object}       options             - options
@param  {Boolean}      options.reversible  - return an array to revert
@return {PatchResult}
Apply a JSON Patch to a JSON document