Skip to content

Commit c6a186e

Browse files
authored
feat: simplify output, clean readme (#25)
BREAKING CHANGE: subPropertiesDiff has been removed from the getObjectDiff output. There is now a single recursive diff key for more simplicity. The types have also been improved.
1 parent eb0e164 commit c6a186e

15 files changed

+247
-363
lines changed

.eslintcache

-1
This file was deleted.

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
2-
dist
2+
dist
3+
.eslintcache

README.md

+23-118
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
<img width="722" alt="superdiff-logo" src="https://user-images.githubusercontent.com/43271780/209532864-24d7449e-1185-4810-9423-be5df1fe877f.png">
22

3-
# SUPERDIFF
4-
5-
This library compares two arrays or objects and returns a full diff of their differences.
63

74
[![CI](https://github.com/DoneDeal0/superdiff/actions/workflows/ci.yml/badge.svg)](https://github.com/DoneDeal0/superdiff/actions/workflows/ci.yml)
85
[![CD](https://github.com/DoneDeal0/superdiff/actions/workflows/cd.yml/badge.svg)](https://github.com/DoneDeal0/superdiff/actions/workflows/cd.yml)
96
![NPM Downloads](https://img.shields.io/npm/dy/%40donedeal0%2Fsuperdiff?logo=npm)
107
![GitHub Tag](https://img.shields.io/github/v/tag/DoneDeal0/superdiff?label=latest%20release)
118

9+
<hr/>
10+
11+
# WHAT IS IT?
12+
13+
This library compares two arrays or objects and returns a full diff of their differences.
14+
15+
<hr/>
16+
1217
## WHY YOU SHOULD USE THIS LIBRARY
1318

14-
All other existing solutions return a strange diff format that often requires additional parsing. They are also limited to object comparison. 👎
19+
All other existing solutions return a strange diff format that often requires additional parsing. They are also limited to object comparison.
1520

1621
**Superdiff** gives you a complete diff for both array <u>and</u> objects in a very readable format. Last but not least, it's battle-tested and super fast. Import. Enjoy. 👍
1722

23+
<hr/>
24+
1825
## DONORS
1926

2027
I am grateful to the generous donors of **Superdiff**!
@@ -27,111 +34,7 @@ I am grateful to the generous donors of **Superdiff**!
2734

2835
</div>
2936

30-
## DIFF FORMAT COMPARISON
31-
32-
Let's compare the diff format of **Superdiff** and **Deep-diff**, the most popular diff lib on npm:
33-
34-
input:
35-
36-
```diff
37-
const objectA = {
38-
id: 54,
39-
user: {
40-
name: "joe",
41-
- member: true,
42-
- hobbies: ["golf", "football"],
43-
age: 66,
44-
},
45-
}
46-
47-
const objectB = {
48-
id: 54,
49-
user: {
50-
name: "joe",
51-
+ member: false,
52-
+ hobbies: ["golf", "chess"],
53-
age: 66,
54-
},
55-
}
56-
```
57-
58-
**Deep-Diff** output:
59-
60-
```js
61-
[
62-
{
63-
kind: "E",
64-
path: ["user", "member"],
65-
lhs: true,
66-
rhs: false,
67-
},
68-
{
69-
kind: "E",
70-
path: ["user", "hobbies", 1],
71-
lhs: "football",
72-
rhs: "chess",
73-
},
74-
];
75-
```
76-
77-
**SuperDiff** output:
78-
79-
```diff
80-
{
81-
type: "object",
82-
+ status: "updated",
83-
diff: [
84-
{
85-
property: "id",
86-
previousValue: 54,
87-
currentValue: 54,
88-
status: "equal",
89-
},
90-
{
91-
property: "user",
92-
previousValue: {
93-
name: "joe",
94-
member: true,
95-
hobbies: ["golf", "football"],
96-
age: 66,
97-
},
98-
currentValue: {
99-
name: "joe",
100-
member: false,
101-
hobbies: ["golf", "chess"],
102-
age: 66,
103-
},
104-
+ status: "updated",
105-
subPropertiesDiff: [
106-
{
107-
property: "name",
108-
previousValue: "joe",
109-
currentValue: "joe",
110-
status: "equal",
111-
},
112-
+ {
113-
+ property: "member",
114-
+ previousValue: true,
115-
+ currentValue: false,
116-
+ status: "updated",
117-
+ },
118-
+ {
119-
+ property: "hobbies",
120-
+ previousValue: ["golf", "football"],
121-
+ currentValue: ["golf", "chess"],
122-
+ status: "updated",
123-
+ },
124-
{
125-
property: "age",
126-
previousValue: 66,
127-
currentValue: 66,
128-
status: "equal",
129-
},
130-
],
131-
},
132-
],
133-
}
134-
```
37+
<hr/>
13538

13639
## FEATURES
13740

@@ -158,17 +61,17 @@ type ObjectDiff = {
15861
status: "added" | "deleted" | "equal" | "updated";
15962
diff: {
16063
property: string;
161-
previousValue: any;
162-
currentValue: any;
64+
previousValue: unknown;
65+
currentValue: unknow;
16366
status: "added" | "deleted" | "equal" | "updated";
16467
// only appears if some subproperties have been added/deleted/updated
165-
subPropertiesDiff?: {
68+
diff?: {
16669
property: string;
167-
previousValue: any;
168-
currentValue: any;
70+
previousValue: unknown;
71+
currentValue: unknown;
16972
status: "added" | "deleted" | "equal" | "updated";
170-
// subDiff is a recursive diff in case of nested subproperties
171-
subDiff?: SubProperties[];
73+
// recursive diff in case of subproperties
74+
diff?: SubDiff[];
17275
}[];
17376
}[];
17477
};
@@ -217,7 +120,7 @@ type ListDiff = {
217120
type: "list";
218121
status: "added" | "deleted" | "equal" | "moved" | "updated";
219122
diff: {
220-
value: any;
123+
value: unknown;
221124
prevIndex: number | null;
222125
newIndex: number | null;
223126
indexDiff: number | null;
@@ -270,6 +173,8 @@ import { isObject } from "@donedeal0/superdiff";
270173

271174
Tests whether a value is an object.
272175

176+
<hr/>
177+
273178
## EXAMPLES
274179

275180
### getListDiff()
@@ -384,7 +289,7 @@ output
384289
age: 66,
385290
},
386291
+ status: "updated",
387-
subPropertiesDiff: [
292+
diff: [
388293
{
389294
property: "name",
390295
previousValue: "joe",

eslint.config.mjs

-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,4 @@ export default [
77
{ settings: { react: { version: "detect" } } },
88
pluginJs.configs.recommended,
99
...tseslint.configs.recommended,
10-
{
11-
rules: {
12-
"@typescript-eslint/no-explicit-any": "off",
13-
"@typescript-eslint/ban-ts-comment": "off"
14-
},
15-
},
1610
];

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@donedeal0/superdiff",
3-
"version": "1.1.3",
3+
"version": "2.0.0",
44
"description": "SuperDiff checks the changes between two objects or arrays. It returns a complete diff with relevant information for each property or piece of data",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { getObjectDiff } from "./object-diff";
22
export { getListDiff } from "./list-diff";
33
export { isEqual, isObject } from "./utils";
4-
export * from "./model";
4+
export * from "./models/list";
5+
export * from "./models/object";

src/list-diff.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { LIST_STATUS, ListDiff, ListDiffStatus, ListOptions } from "./model";
1+
import {
2+
DEFAULT_LIST_DIFF_OPTIONS,
3+
LIST_STATUS,
4+
ListDiff,
5+
ListDiffOptions,
6+
} from "./models/list";
27
import { isEqual, isObject } from "./utils";
38

49
function getLeanDiff(
510
diff: ListDiff["diff"],
6-
showOnly = [] as ListOptions["showOnly"],
11+
showOnly = [] as ListDiffOptions["showOnly"],
712
): ListDiff["diff"] {
813
return diff.filter((value) => showOnly?.includes(value.status));
914
}
1015

1116
function formatSingleListDiff<T>(
1217
listData: T[],
13-
status: ListDiffStatus,
14-
options: ListOptions = { showOnly: [] },
18+
status: LIST_STATUS,
19+
options: ListDiffOptions = { showOnly: [] },
1520
): ListDiff {
1621
const diff = listData.map((data, i) => ({
1722
value: data,
@@ -34,16 +39,16 @@ function formatSingleListDiff<T>(
3439
};
3540
}
3641

37-
function getListStatus(listDiff: ListDiff["diff"]): ListDiffStatus {
42+
function getListStatus(listDiff: ListDiff["diff"]): LIST_STATUS {
3843
return listDiff.some((value) => value.status !== LIST_STATUS.EQUAL)
3944
? LIST_STATUS.UPDATED
4045
: LIST_STATUS.EQUAL;
4146
}
4247

4348
function isReferencedObject(
44-
value: any,
45-
referenceProperty: ListOptions["referenceProperty"],
46-
): value is Record<string, any> {
49+
value: unknown,
50+
referenceProperty: ListDiffOptions["referenceProperty"],
51+
): value is Record<string, unknown> {
4752
if (isObject(value) && !!referenceProperty) {
4853
return Object.hasOwn(value, referenceProperty);
4954
}
@@ -62,12 +67,7 @@ function isReferencedObject(
6267
export const getListDiff = <T>(
6368
prevList: T[] | undefined | null,
6469
nextList: T[] | undefined | null,
65-
options: ListOptions = {
66-
showOnly: [],
67-
referenceProperty: undefined,
68-
considerMoveAsUpdate: false,
69-
ignoreArrayOrder: false,
70-
},
70+
options: ListDiffOptions = DEFAULT_LIST_DIFF_OPTIONS,
7171
): ListDiff => {
7272
if (!prevList && !nextList) {
7373
return {

src/model.ts

-93
This file was deleted.

0 commit comments

Comments
 (0)