npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

diff-delta-json

v0.0.2

Published

Create delta (diff) json. History of json changes

Downloads

3

Readme

delta-json

Delta json, diff json, json changes, json history.

Usage

const Delta = require('diff-delta-json')

let delta = new Delta();
  • get changes in human readable format
let changes = delta.getChanges (obj1, obj2)
  • get delta json
let changes = delta.getDelta (obj1, obj2)
  • get history changes
let changes = delta.createHistory (obj1, obj2, userName) 

Options

Labels and ignore keys can be implemented by Option, default:

let options = {
  labels:{
    beforeLabel: 'before',
    afterLabel: 'after',
    deletedLabel: 'deleted',
    createdLabel: 'created',
    chagedLabel: 'changed'
  },
  ignore:[] // list of keys to ignore  
} 

const delta = new Delta(options);

Examples

const Delta = require('diff-delta-json');
const util = require('util');

const obj1 = {
  name: 'test',
  prop1: {
    prop2: {
      prop3: 'value1',
      prop4: ['1', '2']
    }
  }
};

const obj2 = {
  name: 'test',
  prop1: {
    prop2: {
      prop3: 'value31',
      prop4: ['1']
    }
  },
  prop5: {}
};

get changes in human readable format

const delta = new Delta();
const changes = delta.getChanges(obj1, obj2);

console.log(util.inspect(changes, false, null, true));

/* OUTPUT
{ prop1:
   { prop2:
      { prop3: { before: 'value1', after: 'value31' },
        prop4: { '1': { deleted: '2' } } } },
  prop5: { created: {} } }
*/

get delta json (can be used in partial update)

const delta = new Delta();
const delta = delta.getDelta(obj1, obj2);

console.log(util.inspect(delta, false, null, true));

/* OUTPUT
{ prop1: { prop2: { prop3: 'value31', prop4: [ '1' ] } },
  prop5: {} }
*/

get history changes

const delta = new Delta();

let userName = "Json Walker"; // anonymous if parameter isn't specified
let prevHistory = ['2020-05-14T18:04:50.872Z anonymous created property "prop1" with value "{}"'] // empty array if parameter isn't specified

let history = delta.createHistory(obj1, obj2);

console.log(history);

/* OUTPUT
[ '2020-06-14T18:04:50.871Z anonymous changed property "prop3" from value "value1" to "value31"',
  '2020-06-14T18:04:50.871Z anonymous changed property "prop4" from array "1,2" to "1"',
  '2020-06-14T18:04:50.872Z anonymous created property "prop5" with value "{}"' ]
*/

let history = delta.createHistory(obj1, obj2, userName, prevHistory);

console.log(history);

/* OUTPUT
[ '2020-05-14T18:04:50.872Z anonymous created property "prop1" with value "{}"',
  '2020-06-14T18:04:50.871Z Json Walker changed property "prop3" from value "value1" to "value31"',
  '2020-06-14T18:04:50.871Z Json Walker changed property "prop4" from array "1,2" to "1"',
  '2020-06-14T18:04:50.872Z Json Walker created property "prop5" with value "{}"' ]
*/