Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flat-diff

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flat-diff

A naive diff algorithm based on the unique key path of each value in an Object / JSON

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

flat-diff

A naive diff algorithm based on the unique key path of each value in an Object / JSON

npm package Build Status Coverage Status

Installation

$ npm install flat-diff --save

Example

Given:

  import diff from 'flat-diff';

  const base = {
    a: [1, 2, { b: 1 }, 4, 5, 6],
    b: 'test',
    c: 'prev'
  };

  const target = {
    a: [1, 2, { b: 2 }, 4, 6],
    b: 'test 2',
    d: 'new'
  };

  const metadata = diff(base, target);

will give a result of:

  const metadata = [
    { path: 'a.0', type: 'same', value: 1 },
    { path: 'a.1', type: 'same', value: 2 },
    { path: 'a.2.b', type: 'put', prev: 1, value: 2 },
    { path: 'a.3', type: 'same', value: 4 },
    { path: 'a.4', type: 'put', prev: 5, value: 6 },
    { path: 'a.5', type: 'del', value: 6 },
    { path: 'b', type: 'put', prev: 'test', value: 'test 2' },
    { path: 'c', type: 'del', value: 'prev' },
    { path: 'd', type: 'post', value: 'new' }
  ];

  // Note: path keys will be sorted.

Caveats

Due to how keys are generated, if you have an array and object with number keys in the same path, there will be a conflict.

i.e.

// object
{
  a: {
    '0': 'b',
    '1': 'c'
  }
}

// array
{
  a: ['b', 'c']
}


// both formats above will result in similar paths:
[
  {path: 'a.0', value: 'b'},
  {path: 'a.1', value: 'c'}
]

License

flat-diff is MIT licensed

Keywords

FAQs

Package last updated on 25 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc