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

smart-differences

v1.5.1

Published

This library provides multiple ways to compare javascript objects.

Downloads

6

Readme

smart-differences

This library provides multiple ways to compare javascript objects.

Installation

For NPM:

npm install --save smart-differences

For yarn

yarn add smart-differences

Usage

Here is an example of how to use the library:

const johnProfile = { age: 19, name: "john", emails: { primary: "[email protected]" } };
const fredProfile = { age: 32, name: "fred",
    emails: { primary: "[email protected]", work: "[email protected]" }
};
const diffs = getDifferences(johnProfile, fredProfile);
console.log(diffs);

This would print the following:

{ 
  'age': { left: 19, right: 32 },
  'name': { left: 'john', right: 'fred' },
  'emails.primary': { left: '[email protected]', right: '[email protected]' },
  'emails.work': { left: undefined, right: '[email protected]' } 
}

Customizing the comparaison

There is 3 main ways to customize the comparaison using the options:

pathFilter

Used to include or exclude some path from the comparaison. Using the same objects as above (fred and john):

const whitelistProperties = (properties: string[]): PathFilter => {
    return (paths: string[]) => {
        return paths.filter(x => properties.includes(x)).length > 0;
    };
};

const blacklistProperties = (properties: string[]): PathFilter => {
    return (paths: string[]) => {
        return paths.filter(x => properties.includes(x)).length === 0;
    };
};

const diffsWhitelist = getDifferences(johnProfile, fredProfile, {
    pathFilter: whitelistProperties(["age", "name"])
});
const diffsBlacklist = getDifferences(johnProfile, fredProfile, {
    pathFilter: blacklistProperties(["age", "name"])
});
console.log(diffsWhitelist);
console.log(diffsBlacklist);

would return

//whitelist
{ 
   age: { left: 19, right: 32 },
   name: { left: 'john', right: 'fred' } 
}
/blacklist
{
  'emails.primary': { left: '[email protected]', right: '[email protected]' },
  'emails.work': { left: undefined, right: '[email protected]' } 
}

Beware when using pathFiltering and not using the deepCompare option below, you will receive the head property of a nested object, not not the properties below. For example (based on the deepCompare example below), your filter would receive:

[name, favoriteSong]

When using deepCompare, it would receive:

[name, favoriteSong.name, favoriteSong.artist.name, favoriteSong.year]

deepCompare

Deep compare is an option used when one side of the comparaison is null or undefined, and the other side is an object.

Here is two output of the same compare with and without deepCompare.

const object1 = {
  name: "John",
  favoriteSong: {
      name: "Winter Wonderland",
      artist: {
          name: "Felix Bernard"
      },
      year: 1934
  }
};
const object2 = {
  name: "Fred"
};
//with deepCompare, the differences would be
{ 
  name: { left: 'John', right: 'Fred' },
  'favoriteSong.name': { left: 'Winter Wonderland', right: undefined },
  'favoriteSong.artist.name': { left: 'Felix Bernard', right: undefined },
  'favoriteSong.year': { left: 1934, right: undefined } 
}

//without deepCompare
{ 
  name: { left: 'John', right: 'Fred' },
  favoriteSong: { 
    left: { 
      name: 'Winter Wonderland', 
      artist: {
          name: "Felix Bernard"
      }, 
      year: 1934 
    },
    right: undefined 
  } 
}

Transformations

You can also apply transformations to the values before compare, for example to ignore case or extra spaces.

The first way to do it is globally using the compareTransformations option. The library export an object named StringTransformations with a couple of predefined function, but you can build your own.

const diffs = getDifferences(
    johnProfile,
    { ...johnProfile, name: "JOHn", emails: { primary: "[email protected]" } },
    {
        compareTransformations: [StringTransformations.uppercase]
    }
);

would return no differences.

You can also apply the transformations for each property individually using the pathCompareTransformationsProvider.

const diffs = getDifferences(
    johnProfile,
    { ...johnProfile, name: "  john", emails: { primary: "  [email protected]" } },
    {
        pathCompareTransformationsProvider: (pathElements: string[]) => {
            const property = pathElements.join(".");
            //trim, but only for emails.primary
            if (property === "emails.primary") {
                return [StringTransformations.trim];
            }
            return null;
        }
    }
);

would only trim the spaces for the primary email, and would still detect a difference for the extra spaces in the name property. It would print

{ 
  name: { left: 'john', right: '  john' } 
}

When you return null or undefined in the pathCompareTransformationsProvider function, no transformations are applied. If you defined some transformations in the compareTransformations option, they are only applied if the pathCompareTransformationsProvider function returns null or undefined.

Known issues

Right now, this library doesn't work when comparing Array properties. This is gonna be available in the next release.