Open giampietrozambelli opened 2 weeks ago
Could please paste your old and new code directly here, so I can have more debugging work to reproduce and identify the problem
// Sample 1 - OLD
{
"prop_a":[
{
"firstname": "Andrea",
"lastname": "Chieppa"
},
{
"firstname": "Mario",
"lastname": "Rossi"
}
]
}
// Sample 1 - NEW
{
"prop_a":[
{
"firstname": "Mario",
"lastname": "Rossi"
},
{
"firstname": "Guido",
"lastname": "Bianchi"
}
]
}
// Sample 2 - OLD
{
"prop_a":[
{
"firstname": "Andrea",
"lastname": "Chieppa"
},
{
"firstname": "Mario",
"lastname": "Rossi"
},
{
"new_tag": "new"
}
]
}
// Sample 2 - NEW
{
"prop_a":[
{
"firstname": "Mario",
"lastname": "Rossi"
},
{
"firstname": "Guido",
"lastname": "Bianchi"
},
{
"new_tag": "new"
}
]
}
The code of the sandbox can be found here: Sanbox URL
@otakustay Did you have a chance to reproduce the issue? Could you share more hints?
@otakustay Any news on the ability to reproduce the issue? Or you don't recognize it as an issue?
Sorry for the sever delay, this issue is related to how we compute diff from source text using unidiff
library, by default I didn't pass any options to diffLines
function in my demo, this results a unwanted diff in your case.
I've changed the sandbox, adding an option object to diffLines
call:
const diffText = formatLines(
diffLines(oldText.value, newText.value, {newlineIsToken: true}),
{context: 3},
);
Note the {newlineIsToken: true}
object which helps produce a diff result similar to our expectation, however this introduce another problem that we can see extra empty lines between properties.
This for sure is not correct, and I believe the problem lies inside the formatLines
function from unidiff
package, I'd again search for information about this.
This is not a render issue for react-diff-view
, but a problem with how we generate the diff text itself in demo, maybe I can have a look at how react-diff-viewer
computes the diff text and find some inspiration.
It's also weird to me that the Diff.createPatch
function from diff
package can produce a diff text exactly the same as the image I posted above:
const Diff = require('diff');
const a = `{
"prop_a":[
{
"firstname": "Andrea",
"lastname": "Chieppa"
},
{
"firstname": "Mario",
"lastname": "Rossi"
},
{
"new_tag": "new"
}
]
}`;
const b = `{
"prop_a":[
{
"firstname": "Mario",
"lastname": "Rossi"
},
{
"firstname": "Guido",
"lastname": "Bianchi"
},
{
"new_tag": "new"
}
]
}`;
const patch = Diff.createPatch(
'a.txt',
a,
b,
undefined,
undefined,
{
newlineIsToken: true
}
);
This results:
Index: a.txt
===================================================================
--- a.txt
+++ a.txt
@@ -1,16 +1,16 @@
{
"prop_a":[
{
- "firstname": "Andrea",
+ "firstname": "Mario",
- "lastname": "Chieppa"
+ "lastname": "Rossi"
},
{
- "firstname": "Mario",
+ "firstname": "Guido",
- "lastname": "Rossi"
+ "lastname": "Bianchi"
},
{
"new_tag": "new"
Don't know where the issue is currently
@otakustay I try to diff the 2 following sample json in the react-diff-viewer sandbox https://8rhv2q.csb.app/ .
As in the attached picture:
I would expect to identify the pattern: { "firstname": "Mario", "lastname": "Rossi" } as repeated and identical, while the library compare line per line in this case.
Note that just adding the following new element in the array: _{ "newtag": "new"; }
the library identifies the correct repeated and identical item.
Any suggestions? Did I miss some configuration parameters to let the repeated element properly identified as identical in the 2 input variables? { "firstname": "Mario", "lastname": "Rossi" }
Regards, Giampietro
The source code from the sandbox is: