Skip to content

Commit fee7ae6

Browse files
committed
feat: add buymeacoffee link + discardArrayOrder option
1 parent 207c762 commit fee7ae6

File tree

9 files changed

+213
-39
lines changed

9 files changed

+213
-39
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,35 @@ false;
394394

395395
More examples are availble in the tests of the source code.
396396

397+
<hr/>
398+
399+
### OPTIONS
400+
401+
`getObjectDiff()` and `isEqual()` accept a facultative `options` parameter:
402+
403+
```ts
404+
{
405+
discardArrayOrder?: boolean // false by default
406+
}
407+
```
408+
409+
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.
410+
397411
## CREDITS
398412

399413
DoneDeal0
400414

415+
## SUPPORT
416+
417+
If you use Superdiff, please show your support by buying me coffee:
418+
https://www.buymeacoffee.com/donedeal0
419+
420+
<br/>
421+
<a href="https://www.buymeacoffee.com/donedeal0" target="_blank">
422+
<img src="https://user-images.githubusercontent.com/43271780/178990049-46b05704-1344-4d55-a5a7-7265724edc5c.png"/>
423+
</a>
424+
<br/>
425+
401426
## CONTRIBUTING
402427

403428
Pull requests are welcome!

dist/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
type DiffStatus = "added" | "equal" | "moved" | "deleted" | "updated";
22
type ObjectData = Record<string, any> | undefined | null;
33
type ListData = any;
4+
type Options = {
5+
discardArrayOrder?: boolean;
6+
};
47
type ListDiff = {
58
type: "list";
69
status: DiffStatus;
@@ -31,11 +34,11 @@ type ObjectDiff = {
3134
}[];
3235
};
3336

34-
declare function getObjectDiff(prevData: ObjectData, nextData: ObjectData): ObjectDiff;
37+
declare function getObjectDiff(prevData: ObjectData, nextData: ObjectData, options?: Options): ObjectDiff;
3538

3639
declare const getListDiff: (prevList: ListData[] | undefined | null, nextList: ListData[] | undefined | null) => ListDiff;
3740

38-
declare function isEqual(a: any, b: any): boolean;
41+
declare function isEqual(a: any, b: any, options?: Options): boolean;
3942
declare function isObject(value: any): value is Record<string, any>;
4043

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

dist/index.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ var STATUS = {
1010
};
1111

