1.0.0 • Published 10 years ago
campsi-array-diff v1.0.0
Campsi's Array Diff
Diff two arrays and find the closest changes to apply.
Usage
Array diff takes two parameters : prev and next, which have to be of array type
The function returns a list of changes. A change is a literal object with two properties, oldIndex
and newIndex
:
- If
oldIndex
equals -1, it means that the element wasn't in theprev
array, and thus has been added tonext
. - If
newIndex
equals -1, it means that the element isn't in thenext
array, but was in theprev
- If
newIndex
equalsoldIndex
, no change
Example
var prev = ['foo', 'bar', {complex: true, property: 'foobar'}, 42];
var next = ['bar', 'foo', {property: 'foobar', complex: true}, 'ok'];
var diff = require('./index.js');
console.dir(diff(prev, next));
Will output :
[
{oldIndex: 0, newIndex: 1},
{oldIndex: 1, newIndex: 0},
{oldIndex: 2, newIndex: 2},
{oldIndex: 3, newIndex: -1},
{oldIndex: -1, newIndex: 3}
]
Info
This array diff relies on equals, it means it does not check the strict equality when it comes to object or array values inside the arrays.
1.0.0
10 years ago