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.
Files changed (2) hide show
  1. package/README.md +179 -56
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,109 +1,232 @@
1
- JSON Diff Checker
1
+ <div align="center">
2
2
 
3
- A lightweight JavaScript library that compares two JSON objects and identifies their differences.
3
+ # πŸ” JSON Diff Checker
4
4
 
5
- Features
5
+ **A lightweight, zero-dependency JavaScript library to compare JSON objects and detect differences.**
6
6
 
7
- * Detect added properties.
8
- * Detect removed properties.
9
- * Detect modified properties.
10
- * Compare nested objects.
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
+ [![npm version](https://img.shields.io/npm/v/json-diff-checker.svg?style=flat-square&color=cb3837)](https://www.npmjs.com/package/json-diff-checker)
8
+ [![npm downloads](https://img.shields.io/npm/dm/json-diff-checker.svg?style=flat-square&color=blue)](https://www.npmjs.com/package/json-diff-checker)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT)
10
+ [![Zero Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg?style=flat-square)](https://www.npmjs.com/package/json-diff-checker)
16
11
 
17
- Installation
12
+ </div>
18
13
 
19
- Install the package using npm:
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
- Usage
45
+ ---
24
46
 
25
- Import the compare function:
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
- Create two objects:
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
- skills: ["JavaScript", "Python"]
63
+ city: "Delhi"
35
64
  };
65
+
36
66
  const newData = {
37
67
  name: "Manav",
38
68
  age: 21,
39
- skills: ["JavaScript", "Python", "Node.js"]
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
- Example Output
76
+ #### Output
48
77
 
78
+ ```json
49
79
  {
50
- added: {},
51
- removed: {},
52
- changed: {
53
- age: {
54
- old: 20,
55
- new: 21
56
- },
57
- skills: {
58
- old: ["JavaScript", "Python"],
59
- new: ["JavaScript", "Python", "Node.js"]
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
- Output Structure
99
+ The `compare(oldData, newData)` function returns an object containing three standardized keys:
65
100
 
66
- The compare function returns an object containing three properties:
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
- Property Description
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
- For nested objects, differences are reported recursively.
109
+ ---
74
110
 
75
- Input Validation
111
+ ## πŸ” Comparing Nested Objects
76
112
 
77
- Both inputs must be non-null objects and must not be arrays.
113
+ **JSON Diff Checker** recursively compares nested structures and outputs nested diffs in the exact same predictable format:
78
114
 
79
- Invalid inputs throw a TypeError.
115
+ ```javascript
116
+ const { compare } = require("json-diff-checker");
80
117
 
81
- Example:
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
- Output:
169
+ compare({ name: "Manav" }, [1, 2, 3]);
170
+ // Throws: TypeError: newData must be a valid object
171
+ ```
86
172
 
87
- TypeError: oldData must be a valid object
173
+ ---
88
174
 
89
- Testing
175
+ ## πŸ§ͺ Running Tests
90
176
 
91
- Run the test suite using:
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
- The project uses Jest for automated testing.
218
+ ---
96
219
 
97
- Technologies Used
220
+ ## πŸ‘¨β€πŸ’» Author
98
221
 
99
- * JavaScript
100
- * Node.js
101
- * Jest
222
+ **Manav Sharma**
223
+ * GitHub: [@manavsharma2704](https://github.com/manavsharma2704)
102
224
 
103
- Dependencies
225
+ ---
104
226
 
105
- The comparison functionality is implemented using plain JavaScript without external runtime dependencies.
106
227
 
107
- License
108
228
 
109
- MIT
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.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",