1212
// src/utils.ts
13-
function isEqual(a, b) {
13+
function isEqual(a, b, options = { discardArrayOrder: false }) {
1414
if (typeof a !== typeof b)
1515
return false;
1616
if (Array.isArray(a)) {
1717
if (a.length !== b.length) {
1818
return false;
1919
}
20+
if (options.discardArrayOrder) {
21+
return a.every(
22+
(v) => b.some((nextV) => JSON.stringify(nextV) === JSON.stringify(v))
23+
);
24+
}
2025
return a.every((v, i) => JSON.stringify(v) === JSON.stringify(b[i]));
2126
}
2227
if (typeof a === "object") {
@@ -73,17 +78,17 @@ function formatSingleObjectDiff(data, status) {
7378
diff
7479
};
7580
}
76-
function getPreviousMatch(previousValue, nextSubProperty) {
81+
function getPreviousMatch(previousValue, nextSubProperty, options) {
7782
if (!previousValue) {
7883
return void 0;
7984
}
8085
const previousMatch = Object.entries(previousValue).find(
81-
([subPreviousKey]) => isEqual(subPreviousKey, nextSubProperty)
86+
([subPreviousKey]) => isEqual(subPreviousKey, nextSubProperty, options)
8287
);
8388
return previousMatch ? previousMatch[1] : void 0;
8489
}
85-
function getValueStatus(previousValue, nextValue) {
86-
if (isEqual(previousValue, nextValue)) {
90+
function getValueStatus(previousValue, nextValue, options) {
91+
if (isEqual(previousValue, nextValue, options)) {
8792
return STATUS.EQUAL;
8893
}
8994
return STATUS.UPDATED;
@@ -105,7 +110,7 @@ function getDeletedProperties(previousValue, nextValue) {
105110
}
106111
return void 0;
107112
}
108-
function getSubPropertiesDiff(previousValue, nextValue) {
113+
function getSubPropertiesDiff(previousValue, nextValue, options) {
109114
const subPropertiesDiff = [];
110115
let subDiff;
111116
const deletedMainSubProperties = getDeletedProperties(
@@ -123,7 +128,11 @@ function getSubPropertiesDiff(previousValue, nextValue) {
123128
});
124129
}
125130
Object.entries(nextValue).forEach(([nextSubProperty, nextSubValue]) => {
126-
const previousMatch = getPreviousMatch(previousValue, nextSubProperty);
131+
const previousMatch = getPreviousMatch(
132+
previousValue,
133+
nextSubProperty,
134+
options
135+
);
127136
if (!!!previousMatch && !!nextSubProperty) {
128137
return subPropertiesDiff.push({
129138
name: nextSubProperty,
@@ -135,7 +144,8 @@ function getSubPropertiesDiff(previousValue, nextValue) {
135144
if (isObject(nextSubValue)) {
136145
const data = getSubPropertiesDiff(
137146
previousMatch,
138-
nextSubValue
147+
nextSubValue,
148+
options
139149
);
140150
if (data && data.length > 0) {
141151
subDiff = data;
@@ -146,14 +156,14 @@ function getSubPropertiesDiff(previousValue, nextValue) {
146156
name: nextSubProperty,
147157
previousValue: previousMatch,
148158
currentValue: nextSubValue,
149-
status: getValueStatus(previousMatch, nextSubValue),
159+
status: getValueStatus(previousMatch, nextSubValue, options),
150160
...!!subDiff && { subDiff }
151161
});
152162
}
153163
});
154164
return subPropertiesDiff;
155165
}
156-
function getObjectDiff(prevData, nextData) {
166+
function getObjectDiff(prevData, nextData, options) {
157167
if (!prevData && !nextData) {
158168
return {
159169
type: "object",
@@ -181,7 +191,8 @@ function getObjectDiff(prevData, nextData) {
181191
if (isObject(nextValue)) {
182192
const subPropertiesDiff = getSubPropertiesDiff(
183193
previousValue,
184-
nextValue
194+
nextValue,
195+
options
185196
);
186197
return diff.push({
187198
property: nextProperty,
@@ -195,7 +206,7 @@ function getObjectDiff(prevData, nextData) {
195206
property: nextProperty,
196207
previousValue,
197208
currentValue: nextValue,
198-
status: getValueStatus(previousValue, nextValue)
209+
status: getValueStatus(previousValue, nextValue, options)
199210
});
200211
});
201212
const deletedProperties = getDeletedProperties(prevData, nextData);

dist/index.mjs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ var STATUS = {
88
};
99

1010
// src/utils.ts
11-
function isEqual(a, b) {
11+
function isEqual(a, b, options = { discardArrayOrder: false }) {
1212
if (typeof a !== typeof b)
1313
return false;
1414
if (Array.isArray(a)) {
1515
if (a.length !== b.length) {
1616
return false;
1717
}
18+
if (options.discardArrayOrder) {
19+
return a.every(
20+
(v) => b.some((nextV) => JSON.stringify(nextV) === JSON.stringify(v))
21+
);
22+
}
1823
return a.every((v, i) => JSON.stringify(v) === JSON.stringify(b[i]));
1924
}
2025
if (typeof a === "object") {
@@ -71,17 +76,17 @@ function formatSingleObjectDiff(data, status) {
7176
diff
7277
};
7378
}
74-
function getPreviousMatch(previousValue, nextSubProperty) {
79+
function getPreviousMatch(previousValue, nextSubProperty, options) {
7580
if (!previousValue) {
7681
return void 0;
7782
}
7883
const previousMatch = Object.entries(previousValue).find(
79-
([subPreviousKey]) => isEqual(subPreviousKey, nextSubProperty)
84+
([subPreviousKey]) => isEqual(subPreviousKey, nextSubProperty, options)
8085
);
8186
return previousMatch ? previousMatch[1] : void 0;
8287
}
83-
function getValueStatus(previousValue, nextValue) {
84-
if (isEqual(previousValue, nextValue)) {
88+
function getValueStatus(previousValue, nextValue, options) {
89+
if (isEqual(previousValue, nextValue, options)) {
8590
return STATUS.EQUAL;
8691
}
8792
return STATUS.UPDATED;
@@ -103,7 +108,7 @@ function getDeletedProperties(previousValue, nextValue) {
103108
}
104109
return void 0;
105110
}
106-
function getSubPropertiesDiff(previousValue, nextValue) {
111+
function getSubPropertiesDiff(previousValue, nextValue, options) {
107112
const subPropertiesDiff = [];
108113
let subDiff;
109114
const deletedMainSubProperties = getDeletedProperties(
@@ -121,7 +126,11 @@ function getSubPropertiesDiff(previousValue, nextValue) {
121126
});
122127
}
123128
Object.entries(nextValue).forEach(([nextSubProperty, nextSubValue]) => {
124-
const previousMatch = getPreviousMatch(previousValue, nextSubProperty);
129+
const previousMatch = getPreviousMatch(
130+
previousValue,
131+
nextSubProperty,
132+
options
133+
);
125134
if (!!!previousMatch && !!nextSubProperty) {
126135
return subPropertiesDiff.push({
127136
name: nextSubProperty,
@@ -133,7 +142,8 @@ function getSubPropertiesDiff(previousValue, nextValue) {
133142
if (isObject(nextSubValue)) {
134143
const data = getSubPropertiesDiff(
135144
previousMatch,
136-
nextSubValue
145+
nextSubValue,
146+
options
137147
);
138148
if (data && data.length > 0) {
139149
subDiff = data;
@@ -144,14 +154,14 @@ function getSubPropertiesDiff(previousValue, nextValue) {
144154
name: nextSubProperty,
145155
previousValue: previousMatch,
146156
currentValue: nextSubValue,
147-
status: getValueStatus(previousMatch, nextSubValue),
157+
status: getValueStatus(previousMatch, nextSubValue, options),
148158
...!!subDiff && { subDiff }
149159
});
150160
}
151161
});
152162
return subPropertiesDiff;
153163
}
154-
function getObjectDiff(prevData, nextData) {
164+
function getObjectDiff(prevData, nextData, options) {
155165
if (!prevData && !nextData) {
156166
return {
157167
type: "object",
@@ -179,7 +189,8 @@ function getObjectDiff(prevData, nextData) {
179189
if (isObject(nextValue)) {
180190
const subPropertiesDiff = getSubPropertiesDiff(
181191
previousValue,
182-
nextValue
192+
nextValue,
193+
options
183194
);
184195
return diff.push({
185196
property: nextProperty,
@@ -193,7 +204,7 @@ function getObjectDiff(prevData, nextData) {
193204
property: nextProperty,
194205
previousValue,
195206
currentValue: nextValue,
196-
status: getValueStatus(previousValue, nextValue)
207+
status: getValueStatus(previousValue, nextValue, options)
197208
});
198209
});
199210
const deletedProperties = getDeletedProperties(prevData, nextData);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@donedeal0/superdiff",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
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.js",

src/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const STATUS: Record<string, DiffStatus> = {
99
export type DiffStatus = "added" | "equal" | "moved" | "deleted" | "updated";
1010
export type ObjectData = Record<string, any> | undefined | null;
1111
export type ListData = any;
12+
export type Options = { discardArrayOrder?: boolean };
1213

1314
export type ListDiff = {
1415
type: "list";

0 commit comments

Comments
 (0)