File tree Expand file tree Collapse file tree 2 files changed +92
-0
lines changed Expand file tree Collapse file tree 2 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ // import diff, patch from '../../nodejs/dist/diff.js';
2
+ const { diff, patch } = require ( '../../nodejs/dist' ) ;
3
+
4
+ const a = {
5
+ "a" : 1 ,
6
+ "b" : 2 ,
7
+ "c" : {
8
+ "d" : 3 ,
9
+ "e" : 4 ,
10
+ "f" : {
11
+ "g" : 5 ,
12
+ "h" : 6 ,
13
+ } ,
14
+ } ,
15
+ } ;
16
+
17
+ const b = {
18
+ "a" : 1 ,
19
+ // "b": 2, // b is deleted
20
+ "c" : {
21
+ "d" : 9 , // d is changed
22
+ "e" : 4 ,
23
+ "f" : {
24
+ "g" : 5 ,
25
+ "h" : 7 , // h is changed
26
+ "i" : 10 , // i is added
27
+ } ,
28
+ } ,
29
+ } ;
30
+
31
+ // diff a and b and create a diffData object
32
+ const diffData = diff ( a , b ) ;
33
+
34
+ // print diffData in the console
35
+ console . log ( "DIFF DATA\n" , JSON . stringify ( diffData , null , 2 ) ) ;
36
+
37
+ // apply diffData to a and create a new object
38
+ const patched_a = patch ( a , diffData ) ;
39
+
40
+ // print patched_a in the console
41
+ console . log ( "\n\nPATCHED OBJ\n" , JSON . stringify ( patched_a , null , 2 ) ) ;
42
+
43
+ // check if patched_a is equal to b
44
+ console . log ( "\n\nSAME OBJECT: " , JSON . stringify ( patched_a ) === JSON . stringify ( b ) ) ;
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+
3
+ import json
4
+ from fsoft_diff_patch import diff , patch
5
+
6
+ a = {
7
+ "a" : 1 ,
8
+ "b" : 2 ,
9
+ "c" : {
10
+ "d" : 3 ,
11
+ "e" : 4 ,
12
+ "f" : {
13
+ "g" : 5 ,
14
+ "h" : 6 ,
15
+ },
16
+ },
17
+ }
18
+
19
+ b = {
20
+ "a" : 1 ,
21
+ # "b": 2, # b is deleted
22
+ "c" : {
23
+ "d" : 9 , # d is changed
24
+ "e" : 4 ,
25
+ "f" : {
26
+ "g" : 5 ,
27
+ "h" : 7 , # h is changed
28
+ "i" : 10 , # i is added
29
+ },
30
+ },
31
+ }
32
+
33
+ # diff a and b and produce diff_data
34
+ diff_data = diff (a , b )
35
+
36
+ # print diff_data
37
+ print ("DIFF DATA\n %s" % json .dumps (diff_data , indent = 4 ))
38
+
39
+ # patch a with diff_data and produce patched_a
40
+ patched_a = patch (a , diff_data )
41
+
42
+ # print patched_a
43
+ print ("\n \n PATCHED\n %s" % json .dumps (patched_a , indent = 4 ))
44
+
45
+ # check if patched_a is equal to b
46
+ assert patched_a == b
47
+
48
+ print ("\n \n SUCCESS" )
You can’t perform that action at this time.
0 commit comments