33[ ![ Build Status] ( https://travis-ci.org/mattphillips/deep-object-diff.svg?branch=master )] ( https://travis-ci.org/mattphillips/deep-object-diff )
44[ ![ Coverage Status] ( https://coveralls.io/repos/github/mattphillips/deep-object-diff/badge.svg?branch=master )] ( https://coveralls.io/github/mattphillips/deep-object-diff?branch=master )
55
6- A small library that will deep diff two JavaScript Objects, including nested structures of arrays and objects, and return the difference .
6+ A small library that can deep diff two JavaScript Objects, including nested structures of arrays and objects.
77
8- ### Useage:
8+ ## Installation
9+ ```
10+ npm i --save deep-object-diff
11+ ```
12+
13+ ## Functions available:
14+ - ### [ ` diff(originalObj, updatedObj) ` ] ( #diff )
15+ returns the difference of the original and updated objects
16+
17+ - ### [ ` addedDiff(original, updatedObj) ` ] ( #addeddiff )
18+ returns only the values added to the updated object
19+
20+ - ### [ ` deletedDiff(original, updatedObj) ` ] ( #deleteddiff )
21+ returns only the values deleted in the updated object
22+
23+ - ### [ ` updatedDiff(original, updatedObj) ` ] ( #updateddiff )
24+ returns only the values that have been changed in the updated object
25+
26+ - ### [ ` detailedDiff(original, updatedObj) ` ] ( #detaileddiff )
27+ returns an object with the added, deleted and updated differences
28+
29+ ## Importing
30+
31+ ES6 / Babel:
32+ ``` js
33+ import { diff , addedDiff , deletedDiff , updatedDiff , detailedDiff } from ' deep-object-diff' ;
34+ ```
35+
36+ ES5:
37+ ``` js
38+ const { diff , addedDiff , deletedDiff , detailedDiff , updatedDiff } = require (" deep-object-diff" );
39+
40+ // OR
41+
42+ const diff = require (" deep-object-diff" ).diff ;
43+ const addedDiff = require (" deep-object-diff" ).addedDiff ;
44+ const deletedDiff = require (" deep-object-diff" ).deletedDiff ;
45+ const detailedDiff = require (" deep-object-diff" ).detailedDiff ;
46+ const updatedDiff = require (" deep-object-diff" ).updatedDiff ;
47+ ```
48+
49+ ## Usage:
50+
51+ ### ` diff ` :
952``` js
1053const lhs = {
1154 foo: {
1255 bar: {
13- a: [1 , 2 ],
56+ a: [' a ' , ' b ' ],
1457 b: 2 ,
1558 c: [' x' , ' y' ],
1659 e: 100 // deleted
@@ -22,18 +65,17 @@ const lhs = {
2265const rhs = {
2366 foo: {
2467 bar: {
25- a: [1 ], // updated (value deleted)
68+ a: [' a ' ], // index 1 ('b') deleted
2669 b: 2 , // unchanged
27- c: [' x' , ' y' , ' z' ], // updated (value added)
70+ c: [' x' , ' y' , ' z' ], // 'z' added
2871 d: ' Hello, world!' // added
2972 }
3073 },
3174 buzz: ' fizz' // updated
3275};
3376
34- console .log (diff (lhs, rhs));
35-
36- /* logs:
77+ console .log (diff (lhs, rhs)); // =>
78+ /*
3779{
3880 foo: {
3981 bar: {
@@ -51,3 +93,179 @@ console.log(diff(lhs, rhs));
5193}
5294*/
5395```
96+
97+ ### ` addedDiff ` :
98+ ``` js
99+ const lhs = {
100+ foo: {
101+ bar: {
102+ a: [' a' , ' b' ],
103+ b: 2 ,
104+ c: [' x' , ' y' ],
105+ e: 100 // deleted
106+ }
107+ },
108+ buzz: ' world'
109+ };
110+
111+ const rhs = {
112+ foo: {
113+ bar: {
114+ a: [' a' ], // index 1 ('b') deleted
115+ b: 2 , // unchanged
116+ c: [' x' , ' y' , ' z' ], // 'z' added
117+ d: ' Hello, world!' // added
118+ }
119+ },
120+ buzz: ' fizz' // updated
121+ };
122+
123+ console .log (addedDiff (lhs, rhs));
124+
125+ /*
126+ {
127+ foo: {
128+ bar: {
129+ c: {
130+ '2': 'z'
131+ },
132+ d: 'Hello, world!'
133+ }
134+ }
135+ }
136+ */
137+ ```
138+
139+ ### ` deletedDiff ` :
140+ ``` js
141+ const lhs = {
142+ foo: {
143+ bar: {
144+ a: [' a' , ' b' ],
145+ b: 2 ,
146+ c: [' x' , ' y' ],
147+ e: 100 // deleted
148+ }
149+ },
150+ buzz: ' world'
151+ };
152+
153+ const rhs = {
154+ foo: {
155+ bar: {
156+ a: [' a' ], // index 1 ('b') deleted
157+ b: 2 , // unchanged
158+ c: [' x' , ' y' , ' z' ], // 'z' added
159+ d: ' Hello, world!' // added
160+ }
161+ },
162+ buzz: ' fizz' // updated
163+ };
164+
165+ console .log (deletedDiff (lhs, rhs));
166+
167+ /*
168+ {
169+ foo: {
170+ bar: {
171+ a: {
172+ '1': undefined
173+ },
174+ e: undefined
175+ }
176+ }
177+ }
178+ */
179+ ```
180+
181+ ### ` updatedDiff ` :
182+ ``` js
183+ const lhs = {
184+ foo: {
185+ bar: {
186+ a: [' a' , ' b' ],
187+ b: 2 ,
188+ c: [' x' , ' y' ],
189+ e: 100 // deleted
190+ }
191+ },
192+ buzz: ' world'
193+ };
194+
195+ const rhs = {
196+ foo: {
197+ bar: {
198+ a: [' a' ], // index 1 ('b') deleted
199+ b: 2 , // unchanged
200+ c: [' x' , ' y' , ' z' ], // 'z' added
201+ d: ' Hello, world!' // added
202+ }
203+ },
204+ buzz: ' fizz' // updated
205+ };
206+
207+ console .log (updatedDiff (lhs, rhs));
208+
209+ /*
210+ {
211+ buzz: 'fizz'
212+ }
213+ */
214+ ```
215+
216+ ### ` detailedDiff ` :
217+ ``` js
218+ const lhs = {
219+ foo: {
220+ bar: {
221+ a: [' a' , ' b' ],
222+ b: 2 ,
223+ c: [' x' , ' y' ],
224+ e: 100 // deleted
225+ }
226+ },
227+ buzz: ' world'
228+ };
229+
230+ const rhs = {
231+ foo: {
232+ bar: {
233+ a: [' a' ], // index 1 ('b') deleted
234+ b: 2 , // unchanged
235+ c: [' x' , ' y' , ' z' ], // 'z' added
236+ d: ' Hello, world!' // added
237+ }
238+ },
239+ buzz: ' fizz' // updated
240+ };
241+
242+ console .log (detailedDiff (lhs, rhs));
243+
244+ /*
245+ {
246+ added: {
247+ foo: {
248+ bar: {
249+ c: {
250+ '2': 'z'
251+ },
252+ d: 'Hello, world!'
253+ }
254+ }
255+ },
256+ deleted: {
257+ foo: {
258+ bar: {
259+ a: {
260+ '1': undefined
261+ },
262+ e: undefined
263+ }
264+ }
265+ },
266+ updated: {
267+ buzz: 'fizz'
268+ }
269+ }
270+ */
271+ ```
0 commit comments