All Projects → benjamine → Jsondiffpatch

benjamine / Jsondiffpatch

Licence: mit
Diff & patch JavaScript objects

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to Jsondiffpatch

Gojsondiff
Go JSON Diff
Stars: ✭ 371 (-90.61%)
Mutual labels:  json, patch, diff
dipa
dipa makes it easy to efficiently delta encode large Rust data structures.
Stars: ✭ 243 (-93.85%)
Mutual labels:  diff, delta, patch
Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (-83.85%)
Mutual labels:  json, diff, diffing
deltaq
Fast and portable delta encoding for .NET in 100% safe, managed code.
Stars: ✭ 26 (-99.34%)
Mutual labels:  diff, delta, patch
Diffson
A scala diff/patch library for Json
Stars: ✭ 258 (-93.47%)
Mutual labels:  json, patch, diff
textdiff-create
Create lean text diff deltas.
Stars: ✭ 25 (-99.37%)
Mutual labels:  diff, text-diff
duff
Pure OCaml implementation of libXdiff (Rabin's fingerprint)
Stars: ✭ 20 (-99.49%)
Mutual labels:  diff, patch
ecomparatio
eComparatio: text diff and support for digital edition
Stars: ✭ 21 (-99.47%)
Mutual labels:  diff, text-diff
Aehnlich
Show/Merge differences in directories and their content (text files) in Light/Dark designs
Stars: ✭ 73 (-98.15%)
Mutual labels:  diff, text-diff
go-delta
go-delta - A Go package and utility to generate and apply binary delta updates.
Stars: ✭ 25 (-99.37%)
Mutual labels:  diff, diffing
go-gitdiff
Go library for parsing and applying patches created by Git
Stars: ✭ 41 (-98.96%)
Mutual labels:  diff, patch
Ace Diff
A diff/merging wrapper for Ace Editor built on google-diff-match-patch
Stars: ✭ 257 (-93.5%)
Mutual labels:  diff, diffing
diff-text
Just get the diff of a simple inline text, simple mode.
Stars: ✭ 13 (-99.67%)
Mutual labels:  diff, text-diff
intellij-diff-plugin
Syntax highlighting for .diff files and .patch files in IntelliJ IDEs
Stars: ✭ 17 (-99.57%)
Mutual labels:  diff, patch
Editscript
A library designed to diff and patch Clojure data structures
Stars: ✭ 281 (-92.89%)
Mutual labels:  patch, diff
tmux-eaw-fix
tmux 2.6 以降において East Asian Ambiguous Character を全角文字の幅で表示する
Stars: ✭ 16 (-99.6%)
Mutual labels:  diff, patch
Differencekit
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
Stars: ✭ 2,986 (-24.42%)
Mutual labels:  diff, diffing
Multidiff
Binary data diffing for multiple objects or streams of data
Stars: ✭ 282 (-92.86%)
Mutual labels:  diff, diffing
Api Diff
A command line tool for diffing json rest APIs
Stars: ✭ 164 (-95.85%)
Mutual labels:  json, diff
diffy
Tools for finding and manipulating differences between files
Stars: ✭ 47 (-98.81%)
Mutual labels:  diff, patch

jsondiffpatch

Build Status Code Climate Test Coverage NPM version NPM dependencies

Diff & patch JavaScript objects


Live Demo

  • min+gzipped ~ 16KB
  • browser and server (/dist folder with bundles for UMD, commonjs, or ES modules)
  • (optionally) uses google-diff-match-patch for long text diffs (diff at character level)
  • smart array diffing using LCS, IMPORTANT NOTE: to match objects inside an array you must provide an objectHash function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check Array diff documentation
  • reverse a delta
  • unpatch (eg. revert object to its original state using a delta)
  • simplistic, pure JSON, low footprint delta format
  • multiple output formatters:
    • html (check it at the Live Demo)
    • annotated json (html), makes the JSON delta format self-explained
    • console (colored), try running ./node_modules/.bin/jsondiffpatch left.json right.json
    • JSON Patch format RFC 6902 support
    • write your own! check Formatters documentation
  • BONUS: jsondiffpatch.clone(obj) (deep clone)

Supported platforms

  • Any modern browser and IE8+

Testling Status

And you can test your current browser visiting the test page.

  • Node.js Build Status v8+

Usage

    // sample data
    var country = {
        name: "Argentina",
        capital: "Buenos Aires",
        independence: new Date(1816, 6, 9),
        unasur: true
    };

    // clone country, using dateReviver for Date objects
    var country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver);

    // make some changes
    country2.name = "Republica Argentina";
    country2.population = 41324992;
    delete country2.capital;

    var delta = jsondiffpatch.diff(country, country2);

    assertSame(delta, {
        "name":["Argentina","Republica Argentina"], // old value, new value
        "population":["41324992"], // new value
        "capital":["Buenos Aires", 0, 0] // deleted
    });

    // patch original
    jsondiffpatch.patch(country, delta);

    // reverse diff
    var reverseDelta = jsondiffpatch.reverse(delta);
    // also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta);

    var delta2 = jsondiffpatch.diff(country, country2);
    assert(delta2 === undefined)
    // undefined => no difference

