json-diff-checker 0.0.0-stage → 1.0.0

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 CHANGED
@@ -1,3 +1,109 @@
1
- # Temporary Holding Version
1
+ JSON Diff Checker
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A lightweight JavaScript library that compares two JSON objects and identifies their differences.
4
+
5
+ Features
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.
16
+
17
+ Installation
18
+
19
+ Install the package using npm:
20
+
21
+ npm install json-diff-checker
22
+
23
+ Usage
24
+
25
+ Import the compare function:
26
+
27
+ const { compare } = require("json-diff-checker");
28
+
29
+ Create two objects:
30
+
31
+ const oldData = {
32
+ name: "Manav",
33
+ age: 20,
34
+ skills: ["JavaScript", "Python"]
35
+ };
36
+ const newData = {
37
+ name: "Manav",
38
+ age: 21,
39
+ skills: ["JavaScript", "Python", "Node.js"]
40
+ };
41
+
42
+ Compare the objects:
43
+
44
+ const result = compare(oldData, newData);
45
+ console.log(result);
46
+
47
+ Example Output
48
+
49
+ {
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"]
60
+ }
61
+ }
62
+ }
63
+
64
+ Output Structure
65
+
66
+ The compare function returns an object containing three properties:
67
+
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.
72
+
73
+ For nested objects, differences are reported recursively.
74
+
75
+ Input Validation
76
+
77
+ Both inputs must be non-null objects and must not be arrays.
78
+
79
+ Invalid inputs throw a TypeError.
80
+
81
+ Example:
82
+
83
+ compare(null, { name: "Manav" });
84
+
85
+ Output:
86
+
87
+ TypeError: oldData must be a valid object
88
+
89
+ Testing
90
+
91
+ Run the test suite using:
92
+
93
+ npm test
94
+
95
+ The project uses Jest for automated testing.
96
+
97
+ Technologies Used
98
+
99
+ * JavaScript
100
+ * Node.js
101
+ * Jest
102
+
103
+ Dependencies
104
+
105
+ The comparison functionality is implemented using plain JavaScript without external runtime dependencies.
106
+
107
+ License
108
+
109
+ MIT
package/package.json CHANGED
@@ -1,6 +1,24 @@
1
1
  {
2
2
  "name": "json-diff-checker",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
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
@@ -0,0 +1,7 @@
1
+ const compare = require("./compare");
2
+
3
+ module.exports = {
4
+
5
+ compare
6
+
7
+ };
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
+ });