Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying removal and adding of lines #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ module.exports = {
}
var file1 = fs.readFileSync(files[0]).toString();
var file2 = fs.readFileSync(files[1]).toString();
var {diffLines, diffFile} = diffChecker.diff(file1, file2);

var validCurData = (curData && curData.length !== 2);
var fileType = convertType(path.extname(files[1]).substring(1));
var useCurData = useCurData ? validCurData && (typeof curData.description === "string") && !curData.body : false;;
var markdown = '\n```' + fileType + '\n' + file2 + '\n```';
var diffLines = diffChecker.diff(file1, file2);
var markdown = '\n```' + fileType + '\n' + diffFile.join('\n') + '\n```';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To display if there removed any lines, we had to create a new "file" to display added and removed lines


if(diffLines.length) {
var lines = checkSequence(diffLines);
markdown += '\n <div line-highlight="' + lines.toString() + ',' + onlyFlag + '"></div>' + '\n';
Expand Down
59 changes: 39 additions & 20 deletions src/diffchecker.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
const Diff = require('diff');

function normalizeLines(str) {
const strWithNewLine = str.endsWith('\n') ? str : str + '\n';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When adding lines to the end of a file, the 'diff' package reads it as removing and adding the last line even though there isn't any changes. This line is to ensure a \n at the end so the package can read it as unchanged line.


return strWithNewLine.split('\n').map(line => line.replace(/\s+$/, '')).join('\n');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to clear out trailing white spaces. The package has a property to ignore white spaces, but when trying to rebuild the diff file, it removed all the indentation.

}

module.exports = {
diff: function(ogString, newString) {
var diffLines = [];
var ogLines = ogString.split('\n').map(line => line.trim());
var ogLinesStripped = []
for (var index = 0; index < ogLines.length; index++) {
ogLinesStripped.push(ogLines[index].replace(/;/g, ""));
}
var newLines = newString.split('\n').map(line => line.trim());

for (var index = 0; index < newLines.length; index++) {
var lineNumber = index + 1;
var newLine = newLines[index].replace(/;/g, "");
var newLineIndx = ogLinesStripped.indexOf(newLine);
if (newLine != '') {
if (newLineIndx === -1) {
diffLines.push(lineNumber);
const normalizedOgString = normalizeLines(ogString);
const normalizedNewString = normalizeLines(newString);

const diff = Diff.diffLines(normalizedOgString, normalizedNewString);
const diffLines = [];
const diffFile = [];
let lineNumberNewFile = 1;

diff.forEach((part) => {
const lines = part.value.split('\n');

lines.forEach((line, index) => {
if (index === lines.length - 1 && line === '') {
return;
}
else {
ogLinesStripped.splice(newLineIndx, 1);

if (part.added) {
diffFile.push(`+ ${line}`)
diffLines.push(lineNumberNewFile);
lineNumberNewFile++;
} else if (part.removed) {
diffFile.push(`- ${line}`)
diffLines.push(lineNumberNewFile);

lineNumberNewFile++;
} else {
diffFile.push(` ${line}`)
lineNumberNewFile++;
}
}
}
return diffLines
});
});

return {diffLines, diffFile}
}
}
2 changes: 1 addition & 1 deletion test/dir1/hello-world1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log("hello world");
console.log("Hello World");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added white spaces to ensure the diffChecker, ignores white spaces

4 changes: 2 additions & 2 deletions test/dir2/hello-world2.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
console.log("hello world");
console.log("hello universe");
console.log("Hello World");
console.log("Hello Universe");
8 changes: 4 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("diff", function(){
}
}
},function(newDoc, newScope){
//console.log(newDoc.body);
// console.log(newDoc.body);
assert.ok(newDoc.body.indexOf('<div line-highlight="2,"></div>') >= 0);
});
});
Expand Down Expand Up @@ -56,7 +56,7 @@ describe("diff", function(){
}
}
},function(newDoc, newScope){
//console.log('test 2', newDoc.body);
// console.log('test 2', newDoc.body);
assert.ok(newDoc.body.indexOf('<div line-highlight="2,only"></div>') >= 0);
});
});
Expand Down Expand Up @@ -84,8 +84,8 @@ describe("diff", function(){
}
}
},function(newDoc, newScope){
//console.log('test 3', newDoc.body);
assert.ok(newDoc.body.indexOf('<div line-highlight="3-4,79,93-122,"></div>') >= 0);
// console.log('test 3', newDoc.body);
assert.ok(newDoc.body.indexOf('<div line-highlight="3-5,80-81,93-123,"></div>') >= 0);
});
});

Expand Down