@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 differenceafter
: the after value for differenceoption.routes
: initial routesoption.arrayInOrder
: weather array needs keep order when contrastoption.arraySort sort
: function when array keeps orderoption.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
- number
difference(1, 1);
// []
difference(1, 2);
// [ { routes: [], before: 1, after: 2 } ]
- string
difference('foo', 'foo');
// []
difference('foo', 'bar');
// [ { routes: [], before: 'foo', after: 'bar' } ]
- boolean
difference(true, true);
// []
difference(false, false);
// []
difference(true, false);
// [ { routes: [], before: true, after: false } ]
- 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 withoption.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' }
// ]
- 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 }
// ]
- 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: []
// }
// ]
- RegExp
difference(/foo/, /foo/);
// []
difference(/foo/, /bar/);
// [ { before: /foo/, after: /bar/, routes: [] } ]
option
option.covers
handler
function's falsy return will be ignore in difference result. Usinghandler
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' } ]