Skip to content

Commit 24faa25

Browse files
committed
feat: add granular output
1 parent 4362c98 commit 24faa25

File tree

11 files changed

+899
-134
lines changed

11 files changed

+899
-134
lines changed

README.md

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,25 @@ const objectB = {
9797
+ status: "updated",
9898
subPropertiesDiff: [
9999
{
100-
name: "name",
100+
property: "name",
101101
previousValue: "joe",
102102
currentValue: "joe",
103103
status: "equal",
104104
},
105105
+ {
106-
+ name: "member",
106+
+ property: "member",
107107
+ previousValue: true,
108108
+ currentValue: false,
109109
+ status: "updated",
110110
+ },
111111
+ {
112-
+ name: "hobbies",
112+
+ property: "hobbies",
113113
+ previousValue: ["golf", "football"],
114114
+ currentValue: ["golf", "chess"],
115115
+ status: "updated",
116116
+ },
117117
{
118-
name: "age",
118+
property: "age",
119119
previousValue: 66,
120120
currentValue: 66,
121121
status: "equal",
@@ -156,17 +156,37 @@ type ObjectDiff = {
156156
status: "added" | "deleted" | "equal" | "moved" | "updated";
157157
// only appears if some subproperties have been added/deleted/updated
158158
subPropertiesDiff?: {
159-
name: string;
159+
property: string;
160160
previousValue: any;
161161
currentValue: any;
162162
status: "added" | "deleted" | "equal" | "moved" | "updated";
163163
// subDiff is a recursive diff in case of nested subproperties
164-
subDiff?: Subproperties[];
164+
subDiff?: SubProperties[];
165165
}[];
166166
}[];
167167
};
168168
```
169169

170+
**Options**
171+
172+
```ts
173+
{
174+
ignoreArrayOrder?: boolean // false by default,
175+
showOnly?: {
176+
statuses: ("added" | "deleted" | "updated" | "equal")[], // [] by default
177+
granularity?: "basic" | "deep" // basic by default
178+
}
179+
}
180+
```
181+
182+
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be considered as `equal`, because the two arrays have the same value, just not in the same order.
183+
- `showOnly`: gives you the option to only return the values whose status interest you. It has two parameters:
184+
185+
- `statuses`: status you want to see in the output (ex: `["added", "equal"]`)
186+
- `granularity`:
187+
- `basic` only returns the main properties whose status match your request, without taking into account their eventual subproperties.
188+
- `deep` return main properties whose status match your request but also their relevant subproperties.
189+
170190
### getListDiff()
171191

172192
```js
@@ -197,6 +217,16 @@ type ListDiff = {
197217
};
198218
```
199219

220+
**Options**
221+
222+
```ts
223+
{
224+
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
225+
}
226+
```
227+
228+
- `showOnly` gives you the option to only return the values whose status interest you (ex: `["added", "equal"]`).
229+
200230
### isEqual()
201231

202232
```js
@@ -205,6 +235,16 @@ import { isEqual } from "@donedeal0/superdiff";
205235

206236
Checks if two values are equal.
207237

238+
**Options**
239+
240+
```ts
241+
{
242+
ignoreArrayOrder?: boolean // false by default,
243+
}
244+
```
245+
246+
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be considered as `equal`, because the two arrays have the same value, just not in the same order.
247+
208248
### isObject()
209249

210250
```js
@@ -329,25 +369,25 @@ output
329369
+ status: "updated",
330370
subPropertiesDiff: [
331371
{
332-
name: "name",
372+
property: "name",
333373
previousValue: "joe",
334374
currentValue: "joe",
335375
status: "equal",
336376
},
337377
+ {
338-
+ name: "member",
378+
+ property: "member",
339379
+ previousValue: true,
340380
+ currentValue: false,
341381
+ status: "updated",
342382
+ },
343383
+ {
344-
+ name: "hobbies",
384+
+ property: "hobbies",
345385
+ previousValue: ["golf", "football"],
346386
+ currentValue: ["golf", "chess"],
347387
+ status: "updated",
348388
+ },
349389
{
350-
name: "age",
390+
property: "age",
351391
previousValue: 66,
352392
currentValue: 66,
353393
status: "equal",
@@ -397,25 +437,13 @@ More examples are availble in the tests of the source code.
397437

398438
<hr/>
399439

400-
### OPTIONS
401-
402-
`getObjectDiff()` and `isEqual()` accept a facultative `options` parameter:
403-
404-
```ts
405-
{
406-
discardArrayOrder?: boolean // false by default
407-
}
408-
```
409-
410-
If `discardArrayOrder` is set to `true`, `["hello", "world"]` and `["world", "hello"]` will be considered as `equal`, because the two arrays have the same value, just not in the same order.
411-
412440
## CREDITS
413441

414442
DoneDeal0
415443

416444
## SUPPORT
417445

418-
If you use Superdiff, please show your support by buying me coffee:
446+
If you or your company use Superdiff, please show your support by buying me coffee:
419447
https://www.buymeacoffee.com/donedeal0
420448

421449
<br/>

dist/index.d.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,48 @@ type DiffStatus = "added" | "equal" | "moved" | "deleted" | "updated";
22
type ObjectData = Record<string, any> | undefined | null;
33
type ListData = any;
44
type Options = {
5-
discardArrayOrder?: boolean;
5+
ignoreArrayOrder?: boolean;
66
};
77
type ListDiff = {
8-
type: "list";
8+
type: "list";
9+
status: DiffStatus;
10+
diff: {
11+
value: ListData;
12+
prevIndex: number | null;
13+
newIndex: number | null;
14+
indexDiff: number | null;
915
status: DiffStatus;
10-
diff: {
11-
value: ListData;
12-
prevIndex: number | null;
13-
newIndex: number | null;
14-
indexDiff: number | null;
15-
status: DiffStatus;
16-
}[];
16+
}[];
1717
};
18-
type Subproperties = {
19-
name: string;
20-
previousValue: any;
21-
currentValue: any;
22-
status: DiffStatus;
23-
subDiff?: Subproperties[];
18+
type SubProperties = {
19+
name: string;
20+
previousValue: any;
21+
currentValue: any;
22+
status: DiffStatus;
23+
subDiff?: SubProperties[];
2424
};
2525
type ObjectDiff = {
26-
type: "object";
26+
type: "object";
27+
status: DiffStatus;
28+
diff: {
29+
property: string;
30+
previousValue: any;
31+
currentValue: any;
2732
status: DiffStatus;
28-
diff: {
29-
property: string;
30-
previousValue: any;
31-
currentValue: any;
32-
status: DiffStatus;
33-
subPropertiesDiff?: Subproperties[];
34-
}[];
33+
subPropertiesDiff?: SubProperties[];
34+
}[];
3535
};
3636

37-
declare function getObjectDiff(prevData: ObjectData, nextData: ObjectData, options?: Options): ObjectDiff;
37+
declare function getObjectDiff(
38+
prevData: ObjectData,
39+
nextData: ObjectData,
40+
options?: Options
41+
): ObjectDiff;
3842

39-
declare const getListDiff: (prevList: ListData[] | undefined | null, nextList: ListData[] | undefined | null) => ListDiff;
43+
declare const getListDiff: (
44+
prevList: ListData[] | undefined | null,
45+
nextList: ListData[] | undefined | null
46+
) => ListDiff;
4047

4148
declare function isEqual(a: any, b: any, options?: Options): boolean;
4249
declare function isObject(value: any): value is Record<string, any>;

0 commit comments

Comments
 (0)