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.

380 lines
7.2 KiB

<img width="722" alt="superdiff-logo" src="https://user-images.githubusercontent.com/43271780/209532864-24d7449e-1185-4810-9423-be5df1fe877f.png">
2 years ago
# SUPERDIFF
2 years ago
This library compares two arrays or objects and return a complete diff of their differences.
2 years ago
## WHY YOU SHOULD USE THIS LIB
2 years ago
2 years ago
All other existing solutions return a weird diff format which often require an additionnal parsing. They are also limited to object comparison. Some even have CPU spikes issues. 👎
2 years ago
2 years ago
**Superdiff** gives you a complete diff for both array <u>and</u> objects with a very readable format. Last but not least, it's battled tested. Import. Enjoy. 👍
## DIFF FORMAT COMPARISON
Let's compare the diff format of **Superdiff** and **Deep-diff**, the most popular diff lib on npm:
input:
2 years ago
2 years ago
```diff
2 years ago
const objectA = {
id: 54,
user: {
name: "joe",
- member: true,
- hobbies: ["golf", "football"],
age: 66,
},
}
const objectB = {
id: 54,
user: {
name: "joe",
+ member: false,
+ hobbies: ["golf", "chess"],
age: 66,
},
2 years ago
}
```
2 years ago
**Deep-Diff**
**Deep-Diff** output:
```js
[
DiffEdit {
kind: 'E',
path: [ 'user', 'member' ],
lhs: true,
rhs: false
},
DiffEdit {
kind: 'E',
path: [ 'user', 'hobbies', 1 ],
lhs: 'football',
rhs: 'chess'
}
]
````
**SuperDiff** output:
2 years ago
2 years ago
```diff
2 years ago
{
type: "object",
2 years ago
+ status: "updated",
2 years ago
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,
},
2 years ago
+ status: "updated",
2 years ago
subPropertiesDiff: [
{
name: "name",
previousValue: "joe",
currentValue: "joe",
status: "equal",
},
2 years ago
+ {
+ name: "member",
+ previousValue: true,
+ currentValue: false,
+ status: "updated",
+ },
+ {
+ name: "hobbies",
+ previousValue: ["golf", "football"],
+ currentValue: ["golf", "chess"],
+ status: "updated",
+ },
2 years ago
{
name: "age",
previousValue: 66,
currentValue: 66,
status: "equal",
},
],
},
],
}
```
2 years ago
## FEATURES
**Superdiff** exports 4 functions:
### getObjectDiff()
compare two objects and return a diff for each value and their potential subvalues:
- property name
- status: added, deleted, equal, updated
- previous value, current value
- supports deeply nested objects with any kind of values
format:
```ts
2 years ago
type ObjectDiff = {
type: "object";
status: "added" | "deleted" | "equal" | "moved" | "updated";
diff: {
property: string;
previousValue: any;
currentValue: any;
status: "added" | "deleted" | "equal" | "moved" | "updated";
subPropertiesDiff?: {
name: string;
previousValue: any;
currentValue: any;
status: "added" | "deleted" | "equal" | "moved" | "updated";
// subDiff is a recursive diff in case of nested subproperties
subDiff?: Subproperties[];
}[];
}[];
}
```
### getListDiff()
compare two arrays and return a diff for each value:
- index change: previous index, current index, index difference
- status: added, deleted, equal, moved, updated
- previous value, current value
- supports array of primitive values and objects
- doesn't support yet duplicated values comparison (but will soon)
format:
```ts
2 years ago
type ListDiff = {
type: "list";
status: "added" | "deleted" | "equal" | "moved" | "updated";
diff: {
value: any;
prevIndex: number | null;
newIndex: number | null;
indexDiff: number | null;
status: "added" | "deleted" | "equal" | "moved" | "updated";
}[];
};
```
2 years ago
2 years ago
### isEqual()
2 years ago
2 years ago
check if two values are equal.
### isObject()
check if a value is an object.
## EXAMPLES
### getListDiff()
input
2 years ago
2 years ago
```diff
2 years ago
getListDiff(
2 years ago
- ["mbappe", "mendes", "verratti", "ruiz"],
+ ["mbappe", "messi", "ruiz"]
2 years ago
);
```
2 years ago
output
2 years ago
2 years ago
```diff
2 years ago
{
type: "list",
2 years ago
+ status: "updated",
2 years ago
diff: [
{
value: "mbappe",
prevIndex: 0,
newIndex: 0,
indexDiff: 0,
status: "equal",
},
2 years ago
- {
- 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",
2 years ago
},
],
}
```
2 years ago
### getObjectDiff()
input
```diff
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
```diff
{
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",
},
],
},
],
}
```
### isEqual()
```js
isEqual(
[
{ name: "joe", age: 99 },
{ name: "nina", age: 23 },
],
[
{ name: "joe", age: 98 },
{ name: "nina", age: 23 },
]
);
```
output
```js
false;
```
### isObject()
input
```js
isObject(["hello", "world"]);
```
output
```js
false;
```
More examples are availble in the tests of the source code.
## CREDITS
2 years ago
DoneDeal0
## CONTRIBUTING
Pull requests are welcome!