Git Product home page Git Product logo

aurora-strategic-json-merge-patch's Introduction

Aurora Strategic JSON Merge

Aurora Strategic JSON Merge allows diffing and merging of objects, including array entries, however each array entry needs to have an object with a key attribute to serve as a unique identifier.

Installation

npm install https://github.com/tromkom/aurora-strategic-json-merge-patch

Usage

The key attribute should always be a string

Diffing

Diffs two objects and returns an array of patches

const { diff } = require("aurora-strategic-json-merge-patch");

let base = [
  {
    id: "1",
    val: 123,
    arr: [
      {
        key: "1",
        val: 1
      }
    ]
  },
  {
    id: "2",
    val: 456
  }
];

let newBase = [
  {
    id: "1",
    val: 456,
    arr: [
      {
        key: "1",
        val: 1
      },
      {
        key: "2",
        val: 222
      }
    ]
  }
];

let patches = diff(base, newBase, "id");
console.log(patches);
/*
[
  { id: '1', val: 456, arr: [ { key: '2', val: 222 } ] },
  { id: '2', __self: null }
]
*/

Patching

Using the patch from diffing on an equal base object, will return the new base.

const { patch } = require("aurora-strategic-json-merge-patch");

let base = [
  {
    id: "1",
    val: 123,
    arr: [
      {
        key: "1",
        val: 1
      }
    ]
  },
  {
    id: "2",
    val: 456
  }
];

let patches = [
  { id: "1", val: 456, arr: [{ key: "2", val: 222 }] },
  { id: "2", __self: null }
];

let newBase = patch(base, patches, "id");
console.log(newBase);
/*
[
  {
    id: '1',
    val: 456,
    arr: [ { key: '1', val: 1 }, { key: '2', val: 222 } ]
  }
]
*/

Http/JSON Patching

If you want to use JSON patch you can do that.

Be careful when using JSON patch as the patch will use the "key" as opposed to the index of the array

const { httpPatch } = require("aurora-strategic-json-merge-patch");

let base = [
  {
    id: "1",
    val: 123,
    arr: [
      {
        key: "1",
        val: 1
      }
    ]
  },
  {
    id: "2",
    val: 456
  }
];

let patches = [
  { id: "1", val: 456, arr: [{ key: "2", val: 222 }] },
  { id: "2", __self: null }
];

let httpPatches = base
  .map(x => httpPatch(x, patches, "id", `/${x.id}`).body)
  .flat();
console.log(httpPatches);
/*
[
  { op: 'replace', path: '/1/val', value: 456 },
  { path: '/1/arr/2', op: 'add', value: { key: '2', val: 222 } },
  { path: '/2', op: 'remove' }
]
*/

Merging

Does a strategic JSON Merge that will properly handle arrays (assuming they have a "key").

const { merge } = require("aurora-strategic-json-merge-patch");

let base = [
  {
    id: "1",
    val: 123,
    arr: [
      {
        key: "1",
        val: 1
      }
    ]
  },
  {
    id: "2",
    val: 456
  }
];

let newBase = [
  {
    id: "1",
    val: 456,
    arr: [
      {
        key: "2",
        val: 222
      }
    ]
  }
];

let res = merge(base, newBase, "id");
console.log(res);
/*
[
  {
    id: '1',
    val: 456,
    arr: [ { key: '1', val: 1 }, { key: '2', val: 222 } ]
  },
  { id: '2', val: 456 }
]
*/

aurora-strategic-json-merge-patch's People

Contributors

kasea avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.