Closed as not planned
Description
Consider the following program:
const jsdiff= require('diff');
const s1= [
'first line',
'',
'',
'last line'
]
const s2= [
'first line',
'NEW TEXT',
'',
'last line',
'',
''
];
const result= jsdiff.diffArrays(s1, s2);
console.log(JSON.stringify(result, undefined, 2));
If I run it in node, I get the following output:
[
{
"count": 1,
"value": [
"first line"
]
},
{
"count": 1,
"added": true,
"value": [
"NEW TEXT"
]
},
{
"count": 1,
"value": [
""
]
},
{
"count": 1,
"added": true,
"value": [
"last line"
]
},
{
"count": 1,
"value": [
""
]
},
{
"count": 1,
"removed": true,
"value": [
"last line"
]
},
{
"count": 1,
"added": true,
"value": [
""
]
}
]
Note that the output has 7 elements, including 4 which are actual changes. Now if I replace the last two lines in the array s2
with 'a' and 'b', I get an edit script like so:
[
{
"count": 1,
"value": [
"first line"
]
},
{
"count": 1,
"added": true,
"value": [
"NEW TEXT"
]
},
{
"count": 1,
"value": [
""
]
},
{
"count": 1,
"removed": true,
"value": [
""
]
},
{
"count": 1,
"value": [
"last line"
]
},
{
"count": 2,
"added": true,
"value": [
"a",
"b"
]
}
]
Not that this change is shorter both in total length and also number of additions/removals. It seems obvious to me that the script in the second case could also be written for the first case, so the edit script in the first case is not minimal.
It seems odd that the algorithm should behave differently for empty and non-empty lines.
Metadata
Metadata
Assignees
Labels
No labels