Array diffing:

    // sample data
    var country = {
        name: "Argentina",
        cities: [
        {
            name: 'Buenos Aires',
            population: 13028000,
        },
        {
            name: 'Cordoba',
            population: 1430023,
        },
        {
            name: 'Rosario',
            population: 1136286,
        },
        {
            name: 'Mendoza',
            population: 901126,
        },
        {
            name: 'San Miguel de Tucuman',
            population: 800000,
        }
        ]
    };

    // clone country
    var country2 = JSON.parse(JSON.stringify(country));

    // delete Cordoba
    country.cities.splice(1, 1);

    // add La Plata
    country.cities.splice(4, 0, {
        name: 'La Plata'
        });

    // modify Rosario, and move it
    var rosario = country.cities.splice(1, 1)[0];
    rosario.population += 1234;
    country.cities.push(rosario);

    // create a configured instance, match objects by name
    var diffpatcher = jsondiffpatch.create({
        objectHash: function(obj) {
            return obj.name;
        }
    });

    var delta = diffpatcher.diff(country, country2);

    assertSame(delta, {
        "cities": {
            "_t": "a", // indicates this node is an array (not an object)
            "1": [
                // inserted at index 1
                {
                    "name": "Cordoba",
                    "population": 1430023
                }]
            ,
            "2": {
                // population modified at index 2 (Rosario)
                "population": [
                    1137520,
                    1136286
                ]
            },
            "_3": [
                // removed from index 3
                {
                    "name": "La Plata"
                }, 0, 0],
            "_4": [
                // move from index 4 to index 2
                '', 2, 3]
        }
    });

For more example cases (nested objects or arrays, long text diffs) check test/examples/

If you want to understand deltas, see delta format documentation

Installing

NPM

This works for node, or in browsers if you already do bundling on your app

npm install jsondiffpatch
var jsondiffpatch = require('jsondiffpatch').create(options);

browser

In a browser, you could load directly a bundle in /dist, eg. /dist/jsondiffpatch.umd.js.

Options

var jsondiffpatch = require('jsondiffpatch').create({
    // used to match objects when diffing arrays, by default only === operator is used
    objectHash: function(obj) {
        // this function is used only to when objects are not equal by ref
        return obj._id || obj.id;
    },
    arrays: {
        // default true, detect items moved inside the array (otherwise they will be registered as remove+add)
        detectMove: true,
        // default false, the value of items moved is not included in deltas
        includeValueOnMove: false
    },
    textDiff: {
        // default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch
        minLength: 60
    },
    propertyFilter: function(name, context) {
      /*
       this optional function can be specified to ignore object properties (eg. volatile data)
        name: property name, present in either context.left or context.right objects
        context: the diff context (has context.left and context.right objects)
      */
      return name.slice(0, 1) !== '$';
    },
    cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned
      (using jsondiffpatch.clone by default), to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching the same objects multiple times without serializing deltas.
      instead of true, a function can be specified here to provide a custom clone(value)
      */
});

Visual Diff

<!DOCTYPE html>
<html>
    <head>
        <script type='text/javascript' src="https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"></script>
        <link rel="stylesheet" href="./style.css" type="text/css" />
        <link rel="stylesheet" href="../formatters-styles/html.css" type="text/css" />
        <link rel="stylesheet" href="../formatters-styles/annotated.css" type="text/css" />
    </head>
    <body>
        <div id="visual"></div>
        <hr/>
        <div id="annotated"></div>
        <script>
            var left = { a: 3, b: 4 };
            var right = { a: 5, c: 9 };
            var delta = jsondiffpatch.diff(left, right);

            // beautiful html diff
            document.getElementById('visual').innerHTML = jsondiffpatch.formatters.html.format(delta, left);

            // self-explained json
            document.getElementById('annotated').innerHTML = jsondiffpatch.formatters.annotated.format(delta, left);
        </script>
    </body>
</html>

To see formatters in action check the Live Demo.

For more details check Formatters documentation

Console

# diff two json files, colored output (using chalk lib)
./node_modules/.bin/jsondiffpatch ./left.json ./right.json

# or install globally
npm install -g jsondiffpatch

jsondiffpatch ./demo/left.json ./demo/right.json

console_demo!

Plugins

diff(), patch() and reverse() functions are implemented using Pipes & Filters pattern, making it extremely customizable by adding or replacing filters on a pipe.

Check Plugins documentation for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].