Superdiff provides a complete and readable diff for both arrays and objects. Plus, it supports stream and file inputs for handling large datasets efficiently, is battle-tested, has zero dependencies, and is super fast.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
2.9 KiB

2 years ago
# DATA DIFF
2 years ago
This library compares two arrays or objects and return a complete diff of their differences.
2 years ago
## Object exemple:
2 years ago
`getObjectDiff()` checks base properties but also provides a complete diff of nested properties.
Input
```js
getObjectDiff(
{
id: 54,
user: {
name: "joe",
member: true,
hobbies: ["golf", "football"],
age: 66,
},
},
{
id: 54,
user: {
name: "joe",
member: false,
hobbies: ["golf", "chess"],
age: 66,
},
}
);
```
Output
```js
{
type: "object",
status: "updated",
diff: [
{
property: "id",
previousValue: 54,
currentValue: 54,
status: "equal",
},
{
property: "user",
previousValue: {
name: "joe",
member: true,
hobbies: ["golf", "football"],
age: 66,
},
currentValue: {
name: "joe",
member: false,
hobbies: ["golf", "chess"],
age: 66,
},
status: "updated",
subPropertiesDiff: [
{
name: "name",
previousValue: "joe",
currentValue: "joe",
status: "equal",
},
{
name: "member",
previousValue: true,
currentValue: false,
status: "updated",
},
{
name: "hobbies",
previousValue: ["golf", "football"],
currentValue: ["golf", "chess"],
status: "updated",
},
{
name: "age",
previousValue: 66,
currentValue: 66,
status: "equal",
},
],
},
],
}
```
2 years ago
## List exemple:
2 years ago
`getListDiff()` works with arrays of string, number and objects.
It doesn't work yet with duplicated values.
Input
```js
getListDiff(
["mbappe", "mendes", "verratti", "ruiz"],
["mbappe", "messi", "ruiz"]
);
```
Output
```js
{
type: "list",
status: "updated",
diff: [
{
value: "mbappe",
prevIndex: 0,
newIndex: 0,
indexDiff: 0,
status: "equal",
},
{
value: "mendes",
prevIndex: 1,
newIndex: null,
indexDiff: null,
status: "deleted",
},
{
value: "verratti",
prevIndex: 2,
newIndex: null,
indexDiff: null,
status: "deleted",
},
{
value: "messi",
prevIndex: null,
newIndex: 1,
indexDiff: null,
status: "added",
},
{
value: "ruiz",
prevIndex: 3,
newIndex: 2,
indexDiff: -1,
status: "moved",
},
],
}
```
See tests for more visual examples.