How to use fast-json-patch - 10 common examples

To help you get started, we’ve selected a few fast-json-patch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github caohanyang / json_diff_rfc6902 / tests / diff.js View on Github external
var f_old = require(root + "old.json");
  var app_old = JSON.parse(JSON.stringify(f_old));
  var old_ori = JSON.parse(JSON.stringify(f_old));
  var f_new = require(root + "new.json");

  // var exp_patch = require(root + "expected.json");
  console.time("jdr-diff");
  // var jdr_patch = jdr.diff(f_old, f_new);  , HASH_ID: "title"
  var jdr_patch = jdr.diff(f_old, f_new, {OBJ_COM: true, ARR_COM: true});
  console.timeEnd("jdr-diff");

  console.time("jiff-diff");
  var jiff_patch = jiff.diff(f_old, f_new, jiff_options);
  console.timeEnd("jiff-diff");
  console.time("fjp-diff");
  var fjp_patch = fjp.compare(f_old, f_new);
  console.timeEnd("fjp-diff");
  console.time("rfc6902");
  var rfc6902_patch = rfc6902.createPatch(f_old, f_new);
  console.timeEnd("rfc6902");
  console.time("json8");
  var json8_patch = json8.diff(f_old, f_new);
  console.timeEnd("json8");

  // Use fjp to apply the patch fjp_patch jdr_patch jiff_patch
  fjp.apply(f_old, fjp_patch);

  jdr.apply(app_old, jdr_patch);

  fs.writeFile(root + "jdr_patch.json", JSON.stringify(jdr_patch, null, 2));
  fs.writeFile(root + "fjp_patch.json", JSON.stringify(fjp_patch, null, 2));
  fs.writeFile(root + "jiff_patch.json", JSON.stringify(jiff_patch, null, 2));
github FabricLabs / fabric / types / service.js View on Github external
}

    if (this.store) {
      // TODO: add robust + convenient database opener
      try {
        await this.store.batch(ops, function shareChanges () {
          // TODO: notify status?
        });
      } catch (E) {
        console.error('[FABRIC:SERVICE]', 'Threw Exception:', E);
      }
    }

    if (self.observer) {
      try {
        let patches = manager.generate(self.observer);
        if (patches.length) self.emit('patches', patches);
        if (patches.length) self.emit('message', {
          '@type': 'Commit',
          '@data': patches
        });
      } catch (E) {
        console.error('Could not generate patches:', E);
      }
    }

    return this;
  }
github axway-amplify-streams / axway-amplify-streams-js / stockmarket-angular2 / src / app / app.component.ts View on Github external
.onPatch((patch) => {
        // update the data with the provided patch// update the data with the provided patch
        console.log('--------------- on patch ---------------');
        //  console.log('patch: %o', patch);
        console.log('patch:');

        applyPatch(this.result, patch);

        console.log('result patched:');
        console.log('--------------- end on patch ---------------');

      }, this)
      .onError((error: StreamDataError) => {
github freeCodeCamp / meeting-for-good / client / components / AvailabilityGrid / AvailabilityGrid.js View on Github external
// need to call the full event to edit... since he dosn't have the
    // info that maybe have a guest "deleted"
    try {
      const eventFull = await loadEvent(event._id, true);
      const observerEvent = jsonpatch.observe(eventFull);
      // find for curUser at the array depends if is a participant
      // yet or not
      const curParticipant =
        isCurParticipantUpsert(curUser, eventFull, availabilityCurUserinQuarters.length);
      // because the patch jsonpatch dosent work as espected when you have a arrays of arrays
      // we need to generate a patch to delete all availability and then add ther availability again
      // then merge both patchs arrays.
      curParticipant.availability = [];
      const patchforDelete = jsonpatch.generate(observerEvent);
      curParticipant.availability = availabilityReducer(availabilityCurUserinQuarters);
      const patchesforAdd = jsonpatch.generate(observerEvent);
      const patches = _.concat(patchforDelete, patchesforAdd);
      await this.props.submitAvail(patches);
    } catch (err) {
      console.log('err at submit avail', err);
    }
  }
github freeCodeCamp / meeting-for-good / client / components / AvailabilityGrid / AvailabilityGrid.js View on Github external
// construct the avaqilabily for the cur user from grid
    const availabilityCurUserinQuarters = AvaliabilityCurUserFromGrid(grid, curUser);
    // need to call the full event to edit... since he dosn't have the
    // info that maybe have a guest "deleted"
    try {
      const eventFull = await loadEvent(event._id, true);
      const observerEvent = jsonpatch.observe(eventFull);
      // find for curUser at the array depends if is a participant
      // yet or not
      const curParticipant =
        isCurParticipantUpsert(curUser, eventFull, availabilityCurUserinQuarters.length);
      // because the patch jsonpatch dosent work as espected when you have a arrays of arrays
      // we need to generate a patch to delete all availability and then add ther availability again
      // then merge both patchs arrays.
      curParticipant.availability = [];
      const patchforDelete = jsonpatch.generate(observerEvent);
      curParticipant.availability = availabilityReducer(availabilityCurUserinQuarters);
      const patchesforAdd = jsonpatch.generate(observerEvent);
      const patches = _.concat(patchforDelete, patchesforAdd);
      await this.props.submitAvail(patches);
    } catch (err) {
      console.log('err at submit avail', err);
    }
  }
