json-diff-checker 1.0.0 β 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 +179 -56
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,109 +1,232 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# π JSON Diff Checker
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**A lightweight, zero-dependency JavaScript library to compare JSON objects and detect differences.**
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* Detect array differences.
|
|
12
|
-
* Perform deep comparison of objects and arrays.
|
|
13
|
-
* Handle different data types.
|
|
14
|
-
* Validate input before comparison.
|
|
15
|
-
* No external dependencies for comparison logic.
|
|
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)
|
|
16
11
|
|
|
17
|
-
|
|
12
|
+
</div>
|
|
18
13
|
|
|
19
|
-
|
|
14
|
+
---
|
|
20
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
|
|
21
42
|
npm install json-diff-checker
|
|
43
|
+
```
|
|
22
44
|
|
|
23
|
-
|
|
45
|
+
---
|
|
24
46
|
|
|
25
|
-
|
|
47
|
+
## π Getting Started
|
|
26
48
|
|
|
49
|
+
Import the `compare` function into your project:
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
27
52
|
const { compare } = require("json-diff-checker");
|
|
53
|
+
```
|
|
28
54
|
|
|
29
|
-
|
|
55
|
+
### Basic Example
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const { compare } = require("json-diff-checker");
|
|
30
59
|
|
|
31
60
|
const oldData = {
|
|
32
61
|
name: "Manav",
|
|
33
62
|
age: 20,
|
|
34
|
-
|
|
63
|
+
city: "Delhi"
|
|
35
64
|
};
|
|
65
|
+
|
|
36
66
|
const newData = {
|
|
37
67
|
name: "Manav",
|
|
38
68
|
age: 21,
|
|
39
|
-
|
|
69
|
+
country: "India"
|
|
40
70
|
};
|
|
41
71
|
|
|
42
|
-
Compare the objects:
|
|
43
|
-
|
|
44
72
|
const result = compare(oldData, newData);
|
|
45
73
|
console.log(result);
|
|
74
|
+
```
|
|
46
75
|
|
|
47
|
-
|
|
76
|
+
#### Output
|
|
48
77
|
|
|
78
|
+
```json
|
|
49
79
|
{
|
|
50
|
-
added: {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
old:
|
|
59
|
-
new:
|
|
80
|
+
"added": {
|
|
81
|
+
"country": "India"
|
|
82
|
+
},
|
|
83
|
+
"removed": {
|
|
84
|
+
"city": "Delhi"
|
|
85
|
+
},
|
|
86
|
+
"changed": {
|
|
87
|
+
"age": {
|
|
88
|
+
"old": 20,
|
|
89
|
+
"new": 21
|
|
60
90
|
}
|
|
61
91
|
}
|
|
62
92
|
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## π Understanding the Output
|
|
63
98
|
|
|
64
|
-
|
|
99
|
+
The `compare(oldData, newData)` function returns an object containing three standardized keys:
|
|
65
100
|
|
|
66
|
-
|
|
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 }`). |
|
|
67
106
|
|
|
68
|
-
|
|
69
|
-
added Properties present in the new object but missing from the old object.
|
|
70
|
-
removed Properties present in the old object but missing from the new object.
|
|
71
|
-
changed Properties whose values have changed.
|
|
107
|
+
> **Note:** If the two objects are identical, all three properties return empty objects (`{}`).
|
|
72
108
|
|
|
73
|
-
|
|
109
|
+
---
|
|
74
110
|
|
|
75
|
-
|
|
111
|
+
## π Comparing Nested Objects
|
|
76
112
|
|
|
77
|
-
|
|
113
|
+
**JSON Diff Checker** recursively compares nested structures and outputs nested diffs in the exact same predictable format:
|
|
78
114
|
|
|
79
|
-
|
|
115
|
+
```javascript
|
|
116
|
+
const { compare } = require("json-diff-checker");
|
|
80
117
|
|
|
81
|
-
|
|
118
|
+
const oldData = {
|
|
119
|
+
user: {
|
|
120
|
+
name: "Manav",
|
|
121
|
+
age: 20
|
|
122
|
+
}
|
|
123
|
+
};
|
|
82
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
|
|
83
166
|
compare(null, { name: "Manav" });
|
|
167
|
+
// Throws: TypeError: oldData must be a valid object
|
|
84
168
|
|
|
85
|
-
|
|
169
|
+
compare({ name: "Manav" }, [1, 2, 3]);
|
|
170
|
+
// Throws: TypeError: newData must be a valid object
|
|
171
|
+
```
|
|
86
172
|
|
|
87
|
-
|
|
173
|
+
---
|
|
88
174
|
|
|
89
|
-
|
|
175
|
+
## π§ͺ Running Tests
|
|
90
176
|
|
|
91
|
-
|
|
177
|
+
This project uses **Jest** for unit testing. To run the test suite locally:
|
|
92
178
|
|
|
179
|
+
```bash
|
|
93
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
|
|
94
217
|
|
|
95
|
-
|
|
218
|
+
---
|
|
96
219
|
|
|
97
|
-
|
|
220
|
+
## π¨βπ» Author
|
|
98
221
|
|
|
99
|
-
|
|
100
|
-
*
|
|
101
|
-
* Jest
|
|
222
|
+
**Manav Sharma**
|
|
223
|
+
* GitHub: [@manavsharma2704](https://github.com/manavsharma2704)
|
|
102
224
|
|
|
103
|
-
|
|
225
|
+
---
|
|
104
226
|
|
|
105
|
-
The comparison functionality is implemented using plain JavaScript without external runtime dependencies.
|
|
106
227
|
|
|
107
|
-
License
|
|
108
228
|
|
|
109
|
-
|
|
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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-diff-checker",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A lightweight JavaScript library to compare JSON objects and detect differences.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"json",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"nodejs",
|
|
11
11
|
"deep-diff",
|
|
12
12
|
"object-diff"
|
|
13
|
-
],
|
|
13
|
+
],
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"author": "manav_sharma2704",
|
|
16
16
|
"type": "commonjs",
|