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

object-diff-ast

v0.10.1

Published

Compares two objects and calculates the difference

Downloads

60

Readme

object-diff-ast

🌳 Compares two javascript objects and calculates the difference

Maintainability Test Coverage Build Status PRs Welcome


How to use

Install

npm i object-diff-ast

or

yarn add object-diff-ast

Importing

ES6

import { getDiff, render, getConfigDiff } from 'object-diff-ast'

ES5

const objDiff = require('object-diff-ast');

Functions

  • getDiff(originalObj, updatedObj) - returns the difference of original and updated objects as list of AST nodes.
  • render(nodes, format?) - renders list of AST nodes as formatted string
  • getConfigDiff(path1, path2, format?) - returns diff of to files as formatted string (nodejs env). Supported file extensions: .json, .ini or .yaml

Supported formats: "plain", "json" or "complex" (default)

Usage

getDiff

Returns list of diff objects (Node) that represents difference of two objects.

import { getDiff, JSONObject, Node } from 'object-diff-ast';
const before: JSONObject = {
  field1: 'smth',
  field2: 2,
  field3: true,
  deep: {
    field: {
      k: 'v1',
    },
  },
};

const after: JSONObject = {
  field1: 'smth',
  field2: 3,
  field4: {
    foo: 'bar',
  },
  deep: {
    field: {
      k: 'v2',
    },
  },
};

const diff: Node[] = getDiff(before, after);
console.log(diff);
// output >>>
/*
[
  { key: 'field1', level: 1, type: 'unchanged', oldValue: 'smth' },
  { key: 'field2', level: 1, type: 'changed', oldValue: 2, newValue: 3 },
  { key: 'field3', level: 1, type: 'removed', oldValue: true },
  {
    key: 'deep',
    level: 1,
    type: 'unit',
    unit: 'object',
    children: [
      {
        key: 'field',
        level: 2,
        type: 'unit',
        unit: 'object',
        children: [
          {
            key: 'k',
            level: 3,
            type: 'changed',
            oldValue: 'v1',
            newValue: 'v2',
          },
        ],
      },
    ],
  },
  { key: 'field4', level: 1, type: 'added', newValue: { foo: 'bar' } },
]
*/

Or with arrays as field values

import { getDiff, JSONObject, Node } from 'object-diff-ast';

const before: JSONObject = {
  field: ['first', 'second'],
};

const after: JSONObject = {
  field: ['first', 'third'],
  newField: [1, 2, 3],
};
const diff: Node[] = getDiff(before, after);
console.log(diff);
// output >>>
/*
[
  {
    key: 'field',
    level: 1,
    type: 'unit',
    unit: 'array',
    children: [
      { key: '0', level: 2, type: 'unchanged', oldValue: 'first' },
      { key: '1', level: 2, type: 'changed', oldValue: 'second', newValue: 'third' },
    ],
  },
  { key: 'newField', level: 1, type: 'added', newValue: [1, 2, 3] },
];
*/

render

Renders calculated diff in different formats.

import { getDiff, render, JSONObject } from 'object-diff-ast';

const before: JSONObject = {
  field1: 'smth',
  field2: 2,
  field3: true,
  deep: {
    foo: {
      bar: 'v1',
    },
    list: [1, 2],
  },
};

const after: JSONObject = {
  field1: 'smth',
  field2: 3,
  field4: {
    foo: 'bar',
  },
  deep: {
    foo: {
      bar: 'v2',
    },
    list: [1, 3, 4],
  },
};

const diff = getDiff(before, after);

const complex: string = render(diff); // default format = "complex"
console.log(complex);
// String output >>>
/*
{
    field1: smth
  + field2: 3
  - field2: 2
  - field3: true
    deep: {
        foo: {
          + bar: v2
          - bar: v1
        }
        list: [
            0: 1
          + 1: 3
          - 1: 2
          + 2: 4
        ]
    }
  + field4: {
        foo: bar
    }
}
*/

const plain = render(diff, 'plain');
console.log(plain);
// String output >>>
/*
Property 'field2' was updated. From '2' to '3'
Property 'field3' was removed
Property 'deep.foo.bar' was updated. From 'v1' to 'v2'
Property 'deep.list.1' was updated. From '2' to '3'
Property 'deep.list.2' was added with value '4'
Property 'field4' was added with complex value
*/

const json = render(diff, 'json'); // just the stringified diff object
console.log(json);
// String output >>>
/*
[
  {
    "key": "field1",
    "level": 1,
    "type": "unchanged",
    "oldValue": "smth"
  },
  {
    "key": "field2",
    "level": 1,
    "type": "changed",
    "oldValue": 2,
    "newValue": 3
  },
  {
    "key": "field3",
    "level": 1,
    "type": "removed",
    "oldValue": true
  },
  {
    "key": "deep",
    "level": 1,
    "type": "unit",
    "unit": "object",
    "children": [
      {
        "key": "foo",
        "level": 2,
        "type": "unit",
        "unit": "object",
        "children": [
          {
            "key": "bar",
            "level": 3,
            "type": "changed",
            "oldValue": "v1",
            "newValue": "v2"
          }
        ]
      },
      {
        "key": "list",
        "level": 2,
        "type": "unit",
        "unit": "array",
        "children": [
          {
            "key": "0",
            "level": 3,
            "type": "unchanged",
            "oldValue": 1
          },
          {
            "key": "1",
            "level": 3,
            "type": "changed",
            "oldValue": 2,
            "newValue": 3
          },
          {
            "key": "2",
            "level": 3,
            "type": "added",
            "newValue": 4
          }
        ]
      }
    ]
  },
  {
    "key": "field4",
    "level": 1,
    "type": "added",
    "newValue": {
      "foo": "bar"
    }
  }
]
*/

getConfigDiff

Wrapper over the render function. Returns diff of two configuration files as formatted string. (only Nodejs environment) Support json, ini and yaml file extensions.

import { getConfigDiff } from 'object-diff-ast';

const complexMessage = getConfigDiff('/Users/me/config1.yaml', '/Users/me/config2.yaml');

const plainMessage = getConfigDiff('/Users/me/config1.json', '/Users/me/config2.json', 'plain');

const jsonMessage = getConfigDiff('/Users/me/config1.ini', '/Users/me/config2.ini', 'json');

Console app

You also can use this package as nodejs console app.

App installation

npm i -g object-diff-ast

Usage

getdiff ./config1.yaml ./config2.yaml

or

getdiff ./config1.yaml ./config2.yaml -f plain