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

entity-diff

v1.0.6

Published

A simple entity diff generator.

Downloads

1,522

Readme

Entity diff

A simple entity diff generator.

generates a list of changes made to the entity.

All you need are two objects, one with the entity before the changes and the other with it after the changes.

Try it out

how to Install

npm:

npm install entity-diff

yarn:

yarn add entity-diff

How to use:


import { Audit } from "entity-diff";

const audit = new Audit();

const before = {
  name: "Mason",
  age: 20
};

const after = {
  name: "Mason Floyd",
  age: 20
};

const result = audit.diff(before, after);

// result:
// [
//   {
//     key: "name",
//     from: "Mason",
//     to: "Mason Floyd"
//   }
// ]

Audit Options:

It is possible to change some information in the final result of the diffs using parameters.

Ignoring properties

It is possible to ignore some keys of the objects audited through a list.

  import { Audit } from "entity-diff";

  const before = {
    name: "Mason",
    age: 20
  };

  const after = {
    name: "Mason Floyd", // this change will be ignored
    age: 25
  };

  const ignore = ["name"]; 

  const audit = new Audit({ ignore });

  const result = audit.diff(before, after);

  // result:
  // [
  //   {
  //     key: "age",
  //     from: 20,
  //     to: 25
  //   }
  // ]

Different name in the keys

Changing the name that appears in the key in the result

  import { Audit } from "entity-diff";

  const before = {
    name: "Mason",
    age: 20
  };

  const after = {
    name: "Mason Floyd",
    age: 25
  };

  const options = [{ key: "name", title: "Person name" }]; 

  const audit = new Audit({ options });

  const result = audit.diff(before, after);

  // result:
  // [
  //   {
  //     key: "Person name", // title defined in options
  //     from: "Mason",
  //     to: "Mason Floyd"
  //   }
  // ]

formatting the values displayed in "from" and "to"

It can be done through a function defined in options

  import { format } from 'date-fns'
  import { Audit } from "entity-diff";

  const before = {
    name: "Mason",
    age: 20
  };

  const after = {
    name: "Mason Floyd",
    age: 20,
    updatedAt: "2020-09-08T18:04:56.627Z"
  };

  const options = [
    {
      key: "updatedAt",
      customFormatter: date => format(new Date(date), "MM/dd/yyyy")
    }
  ]; 

  const audit = new Audit({ options });

  const result = audit.diff(before, after);

  // result:
  // [
  //   {
  //     key: "updatedAt",
  //     from: null,
  //     to: "09/08/2020"
  //   }
  // ]

Working with array diffs

when working with arrays, diffs are generated only when there is some value in "arrayOptions".

The "key" property represents the key used to find the entities within the other array. it is optional, but by default entity-diff looks for the "id" key.

  import { Audit } from "entity-diff";

  const before = {
    name: "Mason",
    age: 20,
    roles: [
      {
        id: 1,
        name: "ADM"
      },
      {
        id: 2,
        name: "USER"
      }
    ]
  };

  const after = {
    name: "Mason",
    age: 20,
    roles: [
      {
        id: 1,
        name: "SUPER_USER"
      },
      {
        id: 3,
        name: "TEC"
      }
    ]
  };

  const options = [
    {
      key: "roles",
      arrayOptions: {
        name: "name"
      }
    }
  ]; 

  const audit = new Audit({ options });

  const result = audit.diff(before, after);

  // resut:
  // [
  //     {
  //         "key": "roles",
  //         "type": "ARRAY",
  //         "details": [
  //             {
  //                 "key": "ADM",
  //                 "type": "MODIFIED",
  //                 "details": [
  //                     {
  //                         "key": "name",
  //                         "from": "ADM",
  //                         "to": "SUPER_USER"
  //                     }
  //                 ]
  //             },
  //             {
  //                 "key": "TEC",
  //                 "type": "NEW",
  //                 "details": [
  //                     {
  //                         "key": "id",
  //                         "from": null,
  //                         "to": 3
  //                     },
  //                     {
  //                         "key": "name",
  //                         "from": null,
  //                         "to": "TEC"
  //                     }
  //                 ]
  //             },
  //             {
  //                 "key": "USER",
  //                 "type": "REMOVED",
  //                 "details": [
  //                     {
  //                         "key": "id",
  //                         "from": 2,
  //                         "to": null
  //                     },
  //                     {
  //                         "key": "name",
  //                         "from": "USER",
  //                         "to": null
  //                     }
  //                 ]
  //             }
  //         ]
  //     }
  // ]