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

differences-js

v0.1.1

Published

Checks if there are any differences between two objects and provides the difference.

Downloads

2,321

Readme

Differences.js

Differences.js takes two values of any kind and returns true or false if they are different. It also returns the differences if both values are objects or both values are arrays. Differences.js is used to test if inmutable values change over time. It assumes the first value is the current value and the second one is the old value when calculating differences.

Usage

Basic

To install Differences.js you can use npm.

npm install differences-js

To use Differences.js require it in your project.

var diff = require('differences-js');
var value1 = 'foo';
var value2 = 'bar';


if (diff.values(value1, value2)) {
  // the values are different
} else {
  // the values are the same
}

The API only needs one method. diff.values(currentValue, oldValue)

You can find the changes between two objects.

var changes = diff.values(obj1, obj2);

if (changes) {
  changes.forEach(function(change) {
    var currentValue = obj1[change.name];
    console.log(change.type, change.name, currentValue, change.oldValue);
  });
}

Each change in a list of object changes will have a type of "add", "update", or "delete".

You can find the changes between two arrays.

var splices = diff.values(array1, array2);

if (splices) {
  splices.forEach(function(splice) {
    console.log(splice.index, splice.removed, splice.addedCount);
  });
}

Other Methods

diff.values should be enough for most situations. It will even tell if you if two dates are the same date or not. But other methods may be useful. All of these methods are used by diff.arrays after determining the type of the two values.

diff.basic diffs two basic values. If you pass in two arrays or objects it will return true if they are different instances rather than inspecting them to see if they are the same.

diff.objects will return the differences between two objects and may be useful to run on an array if there may have been changes to its properties, since diff.values will default to using diff.arrays under the hood and won't check for property changes. This will throw an error if either value is not an object/array. Will return an empty array if the objects are equal.

diff.arrays checks the difference between two arrays, returning an array of splice objects or false if they are the same. This will throw an error of eitehr value is not an array. Using diff.values should be sufficent for most use cases. Will return an empty array if the arrays are equal.

Contributions and Issues

Please open a ticket for any bugs or feature requests.

Contributions are welcome. Please fork and send a pull-request.