How to use json8-pointer - 7 common examples

To help you get started, we’ve selected a few json8-pointer 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 sonnyp / JSON8 / packages / patch / lib / diff.js View on Github external
module.exports = function diff(a, b, pre) {
  let patches = [];
  const prefix = pre || [];

  const at = type(a);
  const bt = type(b);

  if (bt !== at) {
    if (at === undefined)
      patches.push({ op: "add", path: encode(prefix), value: b });
    else patches.push({ op: "replace", path: encode(prefix), value: b });
    return patches;
  } else if (bt !== ARRAY && bt !== OBJECT) {
    if (!equal(a, b))
      patches.push({ op: "replace", path: encode(prefix), value: b });
    return patches;
  }

  if (a === b) return patches;

  // both are arrays
  if (Array.isArray(b)) {
    // FIXME let's be smarter about array diffing
    if (a.length === 0 && b.length === 0) return patches;
    if (equal(a, b)) return patches;
    patches.push({ op: "replace", path: encode(prefix), value: b });
  }
github sonnyp / JSON8 / packages / patch / lib / diff.js View on Github external
patches.push({ op: "replace", path: encode(prefix), value: b });
  }
  // both are objects
  else if (bt === OBJECT) {
    let i, l, keys, k;
    keys = Object.keys(b);
    for (i = 0, l = keys.length; i < l; i++) {
      k = keys[i];
      patches = patches.concat(diff(a[k], b[k], prefix.concat([k])));
    }

    keys = Object.keys(a);
    for (i = 0, l = keys.length; i < l; i++) {
      k = keys[i];
      if (b[k] !== undefined) continue;
      patches.push({ op: "remove", path: encode(prefix.concat([k])) });
    }
  }

  return patches;
};
github sonnyp / JSON8 / packages / patch / lib / apply.js View on Github external
function run(doc, patch) {
  const pathTokens = typeof patch.path === "string" ? decode(patch.path) : null;
  const fromTokens = typeof patch.from === "string" ? decode(patch.from) : null;

  switch (patch.op) {
    case "add":
    case "replace":
    case "test":
      if (patch.value === undefined) throw new Error("Missing value parameter");
      return operations[patch.op](doc, pathTokens, patch.value);

    case "move":
    case "copy":
      return operations[patch.op](doc, fromTokens, pathTokens);

    case "remove":
      return operations[patch.op](doc, pathTokens);
  }
github sonnyp / JSON8 / packages / patch / lib / apply.js View on Github external
function run(doc, patch) {
  const pathTokens = typeof patch.path === "string" ? decode(patch.path) : null;
  const fromTokens = typeof patch.from === "string" ? decode(patch.from) : null;

  switch (patch.op) {
    case "add":
    case "replace":
    case "test":
      if (patch.value === undefined) throw new Error("Missing value parameter");
      return operations[patch.op](doc, pathTokens, patch.value);

    case "move":
    case "copy":
      return operations[patch.op](doc, fromTokens, pathTokens);

    case "remove":
      return operations[patch.op](doc, pathTokens);
  }
github sonnyp / JSON8 / packages / patch / lib / operations.js View on Github external
'use strict'

var JSON8Pointer = require('json8-pointer')
var parse = JSON8Pointer.parse
var walk = JSON8Pointer.walk
var JSON8 = require('json8')
var clone = JSON8.clone
var equal = JSON8.equal
var get = require('./extras').get

/**
 * Add the value to the specified JSON Pointer location
 * http://tools.ietf.org/html/rfc6902#section-4.1
 *
 * @param  {Object|Array} doc    - JSON document to set the value to
 * @param  {Path}         path   - JSON Pointer string or tokens path
 * @param  {Any}          value  - value to add
 * @return {Array}               - [document, replaced value]
 */
var add = function(doc, path, value) {
github sonnyp / JSON8 / packages / patch / lib / walk.js View on Github external
module.exports = function walk(doc, tokens) {
  const length = tokens.length;

  let i = 0;
  let target = doc;
  let token;

  while (i < length - 1) {
    token = tokens[i++];

    if (Array.isArray(target)) validArrayToken(token, target.length);
    else if (typeof target !== OBJECT || target === null)
      throw new Error("Cannot be walked");

    target = target[token];
  }

  token = tokens[i];

  if (Array.isArray(target)) validArrayToken(token, target.length);
  else if (typeof target !== OBJECT || target === null)
    throw new Error("Invalid target");

  return [token, target];
};
github sonnyp / JSON8 / packages / patch / lib / operations.js View on Github external
'use strict'

var JSON8Pointer = require('json8-pointer')
var parse = JSON8Pointer.parse
var walk = JSON8Pointer.walk
var JSON8 = require('json8')
var clone = JSON8.clone
var equal = JSON8.equal
var get = require('./extras').get

/**
 * Add the value to the specified JSON Pointer location
 * http://tools.ietf.org/html/rfc6902#section-4.1
 *
 * @param  {Object|Array} doc    - JSON document to set the value to
 * @param  {Path}         path   - JSON Pointer string or tokens path
 * @param  {Any}          value  - value to add
 * @return {Array}               - [document, replaced value]
 */
var add = function(doc, path, value) {
  var tokens = parse(path)

json8-pointer

JSON Pointer toolkit for JavaScript

ISC
Latest version published 3 years ago

Package Health Score

53 / 100
Full package analysis