-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return strWithNewLine.split('\n').map(line => line.replace(/\s+$/, '')).join('\n'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
console.log("hello world"); | ||
console.log("Hello World"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added white spaces to ensure the diffChecker, ignores white spaces |
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"); |
There was a problem hiding this comment.
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