Coder Social home page Coder Social logo

js-tree-diff's Introduction

js-tree-diff

JSON Graph Tree Diffing Module

About

This module provides a function to diff two JSON Graph Trees and results in a list of patch operations.

An example of the output follows:

const tree1 = {
    key: 0,
    path: [],
    label: 'div',
    attrs: { },
    children: [
        {
            key: 0,
            path: [ 0 ],
            label: 'p',
            attrs: { },
            children: []
        }
    ]
};

const tree2 = {
    key: 0,
    path: [],
    label: 'div',
    attrs: { },
    children: [
        {
            key: 0,
            path: [ 0 ],
            label: 'p',
            attrs: { className: 'child-class' },
            children: []
        }
    ]
};

console.log( diff( tree1, tree2 ));
// [{
//     op: 'UPDATE',
//     node: { key: 0, path: [ 0 ] },
//     type: 'attr',
//     attr: 'className',
//     value: 'child-class'
// }];

A JSON Graph Tree is a JSON Object of nodes in the following formats:

`type standard node`: {
    key: `<string>`,
    path: [ `<string>` ],
    label: `<string>`,
    attrs: { `<string>`: `<string>` },
    children: [
        `<node>`
    ]
}

`type leaf node`: {
    key: `<string>`,
    path: [ `<string>` ],
    label: `<string>`,
    attrs: { `<string>`: `<string>` },
    text: `<string>`
}

Provided two trees, the output of the diffing function is a list of patch operations:

`type patch`: {
    op: `<string:operation>`,
    node: {
        key: `<string>`,
        path: [ `<string>` ]
    }
}

Supported operations include:

  • DELETE
  • INSERT - The node attribute contains the entire <node>
  • REORDER - The attribute index: <int> is provided
  • UPDATE - The attributes type: <string>, attr: <string>, and value: <string> are provided

Install

( Not published yet ) $ npm install --save js-tree-diff

Development

Setup

$ npm install

Tests

$ npm run tests

Build ( pre-publish only )

$ npm run build

js-tree-diff's People

Contributors

redappleorigin avatar tylerbwilliams 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.