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

@golden-tiger/difference

v0.0.12

Published

@golden-tiger/difference

Downloads

2

Readme

difference

@golden-tiger/difference

What is difference

difference function will find out what is different between two javascript object. (P.s. number, string and boolean are also javascript object)

How to use difference(before, after, option)

Use difference function to get differences between the first parameter before and the second after. And difference function's third parameter control difference's process.

  • before: the before value for difference

  • after: the after value for difference

  • option.routes: initial routes

  • option.arrayInOrder: weather array needs keep order when contrast

  • option.arraySort sort: function when array keeps order

  • option.covers: Covers' array to custom specific contrast handler for specific routes. Handle function's return should be an array consisted of difference item({ routes, before, after }) or a falsy value. Handle function's return will be pushed into the diff results array when it is not a falsy value.

  • return: Array of difference item. Difference item consists of routes (routes in object), before (before value) and after (after value).

Examples

type

  1. number
difference(1, 1);
// []
difference(1, 2);
// [ { routes: [], before: 1, after: 2 } ]
  1. string
difference('foo', 'foo');
// []
difference('foo', 'bar');
// [ { routes: [], before: 'foo', after: 'bar' } ]
  1. boolean
difference(true, true);
// []
difference(false, false);
// []
difference(true, false);
// [ { routes: [], before: true, after: false } ]
  1. array

option.arrayInOrder(boolean) decides whether an array is in order. Sort function is (a, b) => a < b ? -1 : 1 by default, which can be set with option.arraySort(Function). difference function will take array's sorted result as input.

difference([1, 'foo'], [1, 'foo']);
// []
difference([1, 'foo'], [2, 'bar']);
// [
//   { routes: [ '1' ], before: 'foo', after: 'bar' },
//   { routes: [ '0' ], before: 1, after: 2 }
// ]
difference([1, 2], [2, 1]);
// []
difference([1, 2], [2, 1], {
  arrayInOrder: true,
});
// [
//   { routes: [ '1' ], before: 2, after: 1 },
//   { routes: [ '0' ], before: 1, after: 2 }
// ]
difference(['foo', 'bar'], ['bar', 'foo']);
// []
difference(['foo', 'bar'], ['bar', 'foo'], {
  arrayInOrder: true,
});
// [
//   { routes: [ '1' ], before: 'bar', after: 'foo' },
//   { routes: [ '0' ], before: 'foo', after: 'bar' }
// ]
  1. object
difference({ a: 'foo' }, { a: 'foo' });
// []
difference({ a: 'foo' }, { a: 'bar' });
// [ { routes: [ 'a' ], before: 'foo', after: 'bar' } ]
difference({ a: 'foo' }, { b: 'foo' });
// [
//   { routes: [ 'b' ], before: undefined, after: 'foo' },
//   { routes: [ 'a' ], before: 'foo', after: undefined }
// ]
  1. Date
difference(new Date(), new Date());
// []
difference(new Date(), new Date('2019-08-23T00:00:00.000Z'));
// [
//   {
//     before: 2022-09-15T13:31:17.484Z,
//     after: 2019-08-23T00:00:00.000Z, // The day I met my girl
//     routes: []
//   }
// ]
  1. RegExp
difference(/foo/, /foo/);
// []
difference(/foo/, /bar/);
// [ { before: /foo/, after: /bar/, routes: [] } ]

option

  1. option.covers

handler function's falsy return will be ignore in difference result. Using handler function you can custom the difference result, such as ignoring some specific route value or combining several route values to one difference item.

difference({ a: 'foo' }, { a: 'bar' }, {
  covers: [
    {
      routes: ['a'],
      handler: (before, after, routes) => {
        console.log(before);
        // foo
        console.log(after);
        // bar
        console.log(routes);
        // [ 'a' ]
        return false;
      },
    }
  ],
});
// []
difference({ a: 'foo' }, { a: 'bar' }, {
  covers: [
    {
      routes: ['a'],
      handler: (before, after, routes) => {
        return [
          { routes, before, after },
        ];
      },
    }
  ],
});
// [ { routes: [ 'a' ], before: 'foo', after: 'bar' } ]