github FabricLabs / fabric / types / transition.js View on Github external
_applyTo (state) {
    if (!state) throw new Error('State must be provided.');
    if (!(state instanceof Entity)) throw new Error('State not of known Entity type.');

    let instance = Object.assign({}, state);
    let observer = monitor.observe(instance);

    try {
      monitor.applyPatch(instance, this._state.changes);
    } catch (E) {
      console.error('Could not apply changes:', E);
    }

    let changes = monitor.generate(observer);
    // console.log('changes:', changes);
    return instance;
  }
github FabricLabs / fabric / types / transition.js View on Github external
_applyTo (state) {
    if (!state) throw new Error('State must be provided.');
    if (!(state instanceof Entity)) throw new Error('State not of known Entity type.');

    let instance = Object.assign({}, state);
    let observer = monitor.observe(instance);

    try {
      monitor.applyPatch(instance, this._state.changes);
    } catch (E) {
      console.error('Could not apply changes:', E);
    }

    let changes = monitor.generate(observer);
    // console.log('changes:', changes);
    return instance;
  }
github freeCodeCamp / meeting-for-good / client / components / AvailabilityGrid / AvailabilityGrid.js View on Github external
async submitAvailability() {
    const { curUser } = this.props;
    const { grid, event } = this.state;
    // construct the avaqilabily for the cur user from grid
    const availabilityCurUserinQuarters = AvaliabilityCurUserFromGrid(grid, curUser);
    // need to call the full event to edit... since he dosn't have the
    // info that maybe have a guest "deleted"
    try {
      const eventFull = await loadEvent(event._id, true);
      const observerEvent = jsonpatch.observe(eventFull);
      // find for curUser at the array depends if is a participant
      // yet or not
      const curParticipant =
        isCurParticipantUpsert(curUser, eventFull, availabilityCurUserinQuarters.length);
      // because the patch jsonpatch dosent work as espected when you have a arrays of arrays
      // we need to generate a patch to delete all availability and then add ther availability again
      // then merge both patchs arrays.
      curParticipant.availability = [];
      const patchforDelete = jsonpatch.generate(observerEvent);
      curParticipant.availability = availabilityReducer(availabilityCurUserinQuarters);
      const patchesforAdd = jsonpatch.generate(observerEvent);
      const patches = _.concat(patchforDelete, patchesforAdd);
      await this.props.submitAvail(patches);
    } catch (err) {
      console.log('err at submit avail', err);
    }
github swagger-api / swagger-js / src / specmap / lib / index.js View on Github external
function getInByJsonPath(obj, jsonPath) {
  try {
    return jsonPatch.getValueByPointer(obj, jsonPath)
  }
  catch (e) {
    console.error(e) // eslint-disable-line no-console
    return {}
  }
}
github cujojs / cola / experiments / diff-array.js View on Github external
console.error('FAILED');
	console.error(e);
}

console.log('--------------------------');
console.log('fast-json-patch (modified)');
console.log('--------------------------');
var fastJsonPatch = require('fast-json-patch/src/json-patch-duplex');
setup();

try {
	// FAIL: The act of comparing 2 JSON objects/arrays causes one
	// to be mutated!?
	p = fastJsonPatch.compare(a1, a2);
	console.log('diff a1 a2\n', JSON.stringify(p));
	fastJsonPatch.apply(a1, p)
	console.log('patch a1\n', JSON.stringify(a1));
//	console.log('patch a1\n', JSON.stringify(jsonPatch.patch(p, a1)));
} catch (e) {
	console.error('FAILED');
	console.error(e);
}