Skip to content

Commit 6c22757

Browse files
committed
feat: start referenceProperty for object comparison in lists
1 parent ccddf28 commit 6c22757

File tree

13 files changed

+9643
-1816
lines changed

13 files changed

+9643
-1816
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This library compares two arrays or objects and return a complete diff of their
1010

1111
All other existing solutions return a weird diff format which often require an additional parsing. They are also limited to object comparison. 👎
1212

13-
**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 and super fast. Import. Enjoy. 👍
13+
**Superdiff** gives you a complete diff for both array <u>and</u> objects with a very readable format. Last but not least, it's battle tested and super fast. Import. Enjoy. 👍
1414

1515
## DIFF FORMAT COMPARISON
1616

dist/index.d.mts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
declare const GRANULARITY: Record<string, "basic" | "deep">;
2+
type ListDiffStatus = "added" | "equal" | "moved" | "deleted" | "updated";
3+
type ObjectDiffStatus = "added" | "equal" | "deleted" | "updated";
4+
type ObjectData = Record<string, any> | undefined | null;
5+
type ListData = any;
6+
type ObjectStatusTuple = readonly [
7+
"added",
8+
"equal",
9+
"deleted",
10+
"updated"
11+
];
12+
type ListStatusTuple = readonly [
13+
"added",
14+
"equal",
15+
"deleted",
16+
"moved",
17+
"updated"
18+
];
19+
type isEqualOptions = {
20+
ignoreArrayOrder?: boolean;
21+
};
22+
type ObjectOptions = {
23+
ignoreArrayOrder?: boolean;
24+
showOnly?: {
25+
statuses: Array<ObjectStatusTuple[number]>;
26+
granularity?: (typeof GRANULARITY)[keyof typeof GRANULARITY];
27+
};
28+
};
29+
type ListOptions = {
30+
showOnly?: Array<ListStatusTuple[number]>;
31+
referenceProperty?: string;
32+
};
33+
type ListDiff = {
34+
type: "list";
35+
status: ListDiffStatus;
36+
diff: {
37+
value: ListData;
38+
prevIndex: number | null;
39+
newIndex: number | null;
40+
indexDiff: number | null;
41+
status: ListDiffStatus;
42+
}[];
43+
};
44+
type SubProperties = {
45+
property: string;
46+
previousValue: any;
47+
currentValue: any;
48+
status: ObjectDiffStatus;
49+
subPropertiesDiff?: SubProperties[];
50+
};
51+
type ObjectDiff = {
52+
type: "object";
53+
status: ObjectDiffStatus;
54+
diff: {
55+
property: string;
56+
previousValue: any;
57+
currentValue: any;
58+
status: ObjectDiffStatus;
59+
subPropertiesDiff?: SubProperties[];
60+
}[];
61+
};
62+
63+
/**
64+
* Returns the diff between two objects
65+
* @param {Record<string, any>} prevData - The original object.
66+
* @param {Record<string, any>} nextData - The new object.
67+
* @returns ObjectDiff
68+
*/
69+
declare function getObjectDiff(prevData: ObjectData, nextData: ObjectData, options?: ObjectOptions): ObjectDiff;
70+
71+
/**
72+
* Returns the diff between two arrays
73+
* @param {Array<T>} prevList - The original array.
74+
* @param {Array<T>} nextList - The new array.
75+
* @returns ListDiff
76+
*/
77+
declare const getListDiff: <T>(prevList: T[] | null | undefined, nextList: T[] | null | undefined, options?: ListOptions) => ListDiff;
78+
79+
/**
80+
* Returns true if two data are equal
81+
* @param {any} a - The original data.
82+
* @param {any} b- The data to compare.
83+
* @returns boolean
84+
*/
85+
declare function isEqual(a: any, b: any, options?: isEqualOptions): boolean;
86+
/**
87+
* Returns true if the provided value is an object
88+
* @param {any} value - The data to check.
89+
* @returns value is Record<string, any>
90+
*/
91+
declare function isObject(value: any): value is Record<string, any>;
92+
93+
export { getListDiff, getObjectDiff, isEqual, isObject };

dist/index.d.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ type ObjectOptions = {
2323
ignoreArrayOrder?: boolean;
2424
showOnly?: {
2525
statuses: Array<ObjectStatusTuple[number]>;
26-
granularity?: typeof GRANULARITY[keyof typeof GRANULARITY];
26+
granularity?: (typeof GRANULARITY)[keyof typeof GRANULARITY];
2727
};
2828
};
2929
type ListOptions = {
3030
showOnly?: Array<ListStatusTuple[number]>;
31+
referenceProperty?: string;
3132
};
3233
type ListDiff = {
3334
type: "list";
@@ -59,11 +60,34 @@ type ObjectDiff = {
5960
}[];
6061
};
6162

63+
/**
64+
* Returns the diff between two objects
65+
* @param {Record<string, any>} prevData - The original object.
66+
* @param {Record<string, any>} nextData - The new object.
67+
* @returns ObjectDiff
68+
*/
6269
declare function getObjectDiff(prevData: ObjectData, nextData: ObjectData, options?: ObjectOptions): ObjectDiff;
6370

64-
declare const getListDiff: (prevList: ListData[] | undefined | null, nextList: ListData[] | undefined | null, options?: ListOptions) => ListDiff;
71+
/**
72+
* Returns the diff between two arrays
73+
* @param {Array<T>} prevList - The original array.
74+
* @param {Array<T>} nextList - The new array.
75+
* @returns ListDiff
76+
*/
77+
declare const getListDiff: <T>(prevList: T[] | null | undefined, nextList: T[] | null | undefined, options?: ListOptions) => ListDiff;
6578

79+
/**
80+
* Returns true if two data are equal
81+
* @param {any} a - The original data.
82+
* @param {any} b- The data to compare.
83+
* @returns boolean
84+
*/
6685
declare function isEqual(a: any, b: any, options?: isEqualOptions): boolean;
86+
/**
87+
* Returns true if the provided value is an object
88+
* @param {any} value - The data to check.
89+
* @returns value is Record<string, any>
90+
*/
6791
declare function isObject(value: any): value is Record<string, any>;
6892

6993
export { getListDiff, getObjectDiff, isEqual, isObject };

dist/index.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)