json-diff-checker 0.0.0-stage β 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +231 -2
- package/package.json +22 -4
- package/src/compare.js +124 -0
- package/src/index.js +7 -0
- package/src/utils.js +59 -0
- package/test/compare.test.js +321 -0
package/README.md
CHANGED
|
@@ -1,3 +1,232 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# π JSON Diff Checker
|
|
4
|
+
|
|
5
|
+
**A lightweight, zero-dependency JavaScript library to compare JSON objects and detect differences.**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/json-diff-checker)
|
|
8
|
+
[](https://www.npmjs.com/package/json-diff-checker)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
[](https://www.npmjs.com/package/json-diff-checker)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## π Overview
|
|
17
|
+
|
|
18
|
+
**JSON Diff Checker** helps you compare two JSON objects and pinpoint exactly what changed between them. Whether you're tracking state mutations, debugging API responses, or auditing configuration files, it recursively detects **added**, **removed**, and **modified** propertiesβincluding nested objects and arrays.
|
|
19
|
+
|
|
20
|
+
Built using plain JavaScript with **zero runtime dependencies**.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## β¨ Features
|
|
25
|
+
|
|
26
|
+
* **β Added Properties:** Detects keys present in the new object that were missing from the old object.
|
|
27
|
+
* **β Removed Properties:** Identifies keys that existed in the old object but are missing from the new object.
|
|
28
|
+
* **π Modified Properties:** Captures properties whose values have changed, showing both `old` and `new` states.
|
|
29
|
+
* **πͺ Nested Object Support:** Recursively traverses and compares deeply nested objects.
|
|
30
|
+
* **π Array Comparison:** Detects changes within arrays accurately.
|
|
31
|
+
* **π¬ Deep Value Comparison:** Compares nested values directly rather than relying on shallow reference checks.
|
|
32
|
+
* **π‘οΈ Input Validation:** Rejects invalid inputs early with clear, descriptive `TypeError` messages.
|
|
33
|
+
* **πͺΆ Lightweight:** Zero external runtime dependencies to keep your `node_modules` lean.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## π¦ Installation
|
|
38
|
+
|
|
39
|
+
Install the package via npm:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install json-diff-checker
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## π Getting Started
|
|
48
|
+
|
|
49
|
+
Import the `compare` function into your project:
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
const { compare } = require("json-diff-checker");
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Basic Example
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const { compare } = require("json-diff-checker");
|
|
59
|
+
|
|
60
|
+
const oldData = {
|
|
61
|
+
name: "Manav",
|
|
62
|
+
age: 20,
|
|
63
|
+
city: "Delhi"
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const newData = {
|
|
67
|
+
name: "Manav",
|
|
68
|
+
age: 21,
|
|
69
|
+
country: "India"
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const result = compare(oldData, newData);
|
|
73
|
+
console.log(result);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### Output
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"added": {
|
|
81
|
+
"country": "India"
|
|
82
|
+
},
|
|
83
|
+
"removed": {
|
|
84
|
+
"city": "Delhi"
|
|
85
|
+
},
|
|
86
|
+
"changed": {
|
|
87
|
+
"age": {
|
|
88
|
+
"old": 20,
|
|
89
|
+
"new": 21
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## π Understanding the Output
|
|
98
|
+
|
|
99
|
+
The `compare(oldData, newData)` function returns an object containing three standardized keys:
|
|
100
|
+
|
|
101
|
+
| Property | Type | Description |
|
|
102
|
+
| :--- | :--- | :--- |
|
|
103
|
+
| **`added`** | `Object` | Contains properties added to the new object. |
|
|
104
|
+
| **`removed`** | `Object` | Contains properties removed from the old object. |
|
|
105
|
+
| **`changed`** | `Object` | Contains properties whose values were modified (`{ old, new }`). |
|
|
106
|
+
|
|
107
|
+
> **Note:** If the two objects are identical, all three properties return empty objects (`{}`).
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## π Comparing Nested Objects
|
|
112
|
+
|
|
113
|
+
**JSON Diff Checker** recursively compares nested structures and outputs nested diffs in the exact same predictable format:
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
const { compare } = require("json-diff-checker");
|
|
117
|
+
|
|
118
|
+
const oldData = {
|
|
119
|
+
user: {
|
|
120
|
+
name: "Manav",
|
|
121
|
+
age: 20
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const newData = {
|
|
126
|
+
user: {
|
|
127
|
+
name: "Manav",
|
|
128
|
+
age: 21,
|
|
129
|
+
city: "Delhi"
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
console.log(compare(oldData, newData));
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### Output
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"added": {},
|
|
141
|
+
"removed": {},
|
|
142
|
+
"changed": {
|
|
143
|
+
"user": {
|
|
144
|
+
"added": {
|
|
145
|
+
"city": "Delhi"
|
|
146
|
+
},
|
|
147
|
+
"removed": {},
|
|
148
|
+
"changed": {
|
|
149
|
+
"age": {
|
|
150
|
+
"old": 20,
|
|
151
|
+
"new": 21
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## π οΈ Error Handling
|
|
162
|
+
|
|
163
|
+
Both arguments passed to `compare()` must be valid, non-null plain objects (arrays and primitives are not accepted as root inputs). Providing invalid inputs throws a `TypeError`:
|
|
164
|
+
|
|
165
|
+
```javascript
|
|
166
|
+
compare(null, { name: "Manav" });
|
|
167
|
+
// Throws: TypeError: oldData must be a valid object
|
|
168
|
+
|
|
169
|
+
compare({ name: "Manav" }, [1, 2, 3]);
|
|
170
|
+
// Throws: TypeError: newData must be a valid object
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## π§ͺ Running Tests
|
|
176
|
+
|
|
177
|
+
This project uses **Jest** for unit testing. To run the test suite locally:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
npm test
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The test suite covers:
|
|
184
|
+
- [x] Added, removed, and modified properties
|
|
185
|
+
- [x] Deeply nested object comparisons
|
|
186
|
+
- [x] Array comparisons
|
|
187
|
+
- [x] `null` values and mixed data types
|
|
188
|
+
- [x] Empty and identical objects
|
|
189
|
+
- [x] Key order independence
|
|
190
|
+
- [x] Invalid input error handling
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## π Project Structure
|
|
195
|
+
|
|
196
|
+
```text
|
|
197
|
+
json-diff-checker/
|
|
198
|
+
βββ src/
|
|
199
|
+
β βββ index.js # Package entry point
|
|
200
|
+
β βββ compare.js # Core comparison logic
|
|
201
|
+
β βββ utils.js # Helper & validation utilities
|
|
202
|
+
βββ test/
|
|
203
|
+
β βββ compare.test.js # Jest unit test suite
|
|
204
|
+
βββ package.json
|
|
205
|
+
βββ package-lock.json
|
|
206
|
+
βββ README.md
|
|
207
|
+
βββ LICENSE
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## π§° Built With
|
|
213
|
+
|
|
214
|
+
* **JavaScript (ES6+)** β Core diffing engine
|
|
215
|
+
* **Node.js** β Runtime environment
|
|
216
|
+
* **Jest** β Testing framework
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## π¨βπ» Author
|
|
221
|
+
|
|
222
|
+
**Manav Sharma**
|
|
223
|
+
* GitHub: [@manavsharma2704](https://github.com/manavsharma2704)
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
<div align="center">
|
|
230
|
+
<br />
|
|
231
|
+
β <b>If you find this package useful, consider giving the project a star on <a href="https://github.com/manavsharma2704">GitHub</a>!</b>
|
|
232
|
+
</div>
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-diff-checker",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A lightweight JavaScript library to compare JSON objects and detect differences.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"json",
|
|
7
|
+
"json-diff",
|
|
8
|
+
"json-comparison",
|
|
9
|
+
"javascript",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"deep-diff",
|
|
12
|
+
"object-diff"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "manav_sharma2704",
|
|
16
|
+
"type": "commonjs",
|
|
17
|
+
"main": "src/index.js",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "jest"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"jest": "^30.5.2"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/compare.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const deepEqual = require("./utils");
|
|
2
|
+
function isValidInput(data) {
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
if (data === null) {
|
|
6
|
+
|
|
7
|
+
return false;
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (typeof data !== "object") {
|
|
12
|
+
|
|
13
|
+
return false;
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (Array.isArray(data)) {
|
|
18
|
+
|
|
19
|
+
return false;
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return true;
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function compare(oldData, newData) {
|
|
28
|
+
if (!isValidInput(oldData)) {
|
|
29
|
+
|
|
30
|
+
throw new TypeError("oldData must be a valid object");
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!isValidInput(newData)) {
|
|
35
|
+
|
|
36
|
+
throw new TypeError("newData must be a valid object");
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let added = {};
|
|
41
|
+
|
|
42
|
+
let removed = {};
|
|
43
|
+
|
|
44
|
+
let changed = {};
|
|
45
|
+
|
|
46
|
+
for (let key in newData) {
|
|
47
|
+
|
|
48
|
+
if (!(key in oldData)) {
|
|
49
|
+
|
|
50
|
+
added[key] = newData[key];
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
else if (
|
|
55
|
+
|
|
56
|
+
typeof oldData[key] === "object" &&
|
|
57
|
+
|
|
58
|
+
typeof newData[key] === "object" &&
|
|
59
|
+
|
|
60
|
+
oldData[key] !== null &&
|
|
61
|
+
|
|
62
|
+
newData[key] !== null &&
|
|
63
|
+
|
|
64
|
+
!Array.isArray(oldData[key]) &&
|
|
65
|
+
|
|
66
|
+
!Array.isArray(newData[key])
|
|
67
|
+
|
|
68
|
+
) {
|
|
69
|
+
|
|
70
|
+
let result = compare(oldData[key], newData[key]);
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
|
|
74
|
+
Object.keys(result.added).length > 0 ||
|
|
75
|
+
|
|
76
|
+
Object.keys(result.removed).length > 0 ||
|
|
77
|
+
|
|
78
|
+
Object.keys(result.changed).length > 0
|
|
79
|
+
|
|
80
|
+
) {
|
|
81
|
+
|
|
82
|
+
changed[key] = result;
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
else if (!deepEqual(oldData[key], newData[key])) {
|
|
89
|
+
|
|
90
|
+
changed[key] = {
|
|
91
|
+
|
|
92
|
+
old: oldData[key],
|
|
93
|
+
|
|
94
|
+
new: newData[key]
|
|
95
|
+
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (let key in oldData) {
|
|
103
|
+
|
|
104
|
+
if (!(key in newData)) {
|
|
105
|
+
|
|
106
|
+
removed[key] = oldData[key];
|
|
107
|
+
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
|
|
114
|
+
added: added,
|
|
115
|
+
|
|
116
|
+
removed: removed,
|
|
117
|
+
|
|
118
|
+
changed: changed
|
|
119
|
+
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = compare;
|
package/src/index.js
ADDED
package/src/utils.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
function deepEqual(a, b) {
|
|
2
|
+
|
|
3
|
+
if (a === b) {
|
|
4
|
+
return true;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
if (typeof a !== typeof b) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (a === null || b === null) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (typeof a !== "object") {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (Array.isArray(a) !== Array.isArray(b)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (Array.isArray(a)) {
|
|
24
|
+
|
|
25
|
+
if (a.length !== b.length) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < a.length; i++) {
|
|
30
|
+
if (!deepEqual(a[i], b[i])) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let keysA = Object.keys(a);
|
|
39
|
+
let keysB = Object.keys(b);
|
|
40
|
+
|
|
41
|
+
if (keysA.length !== keysB.length) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (let key of keysA) {
|
|
46
|
+
|
|
47
|
+
if (!Object.prototype.hasOwnProperty.call(b, key)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!deepEqual(a[key], b[key])) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = deepEqual;
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
const { compare } = require("../src/index");
|
|
2
|
+
|
|
3
|
+
test("should detect added properties", () => {
|
|
4
|
+
const oldData = {
|
|
5
|
+
name: "Manav"
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const newData = {
|
|
9
|
+
name: "Manav",
|
|
10
|
+
age: 20
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const result = compare(oldData, newData);
|
|
14
|
+
|
|
15
|
+
expect(result.added).toEqual({
|
|
16
|
+
age: 20
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
test("should detect removed properties", () => {
|
|
22
|
+
const oldData = {
|
|
23
|
+
name: "Manav",
|
|
24
|
+
age: 20
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const newData = {
|
|
28
|
+
name: "Manav"
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const result = compare(oldData, newData);
|
|
32
|
+
|
|
33
|
+
expect(result.removed).toEqual({
|
|
34
|
+
age: 20
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
test("should detect changed properties", () => {
|
|
40
|
+
const oldData = {
|
|
41
|
+
name: "Manav",
|
|
42
|
+
age: 20
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const newData = {
|
|
46
|
+
name: "Manav",
|
|
47
|
+
age: 21
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const result = compare(oldData, newData);
|
|
51
|
+
|
|
52
|
+
expect(result.changed).toEqual({
|
|
53
|
+
age: {
|
|
54
|
+
old: 20,
|
|
55
|
+
new: 21
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
test("should return empty differences for identical objects", () => {
|
|
62
|
+
const oldData = {
|
|
63
|
+
name: "Manav",
|
|
64
|
+
age: 20
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const newData = {
|
|
68
|
+
name: "Manav",
|
|
69
|
+
age: 20
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const result = compare(oldData, newData);
|
|
73
|
+
|
|
74
|
+
expect(result).toEqual({
|
|
75
|
+
added: {},
|
|
76
|
+
removed: {},
|
|
77
|
+
changed: {}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("should detect changes in nested objects", () => {
|
|
82
|
+
|
|
83
|
+
const oldData = {
|
|
84
|
+
|
|
85
|
+
user: {
|
|
86
|
+
|
|
87
|
+
name: "Manav",
|
|
88
|
+
|
|
89
|
+
age: 20
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const newData = {
|
|
96
|
+
|
|
97
|
+
user: {
|
|
98
|
+
|
|
99
|
+
name: "Manav",
|
|
100
|
+
|
|
101
|
+
age: 21
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const result = compare(oldData, newData);
|
|
108
|
+
|
|
109
|
+
expect(result.changed.user.changed.age).toEqual({
|
|
110
|
+
|
|
111
|
+
old: 20,
|
|
112
|
+
|
|
113
|
+
new: 21
|
|
114
|
+
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("should detect added array elements", () => {
|
|
120
|
+
const oldData = {
|
|
121
|
+
skills: ["JavaScript", "Python"]
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const newData = {
|
|
125
|
+
skills: ["JavaScript", "Python", "Node.js"]
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const result = compare(oldData, newData);
|
|
129
|
+
|
|
130
|
+
expect(result.changed.skills).toEqual({
|
|
131
|
+
old: ["JavaScript", "Python"],
|
|
132
|
+
new: ["JavaScript", "Python", "Node.js"]
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
test("should detect removed array elements", () => {
|
|
138
|
+
const oldData = {
|
|
139
|
+
skills: ["JavaScript", "Python", "Node.js"]
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const newData = {
|
|
143
|
+
skills: ["JavaScript", "Python"]
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const result = compare(oldData, newData);
|
|
147
|
+
|
|
148
|
+
expect(result.changed.skills).toEqual({
|
|
149
|
+
old: ["JavaScript", "Python", "Node.js"],
|
|
150
|
+
new: ["JavaScript", "Python"]
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
test("should detect modified array elements", () => {
|
|
156
|
+
const oldData = {
|
|
157
|
+
marks: [80, 90, 70]
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const newData = {
|
|
161
|
+
marks: [80, 95, 70]
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const result = compare(oldData, newData);
|
|
165
|
+
|
|
166
|
+
expect(result.changed.marks).toEqual({
|
|
167
|
+
old: [80, 90, 70],
|
|
168
|
+
new: [80, 95, 70]
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
test("should handle empty objects", () => {
|
|
174
|
+
const result = compare({}, {});
|
|
175
|
+
|
|
176
|
+
expect(result).toEqual({
|
|
177
|
+
added: {},
|
|
178
|
+
removed: {},
|
|
179
|
+
changed: {}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
test("should detect null value changes", () => {
|
|
185
|
+
const oldData = {
|
|
186
|
+
name: null
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const newData = {
|
|
190
|
+
name: "Manav"
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const result = compare(oldData, newData);
|
|
194
|
+
|
|
195
|
+
expect(result.changed.name).toEqual({
|
|
196
|
+
old: null,
|
|
197
|
+
new: "Manav"
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
test("should detect different data types", () => {
|
|
203
|
+
const oldData = {
|
|
204
|
+
age: 20
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const newData = {
|
|
208
|
+
age: "20"
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const result = compare(oldData, newData);
|
|
212
|
+
|
|
213
|
+
expect(result.changed.age).toEqual({
|
|
214
|
+
old: 20,
|
|
215
|
+
new: "20"
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
test("should detect null as an added property", () => {
|
|
221
|
+
const oldData = {};
|
|
222
|
+
|
|
223
|
+
const newData = {
|
|
224
|
+
address: null
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const result = compare(oldData, newData);
|
|
228
|
+
|
|
229
|
+
expect(result.added).toEqual({
|
|
230
|
+
address: null
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
test("should detect identical nested objects", () => {
|
|
236
|
+
const oldData = {
|
|
237
|
+
user: {
|
|
238
|
+
name: "Manav",
|
|
239
|
+
age: 20
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const newData = {
|
|
244
|
+
user: {
|
|
245
|
+
name: "Manav",
|
|
246
|
+
age: 20
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const result = compare(oldData, newData);
|
|
251
|
+
|
|
252
|
+
expect(result.changed).toEqual({});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
test("should consider objects equal regardless of property order", () => {
|
|
257
|
+
|
|
258
|
+
const oldData = {
|
|
259
|
+
user: {
|
|
260
|
+
name: "Manav",
|
|
261
|
+
age: 20
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const newData = {
|
|
266
|
+
user: {
|
|
267
|
+
age: 20,
|
|
268
|
+
name: "Manav"
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const result = compare(oldData, newData);
|
|
273
|
+
|
|
274
|
+
expect(result.changed).toEqual({});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("should reject null inputs", () => {
|
|
278
|
+
|
|
279
|
+
expect(() => compare(null, {})).toThrow();
|
|
280
|
+
|
|
281
|
+
expect(() => compare({}, null)).toThrow();
|
|
282
|
+
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
test("should reject array inputs", () => {
|
|
287
|
+
|
|
288
|
+
expect(() => compare([], {})).toThrow();
|
|
289
|
+
|
|
290
|
+
expect(() => compare({}, [])).toThrow();
|
|
291
|
+
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
test("should reject primitive inputs", () => {
|
|
296
|
+
|
|
297
|
+
expect(() => compare("hello", {})).toThrow();
|
|
298
|
+
|
|
299
|
+
expect(() => compare({}, 20)).toThrow();
|
|
300
|
+
|
|
301
|
+
expect(() => compare(true, {})).toThrow();
|
|
302
|
+
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
test("should show error for invalid oldData", () => {
|
|
308
|
+
|
|
309
|
+
expect(() => compare(null, {}))
|
|
310
|
+
|
|
311
|
+
.toThrow("oldData must be a valid object");
|
|
312
|
+
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("should show error for invalid newData", () => {
|
|
316
|
+
|
|
317
|
+
expect(() => compare({}, null))
|
|
318
|
+
|
|
319
|
+
.toThrow("newData must be a valid object");
|
|
320
|
+
|
|
321
|
+
});
|