0.0.12 • Published 4 months ago

@golden-tiger/difference v0.0.12

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

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' } ]
0.0.12

4 months ago

0.0.8

1 year ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago